Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Add some simple unit tests for sanity.
Browse files Browse the repository at this point in the history
  • Loading branch information
mesbahamin committed Feb 25, 2017
1 parent a8a949f commit b805269
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
51 changes: 51 additions & 0 deletions conftest.py
@@ -0,0 +1,51 @@
from collections import defaultdict
import pytest
from speller import Graph, Node


@pytest.fixture()
def test_graph():
"""Return a `speller.Graph` object of the word 'because'."""
test_graph = Graph()

test_graph._parents_of = defaultdict(
set,
{
Node(value='c', position=2): {Node(value='be', position=0)},
Node(value='au', position=3): {Node(value='c', position=2)},
Node(value='s', position=5): {
Node(value='au', position=3),
Node(value='u', position=4)
},
Node(value='se', position=5): {
Node(value='au', position=3),
Node(value='u', position=4)
},
None: {Node(value='se', position=5)},
Node(value='ca', position=2): {Node(value='be', position=0)},
Node(value='u', position=4): {Node(value='ca', position=2)}
}
)

test_graph._children_of = defaultdict(
set,
{
None: {Node(value='be', position=0), Node(value='b', position=0)},
Node(value='be', position=0): {
Node(value='ca', position=2),
Node(value='c', position=2)
},
Node(value='c', position=2): {Node(value='au', position=3)},
Node(value='au', position=3): {
Node(value='se', position=5),
Node(value='s', position=5)
},
Node(value='ca', position=2): {Node(value='u', position=4)},
Node(value='u', position=4): {
Node(value='se', position=5),
Node(value='s', position=5)
}
}
)

return test_graph
140 changes: 140 additions & 0 deletions tests.py
@@ -1,4 +1,6 @@
from collections import defaultdict
import speller
from speller import Node

ELEMENTS = {
'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al',
Expand All @@ -15,11 +17,149 @@


def test_verify_data():
"""Assert that the set of elements in `speller.py` matches this
canonical set.
"""
assert speller.ELEMENTS == ELEMENTS


def test_elemental_spelling():
"""Assert that we get the expected results when spelling various
inputs.
"""
assert speller.spell('amputation') == [
('Am', 'Pu', 'Ta', 'Ti', 'O', 'N'),
('Am', 'P', 'U', 'Ta', 'Ti', 'O', 'N')
]
assert speller.spell('') == []
assert speller.spell('o') == [('O',)]


def test_find_all_paths():
"""Make simple graph with some branches, and assert that we find all
the paths from the first node to the last.
"""
parents_to_children = {
'a': {'b'},
'b': {'c'},
'c': {'d'},
'd': {'e', 'y', 'z'},
'e': {'f', 'x'},
'f': {'g', 'x'},
'g': {'h'},
'h': {'i'},
'x': {'y'},
'y': {'z'},
}

assert set(speller.find_all_paths(parents_to_children, 'a', 'z')) == set([
('a', 'b', 'c', 'd', 'z'),
('a', 'b', 'c', 'd', 'y', 'z'),
('a', 'b', 'c', 'd', 'e', 'x', 'y', 'z'),
('a', 'b', 'c', 'd', 'e', 'f', 'x', 'y', 'z'),
])


def test_build_spelling_graph():
"""Make a `speller.Graph` object, then build it with a word and
assert that it contains the proper node relationships.
"""
g = speller.Graph()
speller.build_spelling_graph('because', g)

assert g._parents_of == defaultdict(
set,
{
Node(value='c', position=2): {Node(value='be', position=0)},
Node(value='au', position=3): {Node(value='c', position=2)},
Node(value='s', position=5): {
Node(value='au', position=3),
Node(value='u', position=4)
},
Node(value='se', position=5): {
Node(value='au', position=3),
Node(value='u', position=4)
},
None: {Node(value='se', position=5)},
Node(value='ca', position=2): {Node(value='be', position=0)},
Node(value='u', position=4): {Node(value='ca', position=2)}
}
)

assert g._children_of == defaultdict(
set,
{
None: {Node(value='be', position=0), Node(value='b', position=0)},
Node(value='be', position=0): {
Node(value='ca', position=2),
Node(value='c', position=2)
},
Node(value='c', position=2): {Node(value='au', position=3)},
Node(value='au', position=3): {
Node(value='se', position=5),
Node(value='s', position=5)
},
Node(value='ca', position=2): {Node(value='u', position=4)},
Node(value='u', position=4): {
Node(value='se', position=5),
Node(value='s', position=5)
}
}
)


class TestGraph:
"""Tests for the methods of the `speller.Graph` class."""

def test_firsts(self, test_graph):
"""Assert that the graph properly identifies its first nodes."""
assert test_graph.firsts() == {Node('be', 0), Node('b', 0)}

def test_lasts(self, test_graph):
"""Assert that the graph properly identifies its last nodes."""
assert test_graph.lasts() == {Node('se', 5)}

def test_add_edge(self, test_graph):
"""Add an edge to the graph."""
parent = Node('te', 0)
child = Node('st', 2)
test_graph.add_edge(parent, child)
assert test_graph._children_of[parent] == {child}
assert test_graph._parents_of[child] == {parent}

def test_add_edge_with_no_parent(self, test_graph):
"""Add an edge with no parent to the graph. Assert that 'None'
isn't added to `_parents_of[child]`.
"""
parent = None
child = Node('a', 0)
test_graph.add_edge(parent, child)
assert child in test_graph._children_of[parent]
assert None not in test_graph._parents_of[child]

def test_add_edge_with_no_child(self, test_graph):
"""Add an edge with no child to the graph. Assert that `None`
isn't added to `_children_of[parent]`.
"""
parent = Node('z', 25)
child = None
test_graph.add_edge(parent, child)
assert None not in test_graph._children_of[parent]
assert parent in test_graph._parents_of[child]

def test_edges(self, test_graph):
"""Assert that the graph properly lists its edges."""
assert set(test_graph.edges()) == set([
(None, Node(value='b', position=0)),
(None, Node(value='be', position=0)),
(Node(value='be', position=0), Node(value='c', position=2)),
(Node(value='be', position=0), Node(value='ca', position=2)),
(Node(value='c', position=2), Node(value='au', position=3)),
(Node(value='au', position=3), Node(value='s', position=5)),
(Node(value='au', position=3), Node(value='se', position=5)),
(Node(value='ca', position=2), Node(value='u', position=4)),
(Node(value='u', position=4), Node(value='s', position=5)),
(Node(value='u', position=4), Node(value='se', position=5))
])

#TODO(amin): add a test for test_graph.export().

0 comments on commit b805269

Please sign in to comment.