Commits

charettes committed 57bc8c3

Fix an app_cache state alteration leak between tests.

Prior to this fix, all tests that we're run after a
`Monkeypatcher` subclass with defined `installed_apps`
we're using an incoherent app cache. For example,
`ContentType.model_class` which rely on app_cache was
returning bogus results.

  • Participants
  • Parent commits 1267079
  • Branches restore-app-cache-state

Comments (0)

Files changed (2)

south/hacks/django_1_0.py

 """
 
 from django.conf import settings
-from django.db import models
 from django.db.backends.creation import BaseDatabaseCreation
-from django.db.models.loading import AppCache, cache
+from django.db.models.loading import cache
 from django.core import management
 from django.core.management.commands.flush import Command as FlushCommand
 from django.utils.datastructures import SortedDict
         """
         Used to repopulate AppCache after fiddling with INSTALLED_APPS.
         """
-        a = AppCache()
-        a.loaded = False
-        a.handled = {}
-        a.postponed = []
-        a.app_store = SortedDict()
-        a.app_models = SortedDict()
-        a.app_errors = {}
-        a._populate()
+        cache.loaded = False
+        cache.handled = {}
+        cache.postponed = []
+        cache.app_store = SortedDict()
+        cache.app_models = SortedDict()
+        cache.app_errors = {}
+        cache._populate()
     
     
     def clear_app_cache(self):
         """
         cache._populate()
 
+    def store_app_cache_state(self):
+        self.stored_app_cache_state = dict(**cache.__dict__)
+
+    def restore_app_cache_state(self):
+        cache.__dict__ = self.stored_app_cache_state
+
     def patch_flush_during_test_db_creation(self):
         """
         Patches BaseDatabaseCreation.create_test_db to not flush database

south/tests/__init__.py

             pass
         return fake
 
-
     def setUp(self):
         """
         Changes the Django environment so we can run tests against our test apps.
         """
-        if getattr(self, 'installed_apps', None):
+        if hasattr(self, 'installed_apps'):
+            hacks.store_app_cache_state()
             hacks.set_installed_apps(self.installed_apps)
-    
-    
+
     def tearDown(self):
         """
         Undoes what setUp did.
         """
-        if getattr(self, 'installed_apps', None):
+        if hasattr(self, 'installed_apps'):
             hacks.reset_installed_apps()
+            hacks.restore_app_cache_state()
 
 
 # Try importing all tests if asked for (then we can run 'em)