Turned apps.ready into a property. Added tests.

This commit is contained in:
Aymeric Augustin 2013-12-26 15:04:58 +01:00
parent 922430177c
commit 52325b0a04
3 changed files with 29 additions and 12 deletions

View File

@ -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):
"""

View File

@ -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

View File

@ -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)