Skip to content

Commit

Permalink
feat: Adds test mock support with Mockery (#287)
Browse files Browse the repository at this point in the history
Co-authored-by: Wojciech Trocki <w.trocki@mongodb.com>
  • Loading branch information
lantoli and wtrocki committed Mar 25, 2024
1 parent 6991b85 commit bcf87d0
Show file tree
Hide file tree
Showing 55 changed files with 57,786 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/workflows/autoupdate-preview.yaml
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
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
@@ -0,0 +1,11 @@
with-expecter: true
disable-version-string: true
dir: ../mockadmin
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
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
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
@@ -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)

// 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
Expand Up @@ -6,3 +6,11 @@ require (
github.com/go-test/deep v1.1.0
github.com/mongodb-forks/digest v1.0.5
)

require (
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
@@ -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=

0 comments on commit bcf87d0

Please sign in to comment.