def test_setting_and_getting_node(self): tree = SyncTree(**temp_info) root = tree.root root_child1 = tree.add_node(root, **temp_info2) root_child2 = tree.add_node(root, **temp_info2) root_child1_child1 = tree.add_node(root_child1, **temp_info) self.assertEqual(tree.get_node(root_child2._pk), root_child2) self.assertEqual(tree.get_node(root_child1_child1._pk), root_child1_child1) self.assertSetEqual(tree.update_hash_queue, { root._pk, root_child1._pk, root_child2._pk, root_child1_child1._pk})
def test_updated_time_gets_touched_on_adding_info(self): tree = SyncTree(**temp_info) root = tree.root # pk 0 tree.refresh_tree() old = root.get_update_time() tree.refresh_tree() # no change should not cause time updating new = root.get_update_time() self.assertEqual(old, new) root.abc = "Something" tree.refresh_tree() new = root.get_update_time() self.assertNotEqual(old, new) old = root.get_update_time() root_child1 = tree.add_node(root, **temp_info) # pk 1 root_child2 = tree.add_node(root, **temp_info) # pk 2 root_child1_child1 = tree.add_node(root_child1, **temp_info) # pk 3 root_child2_child1 = tree.add_node(root_child2, **temp_info) # pk 4 tree.refresh_tree() new = root.get_update_time() self.assertNotEqual(old, new) temp = tree.add_node(root_child1_child1, **temp_info) # pk5 old_times = [tree.get_node(x).get_update_time() for x in range(5)] tree.refresh_tree() new_times = [tree.get_node(x).get_update_time() for x in range(5)] self.assertEqual(map(lambda x, y: x == y, old_times, new_times),[ False, # update time of root should have changed False, # update time of root_child1 should have changed True, # update time of root_child2 should not have changed False, # update time of root_child1_child1 should have changed True, # update time of root_child2_child1 should not have changed ] ) self.assertTrue(TestSyncTreeCore.validate_last_updated_relationship(tree)) random_tree = TestSyncTreeCore.create_random_tree(1000) random_tree.refresh_tree() self.assertTrue(TestSyncTreeCore.validate_last_updated_relationship(random_tree))
def test_tree_created(self): tree = SyncTree(**temp_info) self.assertIsInstance(tree, SyncTree) with self.assertRaises(RuntimeError): temp = SyncTree() self.assertEqual(tree.root, tree.root._parent) self.assertEqual(tree.root._pk, 0) self.assertEqual(tree.get_node(0), tree.root)
def test_tree_refreshes_properly_on_adding_node_way_down(self): tree = SyncTree(**temp_info) root = tree.root # pk 0 root_child1 = tree.add_node(root, **temp_info) # pk 1 root_child2 = tree.add_node(root, **temp_info) # pk 2 root_child1_child1 = tree.add_node(root_child1, **temp_info) # pk 3 root_child2_child1 = tree.add_node(root_child2, **temp_info) # pk 4 tree.refresh_tree() old_sync_hashes = {x: tree.get_node(x).get_sync_hash() for x in range(5)} root_child1_child1_child1 = tree.add_node(root_child1_child1, **temp_info) tree.refresh_tree() new_sync_hashes = {x: tree.get_node(x).get_sync_hash() for x in range(5)} # root should have total hash and children hash changed self.assertTrue(TestNodeCore.check_sync_hash_old_new( old_sync_hashes[0], new_sync_hashes[0], False, True, False)) # root_child1 should have total hash and children hash changed self.assertTrue(TestNodeCore.check_sync_hash_old_new( old_sync_hashes[1], new_sync_hashes[1], False, True, False)) # root_child2 should have no hash change self.assertTrue(TestNodeCore.check_sync_hash_old_new( old_sync_hashes[2], new_sync_hashes[2], True, True, True)) # root_child1_child1 should have total hash and children hash changed self.assertTrue(TestNodeCore.check_sync_hash_old_new( old_sync_hashes[3], new_sync_hashes[3], False, True, False)) # root_child2_child1 should have no hash change self.assertTrue(TestNodeCore.check_sync_hash_old_new( old_sync_hashes[4], new_sync_hashes[4], True, True, True)) self.assertTrue(TestSyncTreeCore.validate_last_updated_relationship(tree))