Coverage for apis_core/generic/tests/__init__.py: 93%

14 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-09-03 06:15 +0000

1# Set up app with custom test models 

2# https://code.djangoproject.com/ticket/7835#comment:46 

3from django.apps import AppConfig, apps 

4 

5 

6def setup_test_app(package, label=None): 

7 """ 

8 Setup a Django test app for the provided package to allow test models 

9 tables to be created if the containing app has migrations. 

10 

11 This function should be called from app.tests __init__ module and pass 

12 along __package__. 

13 """ 

14 app_config = AppConfig.create(package) 

15 app_config.apps = apps 

16 if label is None: 

17 containing_app_config = apps.get_containing_app_config(package) 

18 label = f"{containing_app_config.label}_tests" 

19 if label in apps.app_configs: 

20 raise ValueError(f"There's already an app registered with the '{label}' label.") 

21 app_config.label = label 

22 apps.app_configs[app_config.label] = app_config 

23 app_config.import_models() 

24 apps.clear_cache() 

25 

26 

27setup_test_app(__package__)