Skip to content

Commit

Permalink
CU-869574kvp: Fix preprocessing tests.
Browse files Browse the repository at this point in the history
Add patch for files/folders where applicable.
Change the paths of attributes where applicable.
  • Loading branch information
mart-r committed Sep 11, 2024
1 parent b7ef2b6 commit 37b5abe
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions tests/utils/test_preprocess_snomed.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
from typing import Dict
import contextlib

from medcat.utils import preprocess_snomed

import unittest
from unittest.mock import patch


EXAMPLE_REFSET_DICT: Dict = {
Expand Down Expand Up @@ -46,22 +49,51 @@ def test_example_no_codfe_fails(self):
with self.assertRaises(KeyError):
preprocess_snomed.get_direct_refset_mapping(EXAMPLE_REFSET_DICT_NO_CODE)


EXAMPLE_SNOMED_PATH_OLD = "SnomedCT_InternationalRF2_PRODUCTION_20220831T120000Z"
EXAMPLE_SNOMED_PATH_NEW = "SnomedCT_UKClinicalRF2_PRODUCTION_20231122T000001Z"


class TestSnomedVersionsOPCS4(unittest.TestCase):
@contextlib.contextmanager
def patch_fake_files(path: str, subfiles: list = [],
subdirs: list = ["Snapshot"]):
def cur_listdir(file_path: str, *args, **kwargs) -> list:
if file_path == path:
return subfiles + subdirs
for sd in subdirs:
subdir = os.path.join(path, sd)
if subdir == path:
return []
raise FileNotFoundError(path)

def test_old_gets_old_OPCS4_mapping_nonuk_ext(self):
snomed = preprocess_snomed.Snomed(EXAMPLE_SNOMED_PATH_OLD, uk_ext=False)
self.assertEqual(snomed.opcs_refset_id, "1126441000000105")
def cur_isfile(file_path: str, *args, **kwargs) -> bool:
print("CUR isfile", file_path)
return file_path == path or file_path in [os.path.join(path, subfiles)]

def cur_isdir(file_path: str, *args, **kwrags) -> bool:
print("CUR isdir", file_path)
return file_path == path or file_path in [os.path.join(path, subdirs)]

with patch("os.listdir", new=cur_listdir):
with patch("os.path.isfile", new=cur_isfile):
with patch("os.path.isdir", new=cur_isdir):
yield


class TestSnomedVersionsOPCS4(unittest.TestCase):

def test_old_gets_old_OPCS4_mapping_uk_ext(self):
snomed = preprocess_snomed.Snomed(EXAMPLE_SNOMED_PATH_OLD, uk_ext=True)
def test_old_gets_old_OPCS4_mapping(self):
with patch_fake_files(EXAMPLE_SNOMED_PATH_OLD):
snomed = preprocess_snomed.Snomed(EXAMPLE_SNOMED_PATH_OLD)
snomed._set_extension(snomed._determine_release(EXAMPLE_SNOMED_PATH_OLD),
snomed._determine_extension(EXAMPLE_SNOMED_PATH_OLD))
self.assertEqual(snomed.opcs_refset_id, "1126441000000105")

def test_new_gets_new_OCPS4_mapping_uk_ext(self):
snomed = preprocess_snomed.Snomed(EXAMPLE_SNOMED_PATH_NEW, uk_ext=True)
def test_new_gets_new_OCPS4_mapping(self):
with patch_fake_files(EXAMPLE_SNOMED_PATH_NEW):
snomed = preprocess_snomed.Snomed(EXAMPLE_SNOMED_PATH_NEW)
snomed._set_extension(snomed._determine_release(EXAMPLE_SNOMED_PATH_NEW),
snomed._determine_extension(EXAMPLE_SNOMED_PATH_NEW))
self.assertEqual(snomed.opcs_refset_id, "1382401000000109")


Expand All @@ -86,15 +118,19 @@ def _pathify(self, in_list: list) -> list:
return [os.path.join(self.PATH, folder) for folder in in_list]

def assert_got_version(self, snomed: preprocess_snomed.Snomed, raw_name: str):
rel = snomed.release
rel_list = snomed.snomed_releases
self.assertIsInstance(rel_list, list)
self.assertEqual(len(rel_list), 1)
rel = rel_list[0]
self.assertIsInstance(rel, str)
self.assertIn(rel, raw_name)
self.assertEqual(rel, raw_name[-16:-8])

def assert_all_work(self, all_paths: list):
for path in all_paths:
with self.subTest(f"Rrelease name: {path}"):
snomed = preprocess_snomed.Snomed(path)
with patch_fake_files(path):
snomed = preprocess_snomed.Snomed(path)
self.assert_got_version(snomed, path)

def test_gets_model_form_basename(self):
Expand Down Expand Up @@ -123,8 +159,8 @@ def test_fails_on_incorrect_paths_strict(self):
def assert_all_get_no_version(self, folder_paths: list):
for folder_path in folder_paths:
with self.subTest(f"Folder: {folder_path}"):
snomed = preprocess_snomed.Snomed(folder_path)
self.assertEqual(snomed.release, preprocess_snomed.Snomed.NO_VERSION_DETECTED)
det_rel = preprocess_snomed.Snomed._determine_release(folder_path, strict=False)
self.assertEqual(det_rel, preprocess_snomed.Snomed.NO_VERSION_DETECTED)

def test_gets_no_version_incorrect_names_nonstrict(self):
self.assert_all_get_no_version(self.FAILING_BASE_NAMES)
Expand Down

0 comments on commit 37b5abe

Please sign in to comment.