def test_improve_parent_node_not_title(): expected = "Great Title!" root = TreeNode('something') child = TreeNode('title', root) improve_parent('Great Title!', child) assert root.value != expected
def test_dict2tree_with_none_input_on_two_entries_gives_tree_with_two_child(): expected = TreeNode('Root') child_1 = TreeNode('value1: foo', expected) child_2 = TreeNode('value2: bar', expected) dictionary = {'value1': 'foo', 'value2': 'bar'} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_simple_dict_with_integers(): expected = TreeNode('Root') child_1 = TreeNode('value1: 1', expected) child_2 = TreeNode('value2: 2', expected) dictionary = {'value1': 1, 'value2': 2} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_simple_dict(): expected = TreeNode('Root') child_1 = TreeNode('value1: foo', expected) child_2 = TreeNode('value2: bar', expected) dictionary = {'value1': 'foo', 'value2': 'bar'} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_dict2tree_two_entries_appends_two_child_to_given_tree(): expected = TreeNode() child_1 = TreeNode('value1: foo', expected) child_2 = TreeNode('value2: bar', expected) dictionary = {'value1': 'foo', 'value2': 'bar'} result = TreeNode() result = TreeNode.dict2tree(dictionary, result) assert result == expected
def test_dict2tree_list_of_list(): expected = TreeNode('Root') child_1 = TreeNode('list', expected) child_intermediate = TreeNode(0, child_1) child_1_1 = TreeNode('value: foo', child_intermediate) dictionary = {'list': [[{'value': 'foo'}]]} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def test_ancestors(): tree = TreeNode(0) child_1 = TreeNode(1, tree) child_2 = TreeNode(2, child_1) child_3 = TreeNode(3, child_2) expected = [tree, child_1, child_2] result = child_3.ancestors() assert result == expected
def test_dict2tree_dict_with_dict(): expected = TreeNode('Root') child_1 = TreeNode('dict1', expected) child_2 = TreeNode('dict2', child_1) child_3 = TreeNode('value: foo', child_2) dictionary = {'dict1': {'dict2': {'value': 'foo'}}} result = TreeNode.dict2tree(dictionary, None) assert result == expected
def schema2rst(data: dict, excluded_key: str, tree_name: str): """Mimics parser.schema2rst method with string input parameters""" tree = TreeNode(tree_name) rst = RST_DIRECTIVES TreeNode.dict2tree(data, tree, excluded_key) rst += _traverse_bfs(tree, _node2rst) return rst
def test_init_with_string(): expected = "foo" node = TreeNode(expected) result = node.value assert result == expected assert node.lvl == 0 assert node.children == [] assert node.parent is None
def test_search_in_parents_siblings_subtrees(): expected = TreeNode('Searched') root = TreeNode('Root') sibling_1 = TreeNode('Sibling 1', root) searching_leaf = TreeNode('Searching', sibling_1) sibling_2 = TreeNode('Sibling 2', root) unrelated_leaf = TreeNode('Unrelated', sibling_2) searched = TreeNode('Searched', sibling_2) result = searching_leaf.search_in_parents_siblings_subtrees('Searched') assert result == expected
def test_eq_false(): tree1 = TreeNode(1) sub_tree_1_1 = TreeNode(2, tree1) sub_tree_1_2 = TreeNode(4, tree1) tree2 = TreeNode(1) sub_tree_2_1 = TreeNode(2, tree2) sub_tree_2_2 = TreeNode(3, tree2) assert not tree1 == tree2
def test_eq_true(): tree1 = TreeNode(1) sub_tree_1_1 = TreeNode(2, tree1) sub_tree_1_2 = TreeNode(3, tree1) tree2 = TreeNode(1) sub_tree_2_1 = TreeNode(2, tree2) sub_tree_2_2 = TreeNode(3, tree2) assert tree1 == tree2
def schema2rst(schema_file, excluded_key): """ Parse a json/yaml schema file into RST text. Example: with open("schema.json") as schema: print(jsonschema2rst(schema)) Args: schema_file(file): a json or yaml schema file descriptor. excluded_key(string): csv containing schema's keywords to ignore Returns: string: a restructured-text string representing ``schema_file`` """ tree = TreeNode( os.path.basename(change_extension(schema_file.name, JSON_EXTENSION))) rst = RST_DIRECTIVES TreeNode.dict2tree(yaml.load(schema_file), tree, excluded_key) rst += _traverse_bfs(tree, _node2rst) return rst
def test_is_leaf_false(): parent = TreeNode('inner') TreeNode('leaf', parent) assert parent.is_leaf() is False
def test_is_leaf_true(): leaf = TreeNode('leaf') assert leaf.is_leaf() is True
def test_init_without_val(): expected = '' result = TreeNode().value assert result == expected
def test_init_with_integer(): expected = '1' result = TreeNode(1).value assert result == expected
def test_init_with_none(): expected = 'None' result = TreeNode(None).value assert result == expected
def test_init_with_int(): expected = "1" result = TreeNode(1).value assert result == expected
def test_ancestors_empty_list_expected(): expected = [] result = TreeNode().ancestors() assert result == expected
def test_dict2tree_none_dict_gives_just_a_node(): expected = TreeNode('Root') result = TreeNode.dict2tree(None, None) assert result == expected
def test_dict2tree_none_parent_gives_tree_with_standard_root_value(): expected = TreeNode('Root') result = TreeNode.dict2tree({}, None) assert result == expected