Пример #1
0
    def test_add_child(self):
        update_queue = set()
        parent = Node(randomPK, update_queue)

        childPK = randomPK + 1
        old_sync_hash = parent.get_sync_hash()
        child = Node(childPK, set())
        parent.add_child(child)
        new_sync_hash = parent.get_sync_hash()

        self.assertEqual(child._parent, parent)
        self.assertIn(child, parent._children)
        self.assertEqual(parent._number_of_children(), 1)

        for x in sum((old_sync_hash, new_sync_hash), ()):
            if x != DEFAULT_HASH_VALUE:
                self.assertTrue(check_valid_hash(x))

        self.assertTrue(TestNodeCore.check_sync_hash_old_new(old_sync_hash, new_sync_hash, False, True, False))

        with self.assertRaises(NotImplementedError):
            parent.remove_child(childPK)

        # update_queue should have only parent added.
        self.assertSetEqual(parent._update_hash_queue, set([parent._pk, child._pk]))
Пример #2
0
    def test_hash_and_queue_changes_in_a_complex_tree(self):
        update_queue = set()

        root = Node(randomPK, update_queue)
        root_child1 = Node(getRandomPK(), update_queue)
        root_child2 = Node(getRandomPK(), update_queue)
        root_child1_child1 = Node(getRandomPK(), update_queue)
        root_child1_child2 = Node(getRandomPK(), update_queue)
        root_child2_child1 = Node(getRandomPK(), update_queue)
        root_child2_child2 = Node(getRandomPK(), update_queue)
        root_child1_child1_child1 = Node(getRandomPK(), update_queue)

        all_nodes = [locals()[x] for x in locals().keys() if 'root' in x]

        # Each new node announces its presence in the update queue
        self.assertSetEqual(update_queue, set([x._pk for x in all_nodes]))

        update_queue = set()
        for x in all_nodes:
            x._update_hash_queue = update_queue


        #start creating relationships
        old_root_hash = root.get_sync_hash()
        old_root_child1_hash = root_child1.get_sync_hash()
        root.add_child(root_child1)
        self.assertSetEqual(update_queue, set([root._pk, root_child1._pk]))
        new_root_hash = root.get_sync_hash()
        new_root_child1_hash = root_child1.get_sync_hash()

        self.assertTrue(TestNodeCore.check_sync_hash_old_new(old_root_hash, new_root_hash, False, True, False))
        self.assertTrue(TestNodeCore.check_sync_hash_old_new(old_root_child1_hash, new_root_child1_hash, True, True, True))

        root_child1.add_child(root_child1_child1)
        self.assertSetEqual(update_queue, set([root._pk, root_child1._pk, root_child1_child1._pk]))

        root.add_child(root_child2)
        self.assertSetEqual(update_queue, set([root._pk, root_child1._pk, root_child1_child1._pk, root_child2._pk]))

        #create complete relationship
        root_child1.add_child(root_child1_child2)
        root_child2.add_child(root_child2_child1)
        root_child2.add_child(root_child2_child2)
        root_child1_child1.add_child(root_child1_child1_child1)

        self.assertEqual(root._number_of_children(), 2)
        self.assertEqual(root_child1._number_of_children(), 2)
        self.assertEqual(root_child2._number_of_children(), 2)
        self.assertEqual(root_child1_child1._number_of_children(), 1)
        self.assertEqual(root_child1_child2._number_of_children(), 0)
        self.assertEqual(root_child2_child1._number_of_children(), 0)
        self.assertEqual(root_child2_child2._number_of_children(), 0)

        self.assertEqual(len(update_queue), 8)

        self.assertIn(root_child1_child1_child1, root_child1_child1._children)

        # test that changing a child's description, changes its hash and adds self
        # pk in the update queue

        update_queue.remove(root_child2_child2._pk)
        old_sync_hash = root_child2_child2.get_sync_hash()
        root_child2_child2.something = "Sometihng"
        new_sync_hash = root_child2_child2.get_sync_hash()
        self.assertIn(root_child2_child2._pk, update_queue)
        self.assertTrue(TestNodeCore.check_sync_hash_old_new(
            old_sync_hash, new_sync_hash, False, False, True))