Completed
Pull Request — master (#132)
by Jace
08:27 queued 07:15
created

describe_path()   B

Complexity

Conditions 7

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 17
rs 7.3333
1
# pylint: disable=unused-variable
2
# pylint: disable=expression-not-assigned
3
# pylint: disable=misplaced-comparison-constant
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
4
5
from unittest.mock import patch, Mock
6
7
import pytest
8
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...
9
10
from memegen.domain import Template
11
12
13
def describe_template():
14
15
    def it_supports_comparison():
16
        t1 = Template('abc', "A Thing")
17
        t2 = Template('def')
18
        t3 = Template('def', "Do This")
19
20
        assert t1 != t2
21
        assert t2 == t3
22
        assert t1 < t3
23
24
    def describe_get_path():
25
26
        @patch('os.path.isfile', Mock(return_value=True))
27
        def it_returns_default_when_no_style(template):
28
            expect(template.get_path()) == "abc/default.png"
29
30
        @patch('os.path.isfile', Mock(return_value=True))
31
        def it_returns_alternate_when_style_provided(template):
32
            expect(template.get_path('Custom')) == "abc/custom.png"
33
34
        @patch('os.path.isfile', Mock(return_value=True))
35
        def it_returns_default_when_style_is_none(template):
36
            expect(template.get_path(None)) == "abc/default.png"
37
38
    def describe_path():
39
40
        def is_returned_when_file_exists(template):
41
            template.root = "my_root"
42
43
            with patch('os.path.isfile', Mock(return_value=True)):
44
                path = template.path
45
46
            assert "my_root/abc/default.png" == path
47
48
        def is_none_when_no_file(template):
49
            template.root = "my_root"
50
51
            with patch('os.path.isfile', Mock(return_value=False)):
52
                path = template.path
53
54
            assert None is path
55
56
    def describe_default_path():
57
58
        def is_based_on_lines(template):
59
            assert "foo/bar" == template.default_path
60
61
        def is_underscore_when_no_lines(template):
62
            template.lines = []
63
64
            assert "_" == template.default_path
65
66
    def describe_sample_path():
67
68
        def is_based_on_lines(template):
69
            assert "foo/bar" == template.sample_path
70
71
        def is_placeholder_when_no_lines(template):
72
            template.lines = []
73
74
            assert "your-text/goes-here" == template.sample_path
75
76
    def describe_validate_meta():
77
78
        def with_no_name(template):
79
            template.name = None
80
81
            assert False is template.validate_meta()
82
83
        def with_no_default_image(template):
84
            assert False is template.validate_meta()
85
86
        def with_nonalphanumberic_name(template):
87
            template.name = "'ABC' Meme"
88
89
            assert False is template.validate_meta()
90
91
    def describe_validate_link():
92
93
        def with_bad_link(template):
94
            mock_response = Mock()
95
            mock_response.status_code = 404
96
97
            with patch('requests.get', Mock(return_value=mock_response)):
98
                template.link = "example.com/fake"
99
100
                assert False is template.validate_link()
101
102
        @patch('os.path.isfile', Mock(return_value=True))
103
        def with_cached_valid_link(template):
104
            template.link = "example.com"
105
106
            assert True is template.validate_link()
107
108
    def describe_validate_size():
109
110
        @pytest.mark.parametrize('dimensions,valid', [
111
            ((Template.MIN_WIDTH, Template.MIN_HEIGHT), True),
112
            ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT), False),
113
            ((Template.MIN_WIDTH, Template.MIN_HEIGHT - 1), False),
114
            ((Template.MIN_WIDTH - 1, Template.MIN_HEIGHT - 1), False),
115
        ])
116
        @patch('PIL.Image.open')
117
        def with_various_dimenions(mock_open, template, dimensions, valid):
118
            mock_img = Mock()
119
            mock_img.size = dimensions
120
            mock_open.return_value = mock_img
121
122
            assert valid is template.validate_size()
123
124
    def describe_validate():
125
126
        def with_no_validators(template):
127
            assert True is template.validate(validators=[])
128
129
        def with_all_passing_validators(template):
130
            """Verify a template is valid if all validators pass."""
131
            mock_validators = [lambda: True]
132
133
            assert True is template.validate(validators=mock_validators)
134
135
        def with_one_failing_validator(template):
136
            """Verify a template is invalid if any validators fail."""
137
            mock_validators = [lambda: False]
138
139
            assert False is template.validate(validators=mock_validators)
140