sintonia/api/libretime_api/tests/runners.py
jo 32cb67806a chore: add pyupgrade pre-commit hook
- add --py3-plus flag to pyupgrade hook
- add --py36-plus flag to pyupgrade hook
2022-01-27 10:37:48 +02:00

23 lines
852 B
Python

from django.test.runner import DiscoverRunner
class ManagedModelTestRunner(DiscoverRunner):
"""
Test runner that automatically makes all unmanaged models in your Django
project managed for the duration of the test run, so that one doesn't need
to execute the SQL manually to create them.
"""
def setup_test_environment(self, *args, **kwargs):
from django.apps import apps
self.unmanaged_models = [m for m in apps.get_models() if not m._meta.managed]
for m in self.unmanaged_models:
m._meta.managed = True
super().setup_test_environment(*args, **kwargs)
def teardown_test_environment(self, *args, **kwargs):
super().teardown_test_environment(*args, **kwargs)
# reset unmanaged models
for m in self.unmanaged_models:
m._meta.managed = False