Skip to content

Commit

Permalink
Run the same set of tests on java procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-asawicki committed Dec 11, 2024
1 parent 5cd2868 commit f49a6c0
Show file tree
Hide file tree
Showing 5 changed files with 568 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package resourceassert

import (
"strconv"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"
)

func (f *ProcedureJavaResourceAssert) HasImportsLength(len int) *ProcedureJavaResourceAssert {
f.AddAssertion(assert.ValueSet("imports.#", strconv.FormatInt(int64(len), 10)))
return f
}

func (f *ProcedureJavaResourceAssert) HasTargetPathEmpty() *ProcedureJavaResourceAssert {
f.AddAssertion(assert.ValueSet("target_path.#", "0"))
return f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package resourceparametersassert

import (
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

func (f *ProcedureResourceParametersAssert) HasAllDefaults() *ProcedureResourceParametersAssert {
return f.
HasEnableConsoleOutput(false).
HasLogLevel(sdk.LogLevelOff).
HasMetricLevel(sdk.MetricLevelNone).
HasTraceLevel(sdk.TraceLevelOff)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package model

import (
"encoding/json"

tfconfig "github.com/hashicorp/terraform-plugin-testing/config"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/datatypes"
)

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

func ProcedureJavaBasicInline(
resourceName string,
id sdk.SchemaObjectIdentifierWithArguments,
returnType datatypes.DataType,
handler string,
procedureDefinition string,
) *ProcedureJavaModel {
return ProcedureJava(resourceName, id.DatabaseName(), handler, id.Name(), returnType.ToSql(), "11", id.SchemaName(), "1.14.0").
WithProcedureDefinition(procedureDefinition)
}

func ProcedureJavaBasicStaged(
resourceName string,
id sdk.SchemaObjectIdentifierWithArguments,
returnType datatypes.DataType,
handler string,
stageLocation string,
pathOnStage string,
) *ProcedureJavaModel {
return ProcedureJava(resourceName, id.DatabaseName(), handler, id.Name(), returnType.ToSql(), "11", id.SchemaName(), "1.14.0").
WithImport(stageLocation, pathOnStage)
}

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

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

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

func (f *ProcedureJavaModel) WithTargetPathParts(stageLocation string, pathOnStage string) *ProcedureJavaModel {
return f.WithTargetPathValue(
tfconfig.ObjectVariable(
map[string]tfconfig.Variable{
"stage_location": tfconfig.StringVariable(stageLocation),
"path_on_stage": tfconfig.StringVariable(pathOnStage),
},
),
)
}
36 changes: 36 additions & 0 deletions pkg/acceptance/helpers/procedure_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ func (c *ProcedureClient) CreateJava(t *testing.T) (*sdk.Procedure, func()) {
return function, c.DropProcedureFunc(t, id)
}

func (c *ProcedureClient) CreateScalaStaged(t *testing.T, id sdk.SchemaObjectIdentifierWithArguments, dataType datatypes.DataType, importPath string, handler string) (*sdk.Procedure, func()) {
t.Helper()
ctx := context.Background()

argName := "x"
argument := sdk.NewProcedureArgumentRequest(argName, dataType)
dt := sdk.NewProcedureReturnsResultDataTypeRequest(dataType)
returns := sdk.NewProcedureReturnsRequest().WithResultDataType(*dt)
packages := []sdk.ProcedurePackageRequest{*sdk.NewProcedurePackageRequest("com.snowflake:snowpark:1.14.0")}

request := sdk.NewCreateForScalaProcedureRequest(id.SchemaObjectId(), *returns, handler, packages, "2.12").
WithArguments([]sdk.ProcedureArgumentRequest{*argument}).
WithImports([]sdk.ProcedureImportRequest{*sdk.NewProcedureImportRequest(importPath)})

err := c.client().CreateForScala(ctx, request)
require.NoError(t, err)

function, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)

return function, c.DropProcedureFunc(t, id)
}

func (c *ProcedureClient) Create(t *testing.T, arguments ...sdk.DataType) *sdk.Procedure {
t.Helper()
return c.CreateWithIdentifier(t, c.ids.RandomSchemaObjectIdentifierWithArguments(arguments...))
Expand Down Expand Up @@ -156,6 +179,19 @@ func (c *ProcedureClient) SampleJavaDefinition(t *testing.T, className string, f
`, className, funcName, argName)
}

func (c *ProcedureClient) SampleJavaDefinitionNoArgs(t *testing.T, className string, funcName string) string {
t.Helper()

return fmt.Sprintf(`
import com.snowflake.snowpark_java.*;
class %[1]s {
public static String %[2]s(Session session) {
return "hello";
}
}
`, className, funcName)
}

// For more references: https://docs.snowflake.com/en/developer-guide/stored-procedure/stored-procedures-javascript
func (c *ProcedureClient) SampleJavascriptDefinition(t *testing.T, argName string) string {
t.Helper()
Expand Down
Loading

0 comments on commit f49a6c0

Please sign in to comment.