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

Autogenerate mockery mocks #2215

Merged
merged 19 commits into from Jun 5, 2019
Merged
Changes from 6 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -293,6 +293,7 @@ get_gotools: .check_gopath.stamp .get_gotools.stamp ## Get golang tools
.get_gotools.stamp:
go install golang.org/x/lint/golint
go install golang.org/x/tools/cmd/goimports
go install github.com/vektra/mockery/cmd/mockery
This conversation was marked as resolved by mkrump

This comment has been minimized.

Copy link
@chrisgilmerproj

chrisgilmerproj Jun 3, 2019

Contributor

I think this would be better to put in pkg/tools/tools.go? The reason these other tools are here is because they were for pre-commit. But I think even they are going to be removed soon.

This comment has been minimized.

Copy link
@mkrump

mkrump Jun 3, 2019

Author Contributor

👍 didn't know about pkg/tools/tools.go

touch .get_gotools.stamp

.PHONY: server_deps
@@ -401,6 +402,10 @@ else
endif
endif

.PHONY: generate_mocks
generate_mocks: get_gotools
go generate $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)

This comment has been minimized.

Copy link
@mikena-truss

mikena-truss Jun 3, 2019

Contributor

If this is slow, at some point, we can get fancy and only update changed files since the last run. I think this is ok for now though.


.PHONY: server_test
server_test: server_deps server_generate db_test_reset db_test_migrate ## Run server unit tests
# Don't run tests in /cmd or /pkg/gen & pass `-short` to exclude long running tests
@@ -2,10 +2,17 @@

[Mockery](https://github.com/vektra/mockery) provides the ability to easily generate mocks for golang interfaces. It removes the boilerplate coding required to use mocks.

To generate a mock for testing purposes, you must use the mockery command line interface tool to do so.
*In Golang, mocks can only be created on interfaces - not structs. So, it is important that for whichever mock you are trying to generate, it should correspond to the appropriate interface.*

`$GOPATH/bin/mockery -name <nameOfInterface> -dir $GOPATH/src/github.com/transcom/mymove/<directoryInterfaceIsLocatedIn>`
## Autogenerating mocks with `go generate`

After you run this command a mock, corresponding to the interface name, will be created in the `/mocks` directory.
The `make generate_mocks` command will regenerate mocks for all interfaces tagged with the appropriate `go generate` command. To add an interface to the list of autogenerated mocks, just add a
`go:generate` comment like below and update the name with your interface name.

*In Golang, mocks can only be created on interfaces - not structs. So, it is important that for whichever mock you are trying to generate, it should correspond to the appropriate interface.*
```.go
// AccessCodeClaimer is the service object interface for ValidateAccessCode
//go:generate mockery -name AccessCodeClaimer
type AccessCodeClaimer interface {
ClaimAccessCode(code string, serviceMemberID uuid.UUID) (*models.AccessCode, *validate.Errors, error)
}
```
@@ -10,7 +10,7 @@ import (
"github.com/gofrs/uuid"
"github.com/stretchr/testify/mock"

"github.com/transcom/mymove/mocks"
"github.com/transcom/mymove/pkg/services/mocks"

accesscodeops "github.com/transcom/mymove/pkg/gen/restapi/apioperations/accesscode"
"github.com/transcom/mymove/pkg/handlers"
@@ -12,11 +12,11 @@ import (

"github.com/transcom/mymove/pkg/auth"

"github.com/transcom/mymove/mocks"
"github.com/transcom/mymove/pkg/gen/apimessages"
sitop "github.com/transcom/mymove/pkg/gen/restapi/apioperations/storage_in_transits"
"github.com/transcom/mymove/pkg/handlers"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services/mocks"
"github.com/transcom/mymove/pkg/testdatagen"
)

@@ -8,11 +8,13 @@ import (
)

// AccessCodeValidator is the service object interface for ValidateAccessCode
//go:generate mockery -name AccessCodeValidator
type AccessCodeValidator interface {
ValidateAccessCode(code string, moveType models.SelectedMoveType) (*models.AccessCode, bool, error)
}

// AccessCodeClaimer is the service object interface for ValidateAccessCode
//go:generate mockery -name AccessCodeClaimer
type AccessCodeClaimer interface {
ClaimAccessCode(code string, serviceMemberID uuid.UUID) (*models.AccessCode, *validate.Errors, error)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -14,11 +14,13 @@ import (
)

// FileStorer is an interface for fileStorer implementation
//go:generate mockery -name FileStorer
type FileStorer interface {
Create(string) (afero.File, error)
}

// FormFiller is an interface for formFiller implementation
//go:generate mockery -name FormFiller
type FormFiller interface {
AppendPage(io.ReadSeeker, map[string]paperworkforms.FieldPos, interface{}) error
Output(io.Writer) error
@@ -5,10 +5,10 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"

"github.com/transcom/mymove/mocks"
"github.com/transcom/mymove/pkg/models"
paperworkforms "github.com/transcom/mymove/pkg/paperwork"
"github.com/transcom/mymove/pkg/services"
"github.com/transcom/mymove/pkg/services/paperwork/mocks"
"github.com/transcom/mymove/pkg/testdatagen"
"github.com/transcom/mymove/pkg/testdatagen/scenario"
)
File renamed without changes.
File renamed without changes.
@@ -12,51 +12,61 @@ import (
)

// StorageInTransitCreator is the service object for creating a Storage In Transit
//go:generate mockery -name StorageInTransitCreator
type StorageInTransitCreator interface {
CreateStorageInTransit(storageInTransitPayload apimessages.StorageInTransit, shipmentID uuid.UUID, session *auth.Session) (*models.StorageInTransit, *validate.Errors, error)
}

// StorageInTransitsIndexer is the service object for retrieving a collection of StorageInTransits
//go:generate mockery -name StorageInTransitsIndexer
type StorageInTransitsIndexer interface {
IndexStorageInTransits(shipmentID uuid.UUID, session *auth.Session) ([]models.StorageInTransit, error)
}

// StorageInTransitApprover is the service object for approving a Storage In Transit
//go:generate mockery -name StorageInTransitApprover
type StorageInTransitApprover interface {
ApproveStorageInTransit(payload apimessages.StorageInTransitApprovalPayload, shipmentID uuid.UUID, session *auth.Session, storageInTransitID uuid.UUID) (*models.StorageInTransit, *validate.Errors, error)
}

// StorageInTransitDenier is the service object for denying a Storage In Transit
//go:generate mockery -name StorageInTransitDenier
type StorageInTransitDenier interface {
DenyStorageInTransit(payload apimessages.StorageInTransitDenialPayload, shipmentID uuid.UUID, session *auth.Session, storageInTransitID uuid.UUID) (*models.StorageInTransit, *validate.Errors, error)
}

// StorageInTransitInSITPlacer is the object for placing a Storage In Transit into SIT status
//go:generate mockery -name StorageInTransitInSITPlacer
type StorageInTransitInSITPlacer interface {
PlaceIntoSITStorageInTransit(payload apimessages.StorageInTransitInSitPayload, shipmentID uuid.UUID, session *auth.Session, storageInTransitID uuid.UUID) (*models.StorageInTransit, *validate.Errors, error)
}

// StorageInTransitDeliverer is the service object for delivering a Storage In Transit
//go:generate mockery -name StorageInTransitDeliverer
type StorageInTransitDeliverer interface {
DeliverStorageInTransit(shipmentID uuid.UUID, session *auth.Session, storageInTransitID uuid.UUID) (*models.StorageInTransit, *validate.Errors, error)
}

// StorageInTransitReleaser is the service object for releasing a Storage In Transit
//go:generate mockery -name StorageInTransitReleaser
type StorageInTransitReleaser interface {
ReleaseStorageInTransit(payload apimessages.StorageInTransitReleasePayload, shipmentID uuid.UUID, session *auth.Session, storageInTransitID uuid.UUID) (*models.StorageInTransit, *validate.Errors, error)
}

// StorageInTransitDeleter is the service object for deleting a Storage In Transit
//go:generate mockery -name StorageInTransitDeleter
type StorageInTransitDeleter interface {
DeleteStorageInTransit(shipmentID uuid.UUID, storageInTransitID uuid.UUID, session *auth.Session) error
}

// StorageInTransitPatcher is the service object for editing a Storage In Transit
//go:generate mockery -name StorageInTransitPatcher
type StorageInTransitPatcher interface {
PatchStorageInTransit(payload apimessages.StorageInTransit, shipmentID uuid.UUID, storageInTransitID uuid.UUID, session *auth.Session) (*models.StorageInTransit, *validate.Errors, error)
}

// StorageInTransitByIDFetcher is the service object for fetching a Storage In Transit
//go:generate mockery -name StorageInTransitByIDFetcher
type StorageInTransitByIDFetcher interface {
FetchStorageInTransitByID(storageInTransitID uuid.UUID, shipmentID uuid.UUID, session *auth.Session) (*models.StorageInTransit, error)
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.