Github user mxmrlv commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/12#discussion_r86747179
  
    --- Diff: tests/workflows/api/test_task_graph.py ---
    @@ -0,0 +1,714 @@
    +# Licensed to the Apache Software Foundation (ASF) under one or more
    +# contributor license agreements.  See the NOTICE file distributed with
    +# this work for additional information regarding copyright ownership.
    +# The ASF licenses this file to You under the Apache License, Version 2.0
    +# (the "License"); you may not use this file except in compliance with
    +# the License.  You may obtain a copy of the License at
    +#
    +#     http://www.apache.org/licenses/LICENSE-2.0
    +#
    +# Unless required by applicable law or agreed to in writing, software
    +# distributed under the License is distributed on an "AS IS" BASIS,
    +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +# See the License for the specific language governing permissions and
    +# limitations under the License.
    +
    +from uuid import uuid4
    +
    +import pytest
    +
    +from aria.workflows.api import task_graph, task
    +
    +
    +class MockTask(task.BaseTask):
    +    def __init__(self):
    +        self.id = str(uuid4())
    +
    +
    [email protected]
    +def graph():
    +    return task_graph.TaskGraph(name='mock-graph')
    +
    +
    +class TestTaskGraphTasks(object):
    +
    +    def test_add_task(self, graph):
    +        task = MockTask()
    +        add_result = graph.add_tasks(task)
    +        assert add_result == [task]
    +        tasks = [t for t in graph.tasks]
    +        assert len(tasks) == 1
    +        assert tasks[0] == task
    +
    +    def test_add_existing_task(self, graph):
    +        task = MockTask()
    +        graph.add_tasks(task)
    +        # adding a task already in graph - should have no effect, and 
return False
    +        add_result = graph.add_tasks(task)
    +        assert add_result == []
    +        tasks = [t for t in graph.tasks]
    +        assert len(tasks) == 1
    +        assert tasks[0] == task
    +
    +    def test_remove_task(self, graph):
    +        task = MockTask()
    +        other_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(other_task)
    +        graph.remove_tasks(other_task)
    +        tasks = [t for t in graph.tasks]
    +        assert len(tasks) == 1
    +        assert tasks[0] == task
    +
    +    def test_remove_tasks_with_dependency(self, graph):
    +        task = MockTask()
    +        dependent_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependent_task)
    +        graph.add_dependency(dependent_task, task)
    +        remove_result = graph.remove_tasks(dependent_task)
    +        assert remove_result == [dependent_task]
    +        tasks = [t for t in graph.tasks]
    +        assert len(tasks) == 1
    +        assert tasks[0] == task
    +        # asserting no dependencies are left for the dependent task
    +        assert len(list(graph.get_dependencies(task))) == 0
    +
    +    def test_remove_nonexistent_task(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        # removing a task not in graph - should have no effect, and return 
False
    +        remove_result = graph.remove_tasks(task_not_in_graph)
    +        assert remove_result == []
    +        tasks = [t for t in graph.tasks]
    +        assert len(tasks) == 1
    +        assert tasks[0] == task
    +
    +    def test_has_task(self, graph):
    +        task = MockTask()
    +        graph.add_tasks(task)
    +        assert graph.has_tasks(task) is True
    +
    +    def test_has_nonexistent_task(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        assert graph.has_tasks(task_not_in_graph) is False
    +
    +    def test_get_task(self, graph):
    +        task = MockTask()
    +        graph.add_tasks(task)
    +        assert graph.get_task(task.id) == task
    +
    +    def test_get_nonexistent_task(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            graph.get_task(task_not_in_graph.id)
    +
    +
    +class TestTaskGraphGraphTraversal(object):
    +
    +    def test_tasks_iteration(self, graph):
    +        task = MockTask()
    +        other_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(other_task)
    +        tasks = [t for t in graph.tasks]
    +        assert set(tasks) == set([task, other_task])
    +
    +    def test_get_dependents(self, graph):
    +        task = MockTask()
    +        dependent_task_1 = MockTask()
    +        dependent_task_2 = MockTask()
    +        transitively_dependent_task = MockTask()
    +
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependent_task_1)
    +        graph.add_tasks(dependent_task_2)
    +        graph.add_tasks(transitively_dependent_task)
    +
    +        graph.add_dependency(dependent_task_1, task)
    +        graph.add_dependency(dependent_task_2, task)
    +        graph.add_dependency(transitively_dependent_task, dependent_task_2)
    +
    +        dependent_tasks = list(graph.get_dependents(task))
    +        # transitively_dependent_task not expected to appear in the result
    +        assert set(dependent_tasks) == set([dependent_task_1, 
dependent_task_2])
    +
    +    def test_get_task_empty_dependents(self, graph):
    +        task = MockTask()
    +        other_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(other_task)
    +        dependent_tasks = list(graph.get_dependents(task))
    +        assert len(dependent_tasks) == 0
    +
    +    def test_get_nonexistent_task_dependents(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            list(graph.get_dependents(task_not_in_graph))
    +
    +    def test_get_dependencies(self, graph):
    +        task = MockTask()
    +        dependency_task_1 = MockTask()
    +        dependency_task_2 = MockTask()
    +        transitively_dependency_task = MockTask()
    +
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependency_task_1)
    +        graph.add_tasks(dependency_task_2)
    +        graph.add_tasks(transitively_dependency_task)
    +
    +        graph.add_dependency(task, dependency_task_1)
    +        graph.add_dependency(task, dependency_task_2)
    +        graph.add_dependency(dependency_task_2, 
transitively_dependency_task)
    +
    +        dependency_tasks = list(graph.get_dependencies(task))
    +        # transitively_dependency_task not expected to appear in the result
    +        assert set(dependency_tasks) == set([dependency_task_1, 
dependency_task_2])
    +
    +    def test_get_task_empty_dependencies(self, graph):
    +        task = MockTask()
    +        other_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(other_task)
    +        dependency_tasks = list(graph.get_dependencies(task))
    +        assert len(dependency_tasks) == 0
    +
    +    def test_get_nonexistent_task_dependencies(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            list(graph.get_dependencies(task_not_in_graph))
    +
    +
    +class TestTaskGraphDependencies(object):
    +
    +    def test_add_dependency(self, graph):
    +        task = MockTask()
    +        dependency_task = MockTask()
    +        unrelated_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependency_task)
    +        graph.add_tasks(unrelated_task)
    +        add_result = graph.add_dependency(task, dependency_task)
    +        assert add_result is True
    +        dependency_tasks = list(graph.get_dependencies(task))
    +        assert len(dependency_tasks) == 1
    +        assert dependency_tasks[0] == dependency_task
    +
    +    def test_add_existing_dependency(self, graph):
    +        task = MockTask()
    +        dependency_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependency_task)
    +        graph.add_dependency(task, dependency_task)
    +        # adding a dependency already in graph - should have no effect, 
and return False
    +        add_result = graph.add_dependency(task, dependency_task)
    +        assert add_result is False
    +        dependency_tasks = list(graph.get_dependencies(task))
    +        assert len(dependency_tasks) == 1
    +        assert dependency_tasks[0] == dependency_task
    +
    +    def test_add_dependency_nonexistent_dependent(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            graph.add_dependency(task_not_in_graph, task)
    +
    +    def test_add_dependency_nonexistent_dependency(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            graph.add_dependency(task, task_not_in_graph)
    +
    +    def test_has_dependency(self, graph):
    +        task = MockTask()
    +        dependency_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependency_task)
    +        graph.add_dependency(task, dependency_task)
    +        assert graph.has_dependency(task, dependency_task) is True
    +
    +    def test_has_nonexistent_dependency(self, graph):
    +        task = MockTask()
    +        other_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(other_task)
    +        assert graph.has_dependency(task, other_task) is False
    +
    +    def test_has_dependency_nonexistent_dependent(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            graph.has_dependency(task_not_in_graph, task)
    +
    +    def test_has_dependency_nonexistent_dependency(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            graph.has_dependency(task, task_not_in_graph)
    +
    +    def test_remove_dependency(self, graph):
    +        task = MockTask()
    +        dependency_task = MockTask()
    +        another_dependent_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependency_task)
    +        graph.add_tasks(another_dependent_task)
    +        graph.add_dependency(task, dependency_task)
    +        graph.add_dependency(another_dependent_task, dependency_task)
    +
    +        remove_result = graph.remove_dependency(task, dependency_task)
    +        assert remove_result is True
    +        assert graph.has_dependency(task, dependency_task) is False
    +        assert graph.has_dependency(another_dependent_task, 
dependency_task) is True
    +
    +    def test_remove_nonexistent_dependency(self, graph):
    +        task = MockTask()
    +        dependency_task = MockTask()
    +        graph.add_tasks(task)
    +        graph.add_tasks(dependency_task)
    +        # removing a dependency not in graph - should have no effect, and 
return False
    +        remove_result = graph.remove_dependency(task, dependency_task)
    +        assert remove_result is False
    +        tasks = [t for t in graph.tasks]
    +        assert set(tasks) == set([task, dependency_task])
    +
    +    def test_remove_dependency_nonexistent_dependent(self, graph):
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            graph.remove_dependency(task_not_in_graph, task)
    +
    +    def test_remove_dependency_nonexistent_dependency(self, graph):
    +        # in this test the dependency *task* is not in the graph, not just 
the dependency itself
    +        task = MockTask()
    +        task_not_in_graph = MockTask()
    +        graph.add_tasks(task)
    +        with pytest.raises(task_graph.TaskNotInGraphError):
    +            graph.remove_dependency(task, task_not_in_graph)
    +
    +
    +class TestTaskGraphArrangements(object):
    +
    +    def test_sequence(self, graph):
    +        tasks = [MockTask(), MockTask(), MockTask()]
    +        graph.sequence(*tasks)
    +        graph_tasks = [t for t in graph.tasks]
    +        assert set(graph_tasks) == set(tasks)
    +        assert len(list(graph.get_dependencies(tasks[0]))) == 0
    +        assert set(graph.get_dependencies(tasks[1])) == set([tasks[0]])
    +        assert set(graph.get_dependencies(tasks[2])) == set([tasks[1]])
    +
    +    def 
test_sequence_with_some_tasks_and_dependencies_already_in_graph(self, graph):
    +        tasks = [MockTask(), MockTask(), MockTask()]
    +        # insert some tasks and dependencies to the graph
    +        graph.add_tasks(tasks[1])
    +        graph.add_tasks(tasks[2])
    +        graph.add_dependency(tasks[2], tasks[1])
    +
    +        graph.sequence(*tasks)
    +        graph_tasks = [t for t in graph.tasks]
    +        assert set(graph_tasks) == set(tasks)
    +        assert len(list(graph.get_dependencies(tasks[0]))) == 0
    +        assert set(graph.get_dependencies(tasks[1])) == set([tasks[0]])
    +        assert set(graph.get_dependencies(tasks[2])) == set([tasks[1]])
    +
    +    def test_sequence_with_nested_sequence(self, graph):
    +        tasks = [MockTask() for _ in xrange(5)]
    +        graph.sequence(tasks[0], graph.sequence(tasks[1], tasks[2], 
tasks[3]), tasks[4])
    +        graph_tasks = [t for t in graph.tasks]
    +        assert set(graph_tasks) == set(tasks)
    +        # first task should have no dependencies
    +        assert len(list(graph.get_dependencies(tasks[0]))) == 0
    +        assert len(list(graph.get_dependencies(tasks[1]))) == 1
    +        assert len(list(graph.get_dependencies(tasks[2]))) == 2
    +        assert len(list(graph.get_dependencies(tasks[3]))) == 2
    +        assert len(list(graph.get_dependencies(tasks[4]))) == 3
    +
    +    def test_sequence_with_nested_parallel(self, graph):
    --- End diff --
    
    rename


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to