Debian Codegraph @

tests/migrations/test_base.py

raw
1 import os
2 import shutil
3 import tempfile
4 from contextlib import contextmanager
5 from importlib import import_module
6
7 from user_commands.utils import AssertFormatterFailureCaughtContext
8
9 from django.apps import apps
10 from django.core.management import call_command
11 from django.db import connection, connections, migrations, models
12 from django.db.migrations.migration import Migration
13 from django.db.migrations.optimizer import MigrationOptimizer
14 from django.db.migrations.recorder import MigrationRecorder
15 from django.db.migrations.serializer import serializer_factory
16 from django.db.migrations.state import ProjectState
17 from django.test import SimpleTestCase, TransactionTestCase
18 from django.test.utils import extend_sys_path
19 from django.utils.module_loading import module_dir
20
21
22 class MigrationTestBase(TransactionTestCase):
23 """
24 Contains an extended set of asserts for testing migrations and schema operations.
25 """
26
27 available_apps = ["migrations"]
28 databases = {"default", "other"}
29
30 def tearDown(self):
31 # Reset applied-migrations state.
32 for db in self.databases:
33 recorder = MigrationRecorder(connections[db])
34 recorder.migration_qs.filter(app="migrations").delete()
35
36 def get_table_description(self, table, using="default"):
37 with connections[using].cursor() as cursor:
38 return connections[using].introspection.get_table_description(cursor, table)
39
40 def assertTableExists(self, table, using="default"):
41 with connections[using].cursor() as cursor:
42 self.assertIn(table, connections[using].introspection.table_names(cursor))
43
44 def assertTableNotExists(self, table, using="default"):
45 with connections[using].cursor() as cursor:
46 self.assertNotIn(
47 table, connections[using].introspection.table_names(cursor)
48 )
49
50 def assertColumnExists(self, table, column, using="default"):
51 self.assertIn(
52 column, [c.name for c in self.get_table_description(table, using=using)]
53 )
54
55 def assertColumnNotExists(self, table, column, using="default"):
56 self.assertNotIn(
57 column, [c.name for c in self.get_table_description(table, using=using)]
58 )
59
60 def _get_column_allows_null(self, table, column, using):
61 return [
62 c.null_ok
63 for c in self.get_table_description(table, using=using)
64 if c.name == column
65 ][0]
66
67 def assertColumnNull(self, table, column, using="default"):
68 self.assertTrue(self._get_column_allows_null(table, column, using))
69
70 def assertColumnNotNull(self, table, column, using="default"):
71 self.assertFalse(self._get_column_allows_null(table, column, using))
72
73 def _get_column_collation(self, table, column, using):
74 return next(
75 f.collation
76 for f in self.get_table_description(table, using=using)
77 if f.name == column
78 )
79
80 def assertColumnCollation(self, table, column, collation, using="default"):
81 self.assertEqual(self._get_column_collation(table, column, using), collation)
82
83 def _get_table_comment(self, table, using):
84 with connections[using].cursor() as cursor:
85 return next(
86 t.comment
87 for t in connections[using].introspection.get_table_list(cursor)
88 if t.name == table
89 )
90
91 def assertTableComment(self, table, comment, using="default"):
92 self.assertEqual(self._get_table_comment(table, using), comment)
93
94 def assertTableCommentNotExists(self, table, using="default"):
95 self.assertIn(self._get_table_comment(table, using), [None, ""])
96
97 def assertIndexExists(
98 self, table, columns, value=True, using="default", index_type=None
99 ):
100 with connections[using].cursor() as cursor:
101 self.assertEqual(
102 value,
103 any(
104 c["index"]
105 for c in connections[using]
106 .introspection.get_constraints(cursor, table)
107 .values()
108 if (
109 c["columns"] == list(columns)
110 and (index_type is None or c["type"] == index_type)
111 and not c["unique"]
112 )
113 ),
114 )
115
116 def assertIndexNotExists(self, table, columns):
117 return self.assertIndexExists(table, columns, False)
118
119 def assertIndexNameExists(self, table, index, using="default"):
120 with connections[using].cursor() as cursor:
121 self.assertIn(
122 index,
123 connection.introspection.get_constraints(cursor, table),
124 )
125
126 def assertIndexNameNotExists(self, table, index, using="default"):
127 with connections[using].cursor() as cursor:
128 self.assertNotIn(
129 index,
130 connection.introspection.get_constraints(cursor, table),
131 )
132
133 def assertConstraintExists(self, table, name, value=True, using="default"):
134 with connections[using].cursor() as cursor:
135 constraints = (
136 connections[using].introspection.get_constraints(cursor, table).items()
137 )
138 self.assertEqual(
139 value,
140 any(c["check"] for n, c in constraints if n == name),
141 )
142
143 def assertConstraintNotExists(self, table, name):
144 return self.assertConstraintExists(table, name, False)
145
146 def assertUniqueConstraintExists(self, table, columns, value=True, using="default"):
147 with connections[using].cursor() as cursor:
148 constraints = (
149 connections[using].introspection.get_constraints(cursor, table).values()
150 )
151 self.assertEqual(
152 value,
153 any(c["unique"] for c in constraints if c["columns"] == list(columns)),
154 )
155
156 def assertFKExists(self, table, columns, to, value=True, using="default"):
157 if not connections[using].features.can_introspect_foreign_keys:
158 return
159 with connections[using].cursor() as cursor:
160 self.assertEqual(
161 value,
162 any(
163 c["foreign_key"] == to
164 for c in connections[using]
165 .introspection.get_constraints(cursor, table)
166 .values()
167 if c["columns"] == list(columns)
168 ),
169 )
170
171 def assertFKNotExists(self, table, columns, to):
172 return self.assertFKExists(table, columns, to, False)
173
174 def assertFormatterFailureCaught(
175 self, *args, module="migrations.test_migrations", **kwargs
176 ):
177 with (
178 self.temporary_migration_module(module=module),
179 AssertFormatterFailureCaughtContext(self) as ctx,
180 ):
181 call_command(*args, stdout=ctx.stdout, stderr=ctx.stderr, **kwargs)
182
183 @contextmanager
184 def temporary_migration_module(self, app_label="migrations", module=None):
185 """
186 Allows testing management commands in a temporary migrations module.
187
188 Wrap all invocations to makemigrations and squashmigrations with this
189 context manager in order to avoid creating migration files in your
190 source tree inadvertently.
191
192 Takes the application label that will be passed to makemigrations or
193 squashmigrations and the Python path to a migrations module.
194
195 The migrations module is used as a template for creating the temporary
196 migrations module. If it isn't provided, the application's migrations
197 module is used, if it exists.
198
199 Returns the filesystem path to the temporary migrations module.
200 """
201 with tempfile.TemporaryDirectory() as temp_dir:
202 target_dir = tempfile.mkdtemp(dir=temp_dir)
203 with open(os.path.join(target_dir, "__init__.py"), "w"):
204 pass
205 target_migrations_dir = os.path.join(target_dir, "migrations")
206
207 if module is None:
208 module = apps.get_app_config(app_label).name + ".migrations"
209
210 try:
211 source_migrations_dir = module_dir(import_module(module))
212 except (ImportError, ValueError):
213 pass
214 else:
215 shutil.copytree(source_migrations_dir, target_migrations_dir)
216
217 with extend_sys_path(temp_dir):
218 new_module = os.path.basename(target_dir) + ".migrations"
219 with self.settings(MIGRATION_MODULES={app_label: new_module}):
220 yield target_migrations_dir
221
222
223 class OperationTestBase(MigrationTestBase):
224 """Common functions to help test operations."""
225
226 @classmethod
227 def setUpClass(cls):
228 super().setUpClass()
229 cls._initial_table_names = frozenset(connection.introspection.table_names())
230
231 def tearDown(self):
232 self.cleanup_test_tables()
233 super().tearDown()
234
235 def cleanup_test_tables(self):
236 table_names = (
237 frozenset(connection.introspection.table_names())
238 - self._initial_table_names
239 )
240 with connection.schema_editor() as editor:
241 with connection.constraint_checks_disabled():
242 for table_name in table_names:
243 editor.execute(
244 editor.sql_delete_table
245 % {
246 "table": editor.quote_name(table_name),
247 }
248 )
249
250 def apply_operations(self, app_label, project_state, operations, atomic=True):
251 migration = Migration("name", app_label)
252 migration.operations = operations
253 with connection.schema_editor(atomic=atomic) as editor:
254 return migration.apply(project_state, editor)
255
256 def unapply_operations(self, app_label, project_state, operations, atomic=True):
257 migration = Migration("name", app_label)
258 migration.operations = operations
259 with connection.schema_editor(atomic=atomic) as editor:
260 return migration.unapply(project_state, editor)
261
262 def make_test_state(self, app_label, operation, **kwargs):
263 """
264 Makes a test state using set_up_test_model and returns the
265 original state and the state after the migration is applied.
266 """
267 project_state = self.set_up_test_model(app_label, **kwargs)
268 new_state = project_state.clone()
269 operation.state_forwards(app_label, new_state)
270 return project_state, new_state
271
272 def set_up_test_model(
273 self,
274 app_label,
275 second_model=False,
276 third_model=False,
277 index=False,
278 multicol_index=False,
279 related_model=False,
280 mti_model=False,
281 proxy_model=False,
282 manager_model=False,
283 unique_together=False,
284 options=False,
285 db_table=None,
286 constraints=None,
287 indexes=None,
288 ):
289 """Creates a test model state and database table."""
290 # Make the "current" state.
291 model_options = {
292 "swappable": "TEST_SWAP_MODEL",
293 "unique_together": [["pink", "weight"]] if unique_together else [],
294 }
295 if options:
296 model_options["permissions"] = [("can_groom", "Can groom")]
297 if db_table:
298 model_options["db_table"] = db_table
299 operations = [
300 migrations.CreateModel(
301 "Pony",
302 [
303 ("id", models.AutoField(primary_key=True)),
304 ("pink", models.IntegerField(default=3)),
305 ("weight", models.FloatField()),
306 ("green", models.IntegerField(null=True)),
307 (
308 "yellow",
309 models.CharField(
310 blank=True, null=True, db_default="Yellow", max_length=20
311 ),
312 ),
313 ],
314 options=model_options,
315 )
316 ]
317 if index:
318 operations.append(
319 migrations.AddIndex(
320 "Pony",
321 models.Index(fields=["pink"], name="pony_pink_idx"),
322 )
323 )
324 if multicol_index:
325 operations.append(
326 migrations.AddIndex(
327 "Pony",
328 models.Index(fields=["pink", "weight"], name="pony_test_idx"),
329 )
330 )
331 if indexes:
332 for index in indexes:
333 operations.append(migrations.AddIndex("Pony", index))
334 if constraints:
335 for constraint in constraints:
336 operations.append(migrations.AddConstraint("Pony", constraint))
337 if second_model:
338 operations.append(
339 migrations.CreateModel(
340 "Stable",
341 [
342 ("id", models.AutoField(primary_key=True)),
343 ],
344 )
345 )
346 if third_model:
347 operations.append(
348 migrations.CreateModel(
349 "Van",
350 [
351 ("id", models.AutoField(primary_key=True)),
352 ],
353 )
354 )
355 if related_model:
356 operations.append(
357 migrations.CreateModel(
358 "Rider",
359 [
360 ("id", models.AutoField(primary_key=True)),
361 ("pony", models.ForeignKey("Pony", models.CASCADE)),
362 (
363 "friend",
364 models.ForeignKey("self", models.CASCADE, null=True),
365 ),
366 ],
367 )
368 )
369 if mti_model:
370 operations.append(
371 migrations.CreateModel(
372 "ShetlandPony",
373 fields=[
374 (
375 "pony_ptr",
376 models.OneToOneField(
377 "Pony",
378 models.CASCADE,
379 auto_created=True,
380 parent_link=True,
381 primary_key=True,
382 to_field="id",
383 serialize=False,
384 ),
385 ),
386 ("cuteness", models.IntegerField(default=1)),
387 ],
388 bases=["%s.Pony" % app_label],
389 )
390 )
391 if proxy_model:
392 operations.append(
393 migrations.CreateModel(
394 "ProxyPony",
395 fields=[],
396 options={"proxy": True},
397 bases=["%s.Pony" % app_label],
398 )
399 )
400 if manager_model:
401 from .models import FoodManager, FoodQuerySet
402
403 operations.append(
404 migrations.CreateModel(
405 "Food",
406 fields=[
407 ("id", models.AutoField(primary_key=True)),
408 ],
409 managers=[
410 ("food_qs", FoodQuerySet.as_manager()),
411 ("food_mgr", FoodManager("a", "b")),
412 ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
413 ],
414 )
415 )
416 return self.apply_operations(app_label, ProjectState(), operations)
417
418
419 class OptimizerTestBase(SimpleTestCase):
420 """Common functions to help test the optimizer."""
421
422 def optimize(self, operations, app_label):
423 """
424 Handy shortcut for getting results + number of loops
425 """
426 optimizer = MigrationOptimizer()
427 return optimizer.optimize(operations, app_label), optimizer._iterations
428
429 def serialize(self, value):
430 return serializer_factory(value).serialize()[0]
431
432 def assertOptimizesTo(
433 self, operations, expected, exact=None, less_than=None, app_label=None
434 ):
435 result, iterations = self.optimize(operations, app_label or "migrations")
436 result = [self.serialize(f) for f in result]
437 expected = [self.serialize(f) for f in expected]
438 self.assertEqual(expected, result)
439 if exact is not None and iterations != exact:
440 raise self.failureException(
441 "Optimization did not take exactly %s iterations (it took %s)"
442 % (exact, iterations)
443 )
444 if less_than is not None and iterations >= less_than:
445 raise self.failureException(
446 "Optimization did not take less than %s iterations (it took %s)"
447 % (less_than, iterations)
448 )
449
450 def assertDoesNotOptimize(self, operations, **kwargs):
451 self.assertOptimizesTo(operations, operations, **kwargs)