#7835: Provide the ability for model definitions that are only availably during
     Reporter:  Russell Keith-Magee  |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Testing framework    |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  feature test models  |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
Changes (by Simon Charette):

 * cc: Simon Charette (added)


 While working on a workaround for this I came up with a non-invasive
 solution that might be acceptable to resolving the ticket.

 The idea is similar to Ashley's solution but is more explicit as it
 require a function call in the `app/tests/__init__.py` module. It does
 however isolate each app into their own app, which prevent name
 collisions, and doesn't require the `app_label = 'test'` assignment on
 each test model.

 The solutions boils down to this function

 def setup_test_app(package, label=None):
     Setup a Django test app for the provided package to allow test models
     tables to be created if the containing app has migrations.

     This function should be called from app.tests __init__ module and pass
     along __package__.
     app_config = AppConfig.create(package)
     app_config.apps = apps
     if label is None:
         containing_app_config = apps.get_containing_app_config(package)
         label = f'{containing_app_config.label}_tests'
     if label in apps.app_configs:
         raise ValueError(f"There's already an app registered with the
 '{label}' label.')
     app_config.label = label
     apps.app_configs[app_config.label] = app_config

 Which when called from `app/tests/__init__.py` as
 `setup_test_app(__package__)` will create an `app_tests` appconfig entry
 and auto-discover the models automatically. Since the `*.tests` modules
 should only be loaded on test discovery the app and its models will only
 be available during tests. Keep in mind that if your test models reference
 models from an application with migrations you'll also need to manually
 create migrations for these tests models but once that's done you should
 be good to go.

 It does feel less magic and convention based than Ashley's solution as it
 prevents conflicts between models and allows multiple test apps per app
 from any test package structure. Thoughts?

Ticket URL: <https://code.djangoproject.com/ticket/7835#comment:46>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
For more options, visit https://groups.google.com/d/optout.

Reply via email to