Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 3m57s
CI Workflow / CI run tests (pull_request) Failing after 5m5s
CI Workflow / CI run build and linting (pull_request) Failing after 4m50s
CI Workflow / Code coverage report (pull_request) Has been skipped
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package postgresFlexAlphaFlavor
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
|
)
|
|
|
|
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 int32) (*postgresflex.GetFlavorsResponse, error) {
|
|
if page == 1 {
|
|
return &postgresflex.GetFlavorsResponse{
|
|
Flavors: []postgresflex.ListFlavors{
|
|
{Id: "flavor-1", Description: "first"},
|
|
{Id: "flavor-2", Description: "second"},
|
|
},
|
|
}, nil
|
|
}
|
|
if page == 2 {
|
|
return &postgresflex.GetFlavorsResponse{
|
|
Flavors: []postgresflex.ListFlavors{
|
|
{Id: "flavor-3", Description: "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 int32
|
|
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 int32
|
|
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))
|
|
}
|
|
}
|