Skip to content

Commit

Permalink
Interoperability with gomock (#131)
Browse files Browse the repository at this point in the history
This PR addresses two major issues
* `mockgen` cannot yet mock higher-level wrappers, like `*AndWait`
because of generics are not yet supported by gomock:
golang/mock#621
* fundamentally fixes #87

# Interoperability with `gomock`

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.

Please read through
[`dbfs_test.go`](https://github.com/databricks/databricks-sdk-go/pull/131/files#diff-ac4ba8227927778705ac100663a6f04a314e590b0d0f19c1699fe63ae62b801e)
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:

```go
ctrl := gomock.NewController(t)
defer ctrl.Finish()
```

## 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 auth with the `databricks.NewMockConfig(nil)`
helper function. Every service has a public property with the name of
the service plus `Service` suffix. You have to manually set the stubs
for every service that is called in unit tests.

```go
w := workspaces.New(databricks.NewMockConfig(nil))
w.Dbfs.DbfsService = 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.
  • Loading branch information
nfx authored Nov 23, 2022
1 parent cf6bddd commit 3e307bb
Show file tree
Hide file tree
Showing 103 changed files with 6,212 additions and 3,618 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ During the Beta period, you must clone and then reference this repository locall
)
func init() {
databricks.WithProduct("your-product", "0.0.1")
databricks.WithProduct("awesome-product", "0.0.1")
}
func main() {
Expand Down Expand Up @@ -191,7 +191,7 @@ import (
)
func init() {
databricks.WithProduct("your-product", "0.0.1")
databricks.WithProduct("awesome-product", "0.0.1")
}
func main() {
Expand Down Expand Up @@ -274,7 +274,7 @@ import (
)
func init() {
databricks.WithProduct("your-product", "0.0.1")
databricks.WithProduct("awesome-product", "0.0.1")
}
func main() {
Expand Down Expand Up @@ -357,7 +357,7 @@ import (
)
func init() {
databricks.WithProduct("your-product", "0.0.1")
databricks.WithProduct("awesome-product", "0.0.1")
}
func main() {
Expand Down Expand Up @@ -426,7 +426,7 @@ import (
)
func init() {
databricks.WithProduct("your-product", "0.0.1")
databricks.WithProduct("awesome-product", "0.0.1")
}
func main() {
Expand Down Expand Up @@ -470,7 +470,7 @@ import (
)
func init() {
databricks.WithProduct("your-product", "0.0.1")
databricks.WithProduct("awesome-product", "0.0.1")
}
func main() {
Expand Down
235 changes: 205 additions & 30 deletions accounts/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions databricks/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package databricks
import "net/http"

func NewMockConfig(auth func(r *http.Request) error) *Config {
if auth == nil {
auth = func(r *http.Request) error {
return nil
}
}
return &Config{
AuthType: "mock",
auth: auth,
Expand Down
Loading

0 comments on commit 3e307bb

Please sign in to comment.