def test_obj_multi_inter(self): key_vals = {'fake_key': 'fake_val'} test_dict = { 'obj_id': { 'fake_ref_0': key_vals, 'fake_ref_1': key_vals, 'fake_ref_2': key_vals } } test_obj = ObjNode('obj_id') _patch_ne(test_obj) for i in range(0, 3): temp_inter = InterNode('fake_path_{}'.format(i), **key_vals) _patch_ne(temp_inter) temp_inter.addParent(test_obj) test_obj.addChild(temp_inter) self.helper_mock.return_value = { 'fake_ref_0': 'fake_path_0', 'fake_ref_1': 'fake_path_1', 'fake_ref_2': 'fake_path_2' } cut_tree = obj_tree._dfs_cut(test_dict, InterNode('fake_ref_0')) _init_patch(cut_tree) self.assertObjEquals([test_obj], cut_tree)
def test_multi_inter_obj(self): key_vals = {'fake_key': 'fake_val'} obj_dict = {'obj_id': key_vals} test_dict = { 'fake_ref_0': obj_dict, 'fake_ref_1': obj_dict, 'fake_ref_2': obj_dict } node_list = [] for i in range(0, 3): temp_inter = InterNode('fake_path_{}'.format(i)) temp_obj = ObjNode('obj_id', **key_vals) _patch_ne(temp_obj) _patch_ne(temp_inter) temp_obj.addParent(temp_inter) temp_inter.addChild(temp_obj) node_list.append(temp_inter) self.helper_mock.return_value = { 'fake_ref_0': 'fake_path_0', 'fake_ref_1': 'fake_path_1', 'fake_ref_2': 'fake_path_2' } cut_tree = obj_tree._dfs_cut(test_dict, InterNode('fake_ref_0')) _init_patch(cut_tree) self.assertObjEquals(node_list, cut_tree)
def _dfs_cut(config, refNode=None): ''' This iterates over the config and converts the dictionaries into nodes of a tree. Args: config (dict): configuration data Returns (list): List comprised of each node in the tree. Left side has the highest nodes and right side has the lowest nodes. ''' if not isinstance(config, dict): return vals = list(config.values()) if not len(vals): return if len(vals) == 1 and not isinstance(vals[0], dict): return if refNode: ref_props = a10_helper.get_ref_props(refNode.ref) else: ref_props = {} node_list = [] for k, v in config.items(): if k in ref_props: mod_fqdn = _extract_modname(ref_props[k]) # Only case in which the keyword is not a string # is when the keyword is being used as an identifier. # These id's do not need to be stored within the InterNode # at this point. inter_val_dict = {} if isinstance(v, dict): for kw, vl in v.items(): if isinstance(kw, str): inter_val_dict[kw] = vl inNode = InterNode(mod_fqdn, **inter_val_dict) child_obj_list = _dfs_cut(v, inNode) if child_obj_list: for child in child_obj_list: inNode.addChild(child) node_list.append(inNode) elif isinstance(v, dict): tempNode = ObjNode(k, **v) child_obj_list = _dfs_cut(v, refNode) if child_obj_list: for child in child_obj_list: tempNode.addChild(child) node_list.append(tempNode) return node_list
def test_multi_mix_multi_mix(self): key_vals = {'fake_key': 'fake_val'} test_dict = { 'fake_ref_0': { 'obj_id_1': key_vals, 'fake_ref_2': key_vals }, 'obj_id_0': { 'fake_ref_3': key_vals, 'fake_ref_4': key_vals, 'obj_id_2': key_vals }, 'fake_ref_1': { 'obj_id_3': key_vals } } inter_0 = InterNode('fake_path_0') inter_1 = InterNode('fake_path_1') inter_2 = InterNode('fake_path_2', **key_vals) inter_3 = InterNode('fake_path_3', **key_vals) inter_4 = InterNode('fake_path_4', **key_vals) obj_0 = ObjNode('obj_id_0') obj_1 = ObjNode('obj_id_1', **key_vals) obj_2 = ObjNode('obj_id_2', **key_vals) obj_3 = ObjNode('obj_id_3', **key_vals) inter_node_list = [inter_0, inter_1, inter_2, inter_3, inter_4] _init_patch(inter_node_list) obj_node_list = [obj_0, obj_1, obj_2, obj_3] _init_patch(obj_node_list) inter_0.addChild(obj_1) inter_0.addChild(inter_2) obj_0.addChild(inter_3) obj_0.addChild(inter_4) obj_0.addChild(obj_2) inter_1.addChild(obj_3) node_list = [inter_0, obj_0, inter_1] self.helper_mock.return_value = { 'fake_ref_0': 'fake_path_0', 'fake_ref_1': 'fake_path_1', 'fake_ref_2': 'fake_path_2', 'fake_ref_3': 'fake_path_3', 'fake_ref_4': 'fake_path_4' } cut_tree = obj_tree._dfs_cut(test_dict, InterNode('fake_ref_0')) _init_patch(cut_tree) self.assertObjEquals(node_list, cut_tree)
def test_obj_inter(self): key_vals = {'fake_key': 'fake_val'} test_dict = {'obj_id': {'fake_ref': key_vals}} test_obj = ObjNode('obj_id') test_inter = InterNode('fake_path', **key_vals) _patch_ne(test_obj) _patch_ne(test_inter) test_inter.addParent(test_obj) test_obj.addChild(test_inter) self.helper_mock.return_value = {'fake_ref': 'fake_path'} # test_inter is passed here to take the place of the root node cut_tree = obj_tree._dfs_cut(test_dict, test_inter) _init_patch(cut_tree) self.assertObjEquals([test_obj], cut_tree)
def test_obj_no_ref_call(self): key_vals = {'fake_key': 'fake_val'} test_dict = {'obj_id': key_vals} test_obj = ObjNode('obj_id', **key_vals) _patch_ne(test_obj) self.helper_mock.return_value = {} cut_tree = obj_tree._dfs_cut(test_dict) helper.get_ref_props.assert_not_called()
def test_obj(self): key_vals = {'fake_key': 'fake_val'} test_dict = {'obj_id': key_vals} test_obj = ObjNode('obj_id', **key_vals) _patch_ne(test_obj) self.helper_mock.return_value = {} cut_tree = obj_tree._dfs_cut(test_dict) _init_patch(cut_tree) self.assertObjEquals([test_obj], cut_tree)
def dfs_cut(obj, refNode=None): ''' This iterates over the tree and extracts refrence objects out of it ''' if type(obj) != dict: return vals = list(obj.values()) if len(vals) == 1 and type(vals[0]) != dict: return if refNode: ref_props = a10_helper.get_ref_props(refNode.ref) else: ref_props = {} node_list = [] for k,v in obj.items(): if k in ref_props: mod_fqdn = extract_modname(ref_props[k]) inNode = InterNode(mod_fqdn) child_obj_list = dfs_cut(v, inNode) if child_obj_list: for child in child_obj_list: inNode.addChild(child) node_list.append(inNode) elif type(v) == dict: tempNode = ObjNode(k, **v) child_obj_list = dfs_cut(v, refNode) if child_obj_list: for child in child_obj_list: tempNode.addChild(child) node_list.append(tempNode) return node_list
def test_obj_obj(self): key_vals = {'fake_key': 'fake_val'} test_dict = {'obj_id': {'obj_id_2': key_vals}} test_obj = ObjNode('obj_id') test_obj_2 = ObjNode('obj_id_2', **key_vals) _patch_ne(test_obj) _patch_ne(test_obj_2) test_obj_2.addParent(test_obj) test_obj.addChild(test_obj_2) cut_tree = obj_tree._dfs_cut(test_dict) _init_patch(cut_tree) self.assertObjEquals([test_obj], cut_tree)