From b7f89ae00af9bca949363ee7e8aea0823a45aba0 Mon Sep 17 00:00:00 2001 From: Julien Kauffmann Date: Sun, 31 Jan 2016 23:16:29 -0500 Subject: [PATCH] Added more unit tests --- pygen/index.py | 5 +- pygen/parsers/json.py | 4 +- pygen/parsers/yaml.py | 4 +- tests/unit/test_exceptions.py | 19 ++++ tests/unit/test_index.py | 96 +++++++++++++++++++ tests/unit/test_parsers/__init__.py | 0 tests/unit/test_parsers/test_base.py | 12 +++ .../test_init.py} | 0 tests/unit/test_parsers/test_json.py | 32 +++++++ tests/unit/test_parsers/test_yaml.py | 42 ++++++++ tox.ini | 3 +- 11 files changed, 211 insertions(+), 6 deletions(-) create mode 100644 tests/unit/test_exceptions.py create mode 100644 tests/unit/test_index.py create mode 100644 tests/unit/test_parsers/__init__.py create mode 100644 tests/unit/test_parsers/test_base.py rename tests/unit/{test_parsers.py => test_parsers/test_init.py} (100%) create mode 100644 tests/unit/test_parsers/test_json.py create mode 100644 tests/unit/test_parsers/test_yaml.py diff --git a/pygen/index.py b/pygen/index.py index e61b2a3..6d4d423 100644 --- a/pygen/index.py +++ b/pygen/index.py @@ -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, diff --git a/pygen/parsers/json.py b/pygen/parsers/json.py index 78308ea..faa0b0b 100644 --- a/pygen/parsers/json.py +++ b/pygen/parsers/json.py @@ -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): diff --git a/pygen/parsers/yaml.py b/pygen/parsers/yaml.py index 75edb4c..a97498d 100644 --- a/pygen/parsers/yaml.py +++ b/pygen/parsers/yaml.py @@ -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): diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py new file mode 100644 index 0000000..95cf12a --- /dev/null +++ b/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'] diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py new file mode 100644 index 0000000..91fce8b --- /dev/null +++ b/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'), + ] diff --git a/tests/unit/test_parsers/__init__.py b/tests/unit/test_parsers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_parsers/test_base.py b/tests/unit/test_parsers/test_base.py new file mode 100644 index 0000000..e8c3a02 --- /dev/null +++ b/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() diff --git a/tests/unit/test_parsers.py b/tests/unit/test_parsers/test_init.py similarity index 100% rename from tests/unit/test_parsers.py rename to tests/unit/test_parsers/test_init.py diff --git a/tests/unit/test_parsers/test_json.py b/tests/unit/test_parsers/test_json.py new file mode 100644 index 0000000..4c89414 --- /dev/null +++ b/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] diff --git a/tests/unit/test_parsers/test_yaml.py b/tests/unit/test_parsers/test_yaml.py new file mode 100644 index 0000000..7a5bf07 --- /dev/null +++ b/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] diff --git a/tox.ini b/tox.ini index dddb1e1..8de721d 100644 --- a/tox.ini +++ b/tox.ini @@ -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