|
| 1 | +"""Test {{ cookiecutter.system_of_record }} SSoT integration customised Diff Object.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +from diffsync.enum import DiffSyncActions |
| 6 | +from nautobot.core.testing import TestCase |
| 7 | + |
| 8 | +from {{ cookiecutter.app_name }}.jobs import CustomOrderingDiff |
| 9 | + |
| 10 | + |
| 11 | +class TestCustomOrderingDiff(TestCase): |
| 12 | + """Test the CustomOrderingDiff.""" |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + """Setup CustomOrderingDiff instance.""" |
| 16 | + self.diff = CustomOrderingDiff() |
| 17 | + self.diff.children = {} |
| 18 | + self.diff.groups = MagicMock() |
| 19 | + |
| 20 | + def test_yields_non_delete_first_then_deletes(self): |
| 21 | + """Verify deletes are yielded last and reversed.""" |
| 22 | + self.diff.groups.return_value = ["group1"] |
| 23 | + child1 = MagicMock() |
| 24 | + child1.action = DiffSyncActions.DELETE |
| 25 | + child2 = MagicMock() |
| 26 | + child2.action = DiffSyncActions.CREATE |
| 27 | + child3 = MagicMock() |
| 28 | + child3.action = DiffSyncActions.DELETE |
| 29 | + child4 = MagicMock() |
| 30 | + child4.action = DiffSyncActions.UPDATE |
| 31 | + |
| 32 | + self.diff.children = {"group1": {"a": child1, "b": child2, "c": child3, "d": child4}} |
| 33 | + |
| 34 | + results = list(self.diff.get_children()) |
| 35 | + # Non-deletes first |
| 36 | + self.assertIn(child2, results[:2]) |
| 37 | + self.assertIn(child4, results[:2]) |
| 38 | + # Deletes at the end (in reverse order) |
| 39 | + self.assertEqual(results[-1], child1) |
| 40 | + self.assertEqual(results[-2], child3) |
| 41 | + |
| 42 | + def test_location_deletes_sorted_by_depth(self): |
| 43 | + """Verify Location deletes are correctly sorted by depth.""" |
| 44 | + self.diff.groups.return_value = ["location"] |
| 45 | + |
| 46 | + def make_loc(keys): |
| 47 | + loc = MagicMock() |
| 48 | + loc.action = DiffSyncActions.DELETE |
| 49 | + loc.keys = keys |
| 50 | + return loc |
| 51 | + |
| 52 | + loc1 = make_loc({"parent__name": "parentA"}) |
| 53 | + loc2 = make_loc({"parent__name": "parentA", "parent__parent__name": "grandparent"}) |
| 54 | + loc3 = make_loc({}) |
| 55 | + |
| 56 | + self.diff.children = {"location": {"loc1": loc1, "loc2": loc2, "loc3": loc3}} |
| 57 | + |
| 58 | + results = list(self.diff.get_children()) |
| 59 | + |
| 60 | + self.assertEqual(results, [loc2, loc1, loc3]) |
| 61 | + |
| 62 | + def test_mixed_groups(self): |
| 63 | + """Verify mixed groups with mixed delete and non-delete children are returned in the correct order""" |
| 64 | + self.diff.groups.return_value = ["group1", "location"] |
| 65 | + |
| 66 | + # Group1 children |
| 67 | + child1 = MagicMock() |
| 68 | + child1.action = DiffSyncActions.UPDATE |
| 69 | + child2 = MagicMock() |
| 70 | + child2.action = DiffSyncActions.DELETE |
| 71 | + |
| 72 | + def make_loc(keys): |
| 73 | + loc = MagicMock() |
| 74 | + loc.action = DiffSyncActions.DELETE |
| 75 | + loc.keys = keys |
| 76 | + return loc |
| 77 | + |
| 78 | + loc1 = make_loc({"parent__name": "p1"}) |
| 79 | + loc2 = make_loc({}) |
| 80 | + |
| 81 | + self.diff.children = {"group1": {"c1": child1, "c2": child2}, "location": {"l1": loc1, "l2": loc2}} |
| 82 | + |
| 83 | + results = list(self.diff.get_children()) |
| 84 | + |
| 85 | + self.assertEqual(results[0], child1) |
| 86 | + self.assertEqual(results[-3:], [loc1, loc2, child2]) |
0 commit comments