Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update and standardize facility mock test #61

Merged
merged 1 commit into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
firebase.google.com/go v3.13.0+incompatible
github.com/99designs/gqlgen v0.13.0
github.com/GoogleCloudPlatform/cloudsql-proxy v1.26.0
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/casbin/casbin/v2 v2.31.3
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
Expand All @@ -38,5 +39,4 @@ require (
gorm.io/datatypes v1.0.2
gorm.io/driver/postgres v1.1.2
gorm.io/gorm v1.21.16
moul.io/http2curl v1.0.0 // indirect
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
github.com/googleapis/gax-go/v2 v2.1.1 h1:dp3bWCh+PPO1zjRRiCSczJav13sBvG4UhNyVTa1KqdU=
github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0eJc8R6ouapiM=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
Expand Down Expand Up @@ -474,7 +473,6 @@ github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
Expand Down Expand Up @@ -683,9 +681,7 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
Expand Down Expand Up @@ -1332,8 +1328,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
149 changes: 63 additions & 86 deletions pkg/mycarehub/infrastructure/database/postgres/gorm/mock/gorm_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,137 +4,114 @@ import (
"context"
"strconv"

"github.com/brianvoe/gofakeit"
"github.com/google/uuid"
"github.com/savannahghi/enumutils"
"github.com/savannahghi/mycarehub/pkg/mycarehub/application/enums"
"github.com/savannahghi/mycarehub/pkg/mycarehub/infrastructure/database/postgres/gorm"
)

// GormMock struct implements mocks of `gorm's`internal methods.
//
// This mock struct should be separate from our own internal methods.
type GormMock struct {
GetOrCreateFacilityFn func(ctx context.Context, facility *gorm.Facility) (*gorm.Facility, error)
RetrieveFacilityFn func(ctx context.Context, id *string, isActive bool) (*gorm.Facility, error)
RetrieveFacilityByMFLCodeFn func(ctx context.Context, MFLCode string, isActive bool) (*gorm.Facility, error)
GetFacilitiesFn func(ctx context.Context) ([]gorm.Facility, error)
DeleteFacilityFn func(ctx context.Context, mfl_code string) (bool, error)
RegisterClientFn func(ctx context.Context, userInput *gorm.User, clientInput *gorm.ClientProfile) (*gorm.ClientUserProfile, error)
MockGetOrCreateFacilityFn func(ctx context.Context, facility *gorm.Facility) (*gorm.Facility, error)
MockRetrieveFacilityFn func(ctx context.Context, id *string, isActive bool) (*gorm.Facility, error)
MockRetrieveFacilityByMFLCodeFn func(ctx context.Context, MFLCode string, isActive bool) (*gorm.Facility, error)
MockGetFacilitiesFn func(ctx context.Context) ([]gorm.Facility, error)
MockDeleteFacilityFn func(ctx context.Context, mfl_code string) (bool, error)
MockRegisterClientFn func(ctx context.Context, userInput *gorm.User, clientInput *gorm.ClientProfile) (*gorm.ClientUserProfile, error)
}

// NewGormMock initializes a new instance of `GormMock` then mocking the case of success.
//
// This initialization initializes all the good cases of your mock tests. i.e all success cases should be defined here.
func NewGormMock() *GormMock {

/*
In this section, you find commonly shared success case structs for mock tests
*/

facilityID := uuid.New().String()
name := gofakeit.Name()
code := "KN001"
county := "Kanairo"
description := gofakeit.HipsterSentence(15)

facility := &gorm.Facility{
FacilityID: &facilityID,
Name: name,
Code: code,
Active: strconv.FormatBool(true),
County: county,
Description: description,
}

var facilities []gorm.Facility
facilities = append(facilities, *facility)

clientProfile := &gorm.ClientUserProfile{
User: &gorm.User{
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Username: gofakeit.Username(),
MiddleName: gofakeit.Name(),
DisplayName: gofakeit.BeerAlcohol(),
Gender: enumutils.GenderMale,
},
Client: &gorm.ClientProfile{
ClientType: enums.ClientTypeOvc,
},
}

return &GormMock{
RegisterClientFn: func(ctx context.Context, userInput *gorm.User, clientInput *gorm.ClientProfile) (*gorm.ClientUserProfile, error) {
return &gorm.ClientUserProfile{
User: &gorm.User{
FirstName: "FirstName",
LastName: "Last Name",
Username: "User Name",
MiddleName: userInput.MiddleName,
DisplayName: "Display Name",
Gender: enumutils.GenderMale,
},
Client: &gorm.ClientProfile{
ClientType: enums.ClientTypeOvc,
},
}, nil
MockRegisterClientFn: func(ctx context.Context, userInput *gorm.User, clientInput *gorm.ClientProfile) (*gorm.ClientUserProfile, error) {
return clientProfile, nil
},

GetOrCreateFacilityFn: func(ctx context.Context, facility *gorm.Facility) (*gorm.Facility, error) {
id := uuid.New().String()
name := "Kanairo One"
code := "KN001"
county := "Kanairo"
description := "This is just for mocking"
return &gorm.Facility{
FacilityID: &id,
Name: name,
Code: code,
Active: strconv.FormatBool(true),
County: county,
Description: description,
}, nil
MockGetOrCreateFacilityFn: func(ctx context.Context, facility *gorm.Facility) (*gorm.Facility, error) {
return facility, nil
},

RetrieveFacilityFn: func(ctx context.Context, id *string, isActive bool) (*gorm.Facility, error) {
facilityID := uuid.New().String()
name := "Kanairo One"
code := "KN001"
county := "Kanairo"
description := "This is just for mocking"
return &gorm.Facility{
FacilityID: &facilityID,
Name: name,
Code: code,
Active: strconv.FormatBool(true),
County: county,
Description: description,
}, nil
MockRetrieveFacilityFn: func(ctx context.Context, id *string, isActive bool) (*gorm.Facility, error) {

return facility, nil
},
GetFacilitiesFn: func(ctx context.Context) ([]gorm.Facility, error) {
var facilities []gorm.Facility
facilityID := uuid.New().String()
name := "Kanairo One"
code := "KN001"
county := "Kanairo"
description := "This is just for mocking"
facilities = append(facilities, gorm.Facility{
FacilityID: &facilityID,
Name: name,
Code: code,
Active: strconv.FormatBool(true),
County: county,
Description: description,
})
MockGetFacilitiesFn: func(ctx context.Context) ([]gorm.Facility, error) {
return facilities, nil
},

DeleteFacilityFn: func(ctx context.Context, mfl_code string) (bool, error) {
MockDeleteFacilityFn: func(ctx context.Context, mfl_code string) (bool, error) {
return true, nil
},

RetrieveFacilityByMFLCodeFn: func(ctx context.Context, MFLCode string, isActive bool) (*gorm.Facility, error) {
facilityID := uuid.New().String()
name := "Kanairo One"
code := "KN001"
county := "Kanairo"
description := "This is just for mocking"
return &gorm.Facility{
FacilityID: &facilityID,
Name: name,
Code: code,
Active: strconv.FormatBool(true),
County: county,
Description: description,
}, nil
MockRetrieveFacilityByMFLCodeFn: func(ctx context.Context, MFLCode string, isActive bool) (*gorm.Facility, error) {
return facility, nil
},
}
}

// GetOrCreateFacility mocks the implementation of `gorm's` GetOrCreateFacility method.
func (gm *GormMock) GetOrCreateFacility(ctx context.Context, facility *gorm.Facility) (*gorm.Facility, error) {
return gm.GetOrCreateFacilityFn(ctx, facility)
return gm.MockGetOrCreateFacilityFn(ctx, facility)
}

// RetrieveFacility mocks the implementation of `gorm's` RetrieveFacility method.
func (gm *GormMock) RetrieveFacility(ctx context.Context, id *string, isActive bool) (*gorm.Facility, error) {
return gm.RetrieveFacilityFn(ctx, id, isActive)
return gm.MockRetrieveFacilityFn(ctx, id, isActive)
}

// RetrieveFacilityByMFLCode mocks the implementation of `gorm's` RetrieveFacility method.
func (gm *GormMock) RetrieveFacilityByMFLCode(ctx context.Context, MFLCode string, isActive bool) (*gorm.Facility, error) {
return gm.RetrieveFacilityByMFLCodeFn(ctx, MFLCode, isActive)
return gm.MockRetrieveFacilityByMFLCodeFn(ctx, MFLCode, isActive)
}

// GetFacilities mocks the implementation of `gorm's` GetFacilities method.
func (gm *GormMock) GetFacilities(ctx context.Context) ([]gorm.Facility, error) {
return gm.GetFacilitiesFn(ctx)
return gm.MockGetFacilitiesFn(ctx)
}

// DeleteFacility mocks the implementation of DeleteFacility method.
func (gm *GormMock) DeleteFacility(ctx context.Context, mflcode string) (bool, error) {
return gm.DeleteFacilityFn(ctx, mflcode)
return gm.MockDeleteFacilityFn(ctx, mflcode)
}

// RegisterClient mocks the implementation of RegisterClient method
Expand All @@ -143,5 +120,5 @@ func (gm *GormMock) RegisterClient(
userInput *gorm.User,
clientInput *gorm.ClientProfile,
) (*gorm.ClientUserProfile, error) {
return gm.RegisterClientFn(ctx, userInput, clientInput)
return gm.MockRegisterClientFn(ctx, userInput, clientInput)
}
Loading