|
| 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 | +} |
0 commit comments