-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for domain module (#83)
- Loading branch information
1 parent
04afd93
commit a3cea0f
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from grimp.adaptors.graph import ImportGraph | ||
|
||
from importlinter.domain.helpers import add_imports | ||
|
||
|
||
def test_add_imports(): | ||
graph = ImportGraph() | ||
import_details = [ | ||
{ | ||
'importer': 'a', | ||
'imported': 'b', | ||
'line_number': 1, | ||
'line_contents': 'lorem ipsum', | ||
}, | ||
{ | ||
'importer': 'c', | ||
'imported': 'd', | ||
'line_number': 2, | ||
'line_contents': 'lorem ipsum 2', | ||
} | ||
] | ||
assert not graph.modules | ||
add_imports(graph, import_details) | ||
assert graph.modules == {'a', 'b', 'c', 'd'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from contextlib import contextmanager | ||
|
||
import pytest | ||
|
||
from importlinter.domain.imports import Module, DirectImport | ||
|
||
|
||
@contextmanager | ||
def does_not_raise(): | ||
yield | ||
|
||
|
||
class TestModule: | ||
def test_object_representation(self): | ||
test_object = Module('new_module') | ||
assert repr(test_object) == '<Module: new_module>' | ||
|
||
@pytest.mark.parametrize(('first_object', 'second_object', 'expected_bool'), | ||
[ | ||
(Module('first'), Module('second'), False), | ||
(Module('same'), Module('same'), True), | ||
(Module('different'), 'different', False), | ||
]) | ||
def test_equal_magic_method(self, first_object, second_object, expected_bool): | ||
comparison_result = first_object == second_object | ||
assert comparison_result is expected_bool | ||
|
||
@pytest.mark.parametrize(('module', 'expected_parent', 'exception'), [ | ||
(Module('parent.child'), Module('parent'), does_not_raise()), | ||
(Module('child'), Module(''), pytest.raises(ValueError)), | ||
]) | ||
def test_parent(self, module, expected_parent, exception): | ||
with exception: | ||
assert module.parent == expected_parent | ||
|
||
@pytest.mark.parametrize(('child', 'parent', 'expected_bool'), [ | ||
(Module('parent.child'), Module('parent'), True), | ||
(Module('grandparent.parent.child'), Module('grandparent'), False), | ||
(Module('first_child'), Module('second_child'), False), | ||
]) | ||
def test_is_child_of(self, child, parent, expected_bool): | ||
assert child.is_child_of(parent) is expected_bool | ||
|
||
|
||
class TestDirectImport: | ||
def test_object_representation(self): | ||
test_object = DirectImport( | ||
importer=Module("mypackage.foo"), | ||
imported=Module("mypackage.bar"), | ||
) | ||
assert repr(test_object) == '<DirectImport: mypackage.foo -> mypackage.bar>' | ||
|
||
@pytest.mark.parametrize(('test_object', 'expected_string'), [ | ||
( | ||
DirectImport( | ||
importer=Module("mypackage.foo"), | ||
imported=Module("mypackage.bar") | ||
), | ||
'mypackage.foo -> mypackage.bar', | ||
), | ||
( | ||
DirectImport( | ||
importer=Module("mypackage.foo"), | ||
imported=Module("mypackage.bar"), | ||
line_number=10, | ||
), | ||
'mypackage.foo -> mypackage.bar (l. 10)', | ||
), | ||
]) | ||
def test_string_object_representation(self, test_object, expected_string): | ||
assert str(test_object) == expected_string |