def test_change_parent_go_up_one_level(self): a = TextLabelFactory() b = TextLabelFactory(parent=a) c = TextLabelFactory(parent=b) c.parent = a c.save() self.assertEqual(c.parent, a) self.assertEqual(c.order, 1) b.refresh_from_db() self.assertEqual(b.order, 2)
def test_change_parent_go_root_recompute_child(self): root = TextLabelFactory() child_1 = TextLabelFactory(parent=root) child_2 = TextLabelFactory(parent=root) child_1.parent = None child_1.save() root.refresh_from_db() child_2.refresh_from_db() self.assertEqual(root.parent, None) self.assertEqual(root.order, 2) self.assertEqual(child_1.parent, None) self.assertEqual(child_1.order, 1) self.assertEqual(child_2.parent, root) self.assertEqual(child_2.order, 1)
def test_circular_dependency(self): a = TextLabelFactory() b = TextLabelFactory(parent=a) c = TextLabelFactory(parent=b) b.parent = c with self.assertRaises(ValueError): return b.save()
class TextLabelComplexeStructureTest(TestCase): @classmethod def setUpTestData(cls): cls.A = TextLabelFactory(order=1) cls.C = TextLabelFactory(parent=cls.A, order=2) cls.E = TextLabelFactory(parent=cls.C, order=1) cls.F = TextLabelFactory(parent=cls.C, order=2) cls.G = TextLabelFactory(parent=cls.C, order=3) def setUp(self): self.B = TextLabelFactory(parent=self.A, order=1) self.D = TextLabelFactory(parent=self.B, order=1) def test_move_to_root_structure(self): self.B.parent = None self.B.save() self.A.refresh_from_db() self.C.refresh_from_db() self.assertEqual(self.B.parent, None) self.assertEqual(self.B.order, 1) #Root level self.assertEqual(self.A.order, 2) #Root level self.assertEqual(self.C.order, 1) def test_change_order_second_level(self): self.B.order = 4 #not exist self.B.save() self.C.refresh_from_db() self.assertEqual(self.B.order, 2) #Inversion self.assertEqual(self.C.order, 1) #Inversion def test_change_on_same_level_no_change_as_result(self): self.E.order = 2 self.E.save() self.F.refresh_from_db() self.G.refresh_from_db() # No change because we have reorder value self.assertEqual(self.E.order, 1) self.assertEqual(self.F.order, 2) self.assertEqual(self.G.order, 3)