1
1
# -*- coding: utf-8 -*-
2
2
import re
3
3
4
+ from django .apps import apps
4
5
from django .conf import settings
5
6
from django .contrib .auth .models import AbstractBaseUser
6
7
from django .core .validators import RegexValidator , validate_slug
11
12
from django .db .migrations .questioner import MigrationQuestioner
12
13
from django .db .migrations .state import ModelState , ProjectState
13
14
from django .test import TestCase , mock , override_settings
15
+ from django .test .utils import isolate_lru_cache
14
16
15
17
from .models import FoodManager , FoodQuerySet
16
18
@@ -1310,22 +1312,26 @@ def test_unmanaged_custom_pk(self):
1310
1312
1311
1313
@override_settings (AUTH_USER_MODEL = "thirdapp.CustomUser" )
1312
1314
def test_swappable (self ):
1313
- before = self .make_project_state ([self .custom_user ])
1314
- after = self .make_project_state ([self .custom_user , self .author_with_custom_user ])
1315
- autodetector = MigrationAutodetector (before , after )
1316
- changes = autodetector ._detect_changes ()
1315
+ with isolate_lru_cache (apps .get_swappable_settings_name ):
1316
+ before = self .make_project_state ([self .custom_user ])
1317
+ after = self .make_project_state ([self .custom_user , self .author_with_custom_user ])
1318
+ autodetector = MigrationAutodetector (before , after )
1319
+ changes = autodetector ._detect_changes ()
1320
+
1317
1321
# Right number/type of migrations?
1318
1322
self .assertNumberMigrations (changes , 'testapp' , 1 )
1319
1323
self .assertOperationTypes (changes , 'testapp' , 0 , ["CreateModel" ])
1320
1324
self .assertOperationAttributes (changes , 'testapp' , 0 , 0 , name = "Author" )
1321
1325
self .assertMigrationDependencies (changes , 'testapp' , 0 , [("__setting__" , "AUTH_USER_MODEL" )])
1322
1326
1323
1327
def test_swappable_changed (self ):
1324
- before = self .make_project_state ([self .custom_user , self .author_with_user ])
1325
- with override_settings (AUTH_USER_MODEL = "thirdapp.CustomUser" ):
1326
- after = self .make_project_state ([self .custom_user , self .author_with_custom_user ])
1327
- autodetector = MigrationAutodetector (before , after )
1328
- changes = autodetector ._detect_changes ()
1328
+ with isolate_lru_cache (apps .get_swappable_settings_name ):
1329
+ before = self .make_project_state ([self .custom_user , self .author_with_user ])
1330
+ with override_settings (AUTH_USER_MODEL = "thirdapp.CustomUser" ):
1331
+ after = self .make_project_state ([self .custom_user , self .author_with_custom_user ])
1332
+ autodetector = MigrationAutodetector (before , after )
1333
+ changes = autodetector ._detect_changes ()
1334
+
1329
1335
# Right number/type of migrations?
1330
1336
self .assertNumberMigrations (changes , 'testapp' , 1 )
1331
1337
self .assertOperationTypes (changes , 'testapp' , 0 , ["AlterField" ])
@@ -1815,11 +1821,13 @@ def test_swappable_first_inheritance(self):
1815
1821
@override_settings (AUTH_USER_MODEL = "thirdapp.CustomUser" )
1816
1822
def test_swappable_first_setting (self ):
1817
1823
"""Tests that swappable models get their CreateModel first."""
1818
- # Make state
1819
- before = self .make_project_state ([])
1820
- after = self .make_project_state ([self .custom_user_no_inherit , self .aardvark ])
1821
- autodetector = MigrationAutodetector (before , after )
1822
- changes = autodetector ._detect_changes ()
1824
+ with isolate_lru_cache (apps .get_swappable_settings_name ):
1825
+ # Make state
1826
+ before = self .make_project_state ([])
1827
+ after = self .make_project_state ([self .custom_user_no_inherit , self .aardvark ])
1828
+ autodetector = MigrationAutodetector (before , after )
1829
+ changes = autodetector ._detect_changes ()
1830
+
1823
1831
# Right number/type of migrations?
1824
1832
self .assertNumberMigrations (changes , 'thirdapp' , 1 )
1825
1833
self .assertOperationTypes (changes , 'thirdapp' , 0 , ["CreateModel" , "CreateModel" ])
@@ -1999,20 +2007,22 @@ def test_circular_dependency_swappable(self):
1999
2007
#23322 - Tests that the dependency resolver knows to explicitly resolve
2000
2008
swappable models.
2001
2009
"""
2002
- tenant = ModelState ("a" , "Tenant" , [
2003
- ("id" , models .AutoField (primary_key = True )),
2004
- ("primary_address" , models .ForeignKey ("b.Address" , models .CASCADE ))],
2005
- bases = (AbstractBaseUser , )
2006
- )
2007
- address = ModelState ("b" , "Address" , [
2008
- ("id" , models .AutoField (primary_key = True )),
2009
- ("tenant" , models .ForeignKey (settings .AUTH_USER_MODEL , models .CASCADE )),
2010
- ])
2011
- # Make state
2012
- before = self .make_project_state ([])
2013
- after = self .make_project_state ([address , tenant ])
2014
- autodetector = MigrationAutodetector (before , after )
2015
- changes = autodetector ._detect_changes ()
2010
+ with isolate_lru_cache (apps .get_swappable_settings_name ):
2011
+ tenant = ModelState ("a" , "Tenant" , [
2012
+ ("id" , models .AutoField (primary_key = True )),
2013
+ ("primary_address" , models .ForeignKey ("b.Address" , models .CASCADE ))],
2014
+ bases = (AbstractBaseUser , )
2015
+ )
2016
+ address = ModelState ("b" , "Address" , [
2017
+ ("id" , models .AutoField (primary_key = True )),
2018
+ ("tenant" , models .ForeignKey (settings .AUTH_USER_MODEL , models .CASCADE )),
2019
+ ])
2020
+ # Make state
2021
+ before = self .make_project_state ([])
2022
+ after = self .make_project_state ([address , tenant ])
2023
+ autodetector = MigrationAutodetector (before , after )
2024
+ changes = autodetector ._detect_changes ()
2025
+
2016
2026
# Right number/type of migrations?
2017
2027
self .assertNumberMigrations (changes , 'a' , 2 )
2018
2028
self .assertOperationTypes (changes , 'a' , 0 , ["CreateModel" ])
@@ -2031,20 +2041,22 @@ def test_circular_dependency_swappable2(self):
2031
2041
swappable models but with the swappable not being the first migrated
2032
2042
model.
2033
2043
"""
2034
- address = ModelState ("a" , "Address" , [
2035
- ("id" , models .AutoField (primary_key = True )),
2036
- ("tenant" , models .ForeignKey (settings .AUTH_USER_MODEL , models .CASCADE )),
2037
- ])
2038
- tenant = ModelState ("b" , "Tenant" , [
2039
- ("id" , models .AutoField (primary_key = True )),
2040
- ("primary_address" , models .ForeignKey ("a.Address" , models .CASCADE ))],
2041
- bases = (AbstractBaseUser , )
2042
- )
2043
- # Make state
2044
- before = self .make_project_state ([])
2045
- after = self .make_project_state ([address , tenant ])
2046
- autodetector = MigrationAutodetector (before , after )
2047
- changes = autodetector ._detect_changes ()
2044
+ with isolate_lru_cache (apps .get_swappable_settings_name ):
2045
+ address = ModelState ("a" , "Address" , [
2046
+ ("id" , models .AutoField (primary_key = True )),
2047
+ ("tenant" , models .ForeignKey (settings .AUTH_USER_MODEL , models .CASCADE )),
2048
+ ])
2049
+ tenant = ModelState ("b" , "Tenant" , [
2050
+ ("id" , models .AutoField (primary_key = True )),
2051
+ ("primary_address" , models .ForeignKey ("a.Address" , models .CASCADE ))],
2052
+ bases = (AbstractBaseUser , )
2053
+ )
2054
+ # Make state
2055
+ before = self .make_project_state ([])
2056
+ after = self .make_project_state ([address , tenant ])
2057
+ autodetector = MigrationAutodetector (before , after )
2058
+ changes = autodetector ._detect_changes ()
2059
+
2048
2060
# Right number/type of migrations?
2049
2061
self .assertNumberMigrations (changes , 'a' , 2 )
2050
2062
self .assertOperationTypes (changes , 'a' , 0 , ["CreateModel" ])
@@ -2062,15 +2074,17 @@ def test_circular_dependency_swappable_self(self):
2062
2074
#23322 - Tests that the dependency resolver knows to explicitly resolve
2063
2075
swappable models.
2064
2076
"""
2065
- person = ModelState ("a" , "Person" , [
2066
- ("id" , models .AutoField (primary_key = True )),
2067
- ("parent1" , models .ForeignKey (settings .AUTH_USER_MODEL , models .CASCADE , related_name = 'children' ))
2068
- ])
2069
- # Make state
2070
- before = self .make_project_state ([])
2071
- after = self .make_project_state ([person ])
2072
- autodetector = MigrationAutodetector (before , after )
2073
- changes = autodetector ._detect_changes ()
2077
+ with isolate_lru_cache (apps .get_swappable_settings_name ):
2078
+ person = ModelState ("a" , "Person" , [
2079
+ ("id" , models .AutoField (primary_key = True )),
2080
+ ("parent1" , models .ForeignKey (settings .AUTH_USER_MODEL , models .CASCADE , related_name = 'children' ))
2081
+ ])
2082
+ # Make state
2083
+ before = self .make_project_state ([])
2084
+ after = self .make_project_state ([person ])
2085
+ autodetector = MigrationAutodetector (before , after )
2086
+ changes = autodetector ._detect_changes ()
2087
+
2074
2088
# Right number/type of migrations?
2075
2089
self .assertNumberMigrations (changes , 'a' , 1 )
2076
2090
self .assertOperationTypes (changes , 'a' , 0 , ["CreateModel" ])
0 commit comments