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 the population process. app_cache._apps_loaded = False app_cache._models_loaded = False with warnings.catch_warnings(): warnings.filterwarnings("ignore", "Overriding setting INSTALLED_APPS") with override_settings(INSTALLED_APPS=['notexists']): with self.assertRaises(ImportError): app_cache.get_model('notexists', 'nomodel') with self.assertRaises(ImportError): app_cache.get_model('notexists', 'nomodel')
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)