def test_get_node_type_by_node(self):
     node_types = NodeTypes.from_csv()
     # assert that the node_type test does not exist
     for category, types in node_types.node_types.items():
         for node_type in types:
             assert node_type[0] != "test"
     assert [] == node_types.get_node_type_by_node(Node("test", "a"))
     # check for a existing node
     assert [('composites', ["Sequence"])] == node_types.get_node_type_by_node(Node('Sequence', 'a'))
 def test_update_properties(self):
     node = Node.from_json(self.node_attributes_no_children_json)
     node.update_properties({"b": 'false'})
     assert 'properties' in node.attributes
     assert 'b' in node.attributes['properties']
     assert 'false' is node.attributes['properties']['b']
     node.update_properties({})
     assert 'properties' not in node.attributes
     node = Node.from_json(self.node_attributes_no_children_json)
     node.update_properties({})
     assert 'properties' not in node.attributes
 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
 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_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_remove_property(self):
     node = Node.from_json(self.node_attributes_children_json)
     assert "properties" not in node.attributes
     node.remove_property("a")
     assert "properties" not in node.attributes
     node.add_property("a", "c")
     assert "properties" in node.attributes
     assert "a" in node.attributes.get("properties")
     node.remove_property("a")
     assert "a" not in node.properties()
 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 create_node_button_clicked(self):
     """
     pyqtSlot that creates a new node by asking for a title and adds it to the view
     """
     if self.app.wait_for_click_filter:
         self.app.wait_for_click_filter.reset_event_filter()
     title = view.windows.Dialogs.text_input_dialog("Create Node",
                                                    "Title of the node:")
     if title or '':
         node = Node(title)
         self.add_node_to_view(node)
 def add_subtree_button_clicked(self):
     """
     pyqtSlot for adding a subtree to the view by selecting the tree in a dialog
     """
     dialog = view.windows.TreeSelectDialog(self.gui.collection)
     category, filename = dialog.show()
     if not (category or filename):
         return
     tree = self.gui.collection.collection.get(category).get(filename)
     category_singular = singularize(capitalize(category))
     node = Node(category_singular, attributes={"name": tree.name})
     # special case for rules, which are defined differently as subtrees as other trees
     if category_singular == 'Role':
         node.attributes['properties'] = {'ROLE': tree.name}
         node.attributes['role'] = tree.name
         self.add_node_to_view(node)
         self.gui.tree.add_subtree(
             self.gui.collection.collection.get('roles').get(filename),
             node.id)
     else:
         self.add_node_to_view(node)
 def test_from_json_wrong_types(self):
     with pytest.raises(InvalidTreeJsonFormatException):
         Node.from_json(self.node_wrong_children_type)
     with pytest.raises(InvalidTreeJsonFormatException):
         Node.from_json(self.node_wrong_id_type)
     with pytest.raises(InvalidTreeJsonFormatException):
         Node.from_json(self.node_wrong_title_type)
 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
class TestNode(object):
    # valid node files and objects
    node_attributes_children = Node("node", "1", {"a": True}, ["2", "3"])
    node_attributes_children_json = read_json(Path('json/nodes/valid/NodeAttributesChildren.json'))
    node_children_no_attributes = Node("node", "1", {}, ["2", "3"])
    node_children_no_attributes_json = read_json(Path('json/nodes/valid/NodeChildrenNoAttributes.json'))
    node_attributes_no_children = Node("node", "1", {"a": True})
    node_attributes_no_children_json = read_json(Path('json/nodes/valid/NodeAttributesNoChildren.json'))
    node_no_attributes_children = Node("node", "1")
    node_no_attributes_children_json = read_json(Path('json/nodes/valid/NodeNoAttributesChildren.json'))

    # invalid node files
    node_no_id = read_json(Path('json/nodes/invalid/NodeNoId.json'))
    node_no_title = read_json(Path('json/nodes/invalid/NodeNoTitle.json'))
    node_wrong_children_type = read_json(Path('json/nodes/invalid/NodeWrongChildrenType.json'))
    node_wrong_id_type = read_json(Path('json/nodes/invalid/NodeWrongIdType.json'))
    node_wrong_title_type = read_json(Path('json/nodes/invalid/NodeWrongTitleType.json'))

    def test_from_json(self):
        assert self.node_attributes_children == Node.from_json(self.node_attributes_children_json)
        assert self.node_attributes_no_children == Node.from_json(self.node_attributes_no_children_json)
        assert self.node_children_no_attributes == Node.from_json(self.node_children_no_attributes_json)
        assert self.node_no_attributes_children == Node.from_json(self.node_no_attributes_children_json)

    def test_id_generator_default_settings(self):
        generated_id = Node.generate_id()
        assert len(generated_id) == 16
        assert all(c.islower() or c.isdigit() for c in generated_id)

    def test_id_generator_custom_settings(self):
        generated_id = Node.generate_id(10, string.ascii_uppercase)
        assert len(generated_id) == 10
        assert all(c.isupper for c in generated_id)

    def test_from_json_incorrect_missing_title(self):
        with pytest.raises(InvalidTreeJsonFormatException):
            Node.from_json(self.node_no_id)

    def test_from_json_incorrect_no_id(self):
        with pytest.raises(InvalidTreeJsonFormatException):
            Node.from_json(self.node_no_title)

    def test_from_json_wrong_types(self):
        with pytest.raises(InvalidTreeJsonFormatException):
            Node.from_json(self.node_wrong_children_type)
        with pytest.raises(InvalidTreeJsonFormatException):
            Node.from_json(self.node_wrong_id_type)
        with pytest.raises(InvalidTreeJsonFormatException):
            Node.from_json(self.node_wrong_title_type)

    def test_add_child(self):
        node = Node.from_json(self.node_attributes_children_json)
        node.add_child("4")
        assert ["2", "3", "4"] == node.children

    def test_remove_child_existent(self):
        node = Node.from_json(self.node_attributes_children_json)
        node.remove_child("2")
        assert ["3"] == node.children

    def test_remove_child_not_existent(self):
        node = Node.from_json(self.node_no_attributes_children_json)
        node.remove_child("2")
        assert [] == node.children

    def test_add_attribute(self):
        node = Node.from_json(self.node_attributes_children_json)
        node.add_attribute("b", False)
        assert {"a": True, "b": False} == node.attributes

    def test_remove_attribute_existent(self):
        node = Node.from_json(self.node_attributes_children_json)
        node.remove_attribute("a")
        assert {} == node.attributes

    def test_remove_attribute_not_existent(self):
        node = Node.from_json(self.node_no_attributes_children_json)
        node.remove_attribute("a")
        assert {} == node.attributes

    def test_create_json(self):
        assert self.node_attributes_children_json == self.node_attributes_children.create_json()
        assert self.node_attributes_no_children_json == self.node_attributes_no_children.create_json()
        assert self.node_children_no_attributes_json == self.node_children_no_attributes.create_json()
        assert self.node_no_attributes_children_json == self.node_no_attributes_children.create_json()

    def test_add_property(self):
        node = Node.from_json(self.node_attributes_children_json)
        node.add_property("b", False)
        assert {"b": False} == node.attributes.get("properties")
        node.add_property("c", "1")
        assert {"b": False, "c": "1"} == node.attributes.get("properties")

    def test_update_properties(self):
        node = Node.from_json(self.node_attributes_no_children_json)
        node.update_properties({"b": 'false'})
        assert 'properties' in node.attributes
        assert 'b' in node.attributes['properties']
        assert 'false' is node.attributes['properties']['b']
        node.update_properties({})
        assert 'properties' not in node.attributes
        node = Node.from_json(self.node_attributes_no_children_json)
        node.update_properties({})
        assert 'properties' not in node.attributes

    def test_remove_property(self):
        node = Node.from_json(self.node_attributes_children_json)
        assert "properties" not in node.attributes
        node.remove_property("a")
        assert "properties" not in node.attributes
        node.add_property("a", "c")
        assert "properties" in node.attributes
        assert "a" in node.attributes.get("properties")
        node.remove_property("a")
        assert "a" not in node.properties()

    def test_properties(self):
        node = Node.from_json(self.node_attributes_no_children_json)
        assert not node.properties()
        node.add_property("a", "b")
        assert {"a": "b"} == node.properties()

    def test_str(self):
        node = Node('b', 'a', {'a': "b"}, ["c"])
        assert str(node) == str(node.create_json())
        assert repr(node) == str(node)
 def test_remove_attribute_not_existent(self):
     node = Node.from_json(self.node_no_attributes_children_json)
     node.remove_attribute("a")
     assert {} == node.attributes
 def test_add_attribute(self):
     node = Node.from_json(self.node_attributes_children_json)
     node.add_attribute("b", False)
     assert {"a": True, "b": False} == node.attributes
 def test_remove_child_not_existent(self):
     node = Node.from_json(self.node_no_attributes_children_json)
     node.remove_child("2")
     assert [] == node.children
 def test_id_generator_default_settings(self):
     generated_id = Node.generate_id()
     assert len(generated_id) == 16
     assert all(c.islower() or c.isdigit() for c in generated_id)
 def test_properties(self):
     node = Node.from_json(self.node_attributes_no_children_json)
     assert not node.properties()
     node.add_property("a", "b")
     assert {"a": "b"} == node.properties()
 def test_from_json_valid3(self):
     tree = Tree("SimpleTree", "1", {"1": Node("Sequence", "1")})
     assert tree == Tree.from_json(self.tree_simple_tree)
 def test_from_json_incorrect_no_id(self):
     with pytest.raises(InvalidTreeJsonFormatException):
         Node.from_json(self.node_no_title)
 def test_add_node(self):
     tree = Tree.from_json(self.tree_dance_strategy)
     tree.add_node(Node("title", "1"))
     assert "1"in tree.nodes.keys()
 def test_remove_node_not_existent(self):
     tree = Tree.from_json(self.tree_dance_strategy)
     assert False is tree.remove_node(Node("title", "non existent node"))
     assert "non existent node" not in tree.nodes.keys()
 def test_id_generator_custom_settings(self):
     generated_id = Node.generate_id(10, string.ascii_uppercase)
     assert len(generated_id) == 10
     assert all(c.isupper for c in generated_id)
 def test_disconnected_node_init(self):
     DisconnectedNode(Node('abcd'))
     DisconnectedNode()
 def test_str(self):
     node = Node('b', 'a', {'a': "b"}, ["c"])
     assert str(node) == str(node.create_json())
     assert repr(node) == str(node)
 def test_add_property(self):
     node = Node.from_json(self.node_attributes_children_json)
     node.add_property("b", False)
     assert {"b": False} == node.attributes.get("properties")
     node.add_property("c", "1")
     assert {"b": False, "c": "1"} == node.attributes.get("properties")
 def test_add_child(self):
     node = Node.from_json(self.node_attributes_children_json)
     node.add_child("4")
     assert ["2", "3", "4"] == node.children
 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_from_json(self):
     assert self.node_attributes_children == Node.from_json(self.node_attributes_children_json)
     assert self.node_attributes_no_children == Node.from_json(self.node_attributes_no_children_json)
     assert self.node_children_no_attributes == Node.from_json(self.node_children_no_attributes_json)
     assert self.node_no_attributes_children == Node.from_json(self.node_no_attributes_children_json)