4
4
5
5
import pytest
6
6
from django .db import connection , migrations , models , transaction
7
+ from django .db .migrations .operations .base import Operation
7
8
from django .db .migrations .state import ProjectState
8
9
from django .test import TransactionTestCase
9
10
from django .test .utils import CaptureQueriesContext
12
13
from django_mysql .test .utils import override_mysql_variables
13
14
14
15
15
- def plugin_exists (plugin_name ) :
16
+ def plugin_exists (plugin_name : str ) -> bool :
16
17
with connection .cursor () as cursor :
17
18
cursor .execute (
18
19
"""SELECT COUNT(*) FROM INFORMATION_SCHEMA.PLUGINS
19
20
WHERE PLUGIN_NAME = %s""" ,
20
21
(plugin_name ,),
21
22
)
22
- return cursor .fetchone ()[0 ] > 0
23
+ count : int = cursor .fetchone ()[0 ]
24
+ return count > 0
23
25
24
26
25
- def table_storage_engine (table_name ) :
27
+ def table_storage_engine (table_name : str ) -> str :
26
28
with connection .cursor () as cursor :
27
29
cursor .execute (
28
30
"""SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES
29
31
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s""" ,
30
32
(table_name ,),
31
33
)
32
- return cursor .fetchone ()[0 ]
34
+ engine : str = cursor .fetchone ()[0 ]
35
+ return engine
33
36
34
37
35
38
class PluginOperationTests (TransactionTestCase ):
@@ -168,21 +171,18 @@ def test_running_without_changes(self):
168
171
operation .database_backwards ("test_arstd" , editor , new_state , project_state )
169
172
assert table_storage_engine ("test_arstd_pony" ) == "MyISAM"
170
173
171
- # Copied from django core migration tests
174
+ # Adapted from django core migration tests:
172
175
173
176
def set_up_test_model (
174
177
self ,
175
- app_label ,
176
- second_model = False ,
177
- third_model = False ,
178
- related_model = False ,
179
- mti_model = False ,
180
- proxy_model = False ,
181
- unique_together = False ,
182
- options = False ,
183
- db_table = None ,
184
- index_together = False ,
185
- ): # pragma: no cover
178
+ app_label : str ,
179
+ * ,
180
+ proxy_model : bool = False ,
181
+ unique_together : bool = False ,
182
+ options : bool = False ,
183
+ db_table : str | None = None ,
184
+ index_together : bool = False ,
185
+ ) -> ProjectState : # pragma: no cover
186
186
"""
187
187
Creates a test model state and database table.
188
188
"""
@@ -220,7 +220,7 @@ def set_up_test_model(
220
220
model_options ["permissions" ] = [("can_groom" , "Can groom" )]
221
221
if db_table :
222
222
model_options ["db_table" ] = db_table
223
- operations = [
223
+ operations : list [ Operation ] = [
224
224
migrations .CreateModel (
225
225
"Pony" ,
226
226
[
@@ -231,49 +231,6 @@ def set_up_test_model(
231
231
options = model_options ,
232
232
)
233
233
]
234
- if second_model :
235
- operations .append (
236
- migrations .CreateModel (
237
- "Stable" , [("id" , models .AutoField (primary_key = True ))]
238
- )
239
- )
240
- if third_model :
241
- operations .append (
242
- migrations .CreateModel (
243
- "Van" , [("id" , models .AutoField (primary_key = True ))]
244
- )
245
- )
246
- if related_model :
247
- operations .append (
248
- migrations .CreateModel (
249
- "Rider" ,
250
- [
251
- ("id" , models .AutoField (primary_key = True )),
252
- ("pony" , models .ForeignKey ("Pony" )),
253
- ("friend" , models .ForeignKey ("self" )),
254
- ],
255
- )
256
- )
257
- if mti_model :
258
- operations .append (
259
- migrations .CreateModel (
260
- "ShetlandPony" ,
261
- fields = [
262
- (
263
- "pony_ptr" ,
264
- models .OneToOneField (
265
- auto_created = True ,
266
- primary_key = True ,
267
- to_field = "id" ,
268
- serialize = False ,
269
- to = "Pony" ,
270
- ),
271
- ),
272
- ("cuteness" , models .IntegerField (default = 1 )),
273
- ],
274
- bases = ["%s.Pony" % app_label ],
275
- )
276
- )
277
234
if proxy_model :
278
235
operations .append (
279
236
migrations .CreateModel (
@@ -286,7 +243,12 @@ def set_up_test_model(
286
243
287
244
return self .apply_operations (app_label , ProjectState (), operations )
288
245
289
- def apply_operations (self , app_label , project_state , operations ):
246
+ def apply_operations (
247
+ self ,
248
+ app_label : str ,
249
+ project_state : ProjectState ,
250
+ operations : list [Operation ],
251
+ ) -> ProjectState :
290
252
migration = migrations .Migration ("name" , app_label )
291
253
migration .operations = operations
292
254
with connection .schema_editor () as editor :
0 commit comments