Skip to content

Commit bcf87d0

Browse files
lantoliwtrocki
andauthored
feat: Adds test mock support with Mockery (#287)
Co-authored-by: Wojciech Trocki <[email protected]>
1 parent 6991b85 commit bcf87d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+57786
-1
lines changed

.github/workflows/autoupdate-preview.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ jobs:
2929
if: steps.verify-changed-files.outputs.files_changed == 'true'
3030
working-directory: ./tools
3131
run: make clean_and_generate
32+
- name: Run mock generation
33+
working-directory: ./tools
34+
if: steps.verify-changed-files.outputs.files_changed == 'true'
35+
run: make generate_mocks
3236
- uses: peter-evans/create-pull-request@v6
3337
if: steps.verify-changed-files.outputs.files_changed == 'true'
3438
env:

.github/workflows/autoupdate-prod.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ jobs:
5050
working-directory: ./tools
5151
run: |
5252
npm install && npm run format
53+
- name: Run mock generation
54+
working-directory: ./tools
55+
if: steps.verify-changed-files.outputs.files_changed == 'true'
56+
run: make generate_mocks
5357
- uses: peter-evans/create-pull-request@v6
5458
if: steps.verify-changed-files.outputs.files_changed == 'true'
5559
env:

.mockery.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
with-expecter: true
2+
disable-version-string: true
3+
dir: ../mockadmin
4+
outpkg: mockadmin
5+
filename: "{{ .InterfaceName | snakecase }}.go"
6+
mockname: "{{.InterfaceName}}"
7+
8+
packages:
9+
go.mongodb.org/atlas-sdk/v20231115008/admin:
10+
config:
11+
include-regex: ".*Api"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Please refer to the [docs](./docs)
5555

5656
See [examples](./examples)
5757

58+
## Test Support
59+
60+
[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.
61+
62+
See [test example with mocks](./examples/mock/cluster_test.go)
63+
5864
## Contributing
5965

6066
See our [CONTRIBUTING.md](CONTRIBUTING.md) Guide.

examples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ export MONGODB_ATLAS_PUBLIC_KEY=somekey
1010
export MONGODB_ATLAS_PRIVATE_KEY=some-secret-key-for-gosdkapi
1111
go run ./aws_cluster/aws.go
1212
```
13+
14+
15+
## Running Examples with Mocked Backend
16+
17+
SDK provides mocks using Testify and Mockery.
18+
One of the SDK examples covers usage of the mockery within tests.
19+
20+
```bash
21+
go test ./mock/cluster_test.go
22+
```

examples/mock/cluster_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package test
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
10+
"go.mongodb.org/atlas-sdk/v20231115008/admin"
11+
"go.mongodb.org/atlas-sdk/v20231115008/mockadmin"
12+
)
13+
14+
func TestListClusters(t *testing.T) {
15+
// Create mock API.
16+
clusterAPI := mockadmin.NewClustersApi(t)
17+
18+
// Program expectations.
19+
list := &admin.PaginatedAdvancedClusterDescription{
20+
TotalCount: admin.PtrInt(2),
21+
Results: &[]admin.AdvancedClusterDescription{
22+
{StateName: admin.PtrString("IDLE")},
23+
{StateName: admin.PtrString("DELETING")},
24+
},
25+
}
26+
clusterAPI.EXPECT().ListClusters(mock.Anything, mock.Anything).Return(admin.ListClustersApiRequest{ApiService: clusterAPI})
27+
clusterAPI.EXPECT().ListClustersExecute(mock.Anything).Return(list, &http.Response{StatusCode: 200}, nil)
28+
29+
// Call functions using the API, they won't make real calls to Atlas API.
30+
clusterCount, err := MyFunctionCallingListClusters(clusterAPI)
31+
32+
// Assert expectations.
33+
assert.Nil(t, err)
34+
assert.Equal(t, 2, clusterCount)
35+
}
36+
37+
func MyFunctionCallingListClusters(clusterAPI admin.ClustersApi) (int, error) {
38+
clusters, _, err := clusterAPI.ListClusters(context.Background(), "my_group_id").Execute()
39+
return clusters.GetTotalCount(), err
40+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ require (
66
github.com/go-test/deep v1.1.0
77
github.com/mongodb-forks/digest v1.0.5
88
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
github.com/stretchr/objx v0.5.2 // indirect
14+
github.com/stretchr/testify v1.9.0
15+
gopkg.in/yaml.v3 v3.0.1 // indirect
16+
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
24
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
35
github.com/mongodb-forks/digest v1.0.5 h1:EJu3wtLZcA0HCvsZpX5yuD193/sW9tHiNvrEM5apXMk=
46
github.com/mongodb-forks/digest v1.0.5/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
10+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
11+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
12+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
16+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)