-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcluster_test.go
67 lines (55 loc) · 2.17 KB
/
cluster_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package examples
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
"go.mongodb.org/atlas-sdk/v20241113004/mockadmin"
)
func myFunctionCallingListClusters(clusterAPI admin.ClustersApi) (int, error) {
clusters, _, err := clusterAPI.ListClusters(context.Background(), "my_group_id").Execute()
if err != nil {
return 0, err
}
return clusters.GetTotalCount(), err
}
func TestListClusters(t *testing.T) {
// Create mock API.
clusterAPI := mockadmin.NewClustersApi(t)
// Program expectations.
list := &admin.PaginatedClusterDescription20240805{
TotalCount: admin.PtrInt(2),
Results: &[]admin.ClusterDescription20240805{
{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)
clusterAPI.EXPECT().ListClustersExecute(mock.Anything).Return(list, &http.Response{StatusCode: 200}, nil)
}
func TestListClustersErrorMocks(t *testing.T) {
// Create mock API.
clusterAPI := mockadmin.NewClustersApi(t)
apiError := admin.GenericOpenAPIError{}
apiError.SetModel(admin.ApiError{
Detail: admin.PtrString("Error when listing clusters"),
Error: 400,
ErrorCode: "CLUSTERS_UNREACHABLE",
Reason: admin.PtrString("Clusters unreachable"),
})
apiError.SetError("Mocked error")
clusterAPI.EXPECT().ListClusters(mock.Anything, mock.Anything).Return(admin.ListClustersApiRequest{ApiService: clusterAPI})
clusterAPI.EXPECT().ListClustersExecute(mock.Anything).Return(nil, &http.Response{StatusCode: 400}, apiError)
// Call functions using the API, they won't make real calls to Atlas API.
_, err := myFunctionCallingListClusters(clusterAPI)
// Assert expectations.
assert.Equal(t, err, apiError)
}