Some checks failed
Publish / Check GoReleaser config (push) Successful in 7s
Publish / Publish provider (push) Successful in 7m41s
CI Workflow / Check GoReleaser config (pull_request) Successful in 6s
CI Workflow / Prepare GO cache (pull_request) Successful in 10m18s
CI Workflow / Test readiness for publishing provider (pull_request) Has been cancelled
CI Workflow / Code coverage report (pull_request) Has been cancelled
CI Workflow / CI run build and linting (pull_request) Has been cancelled
CI Workflow / CI run tests (pull_request) Has been cancelled
TF Acceptance Tests Workflow / Acceptance Tests (pull_request) Failing after 26m13s
## 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)
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Reviewed-on: #85
248 lines
5.9 KiB
Go
248 lines
5.9 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"
|
|
|
|
postgresflexalpha "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
|
|
|
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: int32(1),
|
|
Name: "my-db",
|
|
Owner: "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: int32(1),
|
|
Name: "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.StringValue(""),
|
|
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: 0},
|
|
model: &dataSourceModel{},
|
|
},
|
|
expected: expected{err: true},
|
|
},
|
|
{
|
|
name: "should fail on nil model",
|
|
given: given{
|
|
source: &postgresflexalpha.ListDatabase{Id: int32(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.GetDatabaseResponse
|
|
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.GetDatabaseResponse{
|
|
Id: int32(1),
|
|
Name: "my-db",
|
|
Owner: "my-owner",
|
|
},
|
|
model: &resourceModel{},
|
|
},
|
|
expected: expected{
|
|
model: &resourceModel{
|
|
Id: types.StringNull(),
|
|
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: "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)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|
|
}
|