Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: function tests #3279

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package model

import (
"encoding/json"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/datatypes"
tfconfig "github.com/hashicorp/terraform-plugin-testing/config"
)

func (f *FunctionJavascriptModel) MarshalJSON() ([]byte, error) {
Expand All @@ -14,3 +20,24 @@ func (f *FunctionJavascriptModel) MarshalJSON() ([]byte, error) {
DependsOn: f.DependsOn(),
})
}

func FunctionJavascriptInline(resourceName string, id sdk.SchemaObjectIdentifierWithArguments, functionDefinition string, returnType string) *FunctionJavascriptModel {
f := &FunctionJavascriptModel{ResourceModelMeta: config.Meta(resourceName, resources.FunctionJavascript)}
f.WithDatabase(id.DatabaseName())
f.WithFunctionDefinition(functionDefinition)
f.WithName(id.Name())
f.WithReturnType(returnType)
f.WithSchema(id.SchemaName())
return f
}

func (f *FunctionJavascriptModel) WithArgument(argName string, argDataType datatypes.DataType) *FunctionJavascriptModel {
return f.WithArgumentsValue(
tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"arg_name": tfconfig.StringVariable(argName),
"arg_data_type": tfconfig.StringVariable(argDataType.ToSql()),
},
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package model

import (
"encoding/json"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/datatypes"
tfconfig "github.com/hashicorp/terraform-plugin-testing/config"
)

func (f *FunctionPythonModel) MarshalJSON() ([]byte, error) {
Expand All @@ -14,3 +19,67 @@ func (f *FunctionPythonModel) MarshalJSON() ([]byte, error) {
DependsOn: f.DependsOn(),
})
}

func FunctionPythonBasicInline(resourceName string, id sdk.SchemaObjectIdentifierWithArguments, runtimeVersion string, returnType datatypes.DataType, handler string, functionDefinition string) *FunctionPythonModel {
return FunctionPython(resourceName, id.DatabaseName(), handler, id.Name(), returnType.ToSql(), runtimeVersion, id.SchemaName()).WithFunctionDefinition(functionDefinition)
}

func (f *FunctionPythonModel) WithArgument(argName string, argDataType datatypes.DataType) *FunctionPythonModel {
return f.WithArgumentsValue(
tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"arg_name": tfconfig.StringVariable(argName),
"arg_data_type": tfconfig.StringVariable(argDataType.ToSql()),
},
),
)
}

func (f *FunctionPythonModel) WithImports(imports ...sdk.NormalizedPath) *FunctionPythonModel {
return f.WithImportsValue(
tfconfig.SetVariable(
collections.Map(imports, func(imp sdk.NormalizedPath) tfconfig.Variable {
return tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"stage_location": tfconfig.StringVariable(imp.StageLocation),
"path_on_stage": tfconfig.StringVariable(imp.PathOnStage),
},
)
})...,
),
)
}

func (f *FunctionPythonModel) WithExternalAccessIntegrations(ids ...sdk.AccountObjectIdentifier) *FunctionPythonModel {
return f.WithExternalAccessIntegrationsValue(
tfconfig.SetVariable(
collections.Map(ids, func(id sdk.AccountObjectIdentifier) tfconfig.Variable { return tfconfig.StringVariable(id.Name()) })...,
),
)
}

func (f *FunctionPythonModel) WithSecrets(secrets map[string]sdk.SchemaObjectIdentifier) *FunctionPythonModel {
objects := make([]tfconfig.Variable, 0)
for k, v := range secrets {
objects = append(objects, tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"secret_variable_name": tfconfig.StringVariable(k),
"secret_id": tfconfig.StringVariable(v.FullyQualifiedName()),
},
))
}

return f.WithSecretsValue(
tfconfig.SetVariable(
objects...,
),
)
}

func (f *FunctionPythonModel) WithPackages(pkgs ...string) *FunctionPythonModel {
return f.WithPackagesValue(
tfconfig.SetVariable(
collections.Map(pkgs, func(pkg string) tfconfig.Variable { return tfconfig.StringVariable(pkg) })...,
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package model

import (
"encoding/json"
"strings"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/datatypes"
tfconfig "github.com/hashicorp/terraform-plugin-testing/config"
)

func (f *FunctionScalaModel) MarshalJSON() ([]byte, error) {
Expand All @@ -14,3 +20,96 @@ func (f *FunctionScalaModel) MarshalJSON() ([]byte, error) {
DependsOn: f.DependsOn(),
})
}

func FunctionScalaBasicInline(
resourceName string,
id sdk.SchemaObjectIdentifierWithArguments,
runtimeVersion string,
returnType datatypes.DataType,
handler string,
functionDefinition string,
) *FunctionScalaModel {
return FunctionScala(resourceName, id.DatabaseName(), handler, id.Name(), returnType.ToSql(), runtimeVersion, id.SchemaName()).WithFunctionDefinition(functionDefinition)
}

func (f *FunctionScalaModel) WithArgument(argName string, argDataType datatypes.DataType) *FunctionScalaModel {
return f.WithArgumentsValue(
tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"arg_name": tfconfig.StringVariable(argName),
"arg_data_type": tfconfig.StringVariable(argDataType.ToSql()),
},
),
)
}

func (f *FunctionScalaModel) WithImport(stageLocation string, pathOnStage string) *FunctionScalaModel {
return f.WithImportsValue(
tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"stage_location": tfconfig.StringVariable(strings.TrimPrefix(stageLocation, "@")),
"path_on_stage": tfconfig.StringVariable(pathOnStage),
},
),
)
}

func (f *FunctionScalaModel) WithImports(imports ...sdk.NormalizedPath) *FunctionScalaModel {
return f.WithImportsValue(
tfconfig.SetVariable(
collections.Map(imports, func(imp sdk.NormalizedPath) tfconfig.Variable {
return tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"stage_location": tfconfig.StringVariable(imp.StageLocation),
"path_on_stage": tfconfig.StringVariable(imp.PathOnStage),
},
)
})...,
),
)
}

func (f *FunctionScalaModel) WithPackages(pkgs ...string) *FunctionScalaModel {
return f.WithPackagesValue(
tfconfig.SetVariable(
collections.Map(pkgs, func(pkg string) tfconfig.Variable { return tfconfig.StringVariable(pkg) })...,
),
)
}

func (f *FunctionScalaModel) WithExternalAccessIntegrations(ids ...sdk.AccountObjectIdentifier) *FunctionScalaModel {
return f.WithExternalAccessIntegrationsValue(
tfconfig.SetVariable(
collections.Map(ids, func(id sdk.AccountObjectIdentifier) tfconfig.Variable { return tfconfig.StringVariable(id.Name()) })...,
),
)
}

func (f *FunctionScalaModel) WithSecrets(secrets map[string]sdk.SchemaObjectIdentifier) *FunctionScalaModel {
objects := make([]tfconfig.Variable, 0)
for k, v := range secrets {
objects = append(objects, tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"secret_variable_name": tfconfig.StringVariable(k),
"secret_id": tfconfig.StringVariable(v.FullyQualifiedName()),
},
))
}

return f.WithSecretsValue(
tfconfig.SetVariable(
objects...,
),
)
}

func (f *FunctionScalaModel) WithTargetPathParts(stageLocation string, pathOnStage string) *FunctionScalaModel {
return f.WithTargetPathValue(
tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"stage_location": tfconfig.StringVariable(stageLocation),
"path_on_stage": tfconfig.StringVariable(pathOnStage),
},
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package model

import (
"encoding/json"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/datatypes"
tfconfig "github.com/hashicorp/terraform-plugin-testing/config"
)

func (f *FunctionSqlModel) MarshalJSON() ([]byte, error) {
Expand All @@ -14,3 +20,24 @@ func (f *FunctionSqlModel) MarshalJSON() ([]byte, error) {
DependsOn: f.DependsOn(),
})
}

func FunctionSqlBasicInline(resourceName string, id sdk.SchemaObjectIdentifierWithArguments, functionDefinition string, returnType string) *FunctionSqlModel {
f := &FunctionSqlModel{ResourceModelMeta: config.Meta(resourceName, resources.FunctionSql)}
f.WithDatabase(id.DatabaseName())
f.WithFunctionDefinition(functionDefinition)
f.WithName(id.Name())
f.WithReturnType(returnType)
f.WithSchema(id.SchemaName())
return f
}

func (f *FunctionSqlModel) WithArgument(argName string, argDataType datatypes.DataType) *FunctionSqlModel {
return f.WithArgumentsValue(
tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"arg_name": tfconfig.StringVariable(argName),
"arg_data_type": tfconfig.StringVariable(argDataType.ToSql()),
},
),
)
}
13 changes: 13 additions & 0 deletions pkg/acceptance/helpers/function_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ func (c *FunctionClient) SampleJavascriptDefinition(t *testing.T, argName string
`, argName)
}

func (c *FunctionClient) SampleJavascriptDefinitionNoArgs(t *testing.T) string {
t.Helper()
return `return 1;`
}

func (c *FunctionClient) SamplePythonDefinition(t *testing.T, funcName string, argName string) string {
t.Helper()

Expand Down Expand Up @@ -271,6 +276,14 @@ func (c *FunctionClient) SampleSqlDefinition(t *testing.T) string {
return "3.141592654::FLOAT"
}

func (c *FunctionClient) SampleSqlDefinitionWithArgument(t *testing.T, argName string) string {
t.Helper()

return fmt.Sprintf(`
%s
`, argName)
}

func (c *FunctionClient) PythonIdentityDefinition(t *testing.T, funcName string, argName string) string {
t.Helper()

Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/function_java_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestAcc_FunctionJava_InlineFull(t *testing.T) {
}).
WithTargetPathParts(stage.ID().FullyQualifiedName(), jarName).
WithRuntimeVersion("11").
WithIsSecure("false").
WithIsSecure(r.BooleanFalse).
WithNullInputBehavior(string(sdk.NullInputBehaviorCalledOnNullInput)).
WithReturnResultsBehavior(string(sdk.ReturnResultsBehaviorVolatile)).
WithComment("some comment")
Expand Down
Loading
Loading