def test_missing_app(self): """ Test that repeated app loading doesn't succeed in case there is an error. Refs #17667. """ app_cache = AppCache() # Pretend we're the master app cache to test populate(). app_cache.master = True with override_settings(INSTALLED_APPS=('notexists',)): with self.assertRaises(ImportError): app_cache.get_model('notexists', 'nomodel', seed_cache=True) with self.assertRaises(ImportError): app_cache.get_model('notexists', 'nomodel', seed_cache=True)
def test_missing_app(self): """ Test that repeated app loading doesn't succeed in case there is an error. Refs #17667. """ # AppCache is a Borg, so we can instantiate one and change its # loaded to False to force the following code to actually try to # populate the cache. a = AppCache() a.loaded = False try: with override_settings(INSTALLED_APPS=('notexists',)): self.assertRaises(ImportError, app_cache.get_model, 'notexists', 'nomodel', seed_cache=True) self.assertRaises(ImportError, app_cache.get_model, 'notexists', 'nomodel', seed_cache=True) finally: a.loaded = True
def test_dynamic_load(self): """ Makes a new model at runtime and ensures it goes into the right place. """ old_models = app_cache.get_models(app_cache.get_app_config("app_cache").models_module) # Construct a new model in a new app cache body = {} new_app_cache = AppCache() meta_contents = { 'app_label': "app_cache", 'app_cache': new_app_cache, } meta = type("Meta", tuple(), meta_contents) body['Meta'] = meta body['__module__'] = TotallyNormal.__module__ temp_model = type("SouthPonies", (models.Model,), body) # Make sure it appeared in the right place! self.assertEqual( old_models, app_cache.get_models(app_cache.get_app_config("app_cache").models_module), ) self.assertEqual(new_app_cache.get_model("app_cache", "SouthPonies"), temp_model)