Commits

Anonymous committed 7274b38

[soc2010/app-loading] raise ImproperlyConfigured when trying to register models to an app that isnt listed in INSTALLED_APPS

  • Participants
  • Parent commits d0b5156
  • Branches soc2010/app-loading

Comments (0)

Files changed (2)

File django/core/apps.py

     def get_apps(self):
         "Returns a list of all installed modules that contain models."
         self._populate()
-
-        # Ensure the returned list is always in the same order (with new apps
-        # added at the end). This avoids unstable ordering on the admin app
-        # list page, for example.
         return [app.models_module for app in self.app_instances\
-                if app.models_module]
+                if hasattr(app, 'models_module')]
 
     def get_app(self, app_label, emptyOK=False):
         """
         Register a set of models as belonging to an app.
         """
         app_instance = self.find_app(app_label)
-
-        # Create a new App instance if the ModelBase tries to register
-        # an app that isn't listed in INSTALLED_APPS
         if not app_instance:
-            app_instance = App(app_label)
-            self.app_instances.append(app_instance)
-
+            raise ImproperlyConfigured('Could not find App instance with label "%s". '
+                                       'Please check your INSTALLED_APPS setting'
+                                       % app_label)
         for model in models:
             # Store as 'name: model' pair in a dictionary
             # in the models list of the App instance

File tests/appcachetests/runtests.py

 from django.conf import settings
 from django.utils.datastructures import SortedDict
 from django.core.exceptions import ImproperlyConfigured
-from django.core.apps import MultipleInstancesReturned
+from django.core.apps import cache, MultipleInstancesReturned
 
 # remove when tests are integrated into the django testsuite
 settings.configure()
 
-from django.db.models.loading import cache
 
 class AppCacheTestCase(unittest.TestCase):
     """
 
         # we cannot copy() the whole cache.__dict__ in the setUp function
         # because thread.RLock is un(deep)copyable
-        cache.app_store = SortedDict()
         cache.app_models = SortedDict()
-        cache.app_errors = {}
+        cache.app_instances = []
+
         cache.loaded = False
         cache.handled = {}
         cache.postponed = []
         cache.nesting_level = 0
         cache.write_lock = threading.RLock()
         cache._get_models_cache = {}
-        
-        cache.app_instances = []
 
 class AppCacheReadyTests(AppCacheTestCase):
     """
 
     def test_load_app(self):
         """Should return False after executing the load_app function"""
-        cache.load_app('django.contrib.comments')
+        cache.load_app('nomodel_app')
         self.assertFalse(cache.app_cache_ready())
-        cache.load_app('django.contrib.comments', can_postpone=True)
+        cache.load_app('nomodel_app', can_postpone=True)
         self.assertFalse(cache.app_cache_ready())
 
 class GetAppsTests(AppCacheTestCase):
 
     def test_get_apps(self):
         """Test that the correct models modules are returned"""
-        settings.INSTALLED_APPS = ('django.contrib.auth',
+        settings.INSTALLED_APPS = ('django.contrib.sites',
+                                   'django.contrib.contenttypes',
+                                   'django.contrib.auth',
                                    'django.contrib.flatpages',)
         apps = cache.get_apps()
-        self.assertEqual(len(apps), 2)
+        self.assertEqual(len(apps), 4)
         self.assertTrue(apps[0], 'django.contrib.auth.models')
         self.assertTrue(apps[1], 'django.contrib.flatpages.models')
+        self.assertTrue(apps[2], 'django.contrib.sites.models')
+        self.assertTrue(apps[3], 'django.contrib.contenttypes.models')
         self.assertTrue(cache.app_cache_ready())
 
     def test_empty_models(self):
 
     def test_get_app(self):
         """Test that the correct module is returned"""
-        settings.INSTALLED_APPS = ('django.contrib.auth',)
+        settings.INSTALLED_APPS = ('django.contrib.contenttypes',
+                                   'django.contrib.auth',)
         module = cache.get_app('auth')
         self.assertTrue(module, 'django.contrib.auth.models')
         self.assertTrue(cache.app_cache_ready())
 
     def test_get_models(self):
         """Test that the correct model classes are returned"""
-        settings.INSTALLED_APPS = ('django.contrib.flatpages',) 
+        settings.INSTALLED_APPS = ('django.contrib.sites',
+                                   'django.contrib.flatpages',) 
+        models = cache.get_models()
         from django.contrib.flatpages.models import Site, FlatPage
-        models = cache.get_models()
         self.assertEqual(len(models), 2)
         self.assertEqual(models[0], Site)
         self.assertEqual(models[1], FlatPage)
         Test that the correct model classes are returned if an
         app module is specified
         """
-        settings.INSTALLED_APPS = ('django.contrib.flatpages',)
+        settings.INSTALLED_APPS = ('django.contrib.sites',
+                                   'django.contrib.flatpages',)
+        # populate cache
+        cache.get_app_errors()
+
         from django.contrib.flatpages import models
         from django.contrib.flatpages.models import FlatPage
         rv = cache.get_models(app_mod=models)
 
     def test_include_auto_created(self):
         """Test that auto created models are included if specified"""
-        settings.INSTALLED_APPS = ('django.contrib.flatpages',)
+        settings.INSTALLED_APPS = ('django.contrib.sites',
+                                   'django.contrib.flatpages',)
+        models = cache.get_models(include_auto_created=True)
         from django.contrib.flatpages.models import Site, FlatPage
-        models = cache.get_models(include_auto_created=True)
         self.assertEqual(len(models), 3)
         self.assertEqual(models[0], Site)
         self.assertEqual(models[1].__name__, 'FlatPage_sites')
 
     def test_get_model(self):
         """Test that the correct model is returned"""
-        settings.INSTALLED_APPS = ('django.contrib.flatpages',)
+        settings.INSTALLED_APPS = ('django.contrib.sites',
+                                   'django.contrib.flatpages',)
+        rv = cache.get_model('flatpages', 'FlatPage')
         from django.contrib.flatpages.models import FlatPage
-        self.assertEqual(cache.get_model('flatpages', 'FlatPage'), FlatPage)
+        self.assertEqual(rv, FlatPage)
         self.assertTrue(cache.app_cache_ready())
 
     def test_invalid(self):
         self.assertEqual(len(cache.app_instances), 1)
         self.assertEqual(app.models[0].__name__, 'Person')
 
-    def test_new_instance(self):
+    def test_app_not_installed(self):
         """
-        Test a new app instance is created if one doesn't exist, and the
-        models are attached to it.
+        Test that an exception is raised if models are tried to be registered
+        to an app that isn't listed in INSTALLED_APPS.
         """
-        from model_app.models import Person
-        app = cache.app_instances[0]
-        self.assertEqual(len(cache.app_instances), 1)
-        self.assertEqual(app.name, 'model_app')
-        self.assertEqual(app.models[0].__name__, 'Person')
+        try:
+            from model_app.models import Person
+        except ImproperlyConfigured:
+            pass
+        else:
+            self.fail('ImproperlyConfigured not raised')
 
 if __name__ == '__main__':
     unittest.main()