|
1
|
|
|
# pylint: disable=unused-variable,expression-not-assigned,misplaced-comparison-constant,singleton-comparison |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import tempfile |
|
4
|
|
|
from pathlib import Path |
|
5
|
|
|
from unittest.mock import patch, Mock |
|
6
|
|
|
|
|
7
|
|
|
import pytest |
|
8
|
|
|
from expecter import expect |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
from memegen.domain import Template |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def temp(path): |
|
14
|
|
|
return Path(tempfile.gettempdir(), path) |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def describe_template(): |
|
18
|
|
|
|
|
19
|
|
|
def it_supports_comparison(): |
|
20
|
|
|
t1 = Template('abc', "A Thing") |
|
21
|
|
|
t2 = Template('def') |
|
22
|
|
|
t3 = Template('def', "Do This") |
|
23
|
|
|
|
|
24
|
|
|
assert t1 != t2 |
|
25
|
|
|
assert t2 == t3 |
|
26
|
|
|
assert t1 < t3 |
|
27
|
|
|
|
|
28
|
|
|
def describe_get_path(): |
|
29
|
|
|
|
|
30
|
|
|
@patch('pathlib.Path.is_file', Mock(return_value=True)) |
|
31
|
|
|
def it_returns_default_when_no_style(template): |
|
32
|
|
|
expect(template.get_path()) == Path("abc/default.png") |
|
33
|
|
|
|
|
34
|
|
|
@patch('pathlib.Path.is_file', Mock(return_value=True)) |
|
35
|
|
|
def it_returns_alternate_when_style_provided(template): |
|
36
|
|
|
expect(template.get_path('Custom')) == Path("abc/custom.png") |
|
37
|
|
|
|
|
38
|
|
|
@patch('pathlib.Path.is_file', Mock(return_value=True)) |
|
39
|
|
|
def it_returns_default_when_style_is_none(template): |
|
40
|
|
|
expect(template.get_path(None)) == Path("abc/default.png") |
|
41
|
|
|
|
|
42
|
|
|
@patch('pathlib.Path.is_file', Mock(return_value=False)) |
|
43
|
|
|
def it_considers_urls_valid_styles(template): |
|
44
|
|
|
url = "http://example.com" |
|
45
|
|
|
path = temp("a9b9f04336ce0181a08e774e01113b31") |
|
46
|
|
|
expect(template.get_path(url)) == path |
|
47
|
|
|
|
|
48
|
|
|
@patch('pathlib.Path.is_file', Mock(return_value=True)) |
|
49
|
|
|
def it_caches_file_downloads(template): |
|
50
|
|
|
url = "http://this/will/be/ignored" |
|
51
|
|
|
path = temp("d888710f0697650eb68fc9dcbb976d4c") |
|
52
|
|
|
expect(template.get_path(url)) == path |
|
53
|
|
|
|
|
54
|
|
|
def it_handles_bad_urls(template): |
|
55
|
|
|
expect(template.get_path("http://invalid")) == None |
|
56
|
|
|
|
|
57
|
|
|
def it_handles_invalid_paths(template): |
|
58
|
|
|
expect(template.get_path("@#$%^")) == None |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
def describe_path(): |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def is_returned_when_file_exists(template): |
|
63
|
|
|
template.root = "my_root" |
|
64
|
|
|
|
|
65
|
|
|
with patch('pathlib.Path.is_file', Mock(return_value=True)): |
|
66
|
|
|
path = template.path |
|
67
|
|
|
|
|
68
|
|
|
expect(path) == Path("my_root/abc/default.png") |
|
69
|
|
|
|
|
70
|
|
|
def is_none_when_no_file(template): |
|
71
|
|
|
template.root = "my_root" |
|
72
|
|
|
|
|
73
|
|
|
with patch('pathlib.Path.is_file', Mock(return_value=False)): |
|
74
|
|
|
path = template.path |
|
75
|
|
|
|
|
76
|
|
|
expect(path) == None |
|
77
|
|
|
|
|
78
|
|
|
def describe_default_path(): |
|
79
|
|
|
|
|
80
|
|
|
def is_based_on_lines(template): |
|
81
|
|
|
expect(template.default_path) == "foo/bar" |
|
82
|
|
|
|
|
83
|
|
|
def is_underscore_when_no_lines(template): |
|
84
|
|
|
template.lines = [] |
|
85
|
|
|
|
|
86
|
|
|
expect(template.default_path) == "_" |
|
87
|
|
|
|
|
88
|
|
|
def describe_styles(): |
|
89
|
|
|
|
|
90
|
|
|
@patch('os.listdir', Mock(return_value=[])) |
|
91
|
|
|
def is_empty_when_no_alternate_images(template): |
|
92
|
|
|
expect(template.styles) == [] |
|
93
|
|
|
|
|
94
|
|
|
@patch('os.listdir', Mock(return_value=['foo.jpg', 'bar.png'])) |
|
95
|
|
|
def is_filesnames_of_alternate_images(template): |
|
96
|
|
|
expect(template.styles) == ['bar', 'foo'] |
|
97
|
|
|
|
|
98
|
|
|
def describe_sample_path(): |
|
99
|
|
|
|
|
100
|
|
|
def is_based_on_lines(template): |
|
101
|
|
|
expect(template.sample_path) == "foo/bar" |
|
102
|
|
|
|
|
103
|
|
|
def is_placeholder_when_no_lines(template): |
|
104
|
|
|
template.lines = [] |
|
105
|
|
|
|
|
106
|
|
|
expect(template.sample_path) == "your_text/goes_here" |
|
107
|
|
|
|
|
108
|
|
|
def describe_keywords(): |
|
109
|
|
|
|
|
110
|
|
|
def is_the_set_of_all_relevant_terms(template): |
|
111
|
|
|
template.lines[0] = "A day in the life" |
|
112
|
|
|
|
|
113
|
|
|
expect(template.keywords) == \ |
|
114
|
|
|
{'bar', 'the', 'day', 'in', 'abc', 'a', 'life'} |
|
115
|
|
|
|
|
116
|
|
|
def describe_search(): |
|
117
|
|
|
|
|
118
|
|
|
def it_counts_contained_terms(template): |
|
119
|
|
|
template.key = 'Foo' |
|
120
|
|
|
template.name = "The Foobar Meme" |
|
121
|
|
|
template.aliases.append('Foo') |
|
122
|
|
|
template.aliases.append('Foobar') |
|
123
|
|
|
template.lines[0] = "This on time foobar happened" |
|
124
|
|
|
|
|
125
|
|
|
expect(template.search("Foo")) == 5 |
|
126
|
|
|
|
|
127
|
|
|
def it_treats_none_specially(template): |
|
128
|
|
|
expect(template.search(None)) == -1 |
|
129
|
|
|
|
|
130
|
|
|
def describe_validate_meta(): |
|
131
|
|
|
|
|
132
|
|
|
def with_no_name(template): |
|
133
|
|
|
template.name = None |
|
134
|
|
|
|
|
135
|
|
|
expect(template.validate_meta()) == False |
|
136
|
|
|
|
|
137
|
|
|
def with_no_default_image(template): |
|
138
|
|
|
expect(template.validate_meta()) == False |
|
139
|
|
|
|
|
140
|
|
|
def with_nonalphanumberic_name(template): |
|
141
|
|
|
template.name = "'ABC' Meme" |
|
142
|
|
|
|
|
143
|
|
|
expect(template.validate_meta()) == False |
|
144
|
|
|
|
|
145
|
|
|
def describe_validate_link(): |
|
146
|
|
|
|
|
147
|
|
|
def with_bad_link(template): |
|
148
|
|
|
mock_response = Mock() |
|
149
|
|
|
mock_response.status_code = 404 |
|
150
|
|
|
|
|
151
|
|
|
with patch('requests.head', Mock(return_value=mock_response)): |
|
152
|
|
|
template.link = "example.com/fake" |
|
153
|
|
|
|
|
154
|
|
View Code Duplication |
expect(template.validate_link()) == False |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
@patch('pathlib.Path.is_file', Mock(return_value=True)) |
|
157
|
|
|
def with_cached_valid_link(template): |
|
158
|
|
|
template.link = "already_cached_site.com" |
|
159
|
|
|
|
|
160
|
|
|
expect(template.validate_link()) == True |
|
161
|
|
|
|
|
162
|
|
|
def describe_validate_size(): |
|
163
|
|
|
|
|
164
|
|
|
@pytest.mark.parametrize('dimensions,valid', [ |
|
165
|
|
|
((Template.MIN_WIDTH, Template.MIN_HEIGHT), True), |
|
166
|
|
|
((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT), False), |
|
167
|
|
|
((Template.MIN_WIDTH, Template.MIN_HEIGHT - 1), False), |
|
168
|
|
|
((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT - 1), False), |
|
169
|
|
|
]) |
|
170
|
|
|
@patch('PIL.Image.open') |
|
171
|
|
|
def with_various_dimenions(mock_open, template, dimensions, valid): |
|
172
|
|
|
mock_img = Mock() |
|
173
|
|
|
mock_img.size = dimensions |
|
174
|
|
|
mock_open.return_value = mock_img |
|
175
|
|
|
|
|
176
|
|
|
expect(template.validate_size()) == valid |
|
177
|
|
|
|
|
178
|
|
|
def describe_validate(): |
|
179
|
|
|
|
|
180
|
|
|
def with_no_validators(template): |
|
181
|
|
|
expect(template.validate([])) == True |
|
182
|
|
|
|
|
183
|
|
|
def with_all_passing_validators(template): |
|
184
|
|
|
"""Verify a template is valid if all validators pass.""" |
|
185
|
|
|
mock_validators = [lambda: True] |
|
186
|
|
|
|
|
187
|
|
|
expect(template.validate(validators=mock_validators)) == True |
|
188
|
|
|
|
|
189
|
|
|
def with_one_failing_validator(template): |
|
190
|
|
|
"""Verify a template is invalid if any validators fail.""" |
|
191
|
|
|
mock_validators = [lambda: False] |
|
192
|
|
|
|
|
193
|
|
|
expect(template.validate(validators=mock_validators)) == False |
|
194
|
|
|
|