Skip to content

Commit

Permalink
Place expected results first for better failure diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Sep 5, 2015
1 parent 0b7acaa commit 3be7ad2
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 80 deletions.
6 changes: 6 additions & 0 deletions memegen/test/conftest.py
Expand Up @@ -3,6 +3,7 @@
import pytest

from memegen import services
from memegen.domain import Template


def pytest_configure(config):
Expand Down Expand Up @@ -32,3 +33,8 @@ def link_service():
def image_service():
return services.image.ImageService(template_store=Mock(),
image_store=Mock())


@pytest.fixture
def template():
return Template('abc', name='ABC', lines=['foo', 'bar'])
5 changes: 4 additions & 1 deletion memegen/test/test_app.py
@@ -1,4 +1,4 @@
# pylint: disable=R,C
# pylint: disable=no-self-use

from memegen import app
from memegen import settings
Expand All @@ -8,15 +8,18 @@ class TestCreateApp:

def test_prod(self):
_app = app.create_app(settings.ProdConfig)

assert False is _app.config['DEBUG']
assert False is _app.config['TESTING']

def test_test(self):
_app = app.create_app(settings.TestConfig)

assert True is _app.config['DEBUG']
assert True is _app.config['TESTING']

def test_dev(self):
_app = app.create_app(settings.DevConfig)

assert True is _app.config['DEBUG']
assert False is _app.config['TESTING']
66 changes: 30 additions & 36 deletions memegen/test/test_domain_template.py
@@ -1,4 +1,4 @@
# pylint: disable=R,C,W0621
# pylint: disable=no-self-use

from unittest.mock import patch, Mock

Expand All @@ -7,83 +7,81 @@
from memegen.domain import Template


@pytest.fixture
def template():
return Template('abc', name='ABC', lines=['foo', 'bar'])


class TestTemplate:

def test_comparison(self):
t1 = Template('abc', "A Thing")
t2 = Template('def')
t3 = Template('def', "Do This")

assert t1 != t2
assert t2 == t3
assert t1 < t3

def test_path(self):
template = Template('abc', root="my_root")
def test_path(self, template):
template.root = "my_root"

with patch('os.path.isfile', Mock(return_value=True)):
path = template.path

assert "my_root/abc/default.png" == path

def test_path_is_none_with_no_file(self):
template = Template('abc', root="my_root")
def test_path_is_none_with_no_file(self, template):
template.root = "my_root"

with patch('os.path.isfile', Mock(return_value=False)):
path = template.path
assert path is None

assert None is path

def test_default(self, template):
assert "foo/bar" == template.default

def test_default_with_no_lines(self):
template = Template('abc', lines=[])
def test_default_with_no_lines(self, template):
template.lines = []

assert "" == template.default

def test_validate_meta_with_no_name(self):
template = Template('abc')
def test_validate_meta_with_no_name(self, template):
template.name = None

assert False is template.validate_meta()

def test_validate_meta_with_no_default_image(self):
template = Template('abc', name="ABC")
def test_validate_meta_with_no_default_image(self, template):
assert False is template.validate_meta()

def test_validate_meta_with_nonalphanumberic_name(self):
template = Template('abc', name="'ABC' Meme")
def test_validate_meta_with_nonalphanumberic_name(self, template):
template.name = "'ABC' Meme"

assert False is template.validate_meta()

def test_validate_link_with_bad_link(self, template):
mock_response = Mock()
mock_response.status_code = 404

with patch('requests.get', Mock(return_value=mock_response)):
template.link = "example.com/fake"

assert False is template.validate_link()

def test_validate_pass(self, template):
assert True is template.validate(validators=[])

def test_validate_all_pass(self, template):
"""If no validators find issues, the template is considered valid"""
mock_validators = [
lambda: True,
]
"""Verify a template is valid if all validators pass."""
mock_validators = [lambda: True]

assert True is template.validate(validators=mock_validators)

def test_validate_fail(self, template):
"""If any validators find an issue, the template is considered invalid"""
mock_validators = [
lambda: False,
]
"""Verify a template is invalid if any validators fail."""
mock_validators = [lambda: False]

assert False is template.validate(validators=mock_validators)

@patch('os.path.isfile', Mock(return_value=True))
def test_validate_link(self):
template = Template(key='abc',
name="The ABC Meme",
link="example.com")
def test_validate_link(self, template):
template.link = "example.com"

assert True is template.validate_link()

Expand All @@ -94,13 +92,9 @@ def test_validate_link(self):
((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT - 1), False),
])
@patch('PIL.Image.open')
def test_validate_size(self, mock_open, dimensions, valid):
def test_validate_size(self, mock_open, template, dimensions, valid):
mock_img = Mock()
mock_img.size = dimensions
mock_open.return_value = mock_img

template = Template(key='abc',
name="The ABC Meme",
link="example.com")

assert valid is template.validate_size()
33 changes: 32 additions & 1 deletion memegen/test/test_domain_text.py
@@ -1,4 +1,4 @@
# pylint: disable=R,C
# pylint: disable=no-self-use

from memegen.domain import Text

Expand All @@ -7,22 +7,26 @@ class TestTextInit:

def test_none(self):
text = Text()

assert "" == text.top
assert "" == text.bottom

def test_0_slashes(self):
text = Text("foo")

assert "FOO" == text.top
assert "" == text.bottom

def test_1_slash(self):
text = Text("foo/bar")

assert "FOO" == text.top
assert "BAR" == text.bottom
assert "" == text.get_line(2)

def test_2_slashes(self):
text = Text("foo/bar/qux")

assert "FOO" == text.top
assert "BAR" == text.bottom
assert "QUX" == text.get_line(2)
Expand All @@ -33,111 +37,138 @@ class TestTextLines:

def test_split_underscore_as_spaces(self):
text = Text("hello_world")

assert ["HELLO WORLD"] == text.lines

def test_split_dash_as_spaces(self):
text = Text("hello-world")

assert ["HELLO WORLD"] == text.lines

def test_split_case_as_spaces(self):
text = Text("helloWorld")

assert ["HELLO WORLD"] == text.lines

def test_keep_spaces(self):
text = Text("hello world")

assert ["HELLO WORLD"] == text.lines

def test_case_ignored_after_space(self):
text = Text("HELLO WORLD")

assert ["HELLO WORLD"] == text.lines

def test_ignore_initial_capital(self):
text = Text("HelloWorld")

assert ["HELLO WORLD"] == text.lines

def test_ignore_capital_after_sep(self):
text = Text("hello-World")

assert ["HELLO WORLD"] == text.lines

def test_ignore_capital_after_apostrophe(self):
text = Text("Y'ALL")

assert ["Y'ALL"] == text.lines

def test_strip_spaces(self):
text = Text(" hello World / ")

assert ["HELLO WORLD"] == text.lines

def test_duplicate_capitals_treated_as_spaces(self):
text = Text("IWantTHISPattern_to-Work")

assert ["I WANT THIS PATTERN TO WORK"] == text.lines

def test_no_space_after_apostrophe(self):
text = Text("that'd be great")

assert ["THAT'D BE GREAT"] == text.lines

def test_double_dashes_are_escaped(self):
text = Text("i'm---- /working 9--5")

assert ["I'M--", "WORKING 9-5"] == text.lines

def test_double_underscores_are_escaped(self):
text = Text("Calls ____init____/with __args")

assert ["CALLS __INIT__", "WITH _ARGS"] == text.lines

def test_special_characters_are_kept(self):
text = Text("special?")

assert ["SPECIAL?"] == text.lines

def test_question_marks_are_escaped(self):
text = Text("special~q~Q")

assert ["SPECIAL??"] == text.lines

def test_percents_are_escaped(self):
text = Text("99~p vs. 1~P")

assert ["99% VS. 1%"] == text.lines


class TestTextPath:

def test_case_ignored(self):
text = Text("hello/World")

assert "hello/world" == text.path

def test_single_dashes_kept(self):
text = Text("with-dashes/in-it")

assert "with-dashes/in-it" == text.path

def test_underscores_become_dashes(self):
text = Text("with_underscores/in_it")

assert "with-underscores/in-it" == text.path

def test_case_changes_become_dashes(self):
text = Text("withCaseChanges/InIT")

assert "with-case-changes/in-it" == text.path

def test_extra_spaces_are_stripped(self):
text = Text(" with spaces/ in it / ")

assert "with--spaces/in-it" == text.path

def test_single_underscore_is_kept(self):
text = Text(" _ ")

assert "_" == text.path

def test_duplicate_capitals_are_ignored(self):
text = Text("IWantTHISPattern_to-Work")

assert "i-want-this-pattern-to-work" == text.path

def test_double_dashes_are_escaped(self):
text = Text("i'm---- /working 9--5")

assert "i'm----/working-9--5" == text.path

def test_double_underscores_are_escaped(self):
text = Text("Calls ____init____/with __args")

assert "calls-____init____/with-__args" == text.path

def test_question_marks_are_escaped(self):
text = Text("special?")

assert "special~q" == text.path

def test_percents_are_escaped(self):
text = Text("50% off")

assert "50~p-off" == text.path
2 changes: 1 addition & 1 deletion memegen/test/test_services_image.py
@@ -1,4 +1,4 @@
# pylint: disable=R,C
# pylint: disable=no-self-use

from unittest.mock import patch, Mock

Expand Down
4 changes: 3 additions & 1 deletion memegen/test/test_services_link.py
@@ -1,4 +1,4 @@
# pylint: disable=R,C
# pylint: disable=no-self-use

import pytest

Expand All @@ -7,7 +7,9 @@ class TestLinkService:

def test_decode_encoded_parts(self, link_service):
code = link_service.encode("key", "my/path")

parts = link_service.decode(code)

assert ("key", "my/path") == parts

def test_decode_invalid_code(self, link_service):
Expand Down
2 changes: 1 addition & 1 deletion memegen/test/test_services_template.py
@@ -1,4 +1,4 @@
# pylint: disable=R,C
# pylint: disable=no-self-use

from unittest.mock import patch, Mock

Expand Down
4 changes: 3 additions & 1 deletion memegen/test/test_settings.py
@@ -1,4 +1,4 @@
# pylint: disable=R,C
# pylint: disable=no-self-use

import pytest

Expand All @@ -9,7 +9,9 @@ class TestGetConfig:

def test_get_valid(self):
config = get_config('prod')

assert issubclass(config, Config)
assert 'prod' == config.ENV

def test_get_none(self):
with pytest.raises(AssertionError):
Expand Down

0 comments on commit 3be7ad2

Please sign in to comment.