Skip to content

Commit

Permalink
Added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ereOn committed Feb 1, 2016
1 parent 03096d0 commit b7f89ae
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 6 deletions.
5 changes: 4 additions & 1 deletion pygen/index.py
Expand Up @@ -84,8 +84,11 @@ def generate(self, context):
:param context: The root context to use.
:yields: Triplets of (target_name, filename, content) for the targets.
.. note::
Targets are picked in alphabetical order.
"""
for target_name, target in self.targets.items():
for target_name, target in sorted(self.targets.items()):
for filename, content in target.generate(
environment=self.environment,
context=context,
Expand Down
4 changes: 2 additions & 2 deletions pygen/parsers/json.py
Expand Up @@ -19,8 +19,8 @@ class JsonParser(BaseParser):

@classmethod
def register(cls):
for mimetype in cls.mimetypes:
for extension in cls.extensions:
for mimetype in sorted(cls.mimetypes):
for extension in sorted(cls.extensions):
mimetypes.add_type(mimetype, extension)

def load(self, file):
Expand Down
4 changes: 2 additions & 2 deletions pygen/parsers/yaml.py
Expand Up @@ -25,8 +25,8 @@ class YamlParser(BaseParser):

@classmethod
def register(cls):
for mimetype in cls.mimetypes:
for extension in cls.extensions:
for mimetype in sorted(cls.mimetypes):
for extension in sorted(cls.extensions):
mimetypes.add_type(mimetype, extension)

def load(self, file):
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_exceptions.py
@@ -0,0 +1,19 @@
"""
Exceptions tests.
"""

from pygen.exceptions import (
InvalidScope,
NoParserError,
)


def test_invalid_scope_instanciation():
value = InvalidScope(scope=['a', 'b'])
assert value.scope == ['a', 'b']


def test_no_parser_error_instanciation():
value = NoParserError(mimetype='foo/bar', mimetypes=['foo/foo'])
assert value.mimetype == 'foo/bar'
assert value.mimetypes == ['foo/foo']
96 changes: 96 additions & 0 deletions tests/unit/test_index.py
@@ -0,0 +1,96 @@
"""
Index tests.
"""

import yaml

from mock import (
MagicMock,
patch,
)
from six import StringIO
from jinja2 import (
Environment,
PrefixLoader,
FileSystemLoader,
)

from pygen.index import Index
from pygen.scope import Scope
from pygen.target import Target


def test_load_no_template_paths():
stream = StringIO()
yaml.dump({}, stream)
stream.seek(0)
index = Index.load('', stream)
assert index.targets == {}
assert index.environment.keep_trailing_newline
assert isinstance(index.environment.loader, FileSystemLoader)


def test_load_template_paths():
stream = StringIO()
yaml.dump({
'template_paths': {
'foo': 'foo',
'bar': 'bar',
},
}, stream)
stream.seek(0)
index = Index.load('', stream)
assert index.targets == {}
assert index.environment.keep_trailing_newline
assert isinstance(index.environment.loader, PrefixLoader)


@patch('pygen.index.Target', spec=Target)
def test_load_targets(target):
stream = StringIO()
yaml.dump({
'targets': {
'foo': {
'template_name': 'foo.txt.tpl',
'filename': 'foo.txt',
'scopes': {
'path': 'path',
},
},
},
}, stream)
stream.seek(0)
index = Index.load('', stream)
target.assert_called_once_with(
filename='foo.txt',
template_name='foo.txt.tpl',
scopes={
'path': Scope(['path']),
},
)
assert index.targets == {
'foo': target(),
}


def test_generate():
targets = {
'foo': MagicMock(spec=Target),
'bar': MagicMock(spec=Target),
}
targets['foo'].generate.return_value = (
('foo1', 'myfoo1'),
('foo2', 'myfoo2'),
)
targets['bar'].generate.return_value = (
('bar1', 'mybar1'),
)
context = {}
index = Index(environment=MagicMock(spec=Environment), targets=targets)
result = list(index.generate(context=context))

assert result == [
('bar', 'bar1', 'mybar1'),
('foo', 'foo1', 'myfoo1'),
('foo', 'foo2', 'myfoo2'),
]
Empty file.
12 changes: 12 additions & 0 deletions tests/unit/test_parsers/test_base.py
@@ -0,0 +1,12 @@
"""
Base parser tests.
"""

from pygen.parsers.base import BaseParser


def test_register():
parser = BaseParser()

# The call does nothing: we just ensure coverage is complete.
parser.register()
File renamed without changes.
32 changes: 32 additions & 0 deletions tests/unit/test_parsers/test_json.py
@@ -0,0 +1,32 @@
"""
JsonParser tests.
"""

from mock import (
call,
patch,
)
from six import StringIO

from pygen.parsers.json import JsonParser


@patch('mimetypes.add_type')
def test_register(add_type):
JsonParser.register()
assert add_type.mock_calls == [
call('application/json', '.json'),
call('text/json', '.json'),
]


def test_load():
parser = JsonParser()
result = parser.load(StringIO("[1, 2, 3]"))
assert result == [1, 2, 3]


def test_loads():
parser = JsonParser()
result = parser.loads("[1, 2, 3]")
assert result == [1, 2, 3]
42 changes: 42 additions & 0 deletions tests/unit/test_parsers/test_yaml.py
@@ -0,0 +1,42 @@
"""
JsonParser tests.
"""

from mock import (
call,
patch,
)
from six import StringIO

from pygen.parsers.yaml import YamlParser


@patch('mimetypes.add_type')
def test_register(add_type):
YamlParser.register()
assert add_type.mock_calls == [
call('application/vnd.yaml', '.yaml'),
call('application/vnd.yaml', '.yml'),
call('application/x-yaml', '.yaml'),
call('application/x-yaml', '.yml'),
call('application/yaml', '.yaml'),
call('application/yaml', '.yml'),
call('text/vnd.yaml', '.yaml'),
call('text/vnd.yaml', '.yml'),
call('text/x-yaml', '.yaml'),
call('text/x-yaml', '.yml'),
call('text/yaml', '.yaml'),
call('text/yaml', '.yml'),
]


def test_load():
parser = YamlParser()
result = parser.load(StringIO("[1, 2, 3]"))
assert result == [1, 2, 3]


def test_loads():
parser = YamlParser()
result = parser.loads("[1, 2, 3]")
assert result == [1, 2, 3]
3 changes: 2 additions & 1 deletion tox.ini
Expand Up @@ -32,4 +32,5 @@ commands =
deps =
-rdev_requirements.txt
commands =
coverage run -a --source=pygen -m py.test
coverage run -a --source=pygen -m py.test tests/unit
py.test tests/integration

0 comments on commit b7f89ae

Please sign in to comment.