## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Reviewed-on: #58
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
135 lines
3.7 KiB
Go
135 lines
3.7 KiB
Go
package postgresFlexAlphaFlavor
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
|
|
|
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
|
)
|
|
|
|
type mockRequest struct {
|
|
executeFunc func() (*postgresflex.GetFlavorsResponse, error)
|
|
}
|
|
|
|
func (m *mockRequest) Page(_ int32) postgresflex.ApiGetFlavorsRequestRequest { return m }
|
|
func (m *mockRequest) Size(_ int32) postgresflex.ApiGetFlavorsRequestRequest { return m }
|
|
func (m *mockRequest) Sort(_ postgresflex.FlavorSort) postgresflex.ApiGetFlavorsRequestRequest {
|
|
return m
|
|
}
|
|
func (m *mockRequest) Execute() (*postgresflex.GetFlavorsResponse, error) {
|
|
return m.executeFunc()
|
|
}
|
|
|
|
type mockFlavorsClient struct {
|
|
executeRequest func() postgresflex.ApiGetFlavorsRequestRequest
|
|
}
|
|
|
|
func (m *mockFlavorsClient) GetFlavorsRequest(_ context.Context, _, _ string) postgresflex.ApiGetFlavorsRequestRequest {
|
|
return m.executeRequest()
|
|
}
|
|
|
|
var mockResp = func(page int64) (*postgresflex.GetFlavorsResponse, error) {
|
|
if page == 1 {
|
|
return &postgresflex.GetFlavorsResponse{
|
|
Flavors: &[]postgresflex.ListFlavors{
|
|
{Id: utils.Ptr("flavor-1"), Description: utils.Ptr("first")},
|
|
{Id: utils.Ptr("flavor-2"), Description: utils.Ptr("second")},
|
|
},
|
|
}, nil
|
|
}
|
|
if page == 2 {
|
|
return &postgresflex.GetFlavorsResponse{
|
|
Flavors: &[]postgresflex.ListFlavors{
|
|
{Id: utils.Ptr("flavor-3"), Description: utils.Ptr("three")},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
return &postgresflex.GetFlavorsResponse{
|
|
Flavors: &[]postgresflex.ListFlavors{},
|
|
}, nil
|
|
}
|
|
|
|
func TestGetFlavorsByFilter(t *testing.T) {
|
|
tests := []struct {
|
|
description string
|
|
projectId string
|
|
region string
|
|
mockErr error
|
|
filter func(postgresflex.ListFlavors) bool
|
|
wantCount int
|
|
wantErr bool
|
|
}{
|
|
{
|
|
description: "Success - Get all flavors (2 pages)",
|
|
projectId: "pid", region: "reg",
|
|
filter: func(_ postgresflex.ListFlavors) bool { return true },
|
|
wantCount: 3,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
description: "Success - Filter flavors by description",
|
|
projectId: "pid", region: "reg",
|
|
filter: func(f postgresflex.ListFlavors) bool { return *f.Description == "first" },
|
|
wantCount: 1,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
description: "Error - Missing parameters",
|
|
projectId: "", region: "reg",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(
|
|
tt.description, func(t *testing.T) {
|
|
var currentPage int64
|
|
client := &mockFlavorsClient{
|
|
executeRequest: func() postgresflex.ApiGetFlavorsRequestRequest {
|
|
return &mockRequest{
|
|
executeFunc: func() (*postgresflex.GetFlavorsResponse, error) {
|
|
currentPage++
|
|
return mockResp(currentPage)
|
|
},
|
|
}
|
|
},
|
|
}
|
|
actual, err := getFlavorsByFilter(context.Background(), client, tt.projectId, tt.region, tt.filter)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("getFlavorsByFilter() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
|
|
if !tt.wantErr && len(actual) != tt.wantCount {
|
|
t.Errorf("getFlavorsByFilter() got %d flavors, want %d", len(actual), tt.wantCount)
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestGetAllFlavors(t *testing.T) {
|
|
var currentPage int64
|
|
client := &mockFlavorsClient{
|
|
executeRequest: func() postgresflex.ApiGetFlavorsRequestRequest {
|
|
return &mockRequest{
|
|
executeFunc: func() (*postgresflex.GetFlavorsResponse, error) {
|
|
currentPage++
|
|
return mockResp(currentPage)
|
|
},
|
|
}
|
|
},
|
|
}
|
|
|
|
res, err := getAllFlavors(context.Background(), client, "pid", "reg")
|
|
if err != nil {
|
|
t.Errorf("getAllFlavors() unexpected error: %v", err)
|
|
}
|
|
if len(res) != 3 {
|
|
t.Errorf("getAllFlavors() expected 3 flavor, got %d", len(res))
|
|
}
|
|
}
|