-
Notifications
You must be signed in to change notification settings - Fork 45
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
Moving experimental to testkit #876
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,25 @@ | ||
# Interoperability with `gomock` | ||
# Using the testkit | ||
|
||
When developing large applications, you find yourself in need of mocking APIs. For Go, there's [`gomock`](https://github.com/golang/mock) framework for code generating testing mocks. In this small example, we'll show how to use `gomock` with Databricks SDK for Go. | ||
To test your application it is often necessary to mock external dependency. | ||
The SDK contains a testkit you can use for such a purpose, so you don't have to | ||
use the [`gomock`](https://github.com/golang/mock) framework for code generating | ||
testing mocks. | ||
|
||
Please read through [`dbfs_test.go`](dbfs_test.go) test example. | ||
|
||
## Declaring which mocks to generate | ||
|
||
```go | ||
//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 of `mockgen` command | ||
* `-package=mocks` instructs to generate mocks in the `mocks` package | ||
* `-destination=mocks/dbfs.go` instructs to create `dbfs.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: | ||
The entrypoint `mocks.NewMockWorkspaceClient` exposes an API that allows you | ||
to retrieve a mock object, on which you can set expectations like so: | ||
|
||
```go | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
client := mocks.NewMockWorkspaceClient(t) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please limit the changes only to readme. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should we do with the example? I think it refers to a service that has been renamed from dbfs to files maybe? |
||
client.GetMockJobsAPI().On("ListAll", mock.Anything, jobs.ListJobsRequest{Limit: 10}).Return( | ||
[]jobs.BaseJob{ | ||
{ | ||
JobId: 64, | ||
}, | ||
{ | ||
JobId: 65, | ||
}, | ||
}, nil, | ||
) | ||
``` | ||
|
||
## Mocking individual methods with `gomock` | ||
|
||
Every actual method call must be mocked for the test to pass: | ||
|
||
```go | ||
mockDbfs := mocks.NewMockDbfsService(ctrl) | ||
mockDbfs.EXPECT().Create(gomock.Any(), gomock.Eq(dbfs.Create{ | ||
Path: "/a/b/c", | ||
Overwrite: true, | ||
})).Return(&dbfs.CreateResponse{ | ||
Handle: 123, | ||
}, nil) | ||
``` | ||
|
||
## Testing idioms with Databricks SDK for Go | ||
|
||
You can stub out the HTTP request flow with `httpclient/fixtures.MappingTransport` and `httpclient/fixtures.SliceTransport`. | ||
|
||
Every service has a public `WithImpl()` method, that you can use to set the stubs for every service that is called in the unit tests. | ||
|
||
```go | ||
w := workspaces.New(&databricks.Config{ | ||
HTTPTransport: fixtures.MappingTransport{ | ||
//... | ||
} | ||
}) | ||
w.Dbfs.WithImpl(mockDbfs) | ||
``` | ||
|
||
## Running this example | ||
|
||
1. Run `go mod tidy` in this folder to create `go.sum` file to pick dependency versions. | ||
2. Run `go mod vendor` to download dependencies into `vendor/` directory. | ||
3. Run `go generate ./...` to create `mocks/` directory. | ||
4. Run `go test ./...` to invoke tests with mocks. | ||
Please read through [`listjobs_test.go`](listjobs_test.go) test example. |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package mocking | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
_ "github.com/golang/mock/mockgen/model" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
// "github.com/databricks/databricks-sdk-go/service/dbfs" | ||
"github.com/databricks/databricks-sdk-go/service/jobs" | ||
"github.com/databricks/databricks-sdk-go/testkit/mocks" | ||
) | ||
|
||
func TestListJobs(t *testing.T) { | ||
client := mocks.NewMockWorkspaceClient(t) | ||
client.GetMockJobsAPI().On("ListAll", mock.Anything, jobs.ListJobsRequest{Limit: 10}).Return( | ||
[]jobs.BaseJob{ | ||
{ | ||
JobId: 64, | ||
}, | ||
{ | ||
JobId: 65, | ||
}, | ||
}, nil, | ||
) | ||
jobs, err := client.WorkspaceClient.Jobs.ListAll(context.Background(), jobs.ListJobsRequest{Limit: 10}) | ||
require.NoError(t, err) | ||
require.Equal(t, jobs[0].JobId, int64(64)) | ||
require.Equal(t, jobs[1].JobId, int64(65)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
you should not rename this to anything else just yet.