MCPcopy
hub / github.com/django/django / test_lazy_model_operation

Method test_lazy_model_operation

tests/apps/tests.py:371–415  ·  view source on GitHub ↗

Tests apps.lazy_model_operation().

(self, apps)

Source from the content-addressed store, hash-verified

369
370 @isolate_apps("apps", kwarg_name="apps")
371 def test_lazy_model_operation(self, apps):
372 """
373 Tests apps.lazy_model_operation().
374 """
375 model_classes = []
376 initial_pending = set(apps._pending_operations)
377
378 def test_func(*models):
379 model_classes[:] = models
380
381 class LazyA(models.Model):
382 pass
383
384 # Test models appearing twice, and models appearing consecutively
385 model_keys = [
386 ("apps", model_name)
387 for model_name in ["lazya", "lazyb", "lazyb", "lazyc", "lazya"]
388 ]
389 apps.lazy_model_operation(test_func, *model_keys)
390
391 # LazyModelA shouldn't be waited on since it's already registered,
392 # and LazyModelC shouldn't be waited on until LazyModelB exists.
393 self.assertEqual(
394 set(apps._pending_operations) - initial_pending, {("apps", "lazyb")}
395 )
396
397 # Multiple operations can wait on the same model
398 apps.lazy_model_operation(test_func, ("apps", "lazyb"))
399
400 class LazyB(models.Model):
401 pass
402
403 self.assertEqual(model_classes, [LazyB])
404
405 # Now we are just waiting on LazyModelC.
406 self.assertEqual(
407 set(apps._pending_operations) - initial_pending, {("apps", "lazyc")}
408 )
409
410 class LazyC(models.Model):
411 pass
412
413 # Everything should be loaded - make sure the callback was executed
414 # properly.
415 self.assertEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])
416
417
418class Stub:

Callers

nothing calls this directly

Calls 1

lazy_model_operationMethod · 0.80

Tested by

no test coverage detected