-
Notifications
You must be signed in to change notification settings - Fork 42
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
Interoperability with gomock
#131
Conversation
gomock
generatorgomock
* cannot yet mock higher-level wrappers, like `*AndWait` because of generics are not yet supported by gomock (even in v1.7.0-rc.1)
@@ -53,22 +53,22 @@ func TestMwsAccNetworks(t *testing.T) { | |||
if !a.Config.IsAccountsClient() || !a.Config.IsAws() { | |||
t.SkipNow() | |||
} | |||
netw, err := a.Networks.CreateNetworkConfig(ctx, deployment.CreateNetworkRequest{ | |||
netw, err := a.NetworkConfigurations.CreateNetworkConfig(ctx, deployment.CreateNetworkRequest{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR: the stuttering here can/should be removed: CreateNetworkConfig
-> Create
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are certain risks with it, like "automatic name clashes" - I'll see if it's possible
type ReposAPI struct { | ||
client *client.DatabricksClient | ||
// impl contains low-level REST API interface, that could be overridden | ||
// through WithImpl(ReposService) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WithImpl -> SetImpl, "with" implies its scoped somehow (same as #167)
This PR addresses two major issues
mockgen
cannot yet mock higher-level wrappers, like*AndWait
because of generics are not yet supported by gomock: Mockgen: Support generating mock for interfaces with generics golang/mock#621Interoperability with
gomock
When developing large applications, you find yourself in need of mocking APIs. For Go, there's
gomock
framework for code generating testing mocks. In this small example, we'll show how to usegomock
with Databricks SDK for Go.Please read through
dbfs_test.go
test example.Declaring which mocks to generate
//go:generate go run github.com/golang/mock/mockgen@latest -package=mocks -destination=mocks/dbfs.go github.com/databricks/databricks-sdk-go/service/dbfs DbfsService
go run github.com/golang/mock/mockgen@latest
downloads and executes the latest version ofmockgen
command-package=mocks
instructs to generate mocks in themocks
package-destination=mocks/dbfs.go
instructs to createdbfs.go
file with mock stubs.github.com/databricks/databricks-sdk-go/service/dbfs
tells which Databricks package to look services in.DbfsService
tells which services to generate mocks for.Initializing
gomock
Every test needs the following preamble:
Mocking individual methods with
gomock
Every actual method call must be mocked for the test to pass:
Testing idioms with Databricks SDK for Go
You can stub out the auth with the
databricks.NewMockConfig(nil)
helper function. Every service has a public property with the name of the service plusService
suffix. You have to manually set the stubs for every service that is called in unit tests.Running this example
go mod tidy
in this folder to creatego.sum
file to pick dependency versions.go mod vendor
to download dependencies intovendor/
directory.go generate ./...
to createmocks/
directory.go test ./...
to invoke tests with mocks.