1
1
"""All pytest-django fixtures"""
2
- from typing import List , Any
2
+ from typing import Any , Generator , List
3
3
import os
4
4
from contextlib import contextmanager
5
5
from functools import partial
10
10
from .django_compat import is_django_unittest
11
11
from .lazy_django import skip_if_no_django
12
12
13
+ TYPE_CHECKING = False
14
+ if TYPE_CHECKING :
15
+ import django
16
+
17
+
13
18
__all__ = [
14
19
"django_db_setup" ,
15
20
"db" ,
32
37
33
38
34
39
@pytest .fixture (scope = "session" )
35
- def django_db_modify_db_settings_tox_suffix ():
40
+ def django_db_modify_db_settings_tox_suffix () -> None :
36
41
skip_if_no_django ()
37
42
38
43
tox_environment = os .getenv ("TOX_PARALLEL_ENV" )
@@ -42,7 +47,7 @@ def django_db_modify_db_settings_tox_suffix():
42
47
43
48
44
49
@pytest .fixture (scope = "session" )
45
- def django_db_modify_db_settings_xdist_suffix (request ):
50
+ def django_db_modify_db_settings_xdist_suffix (request ) -> None :
46
51
skip_if_no_django ()
47
52
48
53
xdist_suffix = getattr (request .config , "workerinput" , {}).get ("workerid" )
@@ -53,42 +58,44 @@ def django_db_modify_db_settings_xdist_suffix(request):
53
58
54
59
@pytest .fixture (scope = "session" )
55
60
def django_db_modify_db_settings_parallel_suffix (
56
- django_db_modify_db_settings_tox_suffix ,
57
- django_db_modify_db_settings_xdist_suffix ,
58
- ):
61
+ django_db_modify_db_settings_tox_suffix : None ,
62
+ django_db_modify_db_settings_xdist_suffix : None ,
63
+ ) -> None :
59
64
skip_if_no_django ()
60
65
61
66
62
67
@pytest .fixture (scope = "session" )
63
- def django_db_modify_db_settings (django_db_modify_db_settings_parallel_suffix ):
68
+ def django_db_modify_db_settings (
69
+ django_db_modify_db_settings_parallel_suffix : None ,
70
+ ) -> None :
64
71
skip_if_no_django ()
65
72
66
73
67
74
@pytest .fixture (scope = "session" )
68
- def django_db_use_migrations (request ):
75
+ def django_db_use_migrations (request ) -> bool :
69
76
return not request .config .getvalue ("nomigrations" )
70
77
71
78
72
79
@pytest .fixture (scope = "session" )
73
- def django_db_keepdb (request ):
80
+ def django_db_keepdb (request ) -> bool :
74
81
return request .config .getvalue ("reuse_db" )
75
82
76
83
77
84
@pytest .fixture (scope = "session" )
78
- def django_db_createdb (request ):
85
+ def django_db_createdb (request ) -> bool :
79
86
return request .config .getvalue ("create_db" )
80
87
81
88
82
89
@pytest .fixture (scope = "session" )
83
90
def django_db_setup (
84
91
request ,
85
- django_test_environment ,
92
+ django_test_environment : None ,
86
93
django_db_blocker ,
87
- django_db_use_migrations ,
88
- django_db_keepdb ,
89
- django_db_createdb ,
90
- django_db_modify_db_settings ,
91
- ):
94
+ django_db_use_migrations : bool ,
95
+ django_db_keepdb : bool ,
96
+ django_db_createdb : bool ,
97
+ django_db_modify_db_settings : None ,
98
+ ) -> None :
92
99
"""Top level fixture to ensure test databases are available"""
93
100
from django .test .utils import setup_databases , teardown_databases
94
101
@@ -107,7 +114,7 @@ def django_db_setup(
107
114
** setup_databases_args
108
115
)
109
116
110
- def teardown_database ():
117
+ def teardown_database () -> None :
111
118
with django_db_blocker .unblock ():
112
119
try :
113
120
teardown_databases (db_cfg , verbosity = request .config .option .verbose )
@@ -123,8 +130,11 @@ def teardown_database():
123
130
124
131
125
132
def _django_db_fixture_helper (
126
- request , django_db_blocker , transactional = False , reset_sequences = False
127
- ):
133
+ request ,
134
+ django_db_blocker ,
135
+ transactional : bool = False ,
136
+ reset_sequences : bool = False ,
137
+ ) -> None :
128
138
if is_django_unittest (request ):
129
139
return
130
140
@@ -149,7 +159,7 @@ class ResetSequenceTestCase(django_case):
149
159
from django .db import transaction
150
160
transaction .Atomic ._ensure_durability = False
151
161
152
- def reset_durability ():
162
+ def reset_durability () -> None :
153
163
transaction .Atomic ._ensure_durability = True
154
164
request .addfinalizer (reset_durability )
155
165
@@ -158,15 +168,15 @@ def reset_durability():
158
168
request .addfinalizer (test_case ._post_teardown )
159
169
160
170
161
- def _disable_native_migrations ():
171
+ def _disable_native_migrations () -> None :
162
172
from django .conf import settings
163
173
from django .core .management .commands import migrate
164
174
165
175
class DisableMigrations :
166
- def __contains__ (self , item ) :
176
+ def __contains__ (self , item : str ) -> bool :
167
177
return True
168
178
169
- def __getitem__ (self , item ) :
179
+ def __getitem__ (self , item : str ) -> None :
170
180
return None
171
181
172
182
settings .MIGRATION_MODULES = DisableMigrations ()
@@ -179,7 +189,7 @@ def handle(self, *args, **kwargs):
179
189
migrate .Command = MigrateSilentCommand
180
190
181
191
182
- def _set_suffix_to_test_databases (suffix ) :
192
+ def _set_suffix_to_test_databases (suffix : str ) -> None :
183
193
from django .conf import settings
184
194
185
195
for db_settings in settings .DATABASES .values ():
@@ -201,7 +211,11 @@ def _set_suffix_to_test_databases(suffix):
201
211
202
212
203
213
@pytest .fixture (scope = "function" )
204
- def db (request , django_db_setup , django_db_blocker ):
214
+ def db (
215
+ request ,
216
+ django_db_setup : None ,
217
+ django_db_blocker ,
218
+ ) -> None :
205
219
"""Require a django test database.
206
220
207
221
This database will be setup with the default fixtures and will have
@@ -227,7 +241,11 @@ def db(request, django_db_setup, django_db_blocker):
227
241
228
242
229
243
@pytest .fixture (scope = "function" )
230
- def transactional_db (request , django_db_setup , django_db_blocker ):
244
+ def transactional_db (
245
+ request ,
246
+ django_db_setup : None ,
247
+ django_db_blocker ,
248
+ ) -> None :
231
249
"""Require a django test database with transaction support.
232
250
233
251
This will re-initialise the django database for each test and is
@@ -246,7 +264,11 @@ def transactional_db(request, django_db_setup, django_db_blocker):
246
264
247
265
248
266
@pytest .fixture (scope = "function" )
249
- def django_db_reset_sequences (request , django_db_setup , django_db_blocker ):
267
+ def django_db_reset_sequences (
268
+ request ,
269
+ django_db_setup : None ,
270
+ django_db_blocker ,
271
+ ) -> None :
250
272
"""Require a transactional test database with sequence reset support.
251
273
252
274
This behaves like the ``transactional_db`` fixture, with the addition
@@ -264,7 +286,7 @@ def django_db_reset_sequences(request, django_db_setup, django_db_blocker):
264
286
265
287
266
288
@pytest .fixture ()
267
- def client ():
289
+ def client () -> "django.test.client.Client" :
268
290
"""A Django test client instance."""
269
291
skip_if_no_django ()
270
292
@@ -274,7 +296,7 @@ def client():
274
296
275
297
276
298
@pytest .fixture ()
277
- def async_client ():
299
+ def async_client () -> "django.test.client.AsyncClient" :
278
300
"""A Django test async client instance."""
279
301
skip_if_no_django ()
280
302
@@ -284,21 +306,25 @@ def async_client():
284
306
285
307
286
308
@pytest .fixture ()
287
- def django_user_model (db ):
309
+ def django_user_model (db : None ):
288
310
"""The class of Django's user model."""
289
311
from django .contrib .auth import get_user_model
290
312
291
313
return get_user_model ()
292
314
293
315
294
316
@pytest .fixture ()
295
- def django_username_field (django_user_model ):
317
+ def django_username_field (django_user_model ) -> str :
296
318
"""The fieldname for the username used with Django's user model."""
297
319
return django_user_model .USERNAME_FIELD
298
320
299
321
300
322
@pytest .fixture ()
301
- def admin_user (db , django_user_model , django_username_field ):
323
+ def admin_user (
324
+ db : None ,
325
+ django_user_model ,
326
+ django_username_field : str ,
327
+ ):
302
328
"""A Django admin user.
303
329
304
330
This uses an existing user with username "admin", or creates a new one with
@@ -325,7 +351,10 @@ def admin_user(db, django_user_model, django_username_field):
325
351
326
352
327
353
@pytest .fixture ()
328
- def admin_client (db , admin_user ):
354
+ def admin_client (
355
+ db : None ,
356
+ admin_user ,
357
+ ) -> "django.test.client.Client" :
329
358
"""A Django test client logged in as an admin user."""
330
359
from django .test .client import Client
331
360
@@ -335,7 +364,7 @@ def admin_client(db, admin_user):
335
364
336
365
337
366
@pytest .fixture ()
338
- def rf ():
367
+ def rf () -> "django.test.client.RequestFactory" :
339
368
"""RequestFactory instance"""
340
369
skip_if_no_django ()
341
370
@@ -345,7 +374,7 @@ def rf():
345
374
346
375
347
376
@pytest .fixture ()
348
- def async_rf ():
377
+ def async_rf () -> "django.test.client.AsyncRequestFactory" :
349
378
"""AsyncRequestFactory instance"""
350
379
skip_if_no_django ()
351
380
@@ -357,7 +386,7 @@ def async_rf():
357
386
class SettingsWrapper :
358
387
_to_restore = [] # type: List[Any]
359
388
360
- def __delattr__ (self , attr ) :
389
+ def __delattr__ (self , attr : str ) -> None :
361
390
from django .test import override_settings
362
391
363
392
override = override_settings ()
@@ -368,19 +397,19 @@ def __delattr__(self, attr):
368
397
369
398
self ._to_restore .append (override )
370
399
371
- def __setattr__ (self , attr , value ):
400
+ def __setattr__ (self , attr : str , value ) -> None :
372
401
from django .test import override_settings
373
402
374
403
override = override_settings (** {attr : value })
375
404
override .enable ()
376
405
self ._to_restore .append (override )
377
406
378
- def __getattr__ (self , item ):
407
+ def __getattr__ (self , attr : str ):
379
408
from django .conf import settings
380
409
381
- return getattr (settings , item )
410
+ return getattr (settings , attr )
382
411
383
- def finalize (self ):
412
+ def finalize (self ) -> None :
384
413
for override in reversed (self ._to_restore ):
385
414
override .disable ()
386
415
@@ -429,7 +458,7 @@ def live_server(request):
429
458
430
459
431
460
@pytest .fixture (autouse = True , scope = "function" )
432
- def _live_server_helper (request ):
461
+ def _live_server_helper (request ) -> None :
433
462
"""Helper to make live_server work, internal to pytest-django.
434
463
435
464
This helper will dynamically request the transactional_db fixture
@@ -455,14 +484,22 @@ def _live_server_helper(request):
455
484
456
485
457
486
@contextmanager
458
- def _assert_num_queries (config , num , exact = True , connection = None , info = None ):
487
+ def _assert_num_queries (
488
+ config ,
489
+ num : int ,
490
+ exact : bool = True ,
491
+ connection = None ,
492
+ info = None ,
493
+ ) -> Generator ["django.test.utils.CaptureQueriesContext" , None , None ]:
459
494
from django .test .utils import CaptureQueriesContext
460
495
461
496
if connection is None :
462
- from django .db import connection
497
+ from django .db import connection as conn
498
+ else :
499
+ conn = connection
463
500
464
501
verbose = config .getoption ("verbose" ) > 0
465
- with CaptureQueriesContext (connection ) as context :
502
+ with CaptureQueriesContext (conn ) as context :
466
503
yield context
467
504
num_performed = len (context )
468
505
if exact :
0 commit comments