summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2021-12-15 19:36:08 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-12-17 10:00:33 +0100
commit7e6a2e3b4555c069c50cb949404938345bfb2ea6 (patch)
treeeb5cd23f8aa94703a6fb5c404fd78c0fd5e4070e /tests
parentc1d2e8b9b8f41d3effef03badc78c8b8995a99b6 (diff)
[4.0.x] Fixed #33366 -- Fixed case handling with swappable setting detection in migrations autodetector.
The migration framework uniquely identifies models by case insensitive labels composed of their app label and model names and so does the app registry in most of its methods (e.g. AppConfig.get_model) but it wasn't the case for get_swappable_settings_name() until this change. This likely slipped under the radar for so long and only regressed in b9df2b74b98b4d63933e8061d3cfc1f6f39eb747 because prior to the changes related to the usage of model states instead of rendered models in the auto-detector the exact value settings value was never going through a case folding hoop. Thanks Andrew Chen Wang for the report and Keryn Knight for the investigation. Backport of 43289707809c814a70f0db38ca4f82f35f43dbfd from main
Diffstat (limited to 'tests')
-rw-r--r--tests/field_deconstruction/tests.py20
-rw-r--r--tests/migrations/test_autodetector.py16
2 files changed, 36 insertions, 0 deletions
diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py
index b2fb097100..70846f23d3 100644
--- a/tests/field_deconstruction/tests.py
+++ b/tests/field_deconstruction/tests.py
@@ -211,6 +211,13 @@ class FieldDeconstructionTests(SimpleTestCase):
self.assertEqual(args, [])
self.assertEqual(kwargs, {"to": "auth.user", "on_delete": models.CASCADE})
self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL")
+ # Swap detection for lowercase swappable model.
+ field = models.ForeignKey('auth.user', models.CASCADE)
+ name, path, args, kwargs = field.deconstruct()
+ self.assertEqual(path, 'django.db.models.ForeignKey')
+ self.assertEqual(args, [])
+ self.assertEqual(kwargs, {'to': 'auth.user', 'on_delete': models.CASCADE})
+ self.assertEqual(kwargs['to'].setting_name, 'AUTH_USER_MODEL')
# Test nonexistent (for now) model
field = models.ForeignKey("something.Else", models.CASCADE)
name, path, args, kwargs = field.deconstruct()
@@ -273,6 +280,19 @@ class FieldDeconstructionTests(SimpleTestCase):
self.assertEqual(kwargs, {"to": "auth.permission", "on_delete": models.CASCADE})
self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL")
+ # Model names are case-insensitive.
+ with isolate_lru_cache(apps.get_swappable_settings_name):
+ # It doesn't matter that we swapped out user for permission;
+ # there's no validation. We just want to check the setting stuff
+ # works.
+ field = models.ForeignKey('auth.permission', models.CASCADE)
+ name, path, args, kwargs = field.deconstruct()
+
+ self.assertEqual(path, 'django.db.models.ForeignKey')
+ self.assertEqual(args, [])
+ self.assertEqual(kwargs, {'to': 'auth.permission', 'on_delete': models.CASCADE})
+ self.assertEqual(kwargs['to'].setting_name, 'AUTH_USER_MODEL')
+
def test_one_to_one(self):
# Test basic pointing
from django.contrib.auth.models import Permission
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index fb42a8a455..e0cf9d49ef 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -1961,6 +1961,22 @@ class AutodetectorTests(TestCase):
self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
self.assertMigrationDependencies(changes, 'testapp', 0, [("__setting__", "AUTH_USER_MODEL")])
+ def test_swappable_lowercase(self):
+ model_state = ModelState('testapp', 'Document', [
+ ('id', models.AutoField(primary_key=True)),
+ ('owner', models.ForeignKey(
+ settings.AUTH_USER_MODEL.lower(), models.CASCADE,
+ )),
+ ])
+ with isolate_lru_cache(apps.get_swappable_settings_name):
+ changes = self.get_changes([], [model_state])
+ self.assertNumberMigrations(changes, 'testapp', 1)
+ self.assertOperationTypes(changes, 'testapp', 0, ['CreateModel'])
+ self.assertOperationAttributes(changes, 'testapp', 0, 0, name='Document')
+ self.assertMigrationDependencies(
+ changes, 'testapp', 0, [('__setting__', 'AUTH_USER_MODEL')],
+ )
+
def test_swappable_changed(self):
with isolate_lru_cache(apps.get_swappable_settings_name):
before = self.make_project_state([self.custom_user, self.author_with_user])