Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Dec 13, 2024
1 parent f673c18 commit b2fc163
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 71 deletions.
58 changes: 0 additions & 58 deletions pkg/acceptance/bettertestspoc/assert/resource_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
}
67 changes: 67 additions & 0 deletions pkg/acceptance/bettertestspoc/assert/resource_show_assertions.go
Original file line number Diff line number Diff line change
@@ -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}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -65,21 +77,29 @@ func MapToResourceShowOutputAssertion(field genhelpers.Field) ResourceShowOutput
assertionCreator = "ResourceShowOutputStringUnderlyingValue"
default:
assertionCreator = "ResourceShowOutputValue"
isPrimitive = false
}

// TODO [SNOW-1501905]: handle other mappings if needed
mapper := genhelpers.Identity
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{
Name: field.Name,
ConcreteType: concreteTypeWithoutPtr,
AssertionCreator: assertionCreator,
Mapper: mapper,
}
}, includeFmt
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
////////////////////////////

{{ 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 }}
}

{{ end -}}

////////////////////////////
// Attribute empty checks //
////////////////////////////
///////////////////////////////
// Attribute no value checks //
///////////////////////////////

{{ range .Attributes -}}
func ({{ $assertVar }} *{{ $assertName }}) HasNo{{ .Name }}() *{{ $assertName }} {
Expand All @@ -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 //
///////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{- $assertName := .Name | printf "%sShowOutputAssert" -}}
{{- $nameLowerCase := FirstLetterLowercase .Name -}}
{{- $assertVar := FirstLetter $nameLowerCase }}
{{- $assertVar := FirstLetterLowercase .Name | printf "%sAssert" }}
type {{ $assertName }} struct {
*assert.ResourceAssert
}
Expand Down
1 change: 1 addition & 0 deletions pkg/internal/genhelpers/mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
1 change: 1 addition & 0 deletions pkg/internal/genhelpers/struct_details_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit b2fc163

Please sign in to comment.