157 lines
3.6 KiB
Go
157 lines
3.6 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
|
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
|
)
|
|
|
|
func TestMapFields(t *testing.T) {
|
|
type input struct {
|
|
resp *postgresflexalpha.ListDatabase
|
|
model *postgresflexalpha2.DatabaseModel
|
|
region string
|
|
}
|
|
type expected struct {
|
|
model *postgresflexalpha2.DatabaseModel
|
|
err bool
|
|
}
|
|
|
|
testcases := []struct {
|
|
name string
|
|
given input
|
|
want expected
|
|
}{
|
|
{
|
|
name: "should map fields correctly when given a valid response",
|
|
given: input{
|
|
resp: &postgresflexalpha.ListDatabase{
|
|
Id: ptr(int64(123)),
|
|
Name: ptr("my-db"),
|
|
Owner: ptr("\"my-owner\""),
|
|
},
|
|
model: &postgresflexalpha2.DatabaseModel{},
|
|
region: "eu01",
|
|
},
|
|
want: expected{
|
|
model: &postgresflexalpha2.DatabaseModel{
|
|
Id: types.Int64Value(123),
|
|
Name: types.StringValue("my-db"),
|
|
Region: types.StringValue("eu01"),
|
|
Owner: types.StringValue("my-owner"),
|
|
},
|
|
err: false,
|
|
},
|
|
},
|
|
{
|
|
name: "should use existing model ID if present",
|
|
given: input{
|
|
resp: &postgresflexalpha.ListDatabase{
|
|
Id: ptr(int64(456)),
|
|
Name: ptr("my-db"),
|
|
},
|
|
model: &postgresflexalpha2.DatabaseModel{
|
|
Id: types.Int64Value(789),
|
|
},
|
|
region: "eu01",
|
|
},
|
|
want: expected{
|
|
model: &postgresflexalpha2.DatabaseModel{
|
|
Id: types.Int64Value(789),
|
|
Name: types.StringValue("my-db"),
|
|
Region: types.StringValue("eu01"),
|
|
Owner: types.StringNull(),
|
|
},
|
|
err: false,
|
|
},
|
|
},
|
|
{
|
|
name: "should return an error when response is nil",
|
|
given: input{
|
|
resp: nil,
|
|
model: &postgresflexalpha2.DatabaseModel{},
|
|
},
|
|
want: expected{
|
|
err: true,
|
|
},
|
|
},
|
|
{
|
|
name: "should return an error when response ID is nil",
|
|
given: input{
|
|
resp: &postgresflexalpha.ListDatabase{
|
|
Id: nil,
|
|
},
|
|
model: &postgresflexalpha2.DatabaseModel{},
|
|
},
|
|
want: expected{
|
|
err: true,
|
|
},
|
|
},
|
|
{
|
|
name: "should return an error when model is nil",
|
|
given: input{
|
|
resp: &postgresflexalpha.ListDatabase{
|
|
Id: ptr(int64(123)),
|
|
},
|
|
model: nil,
|
|
},
|
|
want: expected{
|
|
err: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := mapFields(tc.given.resp, tc.given.model, tc.given.region)
|
|
if (err != nil) != tc.want.err {
|
|
t.Fatalf("expected error: %v, got: %v", tc.want.err, err)
|
|
}
|
|
if err == nil {
|
|
if diff := cmp.Diff(tc.want.model, tc.given.model); diff != "" {
|
|
t.Errorf("model mismatch (-want +got):\n%s", diff)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCleanString(t *testing.T) {
|
|
testcases := []struct {
|
|
name string
|
|
given *string
|
|
want *string
|
|
}{
|
|
{
|
|
name: "should remove quotes from a string",
|
|
given: ptr("\"quoted\""),
|
|
want: ptr("quoted"),
|
|
},
|
|
{
|
|
name: "should return nil for a nil input",
|
|
given: nil,
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "should not change a string without quotes",
|
|
given: ptr("unquoted"),
|
|
want: ptr("unquoted"),
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
actual := cleanString(tc.given)
|
|
if (actual == nil && tc.want != nil) || (actual != nil && tc.want == nil) || (actual != nil && *actual != *tc.want) {
|
|
t.Errorf("expected %v, got %v", tc.want, actual)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func ptr[T any](v T) *T {
|
|
return &v
|
|
}
|