From 2629fc7ce7b2d19fc593c3794f50bb14ce1b5e7a Mon Sep 17 00:00:00 2001 From: Steven Maude Date: Thu, 16 Jan 2025 23:19:04 +0000 Subject: [PATCH] WIP: not working base tests fix --- .../base/tests/test_import_data_utils.py | 76 +++++++++++++++++-- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/coding_systems/base/tests/test_import_data_utils.py b/coding_systems/base/tests/test_import_data_utils.py index 046cb2b9..b2df1e33 100644 --- a/coding_systems/base/tests/test_import_data_utils.py +++ b/coding_systems/base/tests/test_import_data_utils.py @@ -2,6 +2,7 @@ import pytest from django.db import DEFAULT_DB_ALIAS, connections +from django.test import TestCase from builder.actions import create_search from builder.actions import save as save_draft_for_review @@ -34,6 +35,55 @@ def bnf_review_version_with_search(bnf_version_with_search): yield bnf_version_with_search +@pytest.fixture(autouse=True) +def coding_systems_database_tmp_dir(coding_systems_tmp_path): + yield coding_systems_tmp_path + + +class BaseDynamicDatabaseTestCase(TestCase): + @property + def db_alias(self): + # The db_alias that will be added temporarily to the DB. + raise NotImplementedError( + "This test class requires a database alias to be set." + ) + + @property + def coding_system(self): + raise NotImplementedError("This test class requires a coding system to be set.") + + @pytest.fixture(autouse=True) + def _get_tmp_dir(self, coding_systems_database_tmp_dir): + self.coding_systems_database_tmp_dir = coding_systems_database_tmp_dir + + def setUp(self): + super().setUp() + + # Mutate *class* state, this attribute determines to which databases + # SimpleTestCase.ensure_connection_patch_method will allow connections. + # We can't patch this directly in the test case as the class is + # constructed dynamically. No need to reset as each test case execution + # gets a new dynamic class. + self.original_databases = type(self).databases + type(self).databases |= frozenset({self.db_alias}) + + self.expected_db_path = ( + self.coding_systems_database_tmp_dir + / f"{self.coding_system}" + / f"{self.db_alias}.sqlite3" + ) + # Set up mock source data. + self.base_import_data_path_inst = None + + # Not necessary to remove the DB as the temp dir is scoped by test case. + + def tearDown(self): + super().tearDown() + # Remove the dynamic database from the test class, as Django doesn't + # know how to roll back when the transaction wrapping the test case ends. + type(self).databases = self.original_databases + + def setup_db(csr, exclude_last_concept=False): # At this point, the test databases have been configured as in-memory sqlite dbs # we copy the default db config and update the name (which in tests is something @@ -104,14 +154,24 @@ def bnf_releases(bnf_csr): cleanup_db(csr) -def test_update_codelist_version_compatibility_no_searches( - bnf_version_asthma, coding_systems_tmp_path, bnf_release -): - # Draft versions are not checked for compatibility; this version is under review - assert bnf_version_asthma.status == Status.PUBLISHED - update_codelist_version_compatibility("bnf", bnf_release.database_alias) - # this version has no searches, but its hierarchy is identical - assert bnf_version_asthma.compatible_releases.exists() +class TestUpdateCodelistVersionCompatibilityNoSearches(BaseDynamicDatabaseTestCase): + db_alias = "bnf_import-data_20221101" + coding_system = "bnf" + + @pytest.fixture(autouse=True) + def _get_bnf_version_asthma(self, bnf_version_asthma): + self.bnf_version_asthma = bnf_version_asthma + + @pytest.fixture(autouse=True) + def _get_bnf_release(self, bnf_release): + self.bnf_release = bnf_release + + def test_update_codelist_version_compatibility_no_searches(self): + # Draft versions are not checked for compatibility; this version is under review + assert self.bnf_version_asthma.status == Status.PUBLISHED + update_codelist_version_compatibility("bnf", self.bnf_release.database_alias) + # this version has no searches, but its hierarchy is identical + assert self.bnf_version_asthma.compatible_releases.exists() def test_update_codelist_draft_version_excluded(