Skip to content

Commit

Permalink
WIP: not working base tests fix
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenMaude committed Jan 16, 2025
1 parent cb8d2e9 commit 2629fc7
Showing 1 changed file with 68 additions and 8 deletions.
76 changes: 68 additions & 8 deletions coding_systems/base/tests/test_import_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 2629fc7

Please sign in to comment.