1
0

test_dictdiffer.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt libs
  5. import salt.utils.dictdiffer as dictdiffer
  6. # Import Salt Testing libs
  7. from tests.support.unit import TestCase
  8. NONE = dictdiffer.RecursiveDictDiffer.NONE_VALUE
  9. class RecursiveDictDifferTestCase(TestCase):
  10. def setUp(self):
  11. old_dict = {
  12. "a": {"b": 1, "c": 2, "e": "old_value", "f": "old_key"},
  13. "j": "value",
  14. }
  15. new_dict = {
  16. "a": {"b": 1, "c": 4, "e": "new_value", "g": "new_key"},
  17. "h": "new_key",
  18. "i": None,
  19. "j": "value",
  20. }
  21. self.recursive_diff = dictdiffer.recursive_diff(
  22. old_dict, new_dict, ignore_missing_keys=False
  23. )
  24. self.recursive_diff_ign = dictdiffer.recursive_diff(old_dict, new_dict)
  25. def tearDown(self):
  26. for attrname in ("recursive_diff", "recursive_diff_missing_keys"):
  27. try:
  28. delattr(self, attrname)
  29. except AttributeError:
  30. continue
  31. def test_added(self):
  32. self.assertEqual(self.recursive_diff.added(), ["a.g", "h", "i"])
  33. def test_removed(self):
  34. self.assertEqual(self.recursive_diff.removed(), ["a.f"])
  35. def test_changed_with_ignore_unset_values(self):
  36. self.recursive_diff.ignore_unset_values = True
  37. self.assertEqual(self.recursive_diff.changed(), ["a.c", "a.e"])
  38. def test_changed_without_ignore_unset_values(self):
  39. self.recursive_diff.ignore_unset_values = False
  40. self.assertEqual(
  41. self.recursive_diff.changed(), ["a.c", "a.e", "a.f", "a.g", "h", "i"]
  42. )
  43. def test_unchanged(self):
  44. self.assertEqual(self.recursive_diff.unchanged(), ["a.b", "j"])
  45. def test_diffs(self):
  46. self.assertDictEqual(
  47. self.recursive_diff.diffs,
  48. {
  49. "a": {
  50. "c": {"old": 2, "new": 4},
  51. "e": {"old": "old_value", "new": "new_value"},
  52. "f": {"old": "old_key", "new": NONE},
  53. "g": {"old": NONE, "new": "new_key"},
  54. },
  55. "h": {"old": NONE, "new": "new_key"},
  56. "i": {"old": NONE, "new": None},
  57. },
  58. )
  59. self.assertDictEqual(
  60. self.recursive_diff_ign.diffs,
  61. {
  62. "a": {
  63. "c": {"old": 2, "new": 4},
  64. "e": {"old": "old_value", "new": "new_value"},
  65. "g": {"old": NONE, "new": "new_key"},
  66. },
  67. "h": {"old": NONE, "new": "new_key"},
  68. "i": {"old": NONE, "new": None},
  69. },
  70. )
  71. def test_new_values(self):
  72. self.assertDictEqual(
  73. self.recursive_diff.new_values,
  74. {
  75. "a": {"c": 4, "e": "new_value", "f": NONE, "g": "new_key"},
  76. "h": "new_key",
  77. "i": None,
  78. },
  79. )
  80. def test_old_values(self):
  81. self.assertDictEqual(
  82. self.recursive_diff.old_values,
  83. {
  84. "a": {"c": 2, "e": "old_value", "f": "old_key", "g": NONE},
  85. "h": NONE,
  86. "i": NONE,
  87. },
  88. )
  89. def test_changes_str(self):
  90. self.assertEqual(
  91. self.recursive_diff.changes_str,
  92. "a:\n"
  93. " c from 2 to 4\n"
  94. " e from 'old_value' to 'new_value'\n"
  95. " f from 'old_key' to nothing\n"
  96. " g from nothing to 'new_key'\n"
  97. "h from nothing to 'new_key'\n"
  98. "i from nothing to None",
  99. )