Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix app loading/test interaction

  • Loading branch information...
commit dbf8b93c527733fb5e3ea101a67bd94db745888e 1 parent 3ffbfe4
Andrew Godwin authored
3  django/db/models/loading.py
@@ -251,7 +251,7 @@ def save_state(self):
251 251
         """
252 252
         return {
253 253
             "app_store": SortedDict(self.app_store.items()),
254  
-            "app_labels": dict(self.app_errors.items()),
  254
+            "app_labels": dict(self.app_labels.items()),
255 255
             "app_models": SortedDict(self.app_models.items()),
256 256
             "app_errors": dict(self.app_errors.items()),
257 257
         }
@@ -264,6 +264,7 @@ def restore_state(self, state):
264 264
         self.app_labels = state['app_labels']
265 265
         self.app_models = state['app_models']
266 266
         self.app_errors = state['app_errors']
  267
+        self._get_models_cache.clear()
267 268
 
268 269
     def temporary_state(self):
269 270
         "Returns a context manager that restores the state on exit"
24  tests/modeltests/schema/tests.py
@@ -7,7 +7,7 @@
7 7
 from django.db.models.fields import IntegerField, TextField, CharField, SlugField
8 8
 from django.db.models.fields.related import ManyToManyField, ForeignKey
9 9
 from django.db.models.loading import cache
10  
-from .models import Author, Book, BookWithSlug, BookWithM2M, AuthorWithM2M, Tag, TagUniqueRename, UniqueTest
  10
+from .models import Author, AuthorWithM2M, Book, BookWithSlug, BookWithM2M, Tag, TagUniqueRename, UniqueTest
11 11
 
12 12
 
13 13
 class SchemaTests(TestCase):
@@ -19,7 +19,7 @@ class SchemaTests(TestCase):
19 19
     as the code it is testing.
20 20
     """
21 21
 
22  
-    models = [Author, Book, BookWithSlug, BookWithM2M, AuthorWithM2M, Tag, TagUniqueRename, UniqueTest]
  22
+    models = [Author, AuthorWithM2M, Book, BookWithSlug, BookWithM2M, Tag, TagUniqueRename, UniqueTest]
23 23
 
24 24
     # Utility functions
25 25
 
@@ -30,8 +30,8 @@ def setUp(self):
30 30
         connection.managed(True)
31 31
         # The unmanaged models need to be removed after the test in order to
32 32
         # prevent bad interactions with the flush operation in other tests.
33  
-        self.old_app_models = copy.deepcopy(cache.app_models)
34  
-        self.old_app_store = copy.deepcopy(cache.app_store)
  33
+        self.cache_state = cache.save_state()
  34
+        cache.load_app("modeltests.schema")
35 35
         for model in self.models:
36 36
             model._meta.managed = True
37 37
 
@@ -39,6 +39,16 @@ def tearDown(self):
39 39
         # Rollback anything that may have happened
40 40
         connection.rollback()
41 41
         # Delete any tables made for our models
  42
+        self.delete_tables()
  43
+        # Unhook our models
  44
+        for model in self.models:
  45
+            model._meta.managed = False
  46
+        if "schema" in self.cache_state['app_labels']:
  47
+            del self.cache_state['app_labels']['schema']
  48
+        cache.restore_state(self.cache_state)
  49
+
  50
+    def delete_tables(self):
  51
+        "Deletes all model tables for our models for a clean test environment"
42 52
         cursor = connection.cursor()
43 53
         connection.disable_constraint_checking()
44 54
         for model in self.models:
@@ -62,12 +72,6 @@ def tearDown(self):
62 72
             else:
63 73
                 connection.commit()
64 74
         connection.enable_constraint_checking()
65  
-        # Unhook our models
66  
-        for model in self.models:
67  
-            model._meta.managed = False
68  
-        cache.app_models = self.old_app_models
69  
-        cache.app_store = self.old_app_store
70  
-        cache._get_models_cache = {}
71 75
 
72 76
     def column_classes(self, model):
73 77
         cursor = connection.cursor()

0 notes on commit dbf8b93

Please sign in to comment.
Something went wrong with that request. Please try again.