test_listdiffer.py 3.7 KB

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