Skip to content

Commit

Permalink
fix: change blaze service constructor to use specific instances inste…
Browse files Browse the repository at this point in the history
…ad of factory
  • Loading branch information
SimonKonar committed Jan 19, 2024
1 parent f8f9a4d commit 782a12b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
12 changes: 11 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.")
Expand Down
21 changes: 8 additions & 13 deletions service/blaze_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
39 changes: 20 additions & 19 deletions test/integration/test_blaze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand All @@ -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"))

Expand All @@ -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"))

Expand Down

0 comments on commit 782a12b

Please sign in to comment.