test_dictdiffer.py 3.8 KB

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