Skip to content

Commit

Permalink
Add unit tests for domain module (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljurczak committed Aug 16, 2020
1 parent 04afd93 commit a3cea0f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Contributors

* Anthony Sottile - https://github.com/asottile
* Łukasz Skarżyński - https://github.com/skarzi
* Daniel Jurczak - https://github.com/danieljurczak
24 changes: 24 additions & 0 deletions tests/unit/domain/test_helpers.py
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'}
71 changes: 71 additions & 0 deletions tests/unit/domain/test_imports.py
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

0 comments on commit a3cea0f

Please sign in to comment.