Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix app loading/test interaction

  • Loading branch information...
commit dbf8b93c527733fb5e3ea101a67bd94db745888e 1 parent 3ffbfe4
@andrewgodwin andrewgodwin authored
Showing with 16 additions and 11 deletions.
  1. +2 −1  django/db/models/loading.py
  2. +14 −10 tests/modeltests/schema/tests.py
View
3  django/db/models/loading.py
@@ -251,7 +251,7 @@ def save_state(self):
"""
return {
"app_store": SortedDict(self.app_store.items()),
- "app_labels": dict(self.app_errors.items()),
+ "app_labels": dict(self.app_labels.items()),
"app_models": SortedDict(self.app_models.items()),
"app_errors": dict(self.app_errors.items()),
}
@@ -264,6 +264,7 @@ def restore_state(self, state):
self.app_labels = state['app_labels']
self.app_models = state['app_models']
self.app_errors = state['app_errors']
+ self._get_models_cache.clear()
def temporary_state(self):
"Returns a context manager that restores the state on exit"
View
24 tests/modeltests/schema/tests.py
@@ -7,7 +7,7 @@
from django.db.models.fields import IntegerField, TextField, CharField, SlugField
from django.db.models.fields.related import ManyToManyField, ForeignKey
from django.db.models.loading import cache
-from .models import Author, Book, BookWithSlug, BookWithM2M, AuthorWithM2M, Tag, TagUniqueRename, UniqueTest
+from .models import Author, AuthorWithM2M, Book, BookWithSlug, BookWithM2M, Tag, TagUniqueRename, UniqueTest
class SchemaTests(TestCase):
@@ -19,7 +19,7 @@ class SchemaTests(TestCase):
as the code it is testing.
"""
- models = [Author, Book, BookWithSlug, BookWithM2M, AuthorWithM2M, Tag, TagUniqueRename, UniqueTest]
+ models = [Author, AuthorWithM2M, Book, BookWithSlug, BookWithM2M, Tag, TagUniqueRename, UniqueTest]
# Utility functions
@@ -30,8 +30,8 @@ def setUp(self):
connection.managed(True)
# The unmanaged models need to be removed after the test in order to
# prevent bad interactions with the flush operation in other tests.
- self.old_app_models = copy.deepcopy(cache.app_models)
- self.old_app_store = copy.deepcopy(cache.app_store)
+ self.cache_state = cache.save_state()
+ cache.load_app("modeltests.schema")
for model in self.models:
model._meta.managed = True
@@ -39,6 +39,16 @@ def tearDown(self):
# Rollback anything that may have happened
connection.rollback()
# Delete any tables made for our models
+ self.delete_tables()
+ # Unhook our models
+ for model in self.models:
+ model._meta.managed = False
+ if "schema" in self.cache_state['app_labels']:
+ del self.cache_state['app_labels']['schema']
+ cache.restore_state(self.cache_state)
+
+ def delete_tables(self):
+ "Deletes all model tables for our models for a clean test environment"
cursor = connection.cursor()
connection.disable_constraint_checking()
for model in self.models:
@@ -62,12 +72,6 @@ def tearDown(self):
else:
connection.commit()
connection.enable_constraint_checking()
- # Unhook our models
- for model in self.models:
- model._meta.managed = False
- cache.app_models = self.old_app_models
- cache.app_store = self.old_app_store
- cache._get_models_cache = {}
def column_classes(self, model):
cursor = connection.cursor()
Please sign in to comment.
Something went wrong with that request. Please try again.