def test_create_new_node(self): """Test create new node.""" db = DB() new_node = db.create_new_node('val1', is_deleted=False) assert new_node.value == 'val1' assert new_node.is_deleted is False assert new_node.parent is None assert db.nodes == {0: new_node} assert db._node_index == 1
def test_add_root(self): """Test add root.""" db = DB() new_node = db.add_root('val1') assert len(db.nodes) == 1 assert db.nodes[0] == new_node assert db.nodes[0].value == 'val1' assert db.nodes[0].is_deleted is False assert db.nodes[0].db_id == 0 assert db.nodes[0].parent is None
def test_new(self): """Test new.""" db = DB.default() cache = Cache() cache.load(5, db) cache.add_node(0) cache.save(db) assert db.nodes[9].value == Cache.default_name
def test_update_value(self): """Test update value.""" db = DB.default() cache = Cache() cache.load(5, db) cache.cache_nodes[0].value = 'new_value' cache.save(db) assert db.nodes[5].value == 'new_value'
def test_is_child(self): """Test is child.""" db = DB() root = db.add_root('val1') child = db.add_to_parent(root, 'val2') grandson = db.add_to_parent(child, 'val3') assert db.is_child(child.db_id, root.db_id) is True assert db.is_child(grandson.db_id, root.db_id) is False
def test_delete(self): """Test delete.""" db = DB.default() cache = Cache() cache.load(5, db) cache.delete(0) cache.save(db) assert db.nodes[5].is_deleted is True assert db.nodes[7].is_deleted is True assert db.nodes[8].is_deleted is True
def test_to_parent(self): """Test add to parent.""" db = DB() root = db.add_root('val1') child1 = db.add_to_parent(root, 'val2') child2 = db.add_to_parent(child1.db_id, 'val3', is_deleted=True) assert child1.is_deleted is False assert child2.is_deleted is True assert root.db_id == 0 assert child1.db_id == 1 assert child2.db_id == 2 assert len(db.nodes) == 3 assert child2.parent == child1 assert child1.children == [child2] assert child1.parent == root assert root.children == [child1]
def test_get_parent_id(self): """Test get parent id.""" db = DB() root = db.add_root('val1') db.add_to_parent(root, 'val2', is_deleted=True) parent_id = db.get_parent_id(1) assert parent_id == 0 parent_id = db.get_parent_id(0) assert parent_id is None with pytest.raises(KeyError): parent_id = db.get_parent_id(4)
def test_to_parent_fail_unknown_parent(self): """Test add to parent fail. case: unknown parent. """ db = DB() db.add_root('val1') with pytest.raises(ValueError): db.add_to_parent(3, 'val2') unknown_node = Node('val2') unknown_node._db_id = 3 with pytest.raises(ValueError): db.add_to_parent(unknown_node, 'val2')
def load(self, db_id: int, db: DB): """Load node from db.""" # node already loaded if db_id in self.db_nodes: return # create node copy new_node_params = db.get_node_params(db_id) cache_id = self._cache_index self._cache_index += 1 new_node = CNode( cache_id=cache_id, node_params=new_node_params, ) self.db_nodes[new_node_params.db_id] = new_node self.cache_nodes[cache_id] = new_node # restore node connections adoptees = [] for orphan in self.orphans: is_child = db.is_child(orphan, db_id) if is_child: child = self.db_nodes[orphan] new_node.append_child(child) adoptees.append(orphan) self.orphans -= set(adoptees) # restore parent and is_deleted attribute # if parent deleted node should be deleted to parent_id = db.get_parent_id(db_id) if parent_id in self.db_nodes: parent = self.db_nodes[parent_id] parent.append_child(new_node) self.orphans.discard(db_id) if parent.is_deleted: new_node.delete() else: self.orphans.add(db_id)
def save(self, db: DB): """Save cache to db.""" # new created nodes appended to the end of dict # in python >= 3.6 dicts are ordered deleted = [] for _cache_id, node in self.cache_nodes.items(): db_id = node.db_id if db_id is not None: deleted_children = db.update_node( db_id, node.value, node.is_deleted, ) if deleted_children: deleted.extend(deleted_children) continue parent = node.parent if parent is None: raise RuntimeError('In cache all new nodes is subnodes') new_node = db.add_to_parent( parent.db_id, node.value, node.is_deleted, ) # now new node have db_id node.db_id = new_node.db_id self.db_nodes[new_node.db_id] = node # Case when delete root node. # But in cache we have not connection from # some subnode to this root. # This subnode should be deleted too. for db_node in deleted: cache_node = self.db_nodes.get(db_node.db_id) if cache_node: cache_node.delete()
def test_new_and_delete_parent(self): """Test new and delete parent.""" db = DB.default() cache = Cache() cache.load(5, db) cache.add_node(0) cache.delete(0) cache.save(db) assert cache.cache_nodes[0].is_deleted is True assert cache.cache_nodes[1].is_deleted is True assert db.nodes[5].is_deleted is True assert db.nodes[7].is_deleted is True assert db.nodes[8].is_deleted is True assert db.nodes[9].is_deleted is True
def test_add_root_fail_double_add(self): """Test add root. case: double add. """ db = DB() db.add_root('val1') with pytest.raises(RuntimeError): db.add_root('val2')
def test_get_node_params(self): """Test get node params.""" db = DB() root = db.add_root('val1') db.add_to_parent(root, 'val2', is_deleted=True) params = db.get_node_params(1) assert params.db_id == 1 assert params.value == 'val2' assert params.is_deleted is True
def test_update_node(self): """Test update node.""" db = DB() root = db.add_root('val1') child = db.add_to_parent(root, 'val2') child2 = db.add_to_parent(child, 'val3') new_value = 'val4' db.update_node( child.db_id, new_value, is_deleted=True, ) assert child.value == new_value assert child.is_deleted is True assert child2.is_deleted is True
def cache(): """Simple cache fixture. 0 root 1 node_1_1 3 node_2_1 5 node_3_1 7 node_4_1 8 node_4_2 6 node_3_2 4 node_2_2 2 node_1_2 """ db = DB.default() new_cache = Cache() for db_id in db.nodes: new_cache.load(db_id, db) return new_cache
def test_save_update_deleted_children(self): """Test save to db. case: auto update deleted children in cache """ db = DB.default() cache = Cache() cache.load(3, db) cache.load(7, db) root_cache = cache.cache_nodes[0] child_cache = cache.cache_nodes[1] root_db = db.nodes[3] child_db = db.nodes[7] root_cache.delete() cache.save(db) assert root_cache.is_deleted is True assert root_db.is_deleted is True assert child_cache.is_deleted is True assert child_db.is_deleted is True
def test_new_and_delete_gp(self): """Test add grand parent, child. Add new to child and delete gp.""" db = DB.default() cache = Cache() cache.load(1, db) cache.load(5, db) cache.add_node(1) cache.delete(0) assert cache.cache_nodes[1].is_deleted is False assert cache.cache_nodes[2].is_deleted is False cache.save(db) assert cache.cache_nodes[0].is_deleted is True # child and new node deleted after load too. assert cache.cache_nodes[1].is_deleted is True assert cache.cache_nodes[2].is_deleted is True assert db.nodes[1].is_deleted is True assert db.nodes[3].is_deleted is True assert db.nodes[5].is_deleted is True assert db.nodes[7].is_deleted is True assert db.nodes[8].is_deleted is True assert db.nodes[6].is_deleted is True assert db.nodes[4].is_deleted is True
def test_load(self): """Test load from db.""" db = DB.default() cache = Cache() cache.load(5, db) m_node = cache.cache_nodes[0] assert m_node.value == 'node_3_1' assert m_node.parent is None assert not m_node.children cache.load(7, db) l_node = cache.cache_nodes[1] assert l_node.value == 'node_4_1' assert l_node.parent is m_node assert not l_node.children assert m_node.children == [l_node] cache.load(3, db) h_node = cache.cache_nodes[2] assert h_node.value == 'node_2_1' assert h_node.parent is None assert h_node.children == [m_node] assert m_node.parent == h_node
def __init__(self): """Initialization.""" super().__init__() self.db = DB.default()
def test_save(self): """Test save to db.""" db = DB.default() cache = Cache() cache.load(3, db) cache.load(5, db) cache.load(7, db) cache.add_node(1) cache.delete(1) cache.cache_nodes[1].value = 'new_value' # Cache struct: # id del cid value # 3 f 0 node_2_1 # 5 t 1 node_value # 7 t 2 node_4_1 # ? t 3 New Node cache.save(db) assert len(cache.db_nodes) == len(cache.cache_nodes) new_node = cache.db_nodes[9] assert cache.cache_nodes[3] == new_node assert new_node.value == 'New Node' # DB struct: # id del cid value # 0 f ? root # 1 f ? node_1_1 # 3 f 0 node_2_1 # 5 t 1 node_value # 7 t 2 node_4_1 # 8 t 3 node_4_2 # 9 t 4 New Node # 6 f ? node_3_2 # 4 f ? node_2_2 # 2 f ? node_1_2 node_1_1 = db.nodes[1] node_2_1 = db.nodes[3] node_3_2 = db.nodes[6] node_modified = db.nodes[5] node_4_1 = db.nodes[7] node_4_2 = db.nodes[8] new_node = db.nodes[9] assert new_node.is_deleted is True assert new_node.value == 'New Node' assert not new_node.children assert new_node.parent == node_modified assert node_4_1.is_deleted is True assert node_4_1.value == 'node_4_1' assert not node_4_1.children assert node_4_1.parent == node_modified assert node_modified.is_deleted is True assert node_modified.value == 'new_value' assert set(node_modified.children) == {node_4_1, node_4_2, new_node} assert node_modified.parent == node_2_1 assert node_2_1.is_deleted is False assert node_2_1.value == 'node_2_1' assert set(node_2_1.children) == {node_modified, node_3_2} assert node_2_1.parent == node_1_1
def test_get(self): """Test get node.""" db = DB() root = db.add_root('val1') assert db.get(0) == root assert db.get(1) is None