From 1500573620fe2d2bba421da80aa4d8298b9262e8 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Tue, 30 Jan 2018 16:44:58 +0100 Subject: [PATCH] Added test for Apps.get_models() when models_ready=False. --- tests/apps/tests.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/apps/tests.py b/tests/apps/tests.py index 2910220b94..2fec1d8b4c 100644 --- a/tests/apps/tests.py +++ b/tests/apps/tests.py @@ -45,9 +45,9 @@ class AppsTests(SimpleTestCase): Tests the ready property of the master registry. """ # The master app registry is always ready when the tests run. - self.assertTrue(apps.ready) + self.assertIs(apps.ready, True) # Non-master app registries are populated in __init__. - self.assertTrue(Apps().ready) + self.assertIs(Apps().ready, True) def test_bad_app_config(self): """ @@ -124,10 +124,10 @@ class AppsTests(SimpleTestCase): """ Tests apps.is_installed(). """ - self.assertTrue(apps.is_installed('django.contrib.admin')) - self.assertTrue(apps.is_installed('django.contrib.auth')) - self.assertTrue(apps.is_installed('django.contrib.staticfiles')) - self.assertFalse(apps.is_installed('django.contrib.admindocs')) + self.assertIs(apps.is_installed('django.contrib.admin'), True) + self.assertIs(apps.is_installed('django.contrib.auth'), True) + self.assertIs(apps.is_installed('django.contrib.staticfiles'), True) + self.assertIs(apps.is_installed('django.contrib.admindocs'), False) @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS) def test_get_model(self): @@ -184,6 +184,19 @@ class AppsTests(SimpleTestCase): new_apps.get_model("apps", "TotallyNormal") self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative) + def test_models_not_loaded(self): + """ + apps.get_models() raises an exception if apps.models_ready isn't True. + """ + apps.models_ready = False + try: + # The cache must be cleared to trigger the exception. + apps.get_models.cache_clear() + with self.assertRaisesMessage(AppRegistryNotReady, "Models aren't loaded yet."): + apps.get_models() + finally: + apps.models_ready = True + def test_dynamic_load(self): """ Makes a new model at runtime and ensures it goes into the right place.