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

feat: Adds test mock support with Mockery #287

Merged
merged 9 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/autoupdate-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ jobs:
if: steps.verify-changed-files.outputs.files_changed == 'true'
working-directory: ./tools
run: make clean_and_generate
- name: Run mock generation
working-directory: ./tools
if: steps.verify-changed-files.outputs.files_changed == 'true'
run: make generate_mocks
- uses: peter-evans/create-pull-request@v6
if: steps.verify-changed-files.outputs.files_changed == 'true'
env:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/autoupdate-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jobs:
working-directory: ./tools
run: |
npm install && npm run format
- name: Run mock generation
working-directory: ./tools
if: steps.verify-changed-files.outputs.files_changed == 'true'
run: make generate_mocks
- uses: peter-evans/create-pull-request@v6
if: steps.verify-changed-files.outputs.files_changed == 'true'
env:
Expand Down
11 changes: 11 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
with-expecter: true
disable-version-string: true
dir: ../mockadmin
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
outpkg: mockadmin
filename: "{{ .InterfaceName | snakecase }}.go"
mockname: "{{.InterfaceName}}"

packages:
go.mongodb.org/atlas-sdk/v20231115008/admin:
config:
include-regex: ".*Api"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Please refer to the [docs](./docs)

See [examples](./examples)

## Test Support

[Testify Mock](https://pkg.go.dev/github.com/stretchr/testify/mock) files are generated using [Mockery](https://github.com/vektra/mockery) to help to unit test clients using Atlas Go SDK.

See [test example with mocks](./examples/mock/cluster_test.go)

## Contributing

See our [CONTRIBUTING.md](CONTRIBUTING.md) Guide.
Expand Down
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ export MONGODB_ATLAS_PUBLIC_KEY=somekey
export MONGODB_ATLAS_PRIVATE_KEY=some-secret-key-for-gosdkapi
go run ./aws_cluster/aws.go
```


## Running Examples with Mocked Backend

SDK provides mocks using Testify and Mockery.
One of the SDK examples covers usage of the mockery within tests.

```bash
go test ./mock/cluster_test.go
```
40 changes: 40 additions & 0 deletions examples/mock/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package test

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas-sdk/v20231115008/mockadmin"
)

func TestListClusters(t *testing.T) {
// Create mock API.
clusterAPI := mockadmin.NewClustersApi(t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is very good example on how to use mockery but does not fit to example of SDK mocking.

Mocking example should use "go.mongodb.org/atlas-sdk/v20231115008/admin" interface that is replaced by mocked implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make follow up PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand, we have import "go.mongodb.org/atlas-sdk/v20231115008/admin" and the admin interface is being used in the tests:
func MyFunctionCallingListClusters(clusterAPI admin.ClustersApi) (int, error)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies. I got confused by the name: MyFunctionCallingListClusters. LGTM


// Program expectations.
list := &admin.PaginatedAdvancedClusterDescription{
TotalCount: admin.PtrInt(2),
Results: &[]admin.AdvancedClusterDescription{
{StateName: admin.PtrString("IDLE")},
{StateName: admin.PtrString("DELETING")},
},
}
clusterAPI.EXPECT().ListClusters(mock.Anything, mock.Anything).Return(admin.ListClustersApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().ListClustersExecute(mock.Anything).Return(list, &http.Response{StatusCode: 200}, nil)

// Call functions using the API, they won't make real calls to Atlas API.
clusterCount, err := MyFunctionCallingListClusters(clusterAPI)

// Assert expectations.
assert.Nil(t, err)
assert.Equal(t, 2, clusterCount)
}

func MyFunctionCallingListClusters(clusterAPI admin.ClustersApi) (int, error) {
clusters, _, err := clusterAPI.ListClusters(context.Background(), "my_group_id").Execute()
return clusters.GetTotalCount(), err
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ require (
github.com/go-test/deep v1.1.0
github.com/mongodb-forks/digest v1.0.5
)

require (
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/mongodb-forks/digest v1.0.5 h1:EJu3wtLZcA0HCvsZpX5yuD193/sW9tHiNvrEM5apXMk=
github.com/mongodb-forks/digest v1.0.5/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=