From 6388283dd196264705e96ade9c9683e620c89aa2 Mon Sep 17 00:00:00 2001 From: Salaton Date: Fri, 10 Mar 2023 16:44:17 +0300 Subject: [PATCH] tests: add pubsub usecase tests --- .../common/testutils/testhelpers.go | 6 +- .../cloudhealthcare/mock/fhir_mock.go | 37 +- pkg/clinical/infrastructure/infrastructure.go | 5 +- .../services/mycarehub/mock/service_mock.go | 21 +- .../services/pubsub/service_test.go | 5 +- pkg/clinical/presentation/config.go | 6 +- pkg/clinical/usecases/clinical/fhir_test.go | 68 ++ .../usecases/clinical/mock/fhir_mock.go | 43 +- .../usecases/clinical/mock/patient_mock.go | 85 +- .../usecases/clinical/patient_unit_test.go | 13 +- .../usecases/clinical/pubsub_helpers.go | 4 +- pkg/clinical/usecases/clinical/pubsub_test.go | 849 ++++++++++++++++++ pkg/clinical/usecases/config_test.go | 6 +- 13 files changed, 1011 insertions(+), 137 deletions(-) create mode 100644 pkg/clinical/usecases/clinical/fhir_test.go create mode 100644 pkg/clinical/usecases/clinical/pubsub_test.go diff --git a/pkg/clinical/application/common/testutils/testhelpers.go b/pkg/clinical/application/common/testutils/testhelpers.go index 4fc8865c..173179dc 100644 --- a/pkg/clinical/application/common/testutils/testhelpers.go +++ b/pkg/clinical/application/common/testutils/testhelpers.go @@ -4,10 +4,12 @@ import ( "context" "log" + "github.com/savannahghi/clinical/pkg/clinical/application/common" "github.com/savannahghi/clinical/pkg/clinical/application/extensions" "github.com/savannahghi/clinical/pkg/clinical/infrastructure" fhir "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare" dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset" + "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub" "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab" "github.com/savannahghi/clinical/pkg/clinical/usecases" "github.com/savannahghi/serverutils" @@ -33,8 +35,10 @@ func InitializeTestService(ctx context.Context) (usecases.Interactor, error) { baseExtension := extensions.NewBaseExtensionImpl() fhir := fhir.NewFHIRStoreImpl(repo) ocl := openconceptlab.NewServiceOCL() + myCareHubClient := common.NewInterServiceClient("mycarehub", baseExtension) + mycarehub := mycarehub.NewServiceMyCareHub(myCareHubClient, baseExtension) - infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl) + infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl, mycarehub) usecases := usecases.NewUsecasesInteractor(infrastructure) diff --git a/pkg/clinical/infrastructure/datastore/cloudhealthcare/mock/fhir_mock.go b/pkg/clinical/infrastructure/datastore/cloudhealthcare/mock/fhir_mock.go index 1b4ceaa3..990d2bd5 100644 --- a/pkg/clinical/infrastructure/datastore/cloudhealthcare/mock/fhir_mock.go +++ b/pkg/clinical/infrastructure/datastore/cloudhealthcare/mock/fhir_mock.go @@ -3,6 +3,7 @@ package mock import ( "context" + "github.com/brianvoe/gofakeit" "github.com/google/uuid" "github.com/savannahghi/clinical/pkg/clinical/domain" "github.com/savannahghi/firebasetools" @@ -259,7 +260,27 @@ func NewFHIRMock() *FHIRMock { return &domain.FHIRAllergyIntoleranceRelayConnection{}, nil }, MockCreateFHIRAllergyIntoleranceFn: func(ctx context.Context, input domain.FHIRAllergyIntoleranceInput) (*domain.FHIRAllergyIntoleranceRelayPayload, error) { - return &domain.FHIRAllergyIntoleranceRelayPayload{}, nil + return &domain.FHIRAllergyIntoleranceRelayPayload{ + Resource: &domain.FHIRAllergyIntolerance{ + ID: new(string), + Text: &domain.FHIRNarrative{}, + Reaction: []*domain.FHIRAllergyintoleranceReaction{ + { + ID: new(string), + Substance: &domain.FHIRCodeableConcept{}, + Manifestation: []*domain.FHIRCodeableConcept{ + { + ID: new(string), + Coding: []*domain.FHIRCoding{}, + Text: gofakeit.Name(), + }, + }, + }, + }, + Meta: &domain.FHIRMeta{}, + Extension: []*domain.FHIRExtension{}, + }, + }, nil }, MockUpdateFHIRAllergyIntoleranceFn: func(ctx context.Context, input domain.FHIRAllergyIntoleranceInput) (*domain.FHIRAllergyIntoleranceRelayPayload, error) { return &domain.FHIRAllergyIntoleranceRelayPayload{}, nil @@ -376,7 +397,19 @@ func NewFHIRMock() *FHIRMock { return true, nil }, MockGetFHIRPatientFn: func(ctx context.Context, id string) (*domain.FHIRPatientRelayPayload, error) { - return &domain.FHIRPatientRelayPayload{}, nil + patientID := uuid.New().String() + patientName := gofakeit.Name() + return &domain.FHIRPatientRelayPayload{ + Resource: &domain.FHIRPatient{ + ID: &patientID, + Name: []*domain.FHIRHumanName{ + { + Given: []*string{&patientName}, + }, + }, + }, + HasOpenEpisodes: false, + }, nil }, MockDeleteFHIRPatientFn: func(ctx context.Context, id string) (bool, error) { return true, nil diff --git a/pkg/clinical/infrastructure/infrastructure.go b/pkg/clinical/infrastructure/infrastructure.go index ee195d15..c011f99d 100644 --- a/pkg/clinical/infrastructure/infrastructure.go +++ b/pkg/clinical/infrastructure/infrastructure.go @@ -6,7 +6,6 @@ import ( "net/http" "net/url" - "github.com/savannahghi/clinical/pkg/clinical/application/common" "github.com/savannahghi/clinical/pkg/clinical/application/extensions" "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub" "github.com/savannahghi/clinical/pkg/clinical/repository" @@ -38,10 +37,8 @@ func NewInfrastructureInteractor( ext extensions.BaseExtension, fhir repository.FHIR, openconceptlab ServiceOCL, + mycarehub mycarehub.IServiceMyCareHub, ) Infrastructure { - myCareHubClient := common.NewInterServiceClient("mycarehub", ext) - mycarehub := mycarehub.NewServiceMyCareHub(myCareHubClient, ext) - return Infrastructure{ fhir, openconceptlab, diff --git a/pkg/clinical/infrastructure/services/mycarehub/mock/service_mock.go b/pkg/clinical/infrastructure/services/mycarehub/mock/service_mock.go index 5dc04ec8..a0500acf 100644 --- a/pkg/clinical/infrastructure/services/mycarehub/mock/service_mock.go +++ b/pkg/clinical/infrastructure/services/mycarehub/mock/service_mock.go @@ -2,7 +2,9 @@ package mock import ( "context" + "time" + "github.com/brianvoe/gofakeit" "github.com/savannahghi/clinical/pkg/clinical/domain" ) @@ -26,15 +28,24 @@ type FakeMyCareHubService struct { func NewFakeMyCareHubServiceMock() *FakeMyCareHubService { return &FakeMyCareHubService{ MockUserProfileFn: func(ctx context.Context, userID string) (*domain.User, error) { + dob := time.Now() return &domain.User{ ID: new(string), - Username: "", - UserType: "", - Name: "", - Gender: "", + Username: gofakeit.Username(), + UserType: "STAFF", + Name: gofakeit.Name(), + Gender: "MALE", Active: false, - Flavour: "", + Flavour: "PRO", Avatar: "", + Contacts: &domain.Contact{ + ID: new(string), + ContactType: "PHONE", + ContactValue: gofakeit.PhoneFormatted(), + Active: true, + OptedIn: true, + }, + DateOfBirth: &dob, }, nil }, MockAddFHIRIDToPatientProfileFn: func( diff --git a/pkg/clinical/infrastructure/services/pubsub/service_test.go b/pkg/clinical/infrastructure/services/pubsub/service_test.go index d7982cdf..a9a2a35a 100644 --- a/pkg/clinical/infrastructure/services/pubsub/service_test.go +++ b/pkg/clinical/infrastructure/services/pubsub/service_test.go @@ -13,6 +13,7 @@ import ( "github.com/savannahghi/clinical/pkg/clinical/infrastructure" fhir "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare" dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset" + "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub" "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab" pubsubmessaging "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/pubsub" "github.com/savannahghi/serverutils" @@ -50,8 +51,10 @@ func InitializeTestPubSub(t *testing.T) (*pubsubmessaging.ServicePubSubMessaging baseExtension := extensions.NewBaseExtensionImpl() fhir := fhir.NewFHIRStoreImpl(repo) ocl := openconceptlab.NewServiceOCL() + myCareHubClient := common.NewInterServiceClient("mycarehub", baseExtension) + mycarehub := mycarehub.NewServiceMyCareHub(myCareHubClient, baseExtension) - infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl) + infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl, mycarehub) pubSub, err := pubsubmessaging.NewServicePubSubMessaging( ctx, pubSubClient, diff --git a/pkg/clinical/presentation/config.go b/pkg/clinical/presentation/config.go index 6d321339..0524b242 100644 --- a/pkg/clinical/presentation/config.go +++ b/pkg/clinical/presentation/config.go @@ -13,10 +13,12 @@ import ( "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/savannahghi/authutils" + "github.com/savannahghi/clinical/pkg/clinical/application/common" "github.com/savannahghi/clinical/pkg/clinical/application/extensions" "github.com/savannahghi/clinical/pkg/clinical/infrastructure" fhir "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare" "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset" + "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub" "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab" pubsubmessaging "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/pubsub" "github.com/savannahghi/clinical/pkg/clinical/presentation/graph" @@ -129,8 +131,10 @@ func Router(ctx context.Context) (*gin.Engine, error) { repo := fhirdataset.NewFHIRRepository(ctx, hsv, project, datasetID, datasetLocation, fhirStoreID) fhir := fhir.NewFHIRStoreImpl(repo) ocl := openconceptlab.NewServiceOCL() + myCareHubClient := common.NewInterServiceClient("mycarehub", baseExtension) + mycarehub := mycarehub.NewServiceMyCareHub(myCareHubClient, baseExtension) - infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl) + infrastructure := infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl, mycarehub) usecases := usecases.NewUsecasesInteractor(infrastructure) handlers := rest.NewPresentationHandlers(usecases) diff --git a/pkg/clinical/usecases/clinical/fhir_test.go b/pkg/clinical/usecases/clinical/fhir_test.go new file mode 100644 index 00000000..9234d165 --- /dev/null +++ b/pkg/clinical/usecases/clinical/fhir_test.go @@ -0,0 +1,68 @@ +package clinical_test + +import ( + "context" + "testing" + + "github.com/google/uuid" + fakeExtMock "github.com/savannahghi/clinical/pkg/clinical/application/extensions/mock" + "github.com/savannahghi/clinical/pkg/clinical/domain" + "github.com/savannahghi/clinical/pkg/clinical/infrastructure" + fakeFHIRMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/mock" + fakeMyCarehubMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub/mock" + fakeOCLMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab/mock" + clinicalUsecase "github.com/savannahghi/clinical/pkg/clinical/usecases/clinical" +) + +func TestUseCasesClinicalImpl_FindOrganizationByID(t *testing.T) { + ctx := context.Background() + type args struct { + ctx context.Context + organizationID string + } + tests := []struct { + name string + args args + want *domain.FHIROrganizationRelayPayload + wantErr bool + }{ + { + name: "Happy Case - Successfully find organisation by ID", + args: args{ + ctx: ctx, + organizationID: uuid.New().String(), + }, + wantErr: false, + }, + { + name: "Sad Case - missing organisation ID", + args: args{ + ctx: ctx, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fakeExt := fakeExtMock.NewFakeBaseExtensionMock() + fakeFHIR := fakeFHIRMock.NewFHIRMock() + fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() + + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) + u := clinicalUsecase.NewUseCasesClinicalImpl(infra) + + got, err := u.FindOrganizationByID(tt.args.ctx, tt.args.organizationID) + if (err != nil) != tt.wantErr { + t.Errorf("UseCasesClinicalImpl.FindOrganizationByID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !tt.wantErr { + if got == nil { + t.Errorf("expected a response but got %v", got) + return + } + } + }) + } +} diff --git a/pkg/clinical/usecases/clinical/mock/fhir_mock.go b/pkg/clinical/usecases/clinical/mock/fhir_mock.go index d7c6b0da..38c21c06 100644 --- a/pkg/clinical/usecases/clinical/mock/fhir_mock.go +++ b/pkg/clinical/usecases/clinical/mock/fhir_mock.go @@ -3,65 +3,24 @@ package mock import ( "context" - "github.com/google/uuid" "github.com/savannahghi/clinical/pkg/clinical/domain" ) // FHIRUsecaseMock struct implements mocks of FHIR methods. type FHIRUsecaseMock struct { - MockFindOrganizationByIDFn func(ctx context.Context, organisationID string) (*domain.FHIROrganizationRelayPayload, error) - MockCreateFHIRObservationFn func(ctx context.Context, input domain.FHIRObservationInput) (*domain.FHIRObservationRelayPayload, error) - MockCreateFHIRAllergyIntoleranceFn func(ctx context.Context, input domain.FHIRAllergyIntoleranceInput) (*domain.FHIRAllergyIntoleranceRelayPayload, error) - MockCreateFHIRMedicationStatementFn func(ctx context.Context, input domain.FHIRMedicationStatementInput) (*domain.FHIRMedicationStatementRelayPayload, error) - MockCreateFHIROrganizationFn func(ctx context.Context, input domain.FHIROrganizationInput) (*domain.FHIROrganizationRelayPayload, error) + MockFindOrganizationByIDFn func(ctx context.Context, organisationID string) (*domain.FHIROrganizationRelayPayload, error) } // NewFHIRUsecaseMock initializes a new instance of FHIR mock func NewFHIRUsecaseMock() *FHIRUsecaseMock { - UUID := uuid.New().String() - return &FHIRUsecaseMock{ - - MockCreateFHIROrganizationFn: func(ctx context.Context, input domain.FHIROrganizationInput) (*domain.FHIROrganizationRelayPayload, error) { - return &domain.FHIROrganizationRelayPayload{ - Resource: &domain.FHIROrganization{ - ID: &UUID, - }, - }, nil - }, - MockCreateFHIRAllergyIntoleranceFn: func(ctx context.Context, input domain.FHIRAllergyIntoleranceInput) (*domain.FHIRAllergyIntoleranceRelayPayload, error) { - return &domain.FHIRAllergyIntoleranceRelayPayload{}, nil - }, - - MockCreateFHIRObservationFn: func(ctx context.Context, input domain.FHIRObservationInput) (*domain.FHIRObservationRelayPayload, error) { - return &domain.FHIRObservationRelayPayload{}, nil - }, - MockFindOrganizationByIDFn: func(ctx context.Context, organisationID string) (*domain.FHIROrganizationRelayPayload, error) { return &domain.FHIROrganizationRelayPayload{}, nil }, - MockCreateFHIRMedicationStatementFn: func(ctx context.Context, input domain.FHIRMedicationStatementInput) (*domain.FHIRMedicationStatementRelayPayload, error) { - return &domain.FHIRMedicationStatementRelayPayload{}, nil - }, } } -// CreateFHIROrganization is a mock implementation of CreateFHIROrganization method -func (fh *FHIRUsecaseMock) CreateFHIROrganization(ctx context.Context, input domain.FHIROrganizationInput) (*domain.FHIROrganizationRelayPayload, error) { - return fh.MockCreateFHIROrganizationFn(ctx, input) -} - -// CreateFHIRAllergyIntolerance is a mock implementation of CreateFHIRAllergyIntolerance method -func (fh *FHIRUsecaseMock) CreateFHIRAllergyIntolerance(ctx context.Context, input domain.FHIRAllergyIntoleranceInput) (*domain.FHIRAllergyIntoleranceRelayPayload, error) { - return fh.MockCreateFHIRAllergyIntoleranceFn(ctx, input) -} - // FindOrganizationByID is a mock implementation of FindOrganizationByID method func (fh *FHIRUsecaseMock) FindOrganizationByID(ctx context.Context, organizationID string) (*domain.FHIROrganizationRelayPayload, error) { return fh.MockFindOrganizationByIDFn(ctx, organizationID) } - -// CreateFHIRMedicationStatement is a mock implementation of CreateFHIRMedicationStatement method -func (fh *FHIRUsecaseMock) CreateFHIRMedicationStatement(ctx context.Context, input domain.FHIRMedicationStatementInput) (*domain.FHIRMedicationStatementRelayPayload, error) { - return fh.MockCreateFHIRMedicationStatementFn(ctx, input) -} diff --git a/pkg/clinical/usecases/clinical/mock/patient_mock.go b/pkg/clinical/usecases/clinical/mock/patient_mock.go index 8bf044ad..01ac04ee 100644 --- a/pkg/clinical/usecases/clinical/mock/patient_mock.go +++ b/pkg/clinical/usecases/clinical/mock/patient_mock.go @@ -2,81 +2,20 @@ package mock import ( "context" - "fmt" + "github.com/savannahghi/clinical/pkg/clinical/application/dto" "github.com/savannahghi/clinical/pkg/clinical/domain" - "github.com/savannahghi/scalarutils" ) // FakeClinical .... type FakeClinical struct { - MockRegisterPatientFn func(ctx context.Context, input domain.SimplePatientRegistrationInput) (*domain.PatientPayload, error) - MockFindPatientByIDFn func(ctx context.Context, id string) (*domain.PatientPayload, error) - MockGetMedicalDataFn func(ctx context.Context, patientID string) (*domain.MedicalData, error) + MockGetMedicalDataFn func(ctx context.Context, patientID string) (*domain.MedicalData, error) + MockCreatePubsubPatientFn func(ctx context.Context, payload dto.CreatePatientPubSubMessage) error } // NewFakeClinicalMock ... func NewFakeClinicalMock() *FakeClinical { return &FakeClinical{ - - MockRegisterPatientFn: func(ctx context.Context, input domain.SimplePatientRegistrationInput) (*domain.PatientPayload, error) { - return &domain.PatientPayload{ - PatientRecord: &domain.FHIRPatient{}, - HasOpenEpisodes: false, - OpenEpisodes: []*domain.FHIREpisodeOfCare{}, - }, nil - }, - - MockFindPatientByIDFn: func(ctx context.Context, id string) (*domain.PatientPayload, error) { - UUID := "FDGFSDG33222" - PatientRef := fmt.Sprintf("Patient/%s", UUID) - gender := domain.PatientContactGenderEnumMale - return &domain.PatientPayload{ - PatientRecord: &domain.FHIRPatient{ - ID: &UUID, - Text: &domain.FHIRNarrative{}, - Identifier: []*domain.FHIRIdentifier{}, - Active: new(bool), - Name: []*domain.FHIRHumanName{}, - Telecom: []*domain.FHIRContactPoint{}, - Gender: (*domain.PatientGenderEnum)(&gender), - BirthDate: &scalarutils.Date{}, - DeceasedBoolean: new(bool), - DeceasedDateTime: &scalarutils.Date{}, - Address: []*domain.FHIRAddress{}, - MaritalStatus: &domain.FHIRCodeableConcept{}, - MultipleBirthBoolean: new(bool), - MultipleBirthInteger: new(string), - Photo: []*domain.FHIRAttachment{}, - Contact: []*domain.FHIRPatientContact{}, - Communication: []*domain.FHIRPatientCommunication{}, - GeneralPractitioner: []*domain.FHIRReference{}, - ManagingOrganization: &domain.FHIRReference{}, - Link: []*domain.FHIRPatientLink{}, - }, - HasOpenEpisodes: false, - OpenEpisodes: []*domain.FHIREpisodeOfCare{ - { - ID: &UUID, - Text: &domain.FHIRNarrative{}, - Identifier: []*domain.FHIRIdentifier{}, - StatusHistory: []*domain.FHIREpisodeofcareStatushistory{}, - Type: []*domain.FHIRCodeableConcept{}, - Diagnosis: []*domain.FHIREpisodeofcareDiagnosis{}, - Patient: &domain.FHIRReference{ - ID: &UUID, - Reference: &PatientRef, - }, - ManagingOrganization: &domain.FHIRReference{}, - Period: &domain.FHIRPeriod{}, - ReferralRequest: []*domain.FHIRReference{}, - CareManager: &domain.FHIRReference{}, - Team: []*domain.FHIRReference{}, - Account: []*domain.FHIRReference{}, - }, - }, - }, nil - }, MockGetMedicalDataFn: func(ctx context.Context, patientID string) (*domain.MedicalData, error) { return &domain.MedicalData{ Regimen: []*domain.FHIRMedicationStatement{}, @@ -87,20 +26,18 @@ func NewFakeClinicalMock() *FakeClinical { CD4Count: []*domain.FHIRObservation{}, }, nil }, + MockCreatePubsubPatientFn: func(ctx context.Context, payload dto.CreatePatientPubSubMessage) error { + return nil + }, } } -// RegisterPatient ... -func (f *FakeClinical) RegisterPatient(ctx context.Context, input domain.SimplePatientRegistrationInput) (*domain.PatientPayload, error) { - return f.MockRegisterPatientFn(ctx, input) -} - -// FindPatientByID ... -func (f *FakeClinical) FindPatientByID(ctx context.Context, id string) (*domain.PatientPayload, error) { - return f.MockFindPatientByIDFn(ctx, id) -} - // GetMedicalData ... func (f *FakeClinical) GetMedicalData(ctx context.Context, patientID string) (*domain.MedicalData, error) { return f.MockGetMedicalDataFn(ctx, patientID) } + +// CreatePubsubPatient mocks the implementation os creating a user using pubsub +func (f *FakeClinical) CreatePubsubPatient(ctx context.Context, payload dto.CreatePatientPubSubMessage) error { + return f.MockCreatePubsubPatientFn(ctx, payload) +} diff --git a/pkg/clinical/usecases/clinical/patient_unit_test.go b/pkg/clinical/usecases/clinical/patient_unit_test.go index 08a556e7..caf47c06 100644 --- a/pkg/clinical/usecases/clinical/patient_unit_test.go +++ b/pkg/clinical/usecases/clinical/patient_unit_test.go @@ -10,6 +10,7 @@ import ( "github.com/savannahghi/clinical/pkg/clinical/domain" "github.com/savannahghi/clinical/pkg/clinical/infrastructure" fakeFHIRMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/mock" + fakeMyCarehubMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub/mock" fakeOCLMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab/mock" clinicalUsecase "github.com/savannahghi/clinical/pkg/clinical/usecases/clinical" "github.com/savannahghi/firebasetools" @@ -76,8 +77,9 @@ func TestUsecaseImpl_CreateFHIROrganization_Unittest(t *testing.T) { FakeExt := fakeExtMock.NewFakeBaseExtensionMock() Fakefhir := fakeFHIRMock.NewFHIRMock() FakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() - infra := infrastructure.NewInfrastructureInteractor(FakeExt, Fakefhir, FakeOCL) + infra := infrastructure.NewInfrastructureInteractor(FakeExt, Fakefhir, FakeOCL, fakeMCH) u := clinicalUsecase.NewUseCasesClinicalImpl(infra) if tt.name == "Sad case" { @@ -209,8 +211,9 @@ func TestClinicalUseCaseImpl_PatientTimeline(t *testing.T) { fakeExt := fakeExtMock.NewFakeBaseExtensionMock() fakeFHIR := fakeFHIRMock.NewFHIRMock() fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() - infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL) + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) u := clinicalUsecase.NewUseCasesClinicalImpl(infra) if tt.name == "Sad Case - Fail to search allergy intolerance" { @@ -368,8 +371,9 @@ func TestClinicalUseCaseImpl_PatientHealthTimeline(t *testing.T) { FakeExt := fakeExtMock.NewFakeBaseExtensionMock() Fakefhir := fakeFHIRMock.NewFHIRMock() FakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() - infra := infrastructure.NewInfrastructureInteractor(FakeExt, Fakefhir, FakeOCL) + infra := infrastructure.NewInfrastructureInteractor(FakeExt, Fakefhir, FakeOCL, fakeMCH) u := clinicalUsecase.NewUseCasesClinicalImpl(infra) got, err := u.PatientHealthTimeline(tt.args.ctx, tt.args.input) @@ -496,8 +500,9 @@ func TestClinicalUseCaseImpl_GetMedicalData(t *testing.T) { fakeExt := fakeExtMock.NewFakeBaseExtensionMock() fakeFHIR := fakeFHIRMock.NewFHIRMock() fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() - infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL) + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) u := clinicalUsecase.NewUseCasesClinicalImpl(infra) if tt.name == "Sad Case - Fail to search medication statement" { diff --git a/pkg/clinical/usecases/clinical/pubsub_helpers.go b/pkg/clinical/usecases/clinical/pubsub_helpers.go index 8d55b66d..528580fe 100644 --- a/pkg/clinical/usecases/clinical/pubsub_helpers.go +++ b/pkg/clinical/usecases/clinical/pubsub_helpers.go @@ -173,7 +173,7 @@ func (c *UseCasesClinicalImpl) ComposeAllergyIntoleranceInput(ctx context.Contex } // create the allergy reaction - var reaction *domain.FHIRAllergyintoleranceReactionInput + var reaction domain.FHIRAllergyintoleranceReactionInput // reaction manifestation is required // @@ -216,7 +216,7 @@ func (c *UseCasesClinicalImpl) ComposeAllergyIntoleranceInput(ctx context.Contex } // add allergy reaction - allergy.Reaction = append(allergy.Reaction, reaction) + allergy.Reaction = append(allergy.Reaction, &reaction) return allergy, nil } diff --git a/pkg/clinical/usecases/clinical/pubsub_test.go b/pkg/clinical/usecases/clinical/pubsub_test.go new file mode 100644 index 00000000..c52bf7fb --- /dev/null +++ b/pkg/clinical/usecases/clinical/pubsub_test.go @@ -0,0 +1,849 @@ +package clinical_test + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/brianvoe/gofakeit" + "github.com/google/uuid" + "github.com/savannahghi/clinical/pkg/clinical/application/dto" + fakeExtMock "github.com/savannahghi/clinical/pkg/clinical/application/extensions/mock" + "github.com/savannahghi/clinical/pkg/clinical/domain" + "github.com/savannahghi/clinical/pkg/clinical/infrastructure" + fakeFHIRMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/mock" + fakeMyCarehubMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub/mock" + fakeOCLMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab/mock" + clinicalUsecase "github.com/savannahghi/clinical/pkg/clinical/usecases/clinical" + "github.com/savannahghi/firebasetools" +) + +func TestUseCasesClinicalImpl_CreatePubsubPatient(t *testing.T) { + ctx := context.Background() + type args struct { + ctx context.Context + payload dto.CreatePatientPubSubMessage + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Happy Case - Successfully create pubsub patient", + args: args{ + ctx: ctx, + payload: dto.CreatePatientPubSubMessage{ + ID: uuid.New().String(), + Active: true, + Counselled: false, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - Fail to get user profile", + args: args{ + ctx: ctx, + payload: dto.CreatePatientPubSubMessage{ + ID: uuid.New().String(), + Active: true, + Counselled: false, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - Fail to check patient existence", + args: args{ + ctx: ctx, + payload: dto.CreatePatientPubSubMessage{ + ID: uuid.New().String(), + Active: true, + Counselled: false, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - Patient already exists", + args: args{ + ctx: ctx, + payload: dto.CreatePatientPubSubMessage{ + ID: uuid.New().String(), + Active: true, + Counselled: false, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - Fail to create patient", + args: args{ + ctx: ctx, + payload: dto.CreatePatientPubSubMessage{ + ID: uuid.New().String(), + Active: true, + Counselled: false, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - Fail to add FHIR ID to profile", + args: args{ + ctx: ctx, + payload: dto.CreatePatientPubSubMessage{ + ID: uuid.New().String(), + Active: true, + Counselled: false, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - invalid patient input", + args: args{ + ctx: ctx, + payload: dto.CreatePatientPubSubMessage{ + ID: uuid.New().String(), + Active: true, + Counselled: false, + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fakeExt := fakeExtMock.NewFakeBaseExtensionMock() + fakeFHIR := fakeFHIRMock.NewFHIRMock() + fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() + + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) + u := clinicalUsecase.NewUseCasesClinicalImpl(infra) + + if tt.name == "Sad Case - Fail to get user profile" { + fakeMCH.MockUserProfileFn = func(ctx context.Context, userID string) (*domain.User, error) { + return nil, fmt.Errorf("failed to get user profile") + } + } + + if tt.name == "Sad Case - Fail to check patient existence" { + fakeFHIR.MockSearchFHIRPatientFn = func(ctx context.Context, searchParams string) (*domain.PatientConnection, error) { + return nil, fmt.Errorf("fail to get a user") + } + } + + if tt.name == "Sad Case - Patient already exists" { + fakeFHIR.MockSearchFHIRPatientFn = func(ctx context.Context, searchParams string) (*domain.PatientConnection, error) { + return &domain.PatientConnection{ + Edges: []*domain.PatientEdge{ + { + Cursor: "", + Node: &domain.FHIRPatient{}, + HasOpenEpisodes: false, + }, + { + Cursor: "", + Node: &domain.FHIRPatient{}, + HasOpenEpisodes: false, + }, + }, + PageInfo: &firebasetools.PageInfo{}, + }, nil + } + } + + if tt.name == "Sad Case - Fail to create patient" { + fakeFHIR.MockCreateFHIRPatientFn = func(ctx context.Context, input domain.FHIRPatientInput) (*domain.PatientPayload, error) { + return nil, fmt.Errorf("failed to create patient") + } + } + + if tt.name == "Sad Case - Fail to add FHIR ID to profile" { + fakeMCH.MockAddFHIRIDToPatientProfileFn = func(ctx context.Context, fhirID, clientID string) error { + return fmt.Errorf("failed to add fhir ID to profile") + } + } + + if tt.name == "Sad Case - invalid patient input" { + fakeMCH.MockUserProfileFn = func(ctx context.Context, userID string) (*domain.User, error) { + dob := time.Now() + return &domain.User{ + ID: new(string), + Username: gofakeit.Username(), + UserType: "STAFF", + Name: gofakeit.Name(), + Gender: "MALE", + Active: false, + Flavour: "PRO", + Avatar: "", + Contacts: &domain.Contact{ + ID: new(string), + ContactType: "PHONE", + ContactValue: "gofakeit.PhoneFormatted()", + Active: true, + OptedIn: true, + }, + DateOfBirth: &dob, + }, nil + } + } + + if err := u.CreatePubsubPatient(tt.args.ctx, tt.args.payload); (err != nil) != tt.wantErr { + t.Errorf("UseCasesClinicalImpl.CreatePubsubPatient() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUseCasesClinicalImpl_CreatePubsubOrganization(t *testing.T) { + ctx := context.Background() + type args struct { + ctx context.Context + data dto.CreateFacilityPubSubMessage + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Happy Case - Successfully create pubsub organization", + args: args{ + ctx: ctx, + data: dto.CreateFacilityPubSubMessage{ + ID: new(string), + Name: "Test Facility", + Code: 0, + Phone: "", + Active: false, + County: "", + Description: "", + }, + }, + wantErr: false, + }, + { + name: "Sad Case - Fail to create pubsub organization", + args: args{ + ctx: ctx, + data: dto.CreateFacilityPubSubMessage{ + ID: new(string), + Name: "Test Facility", + Code: 0, + Phone: "", + Active: false, + County: "", + Description: "", + }, + }, + wantErr: true, + }, + { + name: "Sad Case - Fail to add fhir id to facility", + args: args{ + ctx: ctx, + data: dto.CreateFacilityPubSubMessage{ + ID: new(string), + Name: "Test Facility", + Code: 0, + Phone: "", + Active: false, + County: "", + Description: "", + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fakeExt := fakeExtMock.NewFakeBaseExtensionMock() + fakeFHIR := fakeFHIRMock.NewFHIRMock() + fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() + + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) + u := clinicalUsecase.NewUseCasesClinicalImpl(infra) + + if tt.name == "Sad Case - Fail to create pubsub organization" { + fakeFHIR.MockCreateFHIROrganizationFn = func(ctx context.Context, input domain.FHIROrganizationInput) (*domain.FHIROrganizationRelayPayload, error) { + return nil, fmt.Errorf("failed to create organization") + } + } + + if tt.name == "Sad Case - Fail to add fhir id to facility" { + fakeMCH.MockAddFHIRIDToFacilityFn = func(ctx context.Context, fhirID, facilityID string) error { + return fmt.Errorf("failed to add fhir ID to facility") + } + } + + if err := u.CreatePubsubOrganization(tt.args.ctx, tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UseCasesClinicalImpl.CreatePubsubOrganization() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUseCasesClinicalImpl_CreatePubsubVitals(t *testing.T) { + ctx := context.Background() + type args struct { + ctx context.Context + data dto.CreateVitalSignPubSubMessage + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Happy Case - Successfully create pubsub vitals", + args: args{ + ctx: ctx, + data: dto.CreateVitalSignPubSubMessage{ + PatientID: uuid.NewString(), + OrganizationID: "", + Name: "", + ConceptID: new(string), + Value: "", + Date: time.Time{}, + }, + }, + wantErr: false, + }, + { + name: "Happy Case - Successfully create pubsub vitals - available organizationID", + args: args{ + ctx: ctx, + data: dto.CreateVitalSignPubSubMessage{ + PatientID: uuid.NewString(), + OrganizationID: uuid.NewString(), + Name: "", + ConceptID: new(string), + Value: "", + Date: time.Time{}, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - Fail to find patient", + args: args{ + ctx: ctx, + data: dto.CreateVitalSignPubSubMessage{ + PatientID: uuid.NewString(), + OrganizationID: uuid.NewString(), + Name: "", + ConceptID: new(string), + Value: "", + Date: time.Time{}, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - Fail to find organisation using org ID", + args: args{ + ctx: ctx, + data: dto.CreateVitalSignPubSubMessage{ + PatientID: uuid.NewString(), + OrganizationID: uuid.NewString(), + Name: "", + ConceptID: new(string), + Value: "", + Date: time.Time{}, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - Fail to create observation", + args: args{ + ctx: ctx, + data: dto.CreateVitalSignPubSubMessage{ + PatientID: uuid.NewString(), + OrganizationID: uuid.NewString(), + Name: "", + ConceptID: new(string), + Value: "", + Date: time.Time{}, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - fail to get ciel concept", + args: args{ + ctx: ctx, + data: dto.CreateVitalSignPubSubMessage{ + PatientID: uuid.NewString(), + OrganizationID: uuid.NewString(), + Name: "", + ConceptID: new(string), + Value: "", + Date: time.Time{}, + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fakeExt := fakeExtMock.NewFakeBaseExtensionMock() + fakeFHIR := fakeFHIRMock.NewFHIRMock() + fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() + + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) + u := clinicalUsecase.NewUseCasesClinicalImpl(infra) + + if tt.name == "Sad Case - Fail to find patient" { + fakeFHIR.MockGetFHIRPatientFn = func(ctx context.Context, id string) (*domain.FHIRPatientRelayPayload, error) { + return nil, fmt.Errorf("failed to get fhir patient") + } + } + + if tt.name == "Sad Case - Fail to find organisation using org ID" { + fakeFHIR.MockFindOrganizationByIDFn = func(ctx context.Context, organisationID string) (*domain.FHIROrganizationRelayPayload, error) { + return nil, fmt.Errorf("failed to find org by ID") + } + } + + if tt.name == "Sad Case - Fail to create observation" { + fakeFHIR.MockCreateFHIRObservationFn = func(ctx context.Context, input domain.FHIRObservationInput) (*domain.FHIRObservationRelayPayload, error) { + return nil, fmt.Errorf("failed to create observation") + } + } + + if tt.name == "Sad Case - fail to get ciel concept" { + fakeOCL.MockGetConceptFn = func(ctx context.Context, org, source, concept string, includeMappings, includeInverseMappings bool) (map[string]interface{}, error) { + return nil, fmt.Errorf("failed to get concept") + } + } + + if err := u.CreatePubsubVitals(tt.args.ctx, tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UseCasesClinicalImpl.CreatePubsubVitals() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUseCasesClinicalImpl_CreatePubsubTestResult(t *testing.T) { + ctx := context.Background() + type args struct { + ctx context.Context + data dto.CreatePatientTestResultPubSubMessage + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Happy Case - Successfully create test result", + args: args{ + ctx: ctx, + data: dto.CreatePatientTestResultPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: "", + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Result: dto.TestResult{}, + }, + }, + wantErr: false, + }, + { + name: "Happy Case - Successfully create test result - with organisation ID", + args: args{ + ctx: ctx, + data: dto.CreatePatientTestResultPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Result: dto.TestResult{}, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - fail to get fhir patient", + args: args{ + ctx: ctx, + data: dto.CreatePatientTestResultPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: "", + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Result: dto.TestResult{}, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - fail to get organisation", + args: args{ + ctx: ctx, + data: dto.CreatePatientTestResultPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Result: dto.TestResult{}, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - fail to create observation", + args: args{ + ctx: ctx, + data: dto.CreatePatientTestResultPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Result: dto.TestResult{}, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - fail to get ciel concept", + args: args{ + ctx: ctx, + data: dto.CreatePatientTestResultPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Result: dto.TestResult{}, + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fakeExt := fakeExtMock.NewFakeBaseExtensionMock() + fakeFHIR := fakeFHIRMock.NewFHIRMock() + fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() + + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) + u := clinicalUsecase.NewUseCasesClinicalImpl(infra) + + if tt.name == "Sad Case - fail to get fhir patient" { + fakeFHIR.MockGetFHIRPatientFn = func(ctx context.Context, id string) (*domain.FHIRPatientRelayPayload, error) { + return nil, fmt.Errorf("failed to get fhir patient") + } + } + + if tt.name == "Sad Case - fail to get organisation" { + fakeFHIR.MockFindOrganizationByIDFn = func(ctx context.Context, organisationID string) (*domain.FHIROrganizationRelayPayload, error) { + return nil, fmt.Errorf("failed to find org by ID") + } + } + + if tt.name == "Sad Case - fail to create observation" { + fakeFHIR.MockCreateFHIRObservationFn = func(ctx context.Context, input domain.FHIRObservationInput) (*domain.FHIRObservationRelayPayload, error) { + return nil, fmt.Errorf("failed to create observation") + } + } + + if tt.name == "Sad Case - fail to get ciel concept" { + fakeOCL.MockGetConceptFn = func(ctx context.Context, org, source, concept string, includeMappings, includeInverseMappings bool) (map[string]interface{}, error) { + return nil, fmt.Errorf("failed to get concept") + } + } + + if err := u.CreatePubsubTestResult(tt.args.ctx, tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UseCasesClinicalImpl.CreatePubsubTestResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUseCasesClinicalImpl_CreatePubsubMedicationStatement(t *testing.T) { + ctx := context.Background() + conceptID := "12345" + type args struct { + ctx context.Context + data dto.CreateMedicationPubSubMessage + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Happy Case - Successfully create medication statement", + args: args{ + ctx: ctx, + data: dto.CreateMedicationPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: "", + Name: "", + ConceptID: &conceptID, + Date: time.Time{}, + Value: "", + Drug: &dto.MedicationDrug{ + ConceptID: &conceptID, + }, + }, + }, + wantErr: false, + }, + { + name: "Happy Case - Successfully create medication statement - with organisation ID", + args: args{ + ctx: ctx, + data: dto.CreateMedicationPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: &conceptID, + Date: time.Time{}, + Value: "", + Drug: &dto.MedicationDrug{ + ConceptID: &conceptID, + }, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - Fail to get patient", + args: args{ + ctx: ctx, + data: dto.CreateMedicationPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: &conceptID, + Date: time.Time{}, + Value: "", + Drug: &dto.MedicationDrug{ + ConceptID: &conceptID, + }, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - fail to get organisation", + args: args{ + ctx: ctx, + data: dto.CreateMedicationPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: &conceptID, + Date: time.Time{}, + Value: "", + Drug: &dto.MedicationDrug{ + ConceptID: &conceptID, + }, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - fail to get medication statement", + args: args{ + ctx: ctx, + data: dto.CreateMedicationPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: &conceptID, + Date: time.Time{}, + Value: "", + Drug: &dto.MedicationDrug{ + ConceptID: &conceptID, + }, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - fail to get ciel concept", + args: args{ + ctx: ctx, + data: dto.CreateMedicationPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: uuid.New().String(), + Name: "", + ConceptID: &conceptID, + Date: time.Time{}, + Value: "", + Drug: &dto.MedicationDrug{ + ConceptID: &conceptID, + }, + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fakeExt := fakeExtMock.NewFakeBaseExtensionMock() + fakeFHIR := fakeFHIRMock.NewFHIRMock() + fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() + + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) + u := clinicalUsecase.NewUseCasesClinicalImpl(infra) + + if tt.name == "Sad Case - Fail to get patient" { + fakeFHIR.MockGetFHIRPatientFn = func(ctx context.Context, id string) (*domain.FHIRPatientRelayPayload, error) { + return nil, fmt.Errorf("failed to get fhir patient") + } + } + + if tt.name == "Sad Case - fail to get organisation" { + fakeFHIR.MockFindOrganizationByIDFn = func(ctx context.Context, organisationID string) (*domain.FHIROrganizationRelayPayload, error) { + return nil, fmt.Errorf("failed to find org by ID") + } + } + + if tt.name == "Sad Case - fail to get medication statement" { + fakeFHIR.MockCreateFHIRMedicationStatementFn = func(ctx context.Context, input domain.FHIRMedicationStatementInput) (*domain.FHIRMedicationStatementRelayPayload, error) { + return nil, fmt.Errorf("failed to create medication statement") + } + } + + if tt.name == "Sad Case - fail to get ciel concept" { + fakeOCL.MockGetConceptFn = func(ctx context.Context, org, source, concept string, includeMappings, includeInverseMappings bool) (map[string]interface{}, error) { + return nil, fmt.Errorf("failed to get concept") + } + } + + if err := u.CreatePubsubMedicationStatement(tt.args.ctx, tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UseCasesClinicalImpl.CreatePubsubMedicationStatement() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestUseCasesClinicalImpl_CreatePubsubAllergyIntolerance(t *testing.T) { + ctx := context.Background() + type args struct { + ctx context.Context + data dto.CreatePatientAllergyPubSubMessage + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Happy Case - Successfully create allergy intolerance", + args: args{ + ctx: ctx, + data: dto.CreatePatientAllergyPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: "", + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Reaction: dto.AllergyReaction{}, + Severity: dto.AllergySeverity{}, + }, + }, + wantErr: false, + }, + { + name: "Sad Case - Fail to get user profile", + args: args{ + ctx: ctx, + data: dto.CreatePatientAllergyPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: "", + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Reaction: dto.AllergyReaction{}, + Severity: dto.AllergySeverity{}, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - Fail to create allergy intolerance", + args: args{ + ctx: ctx, + data: dto.CreatePatientAllergyPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: "", + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Reaction: dto.AllergyReaction{}, + Severity: dto.AllergySeverity{}, + }, + }, + wantErr: true, + }, + { + name: "Sad Case - fail to get ciel concept", + args: args{ + ctx: ctx, + data: dto.CreatePatientAllergyPubSubMessage{ + PatientID: uuid.New().String(), + OrganizationID: "", + Name: "", + ConceptID: new(string), + Date: time.Time{}, + Reaction: dto.AllergyReaction{}, + Severity: dto.AllergySeverity{}, + }, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fakeExt := fakeExtMock.NewFakeBaseExtensionMock() + fakeFHIR := fakeFHIRMock.NewFHIRMock() + fakeOCL := fakeOCLMock.NewFakeOCLMock() + fakeMCH := fakeMyCarehubMock.NewFakeMyCareHubServiceMock() + + infra := infrastructure.NewInfrastructureInteractor(fakeExt, fakeFHIR, fakeOCL, fakeMCH) + u := clinicalUsecase.NewUseCasesClinicalImpl(infra) + + if tt.name == "Sad Case - Fail to get user profile" { + fakeFHIR.MockGetFHIRPatientFn = func(ctx context.Context, id string) (*domain.FHIRPatientRelayPayload, error) { + return nil, fmt.Errorf("failed to get patient") + } + } + + if tt.name == "Sad Case - Fail to create allergy intolerance" { + fakeFHIR.MockCreateFHIRAllergyIntoleranceFn = func(ctx context.Context, input domain.FHIRAllergyIntoleranceInput) (*domain.FHIRAllergyIntoleranceRelayPayload, error) { + return nil, fmt.Errorf("failed to create allergy intolerance") + } + } + + if tt.name == "Sad Case - fail to get ciel concept" { + fakeOCL.MockGetConceptFn = func(ctx context.Context, org, source, concept string, includeMappings, includeInverseMappings bool) (map[string]interface{}, error) { + return nil, fmt.Errorf("failed to get concept") + } + } + + if err := u.CreatePubsubAllergyIntolerance(tt.args.ctx, tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UseCasesClinicalImpl.CreatePubsubAllergyIntolerance() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/clinical/usecases/config_test.go b/pkg/clinical/usecases/config_test.go index fc424a3c..3b9edf5f 100644 --- a/pkg/clinical/usecases/config_test.go +++ b/pkg/clinical/usecases/config_test.go @@ -5,12 +5,14 @@ import ( "os" "testing" + "github.com/savannahghi/clinical/pkg/clinical/application/common" "github.com/savannahghi/clinical/pkg/clinical/application/extensions" extensionMock "github.com/savannahghi/clinical/pkg/clinical/application/extensions/mock" "github.com/savannahghi/clinical/pkg/clinical/infrastructure" fhir "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare" dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset" fhirRepoMock "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset/mock" + "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/mycarehub" "github.com/savannahghi/clinical/pkg/clinical/infrastructure/services/openconceptlab" "github.com/savannahghi/clinical/pkg/clinical/presentation/interactor" "github.com/savannahghi/clinical/pkg/clinical/usecases" @@ -91,8 +93,10 @@ func InitializeTestInfrastructure(ctx context.Context) (infrastructure.Infrastru baseExtension := extensions.NewBaseExtensionImpl() fhir := fhir.NewFHIRStoreImpl(repo) ocl := openconceptlab.NewServiceOCL() + myCareHubClient := common.NewInterServiceClient("mycarehub", baseExtension) + mycarehub := mycarehub.NewServiceMyCareHub(myCareHubClient, baseExtension) - return infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl), nil + return infrastructure.NewInfrastructureInteractor(baseExtension, fhir, ocl, mycarehub), nil } func InitializeFakeTestlInteractor(ctx context.Context) (usecases.Interactor, error) {