1
1
import os
2
2
3
- import pretend
4
3
import pytest
5
4
from django .utils import encoding
5
+ from pytest_mock import MockFixture
6
6
7
7
from static_precompiler import compilers , models , settings
8
8
9
9
10
- def test_is_supported (monkeypatch ):
11
- monkeypatch . setattr ("static_precompiler.compilers.base.BaseCompiler.input_extension" , "foo" )
10
+ def test_is_supported (mocker : MockFixture ):
11
+ mocker . patch ("static_precompiler.compilers.base.BaseCompiler.input_extension" , "foo" )
12
12
compiler = compilers .BaseCompiler ()
13
13
14
14
assert compiler .is_supported ("test.foo" ) is True
15
15
assert compiler .is_supported ("test.bar" ) is False
16
16
assert compiler .is_supported ("foo.test" ) is False
17
17
18
18
19
- def test_get_output_filename (monkeypatch ):
19
+ def test_get_output_filename (mocker : MockFixture ):
20
20
compiler = compilers .BaseCompiler ()
21
21
22
- monkeypatch . setattr (compiler , "input_extension" , "coffee" )
23
- monkeypatch . setattr (compiler , "output_extension" , "js" )
22
+ mocker . patch . object (compiler , "input_extension" , "coffee" )
23
+ mocker . patch . object (compiler , "output_extension" , "js" )
24
24
25
25
assert compiler .get_output_filename ("dummy.coffee" ) == "dummy.js"
26
26
assert compiler .get_output_filename ("dummy.coffee.coffee" ) == "dummy.coffee.js"
@@ -47,85 +47,87 @@ def test_get_full_source_path():
47
47
)
48
48
49
49
50
- def test_get_output_path (monkeypatch ):
50
+ def test_get_output_path (mocker : MockFixture ):
51
51
compiler = compilers .BaseCompiler ()
52
- monkeypatch .setattr (compiler , "get_output_filename" , lambda source_path : source_path .replace (".coffee" , ".js" ))
52
+ mocker .patch .object (
53
+ compiler , "get_output_filename" , side_effect = lambda source_path : source_path .replace (".coffee" , ".js" )
54
+ )
53
55
54
56
assert compiler .get_output_path ("scripts/test.coffee" ) == settings .OUTPUT_DIR + "/scripts/test.js"
55
57
assert compiler .get_output_path ("/scripts/test.coffee" ) == settings .OUTPUT_DIR + "/scripts/test.js"
56
58
57
59
58
- def test_get_full_output_path (monkeypatch ):
60
+ def test_get_full_output_path (mocker : MockFixture ):
59
61
compiler = compilers .BaseCompiler ()
60
- monkeypatch . setattr (compiler , "get_output_path" , lambda source_path : settings .OUTPUT_DIR + "/dummy.js" )
62
+ mocker . patch . object (compiler , "get_output_path" , return_value = settings .OUTPUT_DIR + "/dummy.js" )
61
63
62
64
assert compiler .get_full_output_path ("dummy.coffee" ) == os .path .join (settings .ROOT , settings .OUTPUT_DIR , "dummy.js" )
63
65
64
66
65
- def test_get_source_mtime (monkeypatch ):
67
+ def test_get_source_mtime (mocker : MockFixture ):
66
68
compiler = compilers .BaseCompiler ()
67
69
68
- monkeypatch . setattr (compiler , "get_full_source_path" , lambda source_path : "dummy.coffee" )
69
- monkeypatch . setattr ("static_precompiler.mtime.get_mtime" , lambda filename : 1 )
70
+ mocker . patch . object (compiler , "get_full_source_path" , return_value = "dummy.coffee" )
71
+ mocker . patch ("static_precompiler.mtime.get_mtime" , return_value = 1 )
70
72
71
73
assert compiler .get_source_mtime ("dummy.coffee" ) == 1
72
74
73
75
74
- def test_get_output_mtime (monkeypatch ):
76
+ def test_get_output_mtime (mocker : MockFixture ):
75
77
compiler = compilers .BaseCompiler ()
76
78
77
- monkeypatch . setattr (compiler , "get_full_output_path" , lambda output_path : "dummy.js" )
78
- monkeypatch . setattr ("os.path.exists" , lambda path : False )
79
+ mocker . patch . object (compiler , "get_full_output_path" , return_value = "dummy.js" )
80
+ mocker . patch ("os.path.exists" , return_value = False )
79
81
80
82
assert compiler .get_output_mtime ("dummy.coffee" ) is None
81
83
82
- monkeypatch . setattr ("os.path.exists" , lambda path : True )
84
+ mocker . patch ("os.path.exists" , return_value = True )
83
85
84
- monkeypatch . setattr ("static_precompiler.mtime.get_mtime" , lambda filename : 1 )
86
+ mocker . patch ("static_precompiler.mtime.get_mtime" , return_value = 1 )
85
87
assert compiler .get_output_mtime ("dummy.coffee" ) == 1
86
88
87
89
88
- def test_should_compile (monkeypatch ):
90
+ def test_should_compile (mocker : MockFixture ):
89
91
compiler = compilers .BaseCompiler ()
90
92
91
- monkeypatch . setattr (compiler , "get_dependencies" , lambda source_path : ["B" , "C" ])
93
+ mocker . patch . object (compiler , "get_dependencies" , return_value = ["B" , "C" ])
92
94
93
95
mtimes = {
94
96
"A" : 1 ,
95
97
"B" : 3 ,
96
98
"C" : 5 ,
97
99
}
98
100
99
- monkeypatch . setattr (compiler , "get_source_mtime" , lambda x : mtimes [x ])
100
- monkeypatch . setattr (compiler , "get_output_mtime" , lambda x : None )
101
+ mocker . patch . object (compiler , "get_source_mtime" , side_effect = lambda x : mtimes [x ])
102
+ mocker . patch . object (compiler , "get_output_mtime" , return_value = None )
101
103
102
104
assert compiler .should_compile ("A" ) is True
103
105
104
- monkeypatch . setattr (compiler , "supports_dependencies" , True )
106
+ mocker . patch . object (compiler , "supports_dependencies" , True )
105
107
106
- monkeypatch . setattr (compiler , "get_output_mtime" , lambda x : 6 )
108
+ mocker . patch . object (compiler , "get_output_mtime" , return_value = 6 )
107
109
assert compiler .should_compile ("A" ) is False
108
110
109
- monkeypatch . setattr (compiler , "get_output_mtime" , lambda x : 5 )
111
+ mocker . patch . object (compiler , "get_output_mtime" , return_value = 5 )
110
112
assert compiler .should_compile ("A" ) is True
111
113
112
- monkeypatch . setattr (compiler , "get_output_mtime" , lambda x : 4 )
114
+ mocker . patch . object (compiler , "get_output_mtime" , return_value = 4 )
113
115
assert compiler .should_compile ("A" ) is True
114
116
115
- monkeypatch . setattr (compiler , "get_output_mtime" , lambda x : 2 )
117
+ mocker . patch . object (compiler , "get_output_mtime" , return_value = 2 )
116
118
assert compiler .should_compile ("A" ) is True
117
119
118
- monkeypatch . setattr (compiler , "supports_dependencies" , False )
120
+ mocker . patch . object (compiler , "supports_dependencies" , False )
119
121
120
122
assert compiler .should_compile ("A" ) is False
121
123
122
- monkeypatch . setattr (compiler , "get_output_mtime" , lambda x : 1 )
124
+ mocker . patch . object (compiler , "get_output_mtime" , return_value = 1 )
123
125
assert compiler .should_compile ("A" ) is True
124
126
125
- monkeypatch . setattr (compiler , "get_output_mtime" , lambda x : 0 )
127
+ mocker . patch . object (compiler , "get_output_mtime" , return_value = 0 )
126
128
assert compiler .should_compile ("A" ) is True
127
129
128
- monkeypatch . setattr ("static_precompiler.settings.DISABLE_AUTO_COMPILE" , True )
130
+ mocker . patch ("static_precompiler.settings.DISABLE_AUTO_COMPILE" , True )
129
131
assert compiler .should_compile ("A" ) is False
130
132
131
133
@@ -140,60 +142,49 @@ def test_compile_source():
140
142
compiler .compile_source ("source" )
141
143
142
144
143
- def test_compile (monkeypatch ):
145
+ def test_compile (mocker : MockFixture ):
144
146
compiler = compilers .BaseCompiler ()
145
147
146
- monkeypatch . setattr (compiler , "compile_file" , pretend . call_recorder ( lambda * args : "dummy.js" ) )
147
- monkeypatch . setattr (compiler , "update_dependencies" , pretend . call_recorder ( lambda * args : None ) )
148
- monkeypatch . setattr (compiler , "find_dependencies" , pretend . call_recorder ( lambda * args : ["A" , "B" ]) )
149
- monkeypatch . setattr (compiler , "get_output_path" , lambda * args : "dummy.js" )
150
- monkeypatch . setattr (compiler , "is_supported" , lambda * args : False )
151
- monkeypatch . setattr (compiler , "should_compile" , lambda * args , ** kwargs : True )
148
+ compile_file = mocker . patch . object (compiler , "compile_file" , return_value = "dummy.js" )
149
+ update_dependencies = mocker . patch . object (compiler , "update_dependencies" , return_value = None )
150
+ find_dependencies = mocker . patch . object (compiler , "find_dependencies" , return_value = ["A" , "B" ])
151
+ mocker . patch . object (compiler , "get_output_path" , return_value = "dummy.js" )
152
+ is_supported = mocker . patch . object (compiler , "is_supported" , return_value = False )
153
+ should_compile = mocker . patch . object (compiler , "should_compile" , return_value = True )
152
154
153
155
with pytest .raises (ValueError ):
154
156
compiler .compile ("dummy.coffee" )
155
157
156
- # noinspection PyUnresolvedReferences
157
- assert compiler .compile_file .calls == []
158
-
159
- monkeypatch .setattr (compiler , "is_supported" , lambda * args : True )
160
- monkeypatch .setattr (compiler , "should_compile" , lambda * args , ** kwargs : False )
158
+ compile_file .assert_not_called ()
159
+ is_supported .return_value = True
160
+ should_compile .return_value = False
161
161
162
162
assert compiler .compile ("dummy.coffee" ) == "dummy.js"
163
- # noinspection PyUnresolvedReferences
164
- assert compiler .compile_file .calls == []
163
+ compile_file .assert_not_called ()
165
164
166
- monkeypatch . setattr ( compiler , "should_compile" , lambda * args , ** kwargs : True )
165
+ should_compile . return_value = True
167
166
assert compiler .compile ("dummy.coffee" ) == "dummy.js"
168
167
169
- # noinspection PyUnresolvedReferences
170
- assert compiler .compile_file .calls == [pretend .call ("dummy.coffee" )]
168
+ compile_file .assert_called_once_with ("dummy.coffee" )
171
169
172
- # noinspection PyUnresolvedReferences
173
- assert compiler .update_dependencies .calls == []
170
+ update_dependencies .assert_not_called ()
174
171
175
- monkeypatch .setattr (compiler , "supports_dependencies" , True )
176
172
compiler .supports_dependencies = True
177
173
compiler .compile ("dummy.coffee" )
178
- # noinspection PyUnresolvedReferences
179
- assert compiler .find_dependencies .calls == [pretend .call ("dummy.coffee" )]
180
- # noinspection PyUnresolvedReferences
181
- assert compiler .update_dependencies .calls == [pretend .call ("dummy.coffee" , ["A" , "B" ])]
174
+ find_dependencies .assert_called_once_with ("dummy.coffee" )
175
+ update_dependencies .assert_called_once_with ("dummy.coffee" , ["A" , "B" ])
182
176
183
177
184
- def test_compile_lazy (monkeypatch ):
178
+ def test_compile_lazy (mocker : MockFixture ):
185
179
compiler = compilers .BaseCompiler ()
186
180
187
- monkeypatch . setattr (compiler , "compile" , pretend . call_recorder ( lambda path : path ) )
181
+ compile = mocker . patch . object (compiler , "compile" , side_effect = lambda x : x )
188
182
189
183
lazy_compiled = compiler .compile_lazy ("dummy.coffee" )
190
- # noinspection PyUnresolvedReferences
191
- assert compiler .compile .calls == []
184
+ compile .assert_not_called ()
192
185
193
186
assert encoding .force_str (lazy_compiled ) == "dummy.coffee"
194
-
195
- # noinspection PyUnresolvedReferences
196
- assert compiler .compile .calls == [pretend .call ("dummy.coffee" )]
187
+ compile .assert_called_once_with ("dummy.coffee" )
197
188
198
189
199
190
def test_find_dependencies ():
@@ -202,7 +193,7 @@ def test_find_dependencies():
202
193
203
194
204
195
@pytest .mark .django_db
205
- def test_get_dependencies (monkeypatch ):
196
+ def test_get_dependencies (mocker : MockFixture ):
206
197
compiler = compilers .BaseCompiler ()
207
198
208
199
assert models .Dependency .objects .exists () is False
@@ -218,18 +209,18 @@ def get_full_source_path(source_path):
218
209
raise ValueError ()
219
210
return source_path
220
211
221
- monkeypatch . setattr (compiler , "get_full_source_path" , get_full_source_path )
212
+ mocker . patch . object (compiler , "get_full_source_path" , side_effect = get_full_source_path )
222
213
223
214
assert list (models .Dependency .objects .all ()) == [dependency_1 , dependency_2 ]
224
215
225
216
assert compiler .get_dependencies ("spam.scss" ) == ["ham.scss" ]
226
217
227
- # Dependency the refers to non-existing file were removed.
218
+ # The Dependency that refers to non-existing file was removed.
228
219
assert list (models .Dependency .objects .all ()) == [dependency_1 ]
229
220
230
221
231
222
@pytest .mark .django_db
232
- def test_get_dependents (monkeypatch ):
223
+ def test_get_dependents (mocker : MockFixture ):
233
224
compiler = compilers .BaseCompiler ()
234
225
235
226
assert not models .Dependency .objects .exists ()
@@ -245,13 +236,13 @@ def get_full_source_path(source_path):
245
236
raise ValueError ()
246
237
return source_path
247
238
248
- monkeypatch . setattr (compiler , "get_full_source_path" , get_full_source_path )
239
+ mocker . patch . object (compiler , "get_full_source_path" , side_effect = get_full_source_path )
249
240
250
241
assert list (models .Dependency .objects .all ()) == [dependency_1 , dependency_2 ]
251
242
252
243
assert compiler .get_dependents ("spam.scss" ) == ["ham.scss" ]
253
244
254
- # Dependency the refers to non-existing file were removed.
245
+ # The Dependency that refers to non-existing file was removed.
255
246
assert list (models .Dependency .objects .all ()) == [dependency_1 ]
256
247
257
248
0 commit comments