Esempio n. 1
0
    def test_add_transactions(self):
        t = Tree()
        trans1 = Transaction(0, 1, 2)
        self.assertTrue(t.add_transaction(trans1))
        self.assertEqual(t.depth, 0)

        trans2 = Transaction(1, 2, 3)
        self.assertTrue(t.add_transaction(trans2))
        self.assertEqual(t.depth, 1)

        trans3 = Transaction(2, 3, 4)
        self.assertTrue(t.add_transaction(trans3))
        self.assertEqual(t.depth, 2)

        trans4 = Transaction(3, 4, 5)
        self.assertTrue(t.add_transaction(trans4))
        self.assertEqual(t.depth, 2)

        self.assertEqual(len(list(Tree.walk(t.root))), 7)

        expected_remaining_transactions_before_failure = 2**Tree.MAX_DEPTH - 4

        for i in range(expected_remaining_transactions_before_failure):
            tx = Transaction(0, 0, 1)
            t.add_transaction(tx)

        # Cannot add a fifth transaction, since the MAX_DEPTH equals to 2
        trans5 = Transaction(4, 5, 6)
        self.assertFalse(t.add_transaction(trans5))
        self.assertEqual(t.depth, Tree.MAX_DEPTH)
 def test_update_subtree(self):
     tree = Tree.from_json(self.tree_demo_twente_strategy)
     subtree = Tree.from_json(self.tree_simple_tree)
     tree.update_subtree(subtree, tree.root, subtree.root)
     assert 2 == len(tree.nodes)
     tree.update_subtree(subtree, tree.root)
     assert 2 == len(tree.nodes)
 def test_remove_node_by_id(self):
     tree = Tree.from_json(self.tree_dance_strategy)
     assert True is tree.remove_node_by_id("sftlsc94h3q1p")
     assert "sftlsc94h3q1p" not in tree.nodes.keys()
     # remove the root and check if all root is set to ''
     tree = Tree.from_json(self.tree_dance_strategy)
     assert True is tree.remove_node_by_id(tree.root)
     assert tree.root == ''
 def test_add_subtree(self):
     tree = Tree.from_json(self.tree_simple_tree)
     subtree = Tree.from_json(self.tree_demo_twente_strategy)
     main_node = tree.nodes.get('1')
     tree.add_subtree(subtree, main_node.id)
     assert len(main_node.children) == 1
     repeater_node = tree.nodes.get(main_node.children[0])
     assert repeater_node.title == 'Repeater'
     assert len(repeater_node.children) == 1
     parallel_sequence_node = tree.nodes.get(repeater_node.children[0])
     assert 'ParallelSequence' == parallel_sequence_node.title
     assert 2 == len(parallel_sequence_node.children)
 def test_write_tree(self, tmpdir):
     collection = Collection.from_path(self.path)
     tree = self.attack_strategy
     collection.write_tree(tree, Path(tmpdir) / "test.json")
     path = tmpdir / 'test.json'
     read = Tree.from_json(read_json(path))
     assert tree == read
 def test_remove_propagate(self):
     tree = Tree.from_json(self.tree_demo_twente_strategy)
     tree.propagate_role(tree.root, "t")
     tree.remove_propagation(tree.root)
     for node_id, node in tree.nodes.items():
         if node_id != tree.root:
             assert 'ROLE' not in node.attributes['properties']
 def test_write_collection_new_file_new_dir(self, tmpdir):
     collection = Collection({"roles": {"Role.json": Tree("Role", "1", {"1": Node("1", "1")})},
                              "tactics": {},
                              "strategies": {},
                              "keeper": {}})
     collection.write_collection(tmpdir)
     collection.path = tmpdir
     assert collection == Collection.from_path(tmpdir)
 def test_remove_tree_by_name_not_exists(self):
     collection = Collection.from_path(self.path)
     # add a tree, so the if statement goes to the else
     collection.add_tree("roles", "test", Tree('roles', '1'))
     collection.remove_tree_by_name('roles', "Assister")
     assert "Assister.json" not in collection.collection.get('roles')
     collection.remove_tree_by_name('roles', "Assister")
     assert "Assister.json" not in collection.collection.get('roles')
 def test_find_role_subtree_nodes_if_exist(self):
     tree = Tree.from_json(self.tree_demo_twente_strategy)
     assert [] == tree.find_role_subtree_nodes_if_exist('abcd')
     tree = tree.from_json(self.tree_enter_formation_tactic)
     nodes = tree.find_role_subtree_nodes_if_exist('EnterFormationRole')
     assert 7 == len(nodes)
     assert tree.nodes.get('0ia3adfsyai4m') in nodes
     assert tree.nodes.get('mz9t0qn1r1brww') in nodes
     assert tree.nodes.get('3j1eplzumct1ky2l') not in nodes
 def test_write_collection_new_file(self, tmpdir):
     collection = Collection.from_path(self.path)
     collection.write_collection(tmpdir)
     collection.add_tree("roles", "tree.json", Tree("name", "1", {"1": Node("node", "1")}))
     collection.write_collection(tmpdir)
     read = Collection.from_path(tmpdir)
     # sets path equal, so objects are equal
     collection.path = None
     read.path = None
     assert read == collection
Esempio n. 11
0
    def test_tree_serialize(self):
        t = Tree()
        u = Tree.from_json(t.to_json())

        self.assertEqual(t.root.get_hash(), u.root.get_hash())

        trans1 = Transaction(0, 1, 2)
        self.assertTrue(t.add_transaction(trans1))
        trans2 = Transaction(1, 2, 3)
        self.assertTrue(t.add_transaction(trans2))
        trans3 = Transaction(2, 3, 4)
        self.assertTrue(t.add_transaction(trans3))

        u = Tree.from_json(t.to_json())
        self.assertEqual(t.root._child_1.get_hash(),
                         u.root._child_1.get_hash())
        self.assertEqual(t.root.get_hash(), u.root.get_hash())
 def test_from_json_invalid_wrong_attribute_types(self):
     with pytest.raises(InvalidTreeJsonFormatException):
         Tree.from_json(self.tree_wrong_name_type)
     with pytest.raises(InvalidTreeJsonFormatException):
         Tree.from_json(self.tree_wrong_root_type)
     with pytest.raises(InvalidTreeJsonFormatException):
         Tree.from_json(self.tree_wrong_title_type)
Esempio n. 13
0
    def read_tree(self, line_parents, line_label, line_words, line_relations):
        parents = list(map(int, line_parents.split()))
        if line_label:
            labels = list(map(self.parse_label, line_label.split()))
        else:
            labels = None
        words = line_words.split()
        relations = line_relations.split()
        trees = dict()
        root = None

        for i in range(1, len(parents) + 1):
            if i not in trees.keys():
                idx = i
                prev = None
                while True:
                    parent = parents[idx - 1]
                    tree = Tree()
                    if prev:
                        tree.add_child(prev)
                    trees[idx] = tree
                    tree.idx = idx
                    if labels:
                        tree.gold_label = labels[idx - 1]
                    else:
                        tree.gold_label = None
                    tree.word = words[idx - 1]
                    tree.relation = relations[idx - 1]
                    if parent in trees.keys():
                        trees[parent].add_child(tree)
                        break
                    elif parent == 0:
                        root = tree
                        break
                    else:
                        prev = tree
                        idx = parent
        # helper for visualization
        root._viz_all_children = trees
        root._viz_sentence = words
        root._viz_relations = relations
        root._viz_labels = labels
        return root
 def test_remove_node_and_children_by_id(self):
     # test removing a node that does not exist
     tree = Tree.from_json(self.tree_dance_strategy)
     assert False is tree.remove_node_and_children_by_id("non_existing_id")
     # test removing a node without children
     tree.add_node(Node("test_node", "test_node"))
     assert True is tree.remove_node_and_children_by_id("test_node")
     # test removing a node  with non existing children
     tree.add_node(Node("test_node", "test_node", children=['non_existing_node']))
     assert False is tree.remove_node_and_children_by_id("test_node")
     # remove the root and check if all nodes are removed
     assert True is tree.remove_node_and_children_by_id(tree.root)
     assert tree.root == ''
     assert len(tree.nodes) == 0
Esempio n. 15
0
    def read_tree(self, line):
        """
        Parse tree
        Return:
            Tree
        """
        parents = list(map(int, line.split()))
        # sanity check
        assert max(parents) <= len(
            parents), 'Index should be smaller than length! {}'.format(
                ' '.join(parents))
        trees = dict()
        root = None
        for i in range(1, len(parents) + 1):
            if i - 1 not in trees.keys() and parents[i - 1] != -1:
                idx = i
                prev = None
                while True:
                    parent = parents[idx - 1]
                    if parent == -1:
                        break
                    tree = Tree()
                    if prev is not None:
                        tree.add_child(prev)
                    trees[idx - 1] = tree
                    tree.idx = idx - 1
                    if parent - 1 in trees.keys():
                        trees[parent - 1].add_child(tree)
                        break
                    elif parent == 0:
                        root = tree
                        break
                    else:
                        prev = tree
                        idx = parent

        return root
Esempio n. 16
0
 def create_tree(self, category: str):
     """
     Method called when a new tree is created
     :param category: the category of the tree
     """
     name = Dialogs.text_input_dialog('Choose Tree name',
                                      'Choose a name for the tree',
                                      "[A-Za-z0-9_+-]+")
     if not name:
         return
     else:
         filename = name + '.json'
         tree = Tree(name, '')
         self.main_window.show_tree(category, filename, tree)
         self.build_menu_bar()
Esempio n. 17
0
    def test_locate_transaction(self):
        t = Tree()
        trans1 = Transaction(0, 1, 2)
        self.assertTrue(t.add_transaction(trans1))

        trans2 = Transaction(1, 2, 3)
        self.assertTrue(t.add_transaction(trans2))
        self.assertEqual(t.depth, 1)

        self.assertTrue(t.is_present(trans1))

        trans3 = Transaction(0, 1, 2)
        self.assertFalse(t.is_present(trans3))
 def test_find_parent_node_if_exists(self):
     tree = Tree.from_json(self.tree_demo_twente_strategy)
     assert not tree.find_parent_node_if_exists(tree.nodes.get(tree.root))
     assert tree.nodes.get(tree.root) == tree.find_parent_node_if_exists(tree.nodes.get('abcdefgh314'))
 def test_propagate_role_invalid(self):
     tree = Tree.from_json(self.tree_demo_twente_strategy)
     tree.propagate_role("abcd", "t")
     for node_id, node in tree.nodes.items():
         assert 'properties' not in node.attributes
 def test_remove_node_by_id_not_existent(self):
     tree = Tree.from_json(self.tree_dance_strategy)
     tree.remove_node_by_id("non existent node")
     assert "non existent node" not in tree.nodes.keys()
Esempio n. 21
0
    def test_create_tree(self):
        t = Tree()
        self.assertEqual(t.depth, 0)

        self.assertEqual(len(list(Tree.walk(t.root))), 1)
 def test_find_role_subtree_node_above_node(self):
     tree = Tree.from_json(self.tree_demo_twente_strategy)
     assert not tree.find_role_subtree_node_above_node(tree.nodes.get('abcdefgh314'))
     tree = Tree.from_json(self.tree_enter_formation_tactic)
     assert tree.nodes.get('0ia3adfsyai4m') == \
         tree.find_role_subtree_node_above_node(tree.nodes.get('3j1eplzumct1ky2l'))
class TestVerification(object):

    path = Path("json/collection/")
    assister_role = Tree.from_json(read_json(Path('json/collection/roles/Assister.json')))
    attack_strategy = Tree.from_json(read_json(Path('json/collection/strategies/AttackStrategy.json')))
    attactic_tactic = Tree.from_json(read_json(Path('json/collection/tactics/Attactic.json')))

    # invalid trees
    simple_cyclic_tree = Tree.from_json(read_json(Path('json/verification/SimpleCyclicTree.json')))
    simple_unconnected_tree = Tree.from_json(read_json(Path('json/verification/SimpleTreeWithUnconnectedNodes.json')))
    simple_invalid_composites_tree = Tree.from_json(read_json(Path('json/verification/InvalidCompositesTree.json')))
    simple_invalid_decorator_tree = Tree.from_json(read_json(Path('json/verification/InvalidDecoratorTree.json')))
    # 1 has a failing node with a non matching property
    simple_invalid_role_inheritance_tree_1 = Tree.from_json(read_json(
        Path('json/verification/InvalidRoleInheritanceTree1.json')))
    # 2 has a failing node without the properties key
    simple_invalid_role_inheritance_tree_2 = Tree.from_json(read_json(
        Path('json/verification/InvalidRoleInheritanceTree2.json')))
    # 1 Has no root node defined
    simple_invalid_root_node_tree1 = Tree.from_json(read_json(Path(
        'json/verification/InvalidRootNodeTree1.json')))
    # 2 Has a root node which doesn't exist in the list of nodes
    simple_invalid_root_node_tree2 = Tree.from_json(read_json(Path(
        'json/verification/InvalidRootNodeTree2.json')))

    # SSR-Tree
    ssr_tree = Tree.from_json(read_json(Path('json/verification/StrategyStrategyRoleTree.json')))
    # TTR-Tree
    ttr_tree = Tree.from_json(read_json(Path('json/verification/TacticTacticRoleTree.json')))
    # RR-Tree
    rr_tree = Tree.from_json(read_json(Path('json/verification/RoleRoleTree.json')))

    # valid trees
    simple_non_cyclic_tree = Tree.from_json(read_json(Path('json/verification/SimpleNonCyclicTree.json')))
    complex_tree = Tree.from_json(read_json(Path('json/verification/SimpleDefendTactic.json')))
    offensive_strategy_tree = Tree.from_json(read_json(Path('json/verification/OffensiveStrategy.json')))
    keeper_strategy_tree = Tree.from_json(read_json(Path('json/jsons/strategies/KeeperStrategy.json')))

    collection: Dict[str, Dict[str, Tree]] = {
        "roles": {"Assister.json": assister_role, "InvalidCompositesTree.json": simple_invalid_composites_tree,
                  "RoleRoleTree.json": rr_tree},
        "strategies": {"AttackStrategy.json": attack_strategy, "OffensiveStrategy.json": offensive_strategy_tree,
                       "KeeperStrategy.json": keeper_strategy_tree,
                       "StrategyStrategyRole.json": ssr_tree},
        "tactics": {"Attactic.json": attactic_tactic, "SimpleDefendTactic.json": complex_tree,
                    "InvalidRoleInheritanceTree1": simple_invalid_role_inheritance_tree_1,
                    "TacticTacticRole.json": ttr_tree}
    }

    def test_simple_tree_with_cycle(self):
        tree = self.simple_cyclic_tree
        assert 1 is len(Verification.contains_cycles(tree, {}))

    def test_simple_valid_tree(self):
        collection = Collection(self.collection)
        tree = self.simple_non_cyclic_tree
        assert 0 is len(collection.verify_tree(tree))

    def test_simple_unconnected_tree(self):
        tree = self.simple_unconnected_tree
        assert 1 is len(Verification.has_unconnected_nodes(tree))

    def test_invalid_role_inheritance_tree_1(self):
        collection = Collection(self.collection)
        tree = self.simple_invalid_role_inheritance_tree_1
        assert 0 is not len(collection.verify_tree(tree))

    def test_invalid_role_inheritance_tree_2(self):
        collection = Collection(self.collection)
        tree = self.simple_invalid_role_inheritance_tree_2
        assert 0 is not len(collection.verify_tree(tree))

    def test_invalid_composites_tree(self):
        collection = Collection(self.collection)
        tree = self.simple_invalid_composites_tree
        assert 0 is not len(collection.verify_tree(tree, "roles"))

    def test_invalid_decorator_tree(self):
        collection = Collection(self.collection)
        tree = self.simple_invalid_decorator_tree
        assert 0 is not len(collection.verify_tree(tree, "roles"))

    def test_complex_tree(self):
        collection = Collection(self.collection)
        tree = self.complex_tree
        result = collection.get_category_from_node(tree.root)
        assert "tactics" == result
        assert 0 is len(collection.verify_tree(tree, "tactics"))

    def test_offensive_strategy(self):
        collection = Collection(self.collection)
        tree = self.offensive_strategy_tree
        assert 0 is len(collection.verify_tree(tree, "strategies"))

    def test_incorrect_root_nodes_by_writing(self):
        collection = Collection(self.collection)
        tree1 = self.simple_invalid_root_node_tree1
        tree2 = self.simple_invalid_root_node_tree2
        assert len(collection.write_tree(tree1, Path('json/verification/InvalidRootNodeTree1.json'), True)) != 0
        assert len(collection.write_tree(tree2, Path('json/verification/InvalidRootNodeTree2.json'), True)) != 0

    def test_ssr_tree(self):
        collection = Collection(self.collection)
        tree = self.ssr_tree
        assert 1 is len(collection.verify_tree(tree, "strategies"))

    def test_ttr_tree(self):
        collection = Collection(self.collection)
        tree = self.ttr_tree
        assert 1 is len(collection.verify_tree(tree, "tactics"))

    def test_rr_tree(self):
        collection = Collection(self.collection)
        tree = self.rr_tree
        assert 1 is len(collection.verify_tree(tree, "roles"))

    def test_walk_tree(self):
        tree = self.simple_non_cyclic_tree
        assert 3 == len(Verification.walk_tree(tree, tree.nodes.get(tree.root)))
        tree.nodes.get(tree.root).add_child('abcdeffg')
        assert 3 == len(Verification.walk_tree(tree, tree.nodes.get(tree.root)))
        tree.nodes.get(tree.root).add_child(tree.root)
        assert 3 == len(Verification.walk_tree(tree, tree.nodes.get(tree.root)))
 def test_add_tree_folder_not_exists(self):
     collection = Collection.from_path(self.path)
     tree = Tree("name", "1", {"1": Node("node", "1")})
     collection.add_tree("test", "tree.json", tree)
     assert "tree.json" in collection.collection.get('test')
 def test_create_json1(self):
     tree = Tree.from_json(self.tree_dance_strategy)
     assert self.tree_dance_strategy == tree.create_json()
 def test_create_json2(self):
     tree = Tree.from_json(self.tree_demo_twente_strategy)
     assert self.tree_demo_twente_strategy == tree.create_json()
 def test_create_json3(self):
     tree = Tree.from_json(self.tree_simple_tree)
     assert self.tree_simple_tree == tree.create_json()
class TestCollection(object):
    path = Path("json/collection/")
    complete_path = Path('json/jsons/')
    assister_role = Tree.from_json(read_json(Path('json/collection/roles/Assister.json')))
    attack_strategy = Tree.from_json(read_json(Path('json/collection/strategies/AttackStrategy.json')))
    attactic_tactic = Tree.from_json(read_json(Path('json/collection/tactics/Attactic.json')))
    collection: Dict[str, Dict[str, Tree]] = {
        "roles": {"Assister.json": assister_role},
        "strategies": {"AttackStrategy.json": attack_strategy},
        "tactics": {"Attactic.json": attactic_tactic},
        "keeper": {}
    }

    def test_from_path(self):
        collection = Collection.from_path(self.path)
        # collection should not contain the invalid roles/InvalidRole.json
        collection.path = None
        assert 'InvalidRole' not in collection.collection.get('roles').keys()
        assert Collection(self.collection) == collection

    def test_from_path_default(self):
        def_json = Settings.default_json_folder()
        Settings.alter_default_json_folder(self.path)
        collection = Collection.from_path()
        Settings.alter_default_json_folder(def_json)
        # collection should not contain the invalid roles/InvalidRole.json
        assert 'InvalidRole' not in collection.collection.get('roles').keys()
        assert Collection(self.collection) == collection

    def test_build_collection(self):
        collection = Collection()
        collection.build_collection(self.path)
        # check if hidden file is not added to collection
        assert '.hiddendir' not in collection.collection.keys()
        assert '_categoryunderscore' not in collection.collection.keys()
        assert '.hiddenTree.json' not in collection.collection.get('roles')
        # check if file with wrong file extension is not added
        assert 'TreeWithoutJsonFileExtension' not in collection.collection.get('roles')
        assert Collection(self.collection) == collection

    def test_write_collection(self, tmpdir):
        collection = Collection.from_path(self.path)
        collection.write_collection(tmpdir)
        read = Collection.from_path(tmpdir)
        # sets path equal, so objects are equal
        collection.path = None
        read.path = None
        assert read == collection

    def test_write_collection_default_path1(self, tmpdir):
        def_path = Settings.default_json_folder()
        Settings.alter_default_json_folder(tmpdir)
        collection = Collection.from_path()
        collection.write_collection()
        read = Collection.from_path(tmpdir)
        Settings.alter_default_json_folder(def_path)
        # sets path equal, so objects are equal
        collection.path = None
        read.path = None
        assert collection == read

    def test_write_collection_default_path2(self, tmpdir):
        collection = Collection.from_path()
        collection.path = tmpdir
        collection.write_collection()
        read = Collection.from_path(tmpdir)
        # sets path equal, so objects are equal
        collection.path = None
        read.path = None
        assert collection == read

    def test_write_collection_new_file(self, tmpdir):
        collection = Collection.from_path(self.path)
        collection.write_collection(tmpdir)
        collection.add_tree("roles", "tree.json", Tree("name", "1", {"1": Node("node", "1")}))
        collection.write_collection(tmpdir)
        read = Collection.from_path(tmpdir)
        # sets path equal, so objects are equal
        collection.path = None
        read.path = None
        assert read == collection

    def test_write_collection_new_file_new_dir(self, tmpdir):
        collection = Collection({"roles": {"Role.json": Tree("Role", "1", {"1": Node("1", "1")})},
                                 "tactics": {},
                                 "strategies": {},
                                 "keeper": {}})
        collection.write_collection(tmpdir)
        collection.path = tmpdir
        assert collection == Collection.from_path(tmpdir)

    def test_add_tree_folder_exists(self):
        collection = Collection.from_path(self.path)
        tree = Tree("name", "1", {"1": Node("node", "1")})
        collection.add_tree("keeper", "tree.json", tree)
        assert "tree.json" in collection.collection.get('keeper')

    def test_add_tree_folder_not_exists(self):
        collection = Collection.from_path(self.path)
        tree = Tree("name", "1", {"1": Node("node", "1")})
        collection.add_tree("test", "tree.json", tree)
        assert "tree.json" in collection.collection.get('test')

    def test_remove_tree_exists(self):
        collection = Collection.from_path(self.path)
        assert "Assister.json" in collection.collection.get('roles')
        collection.remove_tree("roles", "Assister.json")
        assert "Assister.json" not in collection.collection.get('roles')

    def test_remove_tree_not_exists(self):
        collection = Collection.from_path(self.path)
        collection.remove_tree("roles", "Assister.json")
        assert "Assister.json" not in collection.collection.get('roles')
        collection.remove_tree("roles", "Assister.json")
        assert "Assister.json" not in collection.collection.get('roles')

    def test_remove_tree_by_name_exists(self):
        collection = Collection.from_path(self.path)
        assert "Assister.json" in collection.collection.get('roles')
        collection.remove_tree_by_name('roles', "Assister")
        assert "Assister.json" not in collection.collection.get('roles')

    def test_remove_tree_by_name_not_exists(self):
        collection = Collection.from_path(self.path)
        # add a tree, so the if statement goes to the else
        collection.add_tree("roles", "test", Tree('roles', '1'))
        collection.remove_tree_by_name('roles', "Assister")
        assert "Assister.json" not in collection.collection.get('roles')
        collection.remove_tree_by_name('roles', "Assister")
        assert "Assister.json" not in collection.collection.get('roles')

    def test_get_tree_by_name(self):
        collection = Collection.from_path(self.path)
        assert not collection.get_tree_by_name('abcdefgh')
        assert collection.get_tree_by_name('Assister') == TestCollection.assister_role

    def test_remove_tree_by_name_dir_not_exists(self):
        collection = Collection.from_path(self.path)
        collection.remove_tree_by_name("test", "Assister")
        assert "test" not in collection.collection.keys()

    def test_remove_tree_by_name_tree_not_exists(self):
        collection = Collection.from_path(self.path)
        collection.remove_tree_by_name('roles', "Assister")
        assert "Assister.json" not in collection.collection.get('roles')
        collection.remove_tree_by_name('roles', "Assister")
        assert "Assister.json" not in collection.collection.get('roles')

    def test_categories_and_filenames(self):
        collection = Collection(self.collection)
        collection.collection['keeper'] = {}
        categories_and_filenames = {
            'roles': ['Assister.json'],
            'tactics': ['Attactic.json'],
            'strategies': ['AttackStrategy.json'],
            'keeper': []
        }
        assert categories_and_filenames == collection.categories_and_filenames()

    def test_get_root_nodes_by_category(self):
        collection = Collection.from_path(self.path)
        result = collection.get_root_nodes_by_category("strategies")
        assert "ydjw9of7ndf88" == result[0][0]
        assert "AttackStrategy" == result[0][1]
        result = collection.get_root_nodes_by_category("tactics")
        assert "57mrxn20qviax5qc" == result[0][0]
        assert "Attactic" == result[0][1]
        result = collection.get_root_nodes_by_category("roles")
        assert "sx6fvrxlaoudhmmq9" == result[0][0]
        assert "Assister" == result[0][1]
        result = collection.get_root_nodes_by_category("invalid_category")
        assert len(result) == 0

    def test_get_category_from_node(self):
        collection = Collection.from_path(self.path)
        result = collection.get_category_from_node("ydjw9of7ndf88")
        assert result == "strategies"
        result = collection.get_category_from_node("57mrxn20qviax5qc")
        assert result == "tactics"
        result = collection.get_category_from_node("sx6fvrxlaoudhmmq9")
        assert result == "roles"
        result = collection.get_category_from_node("invalid_category")
        assert not result

    def test_verify_trees(self):
        collection = Collection.from_path(Settings.default_json_folder(), only_verify_mathematical_properties=False)
        for category in collection.collection:
            for file in collection.collection[category]:
                # skip these two files as there is a decorator with two children
                if not (file == "GetBallTestTactic.json" or file == "GetBallTestStrategy.json"):
                    assert 0 is len(collection.verify_tree(collection.collection[category][file], category))

    def test_write_tree(self, tmpdir):
        collection = Collection.from_path(self.path)
        tree = self.attack_strategy
        collection.write_tree(tree, Path(tmpdir) / "test.json")
        path = tmpdir / 'test.json'
        read = Tree.from_json(read_json(path))
        assert tree == read

    def test_json_path(self):
        collection = Collection.from_path(self.path)
        assert collection.jsons_path() == self.path
        collection = Collection.from_path()
        assert collection.jsons_path() == Settings.default_json_folder()

    def test_update_subtrees_in_collection_from_main_tree(self):
        collection = Collection.from_path(self.complete_path)
        collection_copy = deepcopy(collection)
        collection_copy.update_subtrees_in_collection(collection_copy.get_tree_by_name('EnterFormationRole'))
        tree = collection_copy.get_tree_by_name('EnterFormationTactic')
        assert collection_copy.get_tree_by_name('EnterFormationTactic') != \
            collection.get_tree_by_name('EnterFormationTactic')
        old_tree = collection.get_tree_by_name('EnterFormationTactic')
        role_nodes = tree.find_role_subtree_nodes_if_exist('EnterFormationRole')
        for role_node in role_nodes:
            child = role_node.children[0]
            assert child not in old_tree.nodes
            child_node = tree.nodes.get(child)
            old_child_id = old_tree.nodes.get(role_node.id).children[0]
            old_child_node = old_tree.nodes.get(old_child_id)
            assert old_child_node.title == child_node.title
            assert old_child_node.id != child_node.id
            assert len(old_child_node.children) == len(child_node.children)

    def test_update_subtrees_in_collection_from_subtree(self):
        collection = Collection.from_path(self.complete_path)
        tree = collection.get_tree_by_name('EnterFormationTactic')
        tree.nodes.get('x988e2xb3y8h0hmxq').title = 'TestChange'
        collection.update_subtrees_in_collection(tree, tree.nodes.get('l795jdit0tls4k52'))
        role_tree = collection.get_tree_by_name('EnterFormationRole')
        assert len(role_tree.nodes.items()) == 2
        root_node = role_tree.nodes.get(role_tree.root)
        assert root_node.title == 'TestChange'
        assert len(root_node.children) == 1
        assert role_tree.nodes.get(root_node.children[0]).title == 'EnterFormation'

    def test_update_subtrees_in_collection_role_propagation(self):
        # check if role propagation is skipped
        collection = Collection.from_path(self.complete_path)
        node = Node('Role', attributes={'role': 'EnterFormationRole'})
        tree = collection.get_tree_by_name('DemoTeamTwenteStrategy')
        tree.add_node(node)
        tree.nodes.get(tree.root).add_child(node.id)
        other_tree = collection.get_tree_by_name('EnterFormationRole')
        collection.update_subtrees_in_collection(other_tree)
        child = tree.nodes.get(node.children[0])
        assert 'properties' not in child.attributes

    def test_update_subtrees_in_collection_invalid(self):
        # no role subtree
        collection = Collection.from_path(self.complete_path)
        collection_copy = deepcopy(collection)
        tree = collection.get_tree_by_name('DemoTeamTwenteStrategy')
        collection.update_subtrees_in_collection(tree, tree.nodes.get(tree.root))
        assert collection_copy == collection
        # role subtree no children
        node = Node('Role', attributes={'role': 'EnterFormationRole'})
        tree_copy = deepcopy(tree)
        tree.add_node(node)
        tree.nodes.get(tree.root).add_child(node.id)
        collection.update_subtrees_in_collection(tree, node)
        collection.collection['strategies']['DemoTeamTwenteStrategy.json'] = tree_copy
        assert collection == collection_copy
 def test_str(self):
     tree = Tree.from_json(self.tree_simple_tree)
     assert str(tree.create_json()) == str(tree)
     assert str(tree.create_json()) == repr(tree)
 def test_find_role_subtree_below_node(self):
     tree = Tree.from_json(self.tree_enter_formation_tactic)
     assert 7 == len(tree.find_role_subtree_nodes_below_node(tree.nodes.get(tree.root)))
     tree.nodes.get(tree.root).add_child('abcd')
     assert 7 == len(tree.find_role_subtree_nodes_below_node(tree.nodes.get(tree.root)))