Skip to content

Commit

Permalink
Update test_utilities.py
Browse files Browse the repository at this point in the history
Test
- flatten
- convert_to_adj
- add_edge_to
#7
  • Loading branch information
VaeterchenFrost committed Jun 6, 2020
1 parent c05dfcf commit 51b1768
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,71 @@
If not, see https://www.gnu.org/licenses/gpl-3.0.html
"""
from pytest import param, mark, raises
from tdvisu.utilities import flatten, convert_to_adj, add_edge_to


@mark.parametrize(
"arg",
[None, 10, 1.4, [1, 2, 3], [None]]
)
def test_cant_flatten(arg):
"""Fail the flatten method with TypeError"""
with raises(TypeError):
list(flatten(arg))


@mark.parametrize(
"arg, expected",
[((), []),
([], []),
(([None],), [None]),
([(1, 2), (3, 4)], [1, 2, 3, 4]),
([(1, 2)], [1, 2]),
([""], []),
(["String"], ['S', 't', 'r', 'i', 'n', 'g']),
(["String", "Zwei", "Drei"], ['S', 't', 'r', 'i', 'n', 'g', 'Z', 'w', 'e', 'i', 'D', 'r', 'e', 'i'])
]
)
def test_flatten(arg, expected):
"""Test the flatten method."""
assert list(flatten(arg)) == expected


def test_convert_to_adj():
"""Test the convert_to_adj method"""
assert convert_to_adj([(2, 1), (3, 2), (4, 2), (5, 4)]) == {
2: {1: {}, 3: {}, 4: {}}, 1: {2: {}}, 3: {2: {}}, 4: {2: {}, 5: {}}, 5: {4: {}}}


@mark.parametrize(
"edges, adj, vertex1, vertex2, new_adj",
[param(set(), {}, 1, 2, {1: {2}, 2: {1}},
id="empty sets 1"),
param(set(), {}, "B", "A", {"B": {"A"}, "A": {"B"}},
id="empty sets 2"),
param(set([1, 2, 3]), {}, 1, 2, {1: {2}, 2: {1}},
id="doesnt care about prev elements 1"),
param(set([None]), {}, "B", "A", {"B": {"A"}, "A": {"B"}},
id="doesnt care about prev elements 2"),
param({(1, 2), (2, 3)}, {1: {2}, 2: {1, 3}, 3: {2}}, 3, 1,
{1: {2, 3}, 2: {1, 3}, 3: {1, 2}},
id="Closing the triangle"),
param({('a', 1), (1, 2), (4.3, 2)}, {1: {2, 'a'}, 2: {1, 4.3}, 4.3: {2}, 'a': {1}},
2, "a",
{1: {2, 'a'}, 2: {1, 4.3, 'a'}, 4.3: {2}, 'a': {1, 2}},
id="Even with different elements everything works"),
]
)
def test_add_edge_to(edges, adj, vertex1, vertex2, new_adj):
"""Test the add_edge_to method. Sensitive to order in vertices in edges!"""
expect_edges = set(edges)
expect_edges.add((vertex1, vertex2))
add_edge_to(edges, adj, vertex1, vertex2)
assert edges == expect_edges
assert adj == new_adj
# adding second time switched does not change the adjacency
add_edge_to(edges, adj, vertex2, vertex1)
assert adj == new_adj

0 comments on commit 51b1768

Please sign in to comment.