Completed
Push — master ( d36755...584f50 )
by Jace
14s
created

it_supports_comparison()   A

Complexity

Conditions 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 8
rs 9.2
1
# pylint: disable=unused-variable,expression-not-assigned,misplaced-comparison-constant,singleton-comparison
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
introduced by
Bad option value 'singleton-comparison'
Loading history...
2
3
from unittest.mock import patch, Mock
4
5
import pytest
6
from expecter import expect
0 ignored issues
show
Configuration introduced by
The import expecter could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7
8
from memegen.domain import Template
9
10
11
def describe_template():
12
13
    def it_supports_comparison():
14
        t1 = Template('abc', "A Thing")
15
        t2 = Template('def')
16
        t3 = Template('def', "Do This")
17
18
        assert t1 != t2
19
        assert t2 == t3
20
        assert t1 < t3
21
22
    def describe_get_path():
23
24
        @patch('os.path.isfile', Mock(return_value=True))
25
        def it_returns_default_when_no_style(template):
26
            expect(template.get_path()) == "abc/default.png"
27
28
        @patch('os.path.isfile', Mock(return_value=True))
29
        def it_returns_alternate_when_style_provided(template):
30
            expect(template.get_path('Custom')) == "abc/custom.png"
31
32
        @patch('os.path.isfile', Mock(return_value=True))
33
        def it_returns_default_when_style_is_none(template):
34
            expect(template.get_path(None)) == "abc/default.png"
35
36
        @patch('os.path.isfile', Mock(return_value=False))
37
        def it_considers_urls_valid_styles(template):
38
            url = "http://example.com"
39
            path = "/tmp/a9b9f04336ce0181a08e774e01113b31"
40
            expect(template.get_path(url)) == path
41
42
        @patch('os.path.isfile', Mock(return_value=True))
43
        def it_caches_file_downloads(template):
44
            url = "http://this/will/be/ignored"
45
            path = "/tmp/d888710f0697650eb68fc9dcbb976d4c"
46
            expect(template.get_path(url)) == path
47
48
        def it_handles_bad_urls(template):
49
            url = "http://invalid"
50
            expect(template.get_path(url)) == None
51
52
    def describe_path():
53
54
        def is_returned_when_file_exists(template):
55
            template.root = "my_root"
56
57
            with patch('os.path.isfile', Mock(return_value=True)):
58
                path = template.path
59
60
            assert "my_root/abc/default.png" == path
61
62
        def is_none_when_no_file(template):
63
            template.root = "my_root"
64
65
            with patch('os.path.isfile', Mock(return_value=False)):
66
                path = template.path
67
68
            assert None is path
69
70
    def describe_default_path():
71
72
        def is_based_on_lines(template):
73
            assert "foo/bar" == template.default_path
74
75
        def is_underscore_when_no_lines(template):
76
            template.lines = []
77
78
            assert "_" == template.default_path
79
80
    def describe_styles():
81
82
        @patch('os.listdir', Mock(return_value=[]))
83
        def is_empty_when_no_alternate_images(template):
84
            expect(template.styles) == []
85
86
        @patch('os.listdir', Mock(return_value=['foo.jpg', 'bar.png']))
87
        def is_filesnames_of_alternate_images(template):
88
            expect(template.styles) == ['bar', 'foo']
89
90
    def describe_sample_path():
91
92
        def is_based_on_lines(template):
93
            assert "foo/bar" == template.sample_path
94
95
        def is_placeholder_when_no_lines(template):
96
            template.lines = []
97
98
            assert "your-text/goes-here" == template.sample_path
99
100
    def describe_match():
101
102
        def it_returns_none_when_no_match(template):
103
            expect(template.match("")) == (0, None)
104
105
        def it_returns_the_best_matching_result(template):
106
            template.compile_regexes([r"(\w*)/?(abc)", r"(\w*)/?(def)"])
107
108
            expect(template.match("_/def")) == (0.42, "_/def")
109
110
    def describe_validate_meta():
111
112
        def with_no_name(template):
113
            template.name = None
114
115
            assert False is template.validate_meta()
116
117
        def with_no_default_image(template):
118
            assert False is template.validate_meta()
119
120
        def with_nonalphanumberic_name(template):
121
            template.name = "'ABC' Meme"
122
123
            assert False is template.validate_meta()
124
125
    def describe_validate_link():
126
127
        def with_bad_link(template):
128
            mock_response = Mock()
129
            mock_response.status_code = 404
130
131
            with patch('requests.get', Mock(return_value=mock_response)):
132
                template.link = "example.com/fake"
133
134
                assert False is template.validate_link()
135
136
        @patch('os.path.isfile', Mock(return_value=True))
137
        def with_cached_valid_link(template):
138
            template.link = "example.com"
139
140
            assert True is template.validate_link()
141
142
    def describe_validate_size():
143
144
        @pytest.mark.parametrize('dimensions,valid', [
145
            ((Template.MIN_WIDTH, Template.MIN_HEIGHT), True),
146
            ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT), False),
147
            ((Template.MIN_WIDTH, Template.MIN_HEIGHT - 1), False),
148
            ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT - 1), False),
149
        ])
150
        @patch('PIL.Image.open')
151
        def with_various_dimenions(mock_open, template, dimensions, valid):
152
            mock_img = Mock()
153
            mock_img.size = dimensions
154
            mock_open.return_value = mock_img
155
156
            assert valid is template.validate_size()
157
158
    def describe_validate_regexes():
159
160
        def with_missing_split(template):
161
            template.compile_regexes([".*"])
162
163
            assert False is template.validate_regexes()
164
165
    def describe_validate():
166
167
        def with_no_validators(template):
168
            assert True is template.validate(validators=[])
169
170
        def with_all_passing_validators(template):
171
            """Verify a template is valid if all validators pass."""
172
            mock_validators = [lambda: True]
173
174
            assert True is template.validate(validators=mock_validators)
175
176
        def with_one_failing_validator(template):
177
            """Verify a template is invalid if any validators fail."""
178
            mock_validators = [lambda: False]
179
180
            assert False is template.validate(validators=mock_validators)
181