From 52325b0a04cf1fcf444cece197585a35cf65bab9 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 26 Dec 2013 15:04:58 +0100 Subject: [PATCH] Turned apps.ready into a property. Added tests. --- django/apps/registry.py | 7 ++++--- django/db/models/options.py | 4 ++-- tests/apps/tests.py | 30 +++++++++++++++++++++++------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/django/apps/registry.py b/django/apps/registry.py index 2a09513152..e39fb43fa4 100644 --- a/django/apps/registry.py +++ b/django/apps/registry.py @@ -138,9 +138,10 @@ class Apps(object): self.get_models.cache_clear() self._models_loaded = True + @property def ready(self): """ - Returns True if the registry is fully populated. + Whether the registry is fully populated. Useful for code that wants to cache the results of get_models() for themselves once it is safe to do so. @@ -378,9 +379,9 @@ class Apps(object): def app_cache_ready(self): warnings.warn( - "app_cache_ready() is deprecated.", + "app_cache_ready() is deprecated in favor of the ready property.", PendingDeprecationWarning, stacklevel=2) - return self.ready() + return self.ready def get_app(self, app_label): """ diff --git a/django/db/models/options.py b/django/db/models/options.py index 4c5519441e..f50c639975 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -438,7 +438,7 @@ class Options(object): if hasattr(f, 'related'): cache[f.name] = cache[f.attname] = ( f.related, None if f.model == self.model else f.model, True, False) - if apps.ready(): + if apps.ready: self._name_map = cache return cache @@ -564,7 +564,7 @@ class Options(object): and not isinstance(f.rel.to, six.string_types) and self == f.rel.to._meta): cache[f.related] = None - if apps.ready(): + if apps.ready: self._related_many_to_many_cache = cache return cache diff --git a/tests/apps/tests.py b/tests/apps/tests.py index c38d16f8bb..a14199d41c 100644 --- a/tests/apps/tests.py +++ b/tests/apps/tests.py @@ -10,6 +10,29 @@ from .models import TotallyNormal, SoAlternative, new_apps class AppsTests(TestCase): + def test_singleton_master(self): + """ + Ensures that only one master registry can exist. + """ + with self.assertRaises(RuntimeError): + Apps(master=True) + + def test_ready(self): + """ + Tests the ready property of the master registry. + """ + # The master app registry is always ready when the tests run. + self.assertTrue(apps.ready) + + def test_non_master_ready(self): + """ + Tests the ready property of a registry other than the master. + """ + apps = Apps() + # Currently, non-master app registries are artificially considered + # ready regardless of whether populate_models() has run. + self.assertTrue(apps.ready) + def test_models_py(self): """ Tests that the models in the models.py file were loaded correctly. @@ -42,10 +65,3 @@ class AppsTests(TestCase): apps.get_models(apps.get_app_config("apps").models_module), ) self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model) - - def test_singleton_master(self): - """ - Ensures that only one master registry can exist. - """ - with self.assertRaises(RuntimeError): - Apps(master=True)