Skip to content

Commit

Permalink
Add Solution tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhgrant10 committed Aug 19, 2018
1 parent e21ad88 commit ff277da
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/test_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
import pytest
import networkx

from acopy.solvers import Solution


@pytest.fixture
def graph():
G = networkx.Graph()
G.add_edge(1, 2, weight=2)
G.add_edge(2, 3, weight=4)
G.add_edge(3, 4, weight=1)
G.add_edge(4, 1, weight=8)
G.add_edge(1, 3, weight=5)
G.add_edge(2, 4, weight=11)
return G


@pytest.fixture
def create_solution(graph):

def _create_solution(start, *nodes, is_closed=False):
solution = Solution(graph, start)
for node in nodes:
solution.add_node(node)
if is_closed:
solution.close()
return solution

return _create_solution


@pytest.mark.parametrize('is_closed,answer', [
(False, 2),
(True, 2),
])
def test_solution_start(create_solution, is_closed, answer):
solution = create_solution(2, 3, is_closed=is_closed)
assert solution.start == answer


@pytest.mark.parametrize('is_closed,answer', [
(False, 3),
(True, 2),
])
def test_solution_current(create_solution, is_closed, answer):
solution = create_solution(2, 3, is_closed=is_closed)
assert solution.current == answer


@pytest.mark.parametrize('is_closed,answer', [
(False, {2, 3}),
(True, {2, 3}),
])
def test_solution_visited(create_solution, is_closed, answer):
solution = create_solution(2, 3, is_closed=is_closed)
assert solution.visited == answer


@pytest.mark.parametrize('is_closed,answer', [
(False, [2, 3]),
(True, [2, 3]),
])
def test_solution_nodes(create_solution, is_closed, answer):
solution = create_solution(2, 3, is_closed=is_closed)
assert solution.nodes == answer


@pytest.mark.parametrize('is_closed,answer', [
(False, [(2, 3)]),
(True, [(2, 3), (3, 2)]),
])
def test_solution_path(create_solution, is_closed, answer):
solution = create_solution(2, 3, is_closed=is_closed)
assert solution.path == answer


@pytest.mark.parametrize('is_closed,answer', [
(False, 4),
(True, 8),
])
def test_solution_cost(create_solution, is_closed, answer):
solution = create_solution(2, 3, is_closed=is_closed)
assert solution.cost == answer
1 change: 1 addition & 0 deletions tests/test_solver.py → tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import pytest

from acopy.solvers import State
Expand Down

0 comments on commit ff277da

Please sign in to comment.