245 lines
6 KiB
Go
245 lines
6 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
|
datasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
|
)
|
|
|
|
func TestMapFields(t *testing.T) {
|
|
type given struct {
|
|
source *postgresflexalpha.ListDatabase
|
|
model *dataSourceModel
|
|
region string
|
|
}
|
|
type expected struct {
|
|
model *dataSourceModel
|
|
err bool
|
|
}
|
|
|
|
testcases := []struct {
|
|
name string
|
|
given given
|
|
expected expected
|
|
}{
|
|
{
|
|
name: "should map fields correctly",
|
|
given: given{
|
|
source: &postgresflexalpha.ListDatabase{
|
|
Id: utils.Ptr(int64(1)),
|
|
Name: utils.Ptr("my-db"),
|
|
Owner: utils.Ptr("\"my-owner\""),
|
|
},
|
|
model: &dataSourceModel{
|
|
DatabaseModel: datasource.DatabaseModel{
|
|
ProjectId: types.StringValue("my-project"),
|
|
InstanceId: types.StringValue("my-instance"),
|
|
},
|
|
},
|
|
region: "eu01",
|
|
},
|
|
expected: expected{
|
|
model: &dataSourceModel{
|
|
DatabaseModel: datasource.DatabaseModel{
|
|
Id: types.Int64Value(1),
|
|
Name: types.StringValue("my-db"),
|
|
Owner: types.StringValue("my-owner"),
|
|
Region: types.StringValue("eu01"),
|
|
DatabaseId: types.Int64Value(1),
|
|
InstanceId: types.StringValue("my-instance"),
|
|
ProjectId: types.StringValue("my-project"),
|
|
},
|
|
TerraformID: types.StringValue("my-project,eu01,my-instance,1"),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "should preserve existing model ID",
|
|
given: given{
|
|
source: &postgresflexalpha.ListDatabase{
|
|
Id: utils.Ptr(int64(1)),
|
|
Name: utils.Ptr("my-db"),
|
|
},
|
|
model: &dataSourceModel{
|
|
DatabaseModel: datasource.DatabaseModel{
|
|
Id: types.Int64Value(1),
|
|
ProjectId: types.StringValue("my-project"),
|
|
InstanceId: types.StringValue("my-instance"),
|
|
},
|
|
},
|
|
region: "eu01",
|
|
},
|
|
expected: expected{
|
|
model: &dataSourceModel{
|
|
DatabaseModel: datasource.DatabaseModel{
|
|
Id: types.Int64Value(1),
|
|
Name: types.StringValue("my-db"),
|
|
Owner: types.StringNull(), DatabaseId: types.Int64Value(1),
|
|
Region: types.StringValue("eu01"),
|
|
InstanceId: types.StringValue("my-instance"),
|
|
ProjectId: types.StringValue("my-project"),
|
|
},
|
|
TerraformID: types.StringValue("my-project,eu01,my-instance,1"),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "should fail on nil source",
|
|
given: given{
|
|
source: nil,
|
|
model: &dataSourceModel{},
|
|
},
|
|
expected: expected{err: true},
|
|
},
|
|
{
|
|
name: "should fail on nil source ID",
|
|
given: given{
|
|
source: &postgresflexalpha.ListDatabase{Id: nil},
|
|
model: &dataSourceModel{},
|
|
},
|
|
expected: expected{err: true},
|
|
},
|
|
{
|
|
name: "should fail on nil model",
|
|
given: given{
|
|
source: &postgresflexalpha.ListDatabase{Id: utils.Ptr(int64(1))},
|
|
model: nil,
|
|
},
|
|
expected: expected{err: true},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(
|
|
tc.name, func(t *testing.T) {
|
|
err := mapFields(tc.given.source, tc.given.model, tc.given.region)
|
|
if (err != nil) != tc.expected.err {
|
|
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
|
|
}
|
|
if err == nil {
|
|
if diff := cmp.Diff(tc.expected.model, tc.given.model); diff != "" {
|
|
t.Errorf("model mismatch (-want +got):\n%s", diff)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestMapResourceFields(t *testing.T) {
|
|
type given struct {
|
|
source *postgresflexalpha.ListDatabase
|
|
model *resourceModel
|
|
}
|
|
type expected struct {
|
|
model *resourceModel
|
|
err bool
|
|
}
|
|
|
|
testcases := []struct {
|
|
name string
|
|
given given
|
|
expected expected
|
|
}{
|
|
{
|
|
name: "should map fields correctly",
|
|
given: given{
|
|
source: &postgresflexalpha.ListDatabase{
|
|
Id: utils.Ptr(int64(1)),
|
|
Name: utils.Ptr("my-db"),
|
|
Owner: utils.Ptr("\"my-owner\""),
|
|
},
|
|
model: &resourceModel{},
|
|
},
|
|
expected: expected{
|
|
model: &resourceModel{
|
|
Id: types.Int64Value(1),
|
|
Name: types.StringValue("my-db"),
|
|
Owner: types.StringValue("my-owner"),
|
|
DatabaseId: types.Int64Value(1),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "should fail on nil source",
|
|
given: given{
|
|
source: nil,
|
|
model: &resourceModel{},
|
|
},
|
|
expected: expected{err: true},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(
|
|
tc.name, func(t *testing.T) {
|
|
err := mapResourceFields(tc.given.source, tc.given.model)
|
|
if (err != nil) != tc.expected.err {
|
|
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
|
|
}
|
|
if err == nil {
|
|
if diff := cmp.Diff(tc.expected.model, tc.given.model); diff != "" {
|
|
t.Errorf("model mismatch (-want +got):\n%s", diff)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestToCreatePayload(t *testing.T) {
|
|
type given struct {
|
|
model *resourceModel
|
|
}
|
|
type expected struct {
|
|
payload *postgresflexalpha.CreateDatabaseRequestPayload
|
|
err bool
|
|
}
|
|
|
|
testcases := []struct {
|
|
name string
|
|
given given
|
|
expected expected
|
|
}{
|
|
{
|
|
name: "should convert model to payload",
|
|
given: given{
|
|
model: &resourceModel{
|
|
Name: types.StringValue("my-db"),
|
|
Owner: types.StringValue("my-owner"),
|
|
},
|
|
},
|
|
expected: expected{
|
|
payload: &postgresflexalpha.CreateDatabaseRequestPayload{
|
|
Name: utils.Ptr("my-db"),
|
|
Owner: utils.Ptr("my-owner"),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "should fail on nil model",
|
|
given: given{model: nil},
|
|
expected: expected{err: true},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(
|
|
tc.name, func(t *testing.T) {
|
|
actual, err := toCreatePayload(tc.given.model)
|
|
if (err != nil) != tc.expected.err {
|
|
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
|
|
}
|
|
if err == nil {
|
|
if diff := cmp.Diff(tc.expected.payload, actual); diff != "" {
|
|
t.Errorf("payload mismatch (-want +got):\n%s", diff)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|