1
0

test_listdiffer.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, unicode_literals
  4. from salt.utils import dictdiffer
  5. # Import Salt libs
  6. from salt.utils.listdiffer import list_diff
  7. # Import Salt Testing libs
  8. from tests.support.unit import TestCase
  9. NONE = dictdiffer.RecursiveDictDiffer.NONE_VALUE
  10. class ListDictDifferTestCase(TestCase):
  11. def setUp(self):
  12. old_list = [
  13. {"key": 1, "value": "foo1", "int_value": 101},
  14. {"key": 2, "value": "foo2", "int_value": 102},
  15. {"key": 3, "value": "foo3", "int_value": 103},
  16. ]
  17. new_list = [
  18. {"key": 1, "value": "foo1", "int_value": 101},
  19. {"key": 2, "value": "foo2", "int_value": 112},
  20. {"key": 5, "value": "foo5", "int_value": 105},
  21. ]
  22. self.list_diff = list_diff(old_list, new_list, key="key")
  23. def tearDown(self):
  24. for attrname in ("list_diff",):
  25. try:
  26. delattr(self, attrname)
  27. except AttributeError:
  28. continue
  29. def test_added(self):
  30. self.assertEqual(len(self.list_diff.added), 1)
  31. self.assertDictEqual(
  32. self.list_diff.added[0], {"key": 5, "value": "foo5", "int_value": 105}
  33. )
  34. def test_removed(self):
  35. self.assertEqual(len(self.list_diff.removed), 1)
  36. self.assertDictEqual(
  37. self.list_diff.removed[0], {"key": 3, "value": "foo3", "int_value": 103}
  38. )
  39. def test_diffs(self):
  40. self.assertEqual(len(self.list_diff.diffs), 3)
  41. self.assertDictEqual(
  42. self.list_diff.diffs[0], {2: {"int_value": {"new": 112, "old": 102}}}
  43. )
  44. self.assertDictEqual(
  45. self.list_diff.diffs[1],
  46. # Added items
  47. {
  48. 5: {
  49. "int_value": {"new": 105, "old": NONE},
  50. "key": {"new": 5, "old": NONE},
  51. "value": {"new": "foo5", "old": NONE},
  52. }
  53. },
  54. )
  55. self.assertDictEqual(
  56. self.list_diff.diffs[2],
  57. # Removed items
  58. {
  59. 3: {
  60. "int_value": {"new": NONE, "old": 103},
  61. "key": {"new": NONE, "old": 3},
  62. "value": {"new": NONE, "old": "foo3"},
  63. }
  64. },
  65. )
  66. def test_new_values(self):
  67. self.assertEqual(len(self.list_diff.new_values), 2)
  68. self.assertDictEqual(self.list_diff.new_values[0], {"key": 2, "int_value": 112})
  69. self.assertDictEqual(
  70. self.list_diff.new_values[1], {"key": 5, "value": "foo5", "int_value": 105}
  71. )
  72. def test_old_values(self):
  73. self.assertEqual(len(self.list_diff.old_values), 2)
  74. self.assertDictEqual(self.list_diff.old_values[0], {"key": 2, "int_value": 102})
  75. self.assertDictEqual(
  76. self.list_diff.old_values[1], {"key": 3, "value": "foo3", "int_value": 103}
  77. )
  78. def test_changed_all(self):
  79. self.assertEqual(
  80. self.list_diff.changed(selection="all"),
  81. [
  82. "key.2.int_value",
  83. "key.5.int_value",
  84. "key.5.value",
  85. "key.3.int_value",
  86. "key.3.value",
  87. ],
  88. )
  89. def test_changed_intersect(self):
  90. self.assertEqual(
  91. self.list_diff.changed(selection="intersect"), ["key.2.int_value"]
  92. )
  93. def test_changes_str(self):
  94. self.assertEqual(
  95. self.list_diff.changes_str,
  96. "\tidentified by key 2:\n"
  97. "\tint_value from 102 to 112\n"
  98. "\tidentified by key 3:\n"
  99. "\twill be removed\n"
  100. "\tidentified by key 5:\n"
  101. "\twill be added\n",
  102. )