diff --git a/pkg/acceptance/bettertestspoc/config/config.go b/pkg/acceptance/bettertestspoc/config/config.go index 3174d20fca..9a7a900bda 100644 --- a/pkg/acceptance/bettertestspoc/config/config.go +++ b/pkg/acceptance/bettertestspoc/config/config.go @@ -1,8 +1,6 @@ package config import ( - "encoding/json" - "fmt" "reflect" "strings" "testing" @@ -67,7 +65,8 @@ func ProviderFromModel(t *testing.T, model ProviderModel) string { return hcl } -// FromModels allows to combine multiple models. +// FromModels should be used in terraform acceptance tests for Config attribute to get string config from all models. +// FromModels allows to combine multiple model types. // TODO [SNOW-1501905]: introduce some common interface for all three existing models (ResourceModel, DatasourceModel, and ProviderModel) func FromModels(t *testing.T, models ...any) string { t.Helper() @@ -91,50 +90,6 @@ func FromModels(t *testing.T, models ...any) string { return sb.String() } -// FromModel should be used in terraform acceptance tests for Config attribute to get string config from ResourceModel. -// Current implementation is really straightforward but it could be improved and tested. It may not handle all cases (like objects, lists, sets) correctly. -// TODO [SNOW-1501905]: use reflection to build config directly from model struct (or some other different way) -// TODO [SNOW-1501905]: add support for config.TestStepConfigFunc (to use as ConfigFile); the naive implementation would be to just create a tmp directory and save file there -// TODO [SNOW-1501905]: add generating MarshalJSON() function -// TODO [SNOW-1501905]: migrate resources to new config generation method (above needed first) -// Use ResourceFromModel, DatasourceFromModel, ProviderFromModel, and FromModels instead. -func FromModel(t *testing.T, model ResourceModel) string { - t.Helper() - - b, err := json.Marshal(model) - require.NoError(t, err) - - var objMap map[string]json.RawMessage - err = json.Unmarshal(b, &objMap) - require.NoError(t, err) - - var sb strings.Builder - sb.WriteString(fmt.Sprintf(`resource "%s" "%s" {`, model.Resource(), model.ResourceName())) - sb.WriteRune('\n') - for k, v := range objMap { - sb.WriteString(fmt.Sprintf("\t%s = %s\n", k, v)) - } - if len(model.DependsOn()) > 0 { - sb.WriteString(fmt.Sprintf("\tdepends_on = [%s]\n", strings.Join(model.DependsOn(), ", "))) - } - sb.WriteString(`}`) - sb.WriteRune('\n') - s := sb.String() - t.Logf("Generated config:\n%s", s) - return s -} - -// FromModelsDeprecated allows to combine multiple resource models. -// Use FromModels instead. -func FromModelsDeprecated(t *testing.T, models ...ResourceModel) string { - t.Helper() - var sb strings.Builder - for _, model := range models { - sb.WriteString(FromModel(t, model) + "\n") - } - return sb.String() -} - // ConfigVariablesFromModel constructs config.Variables needed in acceptance tests that are using ConfigVariables in // combination with ConfigDirectory. It's necessary for cases not supported by FromModel, like lists of objects. // Use ResourceFromModel, DatasourceFromModel, ProviderFromModel, and FromModels instead. diff --git a/pkg/acceptance/bettertestspoc/config/config_test.go b/pkg/acceptance/bettertestspoc/config/config_test.go index f061058adc..5c6959012d 100644 --- a/pkg/acceptance/bettertestspoc/config/config_test.go +++ b/pkg/acceptance/bettertestspoc/config/config_test.go @@ -35,6 +35,9 @@ resource "snowflake_share" "test" { Item{IntField: 2, StringField: "second item"}, ). WithSingleObject("one", 2). + WithTextFieldExplicitNull(). + WithListFieldEmpty(). + WithMultilineField("some\nmultiline\ncontent"). WithDependsOn("some_other_resource.some_name", "other_resource.some_other_name", "third_resource.third_name") expectedOutput := strings.TrimPrefix(` resource "snowflake_share" "test" { @@ -54,6 +57,13 @@ resource "snowflake_share" "test" { a = "one" b = 2 } + text_field = null + list_field = [] + multiline_field = <