Skip to content

Commit

Permalink
Fixed issues with TracDBCreateDatabaseMixin
Browse files Browse the repository at this point in the history
- The mixin is now compatible with TestCase.setUpTestData
- The mixin now has teardown logic and can be used more than
  once in the testsuite
  • Loading branch information
bmispelon committed Nov 7, 2024
1 parent 1729f82 commit ba9446c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tracdb/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def create_db_tables_for_unmanaged_models(schema_editor):
_create_db_table_for_model(model, schema_editor)


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.
Expand All @@ -75,6 +86,12 @@ class TracDBCreateDatabaseMixin:

@classmethod
def setUpClass(cls):
super().setUpClass()
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)

0 comments on commit ba9446c

Please sign in to comment.