|
| 1 | +from django.test import TestCase |
| 2 | + |
| 3 | +from .models import OrderedModel |
| 4 | + |
| 5 | + |
| 6 | +class OrderableTreeNodeTestCase(TestCase): |
| 7 | + def test_automatic_position_assignment(self): |
| 8 | + """Test that position is automatically assigned to new nodes""" |
| 9 | + root = OrderedModel.objects.create(name="Root") |
| 10 | + assert root.position == 10 |
| 11 | + |
| 12 | + child1 = OrderedModel.objects.create(name="Child 1", parent=root) |
| 13 | + assert child1.position == 10 |
| 14 | + |
| 15 | + child2 = OrderedModel.objects.create(name="Child 2", parent=root) |
| 16 | + assert child2.position == 20 |
| 17 | + |
| 18 | + child3 = OrderedModel.objects.create(name="Child 3", parent=root) |
| 19 | + assert child3.position == 30 |
| 20 | + |
| 21 | + def test_manual_position_respected(self): |
| 22 | + """Test that manually set positions are not overwritten""" |
| 23 | + root = OrderedModel.objects.create(name="Root", position=100) |
| 24 | + assert root.position == 100 |
| 25 | + |
| 26 | + child = OrderedModel.objects.create(name="Child", parent=root, position=50) |
| 27 | + assert child.position == 50 |
| 28 | + |
| 29 | + def test_position_increments_from_max(self): |
| 30 | + """Test that positions increment from the current maximum""" |
| 31 | + root = OrderedModel.objects.create(name="Root") |
| 32 | + |
| 33 | + # Create children with custom positions |
| 34 | + OrderedModel.objects.create(name="Child 1", parent=root, position=100) |
| 35 | + OrderedModel.objects.create(name="Child 2", parent=root, position=200) |
| 36 | + |
| 37 | + # Next auto-assigned position should be max + 10 |
| 38 | + child3 = OrderedModel.objects.create(name="Child 3", parent=root) |
| 39 | + assert child3.position == 210 |
| 40 | + |
| 41 | + def test_siblings_ordered_by_position(self): |
| 42 | + """Test that siblings are correctly ordered by position""" |
| 43 | + root = OrderedModel.objects.create(name="Root") |
| 44 | + |
| 45 | + child1 = OrderedModel.objects.create(name="Child 1", parent=root) |
| 46 | + child2 = OrderedModel.objects.create(name="Child 2", parent=root) |
| 47 | + child3 = OrderedModel.objects.create(name="Child 3", parent=root) |
| 48 | + |
| 49 | + siblings = list(root.children.all()) |
| 50 | + assert siblings[0] == child1 |
| 51 | + assert siblings[1] == child2 |
| 52 | + assert siblings[2] == child3 |
| 53 | + |
| 54 | + def test_reordering_siblings(self): |
| 55 | + """Test that siblings can be manually reordered""" |
| 56 | + root = OrderedModel.objects.create(name="Root") |
| 57 | + |
| 58 | + child1 = OrderedModel.objects.create(name="Child 1", parent=root) # position=10 |
| 59 | + child2 = OrderedModel.objects.create(name="Child 2", parent=root) # position=20 |
| 60 | + child3 = OrderedModel.objects.create(name="Child 3", parent=root) # position=30 |
| 61 | + |
| 62 | + # Move child3 between child1 and child2 |
| 63 | + child3.position = 15 |
| 64 | + child3.save() |
| 65 | + |
| 66 | + siblings = list(root.children.all()) |
| 67 | + assert siblings[0] == child1 |
| 68 | + assert siblings[1] == child3 |
| 69 | + assert siblings[2] == child2 |
| 70 | + |
| 71 | + def test_position_per_parent(self): |
| 72 | + """Test that positions are assigned per parent""" |
| 73 | + root1 = OrderedModel.objects.create(name="Root 1") |
| 74 | + root2 = OrderedModel.objects.create(name="Root 2") |
| 75 | + |
| 76 | + child1_1 = OrderedModel.objects.create(name="Child 1-1", parent=root1) |
| 77 | + child2_1 = OrderedModel.objects.create(name="Child 2-1", parent=root2) |
| 78 | + |
| 79 | + # Both should get position 10 since they have different parents |
| 80 | + assert child1_1.position == 10 |
| 81 | + assert child2_1.position == 10 |
| 82 | + |
| 83 | + child1_2 = OrderedModel.objects.create(name="Child 1-2", parent=root1) |
| 84 | + child2_2 = OrderedModel.objects.create(name="Child 2-2", parent=root2) |
| 85 | + |
| 86 | + # Both should get position 20 |
| 87 | + assert child1_2.position == 20 |
| 88 | + assert child2_2.position == 20 |
| 89 | + |
| 90 | + def test_ordering_with_tree_queries(self): |
| 91 | + """Test that ordering works correctly with tree queries""" |
| 92 | + root = OrderedModel.objects.create(name="Root") |
| 93 | + child1 = OrderedModel.objects.create(name="Child 1", parent=root) |
| 94 | + OrderedModel.objects.create(name="Grandchild 1", parent=child1) |
| 95 | + OrderedModel.objects.create(name="Grandchild 2", parent=child1) |
| 96 | + OrderedModel.objects.create(name="Child 2", parent=root) |
| 97 | + |
| 98 | + # Get tree with tree fields |
| 99 | + nodes = list(OrderedModel.objects.with_tree_fields()) |
| 100 | + |
| 101 | + # Verify depth-first order respects position ordering |
| 102 | + assert nodes[0].name == "Root" |
| 103 | + assert nodes[1].name == "Child 1" |
| 104 | + assert nodes[2].name == "Grandchild 1" |
| 105 | + assert nodes[3].name == "Grandchild 2" |
| 106 | + assert nodes[4].name == "Child 2" |
| 107 | + |
| 108 | + def test_update_preserves_position(self): |
| 109 | + """Test that updating a node doesn't change its position""" |
| 110 | + root = OrderedModel.objects.create(name="Root") |
| 111 | + child = OrderedModel.objects.create(name="Child", parent=root) |
| 112 | + |
| 113 | + original_position = child.position |
| 114 | + assert original_position == 10 |
| 115 | + |
| 116 | + # Update the name |
| 117 | + child.name = "Updated Child" |
| 118 | + child.save() |
| 119 | + |
| 120 | + # Position should remain the same |
| 121 | + assert child.position == original_position |
| 122 | + |
| 123 | + def test_zero_position_is_replaced(self): |
| 124 | + """Test that position=0 triggers auto-assignment""" |
| 125 | + root = OrderedModel.objects.create(name="Root") |
| 126 | + |
| 127 | + # Even if we explicitly set position=0, it should be replaced on create |
| 128 | + child = OrderedModel(name="Child", parent=root, position=0) |
| 129 | + child.save() |
| 130 | + |
| 131 | + assert child.position == 10 |
| 132 | + |
| 133 | + # Create another child to verify positions increment correctly |
| 134 | + child2 = OrderedModel.objects.create(name="Child 2", parent=root) |
| 135 | + assert child2.position == 20 |
| 136 | + |
| 137 | + def test_ordering_inherited_from_meta(self): |
| 138 | + """Test that ordering is inherited from OrderableTreeNode.Meta""" |
| 139 | + # This test verifies that the Meta.ordering is properly inherited |
| 140 | + root = OrderedModel.objects.create(name="Root") |
| 141 | + OrderedModel.objects.create(name="Child 2", parent=root, position=20) |
| 142 | + OrderedModel.objects.create(name="Child 1", parent=root, position=10) |
| 143 | + OrderedModel.objects.create(name="Child 3", parent=root, position=30) |
| 144 | + |
| 145 | + # Query without explicit ordering should use Meta.ordering |
| 146 | + children = list(OrderedModel.objects.filter(parent=root)) |
| 147 | + |
| 148 | + assert children[0].name == "Child 1" |
| 149 | + assert children[1].name == "Child 2" |
| 150 | + assert children[2].name == "Child 3" |
0 commit comments