def test_add_node_at_root(self): d = DataTree() n = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': []} path = '/' d.add_node(path, n) tree = d.get_tree() node = self.__get_list_item_by_id(tree['children'], 'a1') self.assertEqual(n, node)
def test_addnode_without_children(self): d = DataTree() n = {'id': 'a1', 'key': b'1234'} path = '/' d.add_node(path, n) n['children'] = [] n['attrib'] = {} self.assertEqual(d.get_node('/a1'), n)
def test_add_duplicate_node(self): d = DataTree() n = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'b1', 'key': b'1234', 'children': []}]} path = '/' d.add_node(path, n) n = {'id': 'a1', 'key': b'1234', 'children': []} path = '/' self.assertRaises(TMSError, d.add_node, path, n) n = {'id': 'b1', 'key': b'1234', 'children': []} path = '/a1/' self.assertRaises(TMSError, d.add_node, path, n)
def test_add_node_with_missing_optional_field_in_children(self): d = DataTree() n1 = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'a1', 'key': b'1234'}]} d.add_node('/', n1) n2 = {'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': [{'id': 'a1', 'key': b'1234', 'attrib': {}, 'children': []}]} node = d.get_node('/a1') self.assertEqual(n2, node)
def test_get_n_level_hierarchy(self): d = DataTree() path = '' for l in range(100): level_id = 'a' + str(l) n = {'id': level_id, 'key': b'1234', 'children': []} path += '/' d.add_node(path, n) path += level_id path = '' for l in range(100): level_id = 'a' + str(l) n = {'id': level_id, 'key': b'1234', 'children': []} path += '/' + level_id d.get_node(path) self.assertEqual(n.get('id'), level_id)
def test_add_n_level_hierarchy(self): d = DataTree() path = '' for l in range(100): level_id = 'a' + str(l) n = {'id': level_id, 'key': b'1234', 'children': []} path += '/' d.add_node(path, n) path += level_id tree = d.get_tree() t = tree for l in range(100): level_id = 'a' + str(l) n = self.__get_list_item_by_id(t['children'], level_id) self.assertEqual(n.get('id'), level_id) t = n
def test_get_node_children(self): d = DataTree() n = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'b1', 'key': b'1234', 'children': []}]} path = '/' d.add_node(path, n) path = '/a1/b1' for l in range(100): child_id = 'c' + str(l) n = {'id': child_id, 'key': b'1234', 'children': []} d.add_node(path, n) path = '/a1/b1' children = d.get_node_children(path) for l in range(100): child_id = 'c' + str(l) t = self.__get_list_item_by_id(children, child_id) self.assertNotEqual(t, None)
def test_get_n_level_children(self): d = DataTree() n = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'b1', 'key': b'1234', 'children': []}]} path = '/' d.add_node(path, n) path = '/a1/b1' for l in range(100): child_id = 'c' + str(l) n = {'id': child_id, 'key': b'1234', 'children': []} d.add_node(path, n) for l in range(100): path = '/a1/b1/' child_id = 'c' + str(l) path += child_id n = d.get_node(path) self.assertEqual(n.get('id'), child_id)
def test_addnode_with_invalid_path(self): d = DataTree() n = {'children': [], 'key': b'1234', 'id': 'x1'} path = '' self.assertRaises(TMSError, d.add_node, path, n) path = 'abc' self.assertRaises(TMSError, d.add_node, path, n) path = '/a3' self.assertRaises(TMSError, d.add_node, path, n) path = '/a2/' self.assertRaises(TMSError, d.add_node, path, n) path = 1 self.assertRaises(TMSError, d.add_node, path, n) path = {} self.assertRaises(TMSError, d.add_node, path, n) path = '/' d.add_node(path, n) n = {'children': [], 'key': b'1234', 'id': 'x2'} path = '/a4' self.assertRaises(TMSError, d.add_node, path, n)
def test_add_n_children(self): d = DataTree() n = {'id': 'a1', 'key': b'1234', 'children': [{'id': 'b1', 'key': b'1234', 'children': []}]} path = '/' d.add_node(path, n) path = '/a1/b1' for l in range(100): child_id = 'c' + str(l) n = {'id': child_id, 'key': b'1234', 'children': []} d.add_node(path, n) tree = d.get_tree() t = self.__get_list_item_by_id(tree['children'], 'a1') self.assertNotEqual(t, None) t = self.__get_list_item_by_id(t['children'], 'b1') self.assertNotEqual(t, None) for l in range(100): child_id = 'c' + str(l) n = self.__get_list_item_by_id(t['children'], child_id) self.assertEqual(n.get('id'), child_id)
class KeyManager(): __key_tree = None __storage = None def __init__(self, storage): if storage is not None: try: if storage.is_initialized(): self.__storage = storage else: raise ValueError('Storage must be initilized.') except BaseException as e: raise ValueError('Invalid Storage object type') else: raise ValueError( 'Must provide storage for initializing KeyManager') if storage.is_valid(): # decode yaml s = storage.load() py_objs = None try: print(s) py_objs = yaml2py(s) except Exception as e: raise ValueError('Invalid Yaml read from storage') if py_objs is not None: # Import objects to data tree self.__key_tree = DataTree(py_objs) else: raise ValueError( 'Data read from file do not conform with DataTree format') else: self.__key_tree = DataTree() self.__save_key_tree() def __save_key_tree(self): data = self.__key_tree.get_tree() yaml = '' try: yaml = py2yaml(data) except BaseException as e: raise ValueError('Failed to encode key tree to yaml') try: file_data = bytes(yaml, 'utf-8') self.__storage.save(file_data) except BaseException as e: raise ValueError('Error saving key tree to storage') def add_key(self, path, key_id, key, attrib=None): try: key_node = {'id': key_id, 'key': key, 'attrib': attrib} self.__key_tree.add_node(path, key_node) self.__save_key_tree() except BaseException as e: raise ValueError('Error adding key.') def get_key(self, path): return self.__key_tree.get_node(path) def del_key(self, path): pass def update_key(self, path, key=None, attrib=None): pass def export_key(self, path, out_path): pass def reseal_storage(self, new_key): pass