diff --git a/pkg/acceptance/bettertestspoc/assert/resource_assertions.go b/pkg/acceptance/bettertestspoc/assert/resource_assertions.go index 09c8c875cc..026f90e770 100644 --- a/pkg/acceptance/bettertestspoc/assert/resource_assertions.go +++ b/pkg/acceptance/bettertestspoc/assert/resource_assertions.go @@ -3,12 +3,10 @@ package assert import ( "errors" "fmt" - "strconv" "strings" "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/importchecks" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" ) @@ -92,62 +90,6 @@ func ValueNotSet(fieldName string) ResourceAssertion { return ResourceAssertion{fieldName: fieldName, resourceAssertionType: resourceAssertionTypeValueNotSet} } -const showOutputPrefix = "show_output.0." - -func ResourceShowOutputBoolValueSet(fieldName string, expected bool) ResourceAssertion { - return ResourceShowOutputValueSet(fieldName, strconv.FormatBool(expected)) -} - -func ResourceShowOutputIntValueSet(fieldName string, expected int) ResourceAssertion { - return ResourceShowOutputValueSet(fieldName, strconv.Itoa(expected)) -} - -func ResourceShowOutputFloatValueSet(fieldName string, expected float64) ResourceAssertion { - return ResourceShowOutputValueSet(fieldName, strconv.FormatFloat(expected, 'f', -1, 64)) -} - -func ResourceShowOutputStringUnderlyingValueSet[U ~string](fieldName string, expected U) ResourceAssertion { - return ResourceShowOutputValueSet(fieldName, string(expected)) -} - -func ResourceShowOutputValueSet(fieldName string, expected string) ResourceAssertion { - return ResourceAssertion{fieldName: showOutputPrefix + fieldName, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} -} - -func ResourceShowOutputValueNotSet(fieldName string) ResourceAssertion { - return ResourceAssertion{fieldName: showOutputPrefix + fieldName, resourceAssertionType: resourceAssertionTypeValueNotSet} -} - -func ResourceShowOutputValuePresent(fieldName string) ResourceAssertion { - return ResourceAssertion{fieldName: showOutputPrefix + fieldName, resourceAssertionType: resourceAssertionTypeValuePresent} -} - -const ( - parametersPrefix = "parameters.0." - parametersValueSuffix = ".0.value" - parametersLevelSuffix = ".0.level" -) - -func ResourceParameterBoolValueSet[T ~string](parameterName T, expected bool) ResourceAssertion { - return ResourceParameterValueSet(parameterName, strconv.FormatBool(expected)) -} - -func ResourceParameterIntValueSet[T ~string](parameterName T, expected int) ResourceAssertion { - return ResourceParameterValueSet(parameterName, strconv.Itoa(expected)) -} - -func ResourceParameterStringUnderlyingValueSet[T ~string, U ~string](parameterName T, expected U) ResourceAssertion { - return ResourceParameterValueSet(parameterName, string(expected)) -} - -func ResourceParameterValueSet[T ~string](parameterName T, expected string) ResourceAssertion { - return ResourceAssertion{fieldName: parametersPrefix + strings.ToLower(string(parameterName)) + parametersValueSuffix, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} -} - -func ResourceParameterLevelSet[T ~string](parameterName T, parameterType sdk.ParameterType) ResourceAssertion { - return ResourceAssertion{fieldName: parametersPrefix + strings.ToLower(string(parameterName)) + parametersLevelSuffix, expectedValue: string(parameterType), resourceAssertionType: resourceAssertionTypeValueSet} -} - // ToTerraformTestCheckFunc implements TestCheckFuncProvider to allow easier creation of new resource assertions. // It goes through all the assertion accumulated earlier and gathers the results of the checks. func (r *ResourceAssert) ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc { diff --git a/pkg/acceptance/bettertestspoc/assert/resource_parameter_assertions.go b/pkg/acceptance/bettertestspoc/assert/resource_parameter_assertions.go new file mode 100644 index 0000000000..6a95a61123 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/assert/resource_parameter_assertions.go @@ -0,0 +1,66 @@ +package assert + +import ( + "strconv" + "strings" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" +) + +const ( + parametersPrefix = "parameters.0." + parametersValueSuffix = ".0.value" + parametersLevelSuffix = ".0.level" +) + +func ResourceParameterBoolValueSet[T ~string](parameterName T, expected bool) ResourceAssertion { + return ResourceParameterValueSet(parameterName, strconv.FormatBool(expected)) +} + +func ResourceParameterBoolValueNotSet[T ~string](parameterName T) ResourceAssertion { + return ResourceParameterValueNotSet(parameterName) +} + +func ResourceParameterBoolValuePresent[T ~string](parameterName T) ResourceAssertion { + return ResourceParameterValuePresent(parameterName) +} + +func ResourceParameterIntValueSet[T ~string](parameterName T, expected int) ResourceAssertion { + return ResourceParameterValueSet(parameterName, strconv.Itoa(expected)) +} + +func ResourceParameterIntValueNotSet[T ~string](parameterName T) ResourceAssertion { + return ResourceParameterValueNotSet(parameterName) +} + +func ResourceParameterIntValuePresent[T ~string](parameterName T) ResourceAssertion { + return ResourceParameterValuePresent(parameterName) +} + +func ResourceParameterStringUnderlyingValueSet[T ~string, U ~string](parameterName T, expected U) ResourceAssertion { + return ResourceParameterValueSet(parameterName, string(expected)) +} + +func ResourceParameterStringUnderlyingValueNotSet[T ~string](parameterName T) ResourceAssertion { + return ResourceParameterValueNotSet(parameterName) +} + +func ResourceParameterStringUnderlyingValuePresent[T ~string](parameterName T) ResourceAssertion { + return ResourceParameterValuePresent(parameterName) +} + +func ResourceParameterValueSet[T ~string](parameterName T, expected string) ResourceAssertion { + return ResourceAssertion{fieldName: parametersPrefix + strings.ToLower(string(parameterName)) + parametersValueSuffix, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} +} + +func ResourceParameterValueNotSet[T ~string](parameterName T) ResourceAssertion { + return ResourceAssertion{fieldName: parametersPrefix + strings.ToLower(string(parameterName)) + parametersValueSuffix, resourceAssertionType: resourceAssertionTypeValueNotSet} +} + +func ResourceParameterValuePresent[T ~string](parameterName T) ResourceAssertion { + return ResourceAssertion{fieldName: parametersPrefix + strings.ToLower(string(parameterName)) + parametersValueSuffix, resourceAssertionType: resourceAssertionTypeValuePresent} +} + +func ResourceParameterLevelSet[T ~string](parameterName T, parameterType sdk.ParameterType) ResourceAssertion { + return ResourceAssertion{fieldName: parametersPrefix + strings.ToLower(string(parameterName)) + parametersLevelSuffix, expectedValue: string(parameterType), resourceAssertionType: resourceAssertionTypeValueSet} +} diff --git a/pkg/acceptance/bettertestspoc/assert/resource_show_assertions.go b/pkg/acceptance/bettertestspoc/assert/resource_show_assertions.go new file mode 100644 index 0000000000..cecce36f91 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/assert/resource_show_assertions.go @@ -0,0 +1,67 @@ +package assert + +import ( + "strconv" +) + +const showOutputPrefix = "show_output.0." + +func ResourceShowOutputBoolValueSet(fieldName string, expected bool) ResourceAssertion { + return ResourceShowOutputValueSet(fieldName, strconv.FormatBool(expected)) +} + +func ResourceShowOutputBoolValueNotSet(fieldName string) ResourceAssertion { + return ResourceShowOutputValueNotSet(fieldName) +} + +func ResourceShowOutputBoolValuePresent(fieldName string) ResourceAssertion { + return ResourceShowOutputValuePresent(fieldName) +} + +func ResourceShowOutputIntValueSet(fieldName string, expected int) ResourceAssertion { + return ResourceShowOutputValueSet(fieldName, strconv.Itoa(expected)) +} + +func ResourceShowOutputIntValueNotSet(fieldName string) ResourceAssertion { + return ResourceShowOutputValueNotSet(fieldName) +} + +func ResourceShowOutputIntValuePresent(fieldName string) ResourceAssertion { + return ResourceShowOutputValuePresent(fieldName) +} + +func ResourceShowOutputFloatValueSet(fieldName string, expected float64) ResourceAssertion { + return ResourceShowOutputValueSet(fieldName, strconv.FormatFloat(expected, 'f', -1, 64)) +} + +func ResourceShowOutputFloatValueNotSet(fieldName string) ResourceAssertion { + return ResourceShowOutputValueNotSet(fieldName) +} + +func ResourceShowOutputFloatValuePresent(fieldName string) ResourceAssertion { + return ResourceShowOutputValuePresent(fieldName) +} + +func ResourceShowOutputStringUnderlyingValueSet[U ~string](fieldName string, expected U) ResourceAssertion { + return ResourceShowOutputValueSet(fieldName, string(expected)) +} + +func ResourceShowOutputStringUnderlyingValueNotSet(fieldName string) ResourceAssertion { + return ResourceShowOutputValueNotSet(fieldName) +} + +func ResourceShowOutputStringUnderlyingValuePresent(fieldName string) ResourceAssertion { + return ResourceShowOutputValuePresent(fieldName) +} + +func ResourceShowOutputValueSet(fieldName string, expected string) ResourceAssertion { + return ResourceAssertion{fieldName: showOutputPrefix + fieldName, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} +} + +func ResourceShowOutputValueNotSet(fieldName string) ResourceAssertion { + return ResourceAssertion{fieldName: showOutputPrefix + fieldName, resourceAssertionType: resourceAssertionTypeValueNotSet} +} + +func ResourceShowOutputValuePresent(fieldName string) ResourceAssertion { + return ResourceAssertion{fieldName: showOutputPrefix + fieldName, resourceAssertionType: resourceAssertionTypeValuePresent} +} diff --git a/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/model.go b/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/model.go index 47bab21d60..6f7f53bc26 100644 --- a/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/model.go +++ b/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/model.go @@ -37,7 +37,7 @@ func ModelFromResourceSchemaDetails(resourceSchemaDetails genhelpers.ResourceSch attributes = append(attributes, ResourceAttributeAssertionModel{ Name: attr.Name, // TODO [SNOW-1501905]: add attribute type logic; allow type safe assertions, not only strings - AttributeType: "string", + AttributeType: attr.AttributeType.String(), }) } diff --git a/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/templates/assertions.tmpl b/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/templates/assertions.tmpl index cb6c58afde..278f4d62ca 100644 --- a/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/templates/assertions.tmpl +++ b/pkg/acceptance/bettertestspoc/assert/resourceassert/gen/templates/assertions.tmpl @@ -15,15 +15,34 @@ {{ end -}} +/////////////////////////////// +// Attribute no value checks // +/////////////////////////////// + +{{ range .Attributes -}} + func ({{ $assertVar }} *{{ $assertName }}) HasNo{{ SnakeCaseToCamel .Name }}() *{{ $assertName }} { + {{ if or (eq .AttributeType "TypeList") (eq .AttributeType "TypeSet") -}} + {{ $assertVar }}.AddAssertion(assert.ValueSet("{{ .Name }}.#", "0")) + return {{ $assertVar }} + {{ else -}} + {{ $assertVar }}.AddAssertion(assert.ValueNotSet("{{ .Name }}")) + return {{ $assertVar }} + {{ end -}} + } + +{{ end -}} + //////////////////////////// // Attribute empty checks // //////////////////////////// {{ range .Attributes -}} - func ({{ $assertVar }} *{{ $assertName }}) HasNo{{ SnakeCaseToCamel .Name }}() *{{ $assertName }} { - {{ $assertVar }}.AddAssertion(assert.ValueNotSet("{{ .Name }}")) - return {{ $assertVar }} - } + {{ if eq .AttributeType "TypeString" -}} + func ({{ $assertVar }} *{{ $assertName }}) Has{{ SnakeCaseToCamel .Name }}Empty() *{{ $assertName }} { + {{ $assertVar }}.AddAssertion(assert.ValueSet("{{ .Name }}", "")) + return {{ $assertVar }} + } + {{ end -}} {{ end -}} diff --git a/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/model.go b/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/model.go index adb50cfa54..6f4df8322b 100644 --- a/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/model.go +++ b/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/model.go @@ -31,23 +31,35 @@ type ResourceShowOutputAssertionModel struct { func ModelFromSdkObjectDetails(sdkObject genhelpers.SdkObjectDetails) ResourceShowOutputAssertionsModel { attributes := make([]ResourceShowOutputAssertionModel, len(sdkObject.Fields)) + includeFmt := false for idx, field := range sdkObject.Fields { - attributes[idx] = MapToResourceShowOutputAssertion(field) + showOutputAssertions, inFmt := MapToResourceShowOutputAssertion(field) + if !includeFmt && inFmt { + includeFmt = true + } + attributes[idx] = showOutputAssertions } name, _ := strings.CutPrefix(sdkObject.Name, "sdk.") packageWithGenerateDirective := os.Getenv("GOPACKAGE") + imports := genhelpers.AdditionalStandardImports(sdkObject.Fields) + if includeFmt { + imports = append(imports, "fmt") + } return ResourceShowOutputAssertionsModel{ Name: name, Attributes: attributes, PreambleModel: PreambleModel{ PackageName: packageWithGenerateDirective, - AdditionalStandardImports: genhelpers.AdditionalStandardImports(sdkObject.Fields), + AdditionalStandardImports: imports, }, } } -func MapToResourceShowOutputAssertion(field genhelpers.Field) ResourceShowOutputAssertionModel { +func MapToResourceShowOutputAssertion(field genhelpers.Field) (ResourceShowOutputAssertionModel, bool) { // TODO: Temporary + isPrimitive := true + includeFmt := false + concreteTypeWithoutPtr, _ := strings.CutPrefix(field.ConcreteType, "*") // TODO [SNOW-1501905]: get a runtime name for the assertion creator var assertionCreator string @@ -65,6 +77,7 @@ func MapToResourceShowOutputAssertion(field genhelpers.Field) ResourceShowOutput assertionCreator = "ResourceShowOutputStringUnderlyingValue" default: assertionCreator = "ResourceShowOutputValue" + isPrimitive = false } // TODO [SNOW-1501905]: handle other mappings if needed @@ -72,8 +85,15 @@ func MapToResourceShowOutputAssertion(field genhelpers.Field) ResourceShowOutput switch concreteTypeWithoutPtr { case "sdk.AccountObjectIdentifier": mapper = genhelpers.Name + case "sdk.AccountIdentifier", "sdk.DatabaseObjectIdentifier", "sdk.SchemaObjectIdentifier", "sdk.SchemaObjectIdentifierWithArguments", "sdk.ExternalObjectIdentifier": + mapper = genhelpers.FullyQualifiedName case "time.Time": mapper = genhelpers.ToString + default: + if !isPrimitive { + mapper = genhelpers.PrintToString + includeFmt = true + } } return ResourceShowOutputAssertionModel{ @@ -81,5 +101,5 @@ func MapToResourceShowOutputAssertion(field genhelpers.Field) ResourceShowOutput ConcreteType: concreteTypeWithoutPtr, AssertionCreator: assertionCreator, Mapper: mapper, - } + }, includeFmt } diff --git a/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/assertions.tmpl b/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/assertions.tmpl index 1bd9e5bae7..4d3fb4da9a 100644 --- a/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/assertions.tmpl +++ b/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/assertions.tmpl @@ -9,6 +9,7 @@ //////////////////////////// {{ range .Attributes -}} + {{/* TODO: jeśli nie znasz typu -> defaultuj do stringa */}} func ({{ $assertVar }} *{{ $assertName }}) Has{{ .Name }}(expected {{ .ConcreteType }}) *{{ $assertName }} { {{ $assertVar }}.AddAssertion(assert.{{ .AssertionCreator }}Set("{{ SnakeCase .Name }}", {{ RunMapper .Mapper "expected" }})) return {{ $assertVar }} @@ -16,9 +17,9 @@ {{ end -}} -//////////////////////////// -// Attribute empty checks // -//////////////////////////// +/////////////////////////////// +// Attribute no value checks // +/////////////////////////////// {{ range .Attributes -}} func ({{ $assertVar }} *{{ $assertName }}) HasNo{{ .Name }}() *{{ $assertName }} { @@ -28,6 +29,21 @@ {{ end -}} +//////////////////////////// +// Attribute empty checks // +//////////////////////////// + +{{ range .Attributes -}} + + {{ if eq .ConcreteType "string" -}} + func ({{ $assertVar }} *{{ $assertName }}) Has{{ .Name }}Empty() *{{ $assertName }} { + {{ $assertVar }}.AddAssertion(assert.{{ .AssertionCreator }}Set("{{ SnakeCase .Name }}", "")) + return {{ $assertVar }} + } + {{ end }} + +{{ end -}} + /////////////////////////////// // Attribute presence checks // /////////////////////////////// diff --git a/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/definition.tmpl b/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/definition.tmpl index 62df9799b9..b2751da61c 100644 --- a/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/definition.tmpl +++ b/pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/gen/templates/definition.tmpl @@ -2,7 +2,7 @@ {{- $assertName := .Name | printf "%sShowOutputAssert" -}} {{- $nameLowerCase := FirstLetterLowercase .Name -}} -{{- $assertVar := FirstLetter $nameLowerCase }} +{{- $assertVar := FirstLetterLowercase .Name | printf "%sAssert" }} type {{ $assertName }} struct { *assert.ResourceAssert } diff --git a/pkg/internal/genhelpers/mappers.go b/pkg/internal/genhelpers/mappers.go index 53f86e5521..be7f38516d 100644 --- a/pkg/internal/genhelpers/mappers.go +++ b/pkg/internal/genhelpers/mappers.go @@ -5,6 +5,7 @@ import "fmt" type Mapper func(string) string var ( + PrintToString = func(field string) string { return fmt.Sprintf(`fmt.Sprintf("%%v", %s)`, field) } Identity = func(field string) string { return field } ToString = func(field string) string { return fmt.Sprintf("%s.String()", field) } FullyQualifiedName = func(field string) string { return fmt.Sprintf("%s.FullyQualifiedName()", field) } diff --git a/pkg/internal/genhelpers/struct_details_extractor.go b/pkg/internal/genhelpers/struct_details_extractor.go index 0b4baf2608..4e5cbdcce7 100644 --- a/pkg/internal/genhelpers/struct_details_extractor.go +++ b/pkg/internal/genhelpers/struct_details_extractor.go @@ -83,6 +83,7 @@ func AdditionalStandardImports(fields []Field) []string { } additionalImports := make([]string, 0) for k := range imports { + k, _ := strings.CutPrefix(k, "[]") if !slices.Contains([]string{"sdk"}, k) { additionalImports = append(additionalImports, k) }