mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Refs #24590 -- Ensured isolation between autodetector tests
Fixed a regression introduced in e1427cc609
when running tests in reverse order.
This commit is contained in:
parent
22a791e608
commit
8e631a3175
@ -226,10 +226,11 @@ class FieldDeconstructionTests(SimpleTestCase):
|
||||
# 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")
|
||||
|
||||
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_image_field(self):
|
||||
field = models.ImageField(upload_to="foo/barness", width_field="width", height_field="height")
|
||||
@ -305,10 +306,11 @@ class FieldDeconstructionTests(SimpleTestCase):
|
||||
# there's no validation. We just want to check the setting stuff works.
|
||||
field = models.ManyToManyField("auth.Permission")
|
||||
name, path, args, kwargs = field.deconstruct()
|
||||
self.assertEqual(path, "django.db.models.ManyToManyField")
|
||||
self.assertEqual(args, [])
|
||||
self.assertEqual(kwargs, {"to": "auth.Permission"})
|
||||
self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL")
|
||||
|
||||
self.assertEqual(path, "django.db.models.ManyToManyField")
|
||||
self.assertEqual(args, [])
|
||||
self.assertEqual(kwargs, {"to": "auth.Permission"})
|
||||
self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL")
|
||||
|
||||
def test_null_boolean_field(self):
|
||||
field = models.NullBooleanField()
|
||||
|
@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import AbstractBaseUser
|
||||
from django.core.validators import RegexValidator, validate_slug
|
||||
@ -11,6 +12,7 @@ from django.db.migrations.loader import MigrationLoader
|
||||
from django.db.migrations.questioner import MigrationQuestioner
|
||||
from django.db.migrations.state import ModelState, ProjectState
|
||||
from django.test import TestCase, mock, override_settings
|
||||
from django.test.utils import isolate_lru_cache
|
||||
|
||||
from .models import FoodManager, FoodQuerySet
|
||||
|
||||
@ -1310,10 +1312,12 @@ class AutodetectorTests(TestCase):
|
||||
|
||||
@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
|
||||
def test_swappable(self):
|
||||
before = self.make_project_state([self.custom_user])
|
||||
after = self.make_project_state([self.custom_user, self.author_with_custom_user])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
with isolate_lru_cache(apps.get_swappable_settings_name):
|
||||
before = self.make_project_state([self.custom_user])
|
||||
after = self.make_project_state([self.custom_user, self.author_with_custom_user])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
|
||||
# Right number/type of migrations?
|
||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||
self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel"])
|
||||
@ -1321,11 +1325,13 @@ class AutodetectorTests(TestCase):
|
||||
self.assertMigrationDependencies(changes, 'testapp', 0, [("__setting__", "AUTH_USER_MODEL")])
|
||||
|
||||
def test_swappable_changed(self):
|
||||
before = self.make_project_state([self.custom_user, self.author_with_user])
|
||||
with override_settings(AUTH_USER_MODEL="thirdapp.CustomUser"):
|
||||
after = self.make_project_state([self.custom_user, self.author_with_custom_user])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
with isolate_lru_cache(apps.get_swappable_settings_name):
|
||||
before = self.make_project_state([self.custom_user, self.author_with_user])
|
||||
with override_settings(AUTH_USER_MODEL="thirdapp.CustomUser"):
|
||||
after = self.make_project_state([self.custom_user, self.author_with_custom_user])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
|
||||
# Right number/type of migrations?
|
||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
|
||||
@ -1815,11 +1821,13 @@ class AutodetectorTests(TestCase):
|
||||
@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
|
||||
def test_swappable_first_setting(self):
|
||||
"""Tests that swappable models get their CreateModel first."""
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([self.custom_user_no_inherit, self.aardvark])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
with isolate_lru_cache(apps.get_swappable_settings_name):
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([self.custom_user_no_inherit, self.aardvark])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
|
||||
# Right number/type of migrations?
|
||||
self.assertNumberMigrations(changes, 'thirdapp', 1)
|
||||
self.assertOperationTypes(changes, 'thirdapp', 0, ["CreateModel", "CreateModel"])
|
||||
@ -1999,20 +2007,22 @@ class AutodetectorTests(TestCase):
|
||||
#23322 - Tests that the dependency resolver knows to explicitly resolve
|
||||
swappable models.
|
||||
"""
|
||||
tenant = ModelState("a", "Tenant", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("primary_address", models.ForeignKey("b.Address", models.CASCADE))],
|
||||
bases=(AbstractBaseUser, )
|
||||
)
|
||||
address = ModelState("b", "Address", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("tenant", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)),
|
||||
])
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([address, tenant])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
with isolate_lru_cache(apps.get_swappable_settings_name):
|
||||
tenant = ModelState("a", "Tenant", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("primary_address", models.ForeignKey("b.Address", models.CASCADE))],
|
||||
bases=(AbstractBaseUser, )
|
||||
)
|
||||
address = ModelState("b", "Address", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("tenant", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)),
|
||||
])
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([address, tenant])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
|
||||
# Right number/type of migrations?
|
||||
self.assertNumberMigrations(changes, 'a', 2)
|
||||
self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
|
||||
@ -2031,20 +2041,22 @@ class AutodetectorTests(TestCase):
|
||||
swappable models but with the swappable not being the first migrated
|
||||
model.
|
||||
"""
|
||||
address = ModelState("a", "Address", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("tenant", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)),
|
||||
])
|
||||
tenant = ModelState("b", "Tenant", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("primary_address", models.ForeignKey("a.Address", models.CASCADE))],
|
||||
bases=(AbstractBaseUser, )
|
||||
)
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([address, tenant])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
with isolate_lru_cache(apps.get_swappable_settings_name):
|
||||
address = ModelState("a", "Address", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("tenant", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)),
|
||||
])
|
||||
tenant = ModelState("b", "Tenant", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("primary_address", models.ForeignKey("a.Address", models.CASCADE))],
|
||||
bases=(AbstractBaseUser, )
|
||||
)
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([address, tenant])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
|
||||
# Right number/type of migrations?
|
||||
self.assertNumberMigrations(changes, 'a', 2)
|
||||
self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
|
||||
@ -2062,15 +2074,17 @@ class AutodetectorTests(TestCase):
|
||||
#23322 - Tests that the dependency resolver knows to explicitly resolve
|
||||
swappable models.
|
||||
"""
|
||||
person = ModelState("a", "Person", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("parent1", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, related_name='children'))
|
||||
])
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([person])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
with isolate_lru_cache(apps.get_swappable_settings_name):
|
||||
person = ModelState("a", "Person", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("parent1", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, related_name='children'))
|
||||
])
|
||||
# Make state
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([person])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
|
||||
# Right number/type of migrations?
|
||||
self.assertNumberMigrations(changes, 'a', 1)
|
||||
self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
|
||||
|
Loading…
Reference in New Issue
Block a user