diff options
author | Alex Hill <alex@hill.net.au> | 2015-03-03 16:43:56 +0800 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2015-03-25 16:48:17 -0400 |
commit | 720ff740e70e649a97fcf0232fec132569a52c7e (patch) | |
tree | 688533ec18f1d970b8c7671ef94be83b67720b80 /tests/apps | |
parent | 0f6f80c2e7736ec4e2aa40287fe8c37ffff0a783 (diff) | |
download | django-720ff740e70e649a97fcf0232fec132569a52c7e.tar.gz |
Fixed #24215 -- Refactored lazy model operations
This adds a new method, Apps.lazy_model_operation(), and a helper function,
lazy_related_operation(), which together supersede add_lazy_relation() and
make lazy model operations the responsibility of the App registry. This
system no longer uses the class_prepared signal.
Diffstat (limited to 'tests/apps')
-rw-r--r-- | tests/apps/tests.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/apps/tests.py b/tests/apps/tests.py index 89e295ffde..347996454f 100644 --- a/tests/apps/tests.py +++ b/tests/apps/tests.py @@ -259,6 +259,44 @@ class AppsTests(TestCase): finally: apps.apps_ready = True + def test_lazy_model_operation(self): + """ + Tests apps.lazy_model_operation(). + """ + model_classes = [] + initial_pending = set(apps._pending_operations) + + def test_func(*models): + model_classes[:] = models + + class LazyA(models.Model): + pass + + # Test models appearing twice, and models appearing consecutively + model_keys = [('apps', model_name) for model_name in ['lazya', 'lazyb', 'lazyb', 'lazyc', 'lazya']] + apps.lazy_model_operation(test_func, *model_keys) + + # LazyModelA shouldn't be waited on since it's already registered, + # and LazyModelC shouldn't be waited on until LazyModelB exists. + self.assertSetEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyb')}) + + # Test that multiple operations can wait on the same model + apps.lazy_model_operation(test_func, ('apps', 'lazyb')) + + class LazyB(models.Model): + pass + + self.assertListEqual(model_classes, [LazyB]) + + # Now we are just waiting on LazyModelC. + self.assertSetEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyc')}) + + class LazyC(models.Model): + pass + + # Everything should be loaded - make sure the callback was executed properly. + self.assertListEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA]) + class Stub(object): def __init__(self, **kwargs): |