Skip to content

Commit

Permalink
Add tests for validators and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
storax committed Nov 6, 2016
1 parent 6bf883e commit e01510b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/reddel_server/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ValidatorInterface(six.with_metaclass(abc.ABCMeta, object)):
Override :meth:`ValidatorInterface.__call__`.
"""
@abc.abstractmethod
def __call__(self, red):
def __call__(self, red): # pragma: no cover
"""Validate the given redbaron source
:param red: the source
Expand All @@ -26,7 +26,7 @@ def __call__(self, red):
"""
pass

def transform(self, red):
def transform(self, red): # pragma: no cover
"""Transform the given red baron
:param red: a red baron or other nodes
Expand Down
20 changes: 20 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Tests for the utils module"""
import pytest

from reddel_server import utils


def test_get_attr_from_dotted_path(mocker):
"""Test the function call importlib correctly"""
class MyClass:
pass

importmock = mocker.Mock()
modmock = mocker.Mock()
modmock.MyClass = MyClass
importmock.return_value = modmock

mockedimport = mocker.patch("importlib.import_module", importmock)
result = utils.get_attr_from_dotted_path("test.this.path.MyClass")
assert result is MyClass
mockedimport.assert_called_once_with('test.this.path')
42 changes: 42 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Tests for the validators module"""
import pytest
import redbaron

import reddel_server

DEF_SRC = """def foo(bar):
spam = bar + 1
return spam
"""

DEF_SRC2 = DEF_SRC + """
def foo2(bar):
pass
"""

@pytest.mark.parametrize('identifiers,single,src,valid', [
[['def', 'defnode'], True, DEF_SRC, True],
[['def', 'defnode'], True, DEF_SRC2, False],
[['name'], True, DEF_SRC, False],
[['def'], False, DEF_SRC2, True]
])
def test_barontype_call(identifiers, single, src, valid):
val = reddel_server.BaronTypeValidator(identifiers, single=single)
red = redbaron.RedBaron(src)
if valid:
val(red)
else:
with pytest.raises(reddel_server.ValidationError):
val(red)

def test_barontype_transform_single():
val = reddel_server.BaronTypeValidator((), single=True)
red = redbaron.RedBaron(DEF_SRC)
transformed = val.transform(red)
assert transformed is red[0]

def test_barontype_transform_singleOff():
val = reddel_server.BaronTypeValidator((), single=False)
red = redbaron.RedBaron(DEF_SRC)
transformed = val.transform(red)
assert transformed is red
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ deps =
pytest
pytest-travis-fold
pytest-cov
pytest-mock
commands =
{posargs:py.test --cov --cov-report=term-missing -vv tests}

Expand Down

0 comments on commit e01510b

Please sign in to comment.