From 782a12b26f75bc985d3b419948b7faa4f22cbd5e Mon Sep 17 00:00:00 2001 From: SimonKonar Date: Fri, 19 Jan 2024 11:32:53 +0100 Subject: [PATCH] fix: change blaze service constructor to use specific instances instead of factory --- main.py | 12 ++++++++++- service/blaze_service.py | 21 +++++++----------- test/integration/test_blaze.py | 39 +++++++++++++++++----------------- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index 1d75fc7..723f4cf 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,9 @@ import logging import sys from service.blaze_service import BlazeService +from service.condition_service import ConditionService +from service.patient_service import PatientService +from service.sample_service import SampleService from util.config import BLAZE_URL from util.custom_logger import setup_logger from util.http_util import is_endpoint_available @@ -13,7 +16,14 @@ logger.info("Starting FHIR_Module...") if is_endpoint_available(endpoint_url=BLAZE_URL, wait_time=10, max_attempts=5): repository_factory = get_repository_factory() - blaze_service = BlazeService(repository_factory=repository_factory, blaze_url=BLAZE_URL) + patient_service = PatientService(repository_factory.create_sample_donor_repository()) + condition_service = ConditionService(repository_factory.create_condition_repository()) + sample_service = SampleService(repository_factory.create_sample_repository()) + sample_collection_repository = repository_factory.create_sample_collection_repository() + blaze_service = BlazeService(patient_service=patient_service, condition_service=condition_service, + sample_service=sample_service, blaze_url=BLAZE_URL, + sample_collection_repository=sample_collection_repository) + blaze_service.sync_samples() blaze_service.sync() else: logger.error("Exiting FHIR_Module.") diff --git a/service/blaze_service.py b/service/blaze_service.py index f251136..469c005 100644 --- a/service/blaze_service.py +++ b/service/blaze_service.py @@ -9,7 +9,6 @@ from exception.patient_not_found import PatientNotFoundError from model.sample import Sample from model.sample_donor import SampleDonor -from persistence.factories.repository_factory import RepositoryFactory from persistence.sample_collection_repository import SampleCollectionRepository from service.condition_service import ConditionService from service.patient_service import PatientService @@ -24,26 +23,22 @@ class BlazeService: """Service class for business operations/interactions with a Blaze FHIR server.""" - def __init__(self, repository_factory: RepositoryFactory, - # patient_service: PatientService, condition_service: ConditionService, - # sample_service: SampleService + def __init__(self, + patient_service: PatientService, condition_service: ConditionService, + sample_service: SampleService, blaze_url: str, - # sample_collection_repository: SampleCollectionRepository + sample_collection_repository: SampleCollectionRepository ): """ Class for interacting with a Blaze Store FHIR server :param blaze_url: Base url of the FHIR server Must be without a trailing /. """ - self._patient_service = PatientService(repository_factory.create_sample_donor_repository()) - self._condition_service = ConditionService(repository_factory.create_condition_repository()) - self._sample_service = SampleService(repository_factory.create_sample_repository()) - self._sample_collection_repository = repository_factory.create_sample_collection_repository() - # self._patient_service = patient_service - # self._condition_service = condition_service - # self._sample_service = sample_service + self._patient_service = patient_service + self._condition_service = condition_service + self._sample_service = sample_service self._blaze_url = blaze_url - # self._sample_collection_repository = sample_collection_repository + self._sample_collection_repository = sample_collection_repository self._credentials = BLAZE_AUTH def sync(self): diff --git a/test/integration/test_blaze.py b/test/integration/test_blaze.py index a177642..72123fb 100644 --- a/test/integration/test_blaze.py +++ b/test/integration/test_blaze.py @@ -63,26 +63,15 @@ def get_all(self) -> Generator[SampleCollection, None, None]: yield from self.sample_collections -class RepositoryFactoryStub(RepositoryFactory): - - def create_condition_repository(self) -> ConditionRepository: - return ConditionRepoStub() - - def create_sample_collection_repository(self) -> SampleCollectionRepository: - return SampleCollectionRepoStub() - - def create_sample_repository(self) -> SampleRepository: - return SampleRepoStub() - - def create_sample_donor_repository(self) -> SampleDonorRepository: - return SampleDonorRepoStub() - - class TestBlazeStore(unittest.TestCase): @pytest.fixture(autouse=True) def run_around_tests(self): - self.blaze_service = BlazeService(RepositoryFactoryStub(), blaze_url='http://localhost:8080/fhir') + self.blaze_service = BlazeService(patient_service=PatientService(SampleDonorRepoStub()), + condition_service=ConditionService(ConditionRepoStub()), + sample_service=SampleService(SampleRepoStub()), + blaze_url='http://localhost:8080/fhir', + sample_collection_repository=SampleCollectionRepoStub()) yield # run test try: for donor in SampleDonorRepoStub().get_all(): @@ -112,7 +101,11 @@ def test_delete_nonexistent_resource_404(self): self.assertEqual(404, self.blaze_service.delete_fhir_resource("Patient", "newId")) def test_upload_all_patients_when_blaze_unreachable(self): - self.blaze_service = BlazeService(RepositoryFactoryStub(), blaze_url='http://localhost:44/wrong') + self.blaze_service = BlazeService(PatientService(SampleDonorRepoStub()), + blaze_url='http://localhost:44/wrong', + condition_service=ConditionService(ConditionRepoStub()), + sample_service=SampleService(SampleRepoStub()), + sample_collection_repository=SampleCollectionRepoStub()) self.assertEqual(404, self.blaze_service.initial_upload_of_all_patients()) def test_is_present_in_blaze(self): @@ -127,7 +120,11 @@ def test_sync_one_new_patient(self): self.blaze_service.initial_upload_of_all_patients() num_of_patients_before_sync = self.blaze_service.get_number_of_resources("Patient") donor_repo.add(SampleDonor("uniqueNewPatient5")) - self.blaze_service = BlazeService(RepositoryFactoryStub(), blaze_url='http://localhost:8080/fhir') + self.blaze_service = BlazeService(PatientService(donor_repo), + blaze_url='http://localhost:8080/fhir', + condition_service=ConditionService(ConditionRepoStub()), + sample_service=SampleService(SampleRepoStub()), + sample_collection_repository=SampleCollectionRepoStub()) self.blaze_service.sync_patients() self.assertEqual(num_of_patients_before_sync + 1, self.blaze_service.get_number_of_resources("Patient")) @@ -152,7 +149,11 @@ def test_sync_one_new_condition(self): self.blaze_service.sync_conditions() self.assertFalse(self.blaze_service.patient_has_condition("fakeId", "C50.6")) condition_repo.add(Condition(patient_id="fakeId", icd_10_code="C50.6")) - self.blaze_service = BlazeService(RepositoryFactoryStub(), blaze_url='http://localhost:8080/fhir') + self.blaze_service = BlazeService(PatientService(SampleDonorRepoStub()), + blaze_url='http://localhost:8080/fhir', + condition_service=ConditionService(condition_repo), + sample_service=SampleService(SampleRepoStub()), + sample_collection_repository=SampleCollectionRepoStub()) self.blaze_service.sync_conditions() self.assertTrue(self.blaze_service.patient_has_condition("fakeId", "C50.6"))