Skip to content

Commit

Permalink
tests: add pubsub usecase tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Salaton committed Mar 10, 2023
1 parent 7b43a76 commit 6388283
Show file tree
Hide file tree
Showing 13 changed files with 1,011 additions and 137 deletions.
6 changes: 5 additions & 1 deletion pkg/clinical/application/common/testutils/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions pkg/clinical/infrastructure/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package mock

import (
"context"
"time"

"github.com/brianvoe/gofakeit"
"github.com/savannahghi/clinical/pkg/clinical/domain"
)

Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion pkg/clinical/infrastructure/services/pubsub/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion pkg/clinical/presentation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
68 changes: 68 additions & 0 deletions pkg/clinical/usecases/clinical/fhir_test.go
Original file line number Diff line number Diff line change
@@ -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
}
}
})
}
}
43 changes: 1 addition & 42 deletions pkg/clinical/usecases/clinical/mock/fhir_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6388283

Please sign in to comment.