From 1045fb9697db505f5fd1ca0ebe4be8b6479df981 Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Sun, 19 Jan 2025 01:54:15 +0100 Subject: [PATCH] [Internal] Delete examples/mocking module (#1126) ## What changes are proposed in this pull request? This PR deletes the `examples/mocking` directory as it is no longer maintained. This mocker example is built on the assumption that every `service` has `WithImpl()` that is used to set the stubs for every `service`. But this is no longer the case. This mocker is not even able to compile. ## How is this tested? N/A --- examples/mocking/.gitignore | 4 --- examples/mocking/README.md | 62 ----------------------------------- examples/mocking/dbfs_test.go | 48 --------------------------- examples/mocking/go.mod | 11 ------- 4 files changed, 125 deletions(-) delete mode 100644 examples/mocking/.gitignore delete mode 100644 examples/mocking/README.md delete mode 100644 examples/mocking/dbfs_test.go delete mode 100644 examples/mocking/go.mod diff --git a/examples/mocking/.gitignore b/examples/mocking/.gitignore deleted file mode 100644 index 8915b6bcf..000000000 --- a/examples/mocking/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -go.sum -vendor -mocks -mocks/* \ No newline at end of file diff --git a/examples/mocking/README.md b/examples/mocking/README.md deleted file mode 100644 index 1151ff8b2..000000000 --- a/examples/mocking/README.md +++ /dev/null @@ -1,62 +0,0 @@ -# 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`](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: - -```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 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. \ No newline at end of file diff --git a/examples/mocking/dbfs_test.go b/examples/mocking/dbfs_test.go deleted file mode 100644 index 34898c0fb..000000000 --- a/examples/mocking/dbfs_test.go +++ /dev/null @@ -1,48 +0,0 @@ -package mocking - -import ( - "context" - "mocking/mocks" - "strings" - "testing" - - "github.com/golang/mock/gomock" - _ "github.com/golang/mock/mockgen/model" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/dbfs" -) - -//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 - -func TestDbfsHighLevelAPI(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockDbfs := mocks.NewMockDbfsService(ctrl) - - ctx := context.Background() - mockDbfs.EXPECT().Create(gomock.Any(), gomock.Eq(dbfs.Create{ - Path: "/a/b/c", - Overwrite: true, - })).Return(&dbfs.CreateResponse{ - Handle: 123, - }, nil) - mockDbfs.EXPECT().AddBlock(gomock.Any(), gomock.Eq(dbfs.AddBlock{ - Handle: 123, - Data: "YWJj", - })) - mockDbfs.EXPECT().Close(gomock.Any(), gomock.Eq(dbfs.Close{ - Handle: 123, - })) - - w, err := databricks.NewWorkspaceClient() - require.NoError(t, err) - - w.Dbfs.WithImpl(mockDbfs) - - err = w.Dbfs.Overwrite(ctx, "/a/b/c", strings.NewReader("abc")) - assert.NoError(t, err) -} diff --git a/examples/mocking/go.mod b/examples/mocking/go.mod deleted file mode 100644 index c2366a257..000000000 --- a/examples/mocking/go.mod +++ /dev/null @@ -1,11 +0,0 @@ -module mocking - -go 1.18 - -require ( - github.com/databricks/databricks-sdk-go v0.0.0 - github.com/golang/mock v1.6.0 - github.com/stretchr/testify v1.8.1 -) - -replace github.com/databricks/databricks-sdk-go v0.0.0 => ../..