From ba9446cd0860be0eeb7c2b3f1d7f18e9a9a13c23 Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Sun, 27 Oct 2024 00:36:40 +0200 Subject: [PATCH] Fixed issues with TracDBCreateDatabaseMixin - The mixin is now compatible with TestCase.setUpTestData - The mixin now has teardown logic and can be used more than once in the testsuite --- tracdb/testutils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tracdb/testutils.py b/tracdb/testutils.py index 1a9698932..1d15d48cc 100644 --- a/tracdb/testutils.py +++ b/tracdb/testutils.py @@ -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. @@ -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)