-
-
Notifications
You must be signed in to change notification settings - Fork 995
/
Copy pathtestutils.py
43 lines (35 loc) · 1.29 KB
/
testutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from django.apps import apps
from django.db import connections
def create_db_tables_for_unmanaged_models(schema_editor):
"""
Create tables for all unmanaged models in the tracdb app.
"""
appconfig = apps.get_app_config("tracdb")
for model in appconfig.get_models():
if model._meta.managed:
continue
schema_editor.create_model(model)
def destroy_db_tables_for_unmanaged_models(schema_editor):
"""
Destroy tables for all unmanaged models in the tracdb app
"""
appconfig = apps.get_app_config("tracdb")
for model in appconfig.get_models():
if model._meta.managed:
continue
schema_editor.delete_model(model)
class TracDBCreateDatabaseMixin:
"""
A TestCase mixin that creates test tables for all the tracdb apps.
Make sure you have databases = {"trac"} defined on your TestCase subclass.
"""
@classmethod
def setUpClass(cls):
with connections["trac"].schema_editor() as schema_editor:
create_db_tables_for_unmanaged_models(schema_editor)
super().setUpClass()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
with connections["trac"].schema_editor() as schema_editor:
destroy_db_tables_for_unmanaged_models(schema_editor)