From 21132a890ef89352f61d16d919dbfd2efbf9fb68 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 5 Dec 2024 10:43:01 +0100 Subject: [PATCH] test: table acceptance and integration tests --- pkg/datasources/tables_acceptance_test.go | 45 +++++++++++++++------- pkg/sdk/testint/tables_integration_test.go | 10 +++-- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/pkg/datasources/tables_acceptance_test.go b/pkg/datasources/tables_acceptance_test.go index 8c6ffeba62..40b32e772e 100644 --- a/pkg/datasources/tables_acceptance_test.go +++ b/pkg/datasources/tables_acceptance_test.go @@ -5,7 +5,6 @@ import ( "testing" acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -16,6 +15,7 @@ func TestAcc_Tables(t *testing.T) { tableName := acc.TestClient().Ids.Alpha() stageName := acc.TestClient().Ids.Alpha() externalTableName := acc.TestClient().Ids.Alpha() + resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, PreCheck: func() { acc.TestAccPreCheck(t) }, @@ -26,12 +26,22 @@ func TestAcc_Tables(t *testing.T) { Steps: []resource.TestStep{ { Config: tables(databaseName, schemaName, tableName, stageName, externalTableName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.snowflake_tables.t", "database", databaseName), - resource.TestCheckResourceAttr("data.snowflake_tables.t", "schema", schemaName), - resource.TestCheckResourceAttrSet("data.snowflake_tables.t", "tables.#"), - resource.TestCheckResourceAttr("data.snowflake_tables.t", "tables.#", "1"), - resource.TestCheckResourceAttr("data.snowflake_tables.t", "tables.0.name", tableName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.#", "1"), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.name", tableName), + resource.TestCheckResourceAttrSet("data.snowflake_tables.in_schema", "tables.0.created_on"), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.database_name", databaseName), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.schema_name", schemaName), + resource.TestCheckResourceAttrSet("data.snowflake_tables.in_schema", "tables.0.owner"), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.comment", ""), + resource.TestCheckResourceAttrSet("data.snowflake_tables.in_schema", "tables.0.text"), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.is_secure", "false"), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.is_materialized", "false"), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.owner_role_type", "ROLE"), + resource.TestCheckResourceAttr("data.snowflake_tables.in_schema", "tables.0.change_tracking", "OFF"), + + resource.TestCheckResourceAttr("data.snowflake_tables.filtering", "tables.#", "1"), + resource.TestCheckResourceAttr("data.snowflake_tables.filtering", "tables.0.name", tableName), ), }, }, @@ -40,7 +50,6 @@ func TestAcc_Tables(t *testing.T) { func tables(databaseName string, schemaName string, tableName string, stageName string, externalTableName string) string { return fmt.Sprintf(` - resource snowflake_database "d" { name = "%v" } @@ -74,16 +83,26 @@ func tables(databaseName string, schemaName string, tableName string, stageName column { name = "column1" type = "STRING" - as = "TO_VARCHAR(TO_TIMESTAMP_NTZ(value:unix_timestamp_property::NUMBER, 3), 'yyyy-mm-dd-hh')" + as = "TO_VARCHAR(TO_TIMESTAMP_NTZ(value:unix_timestamp_property::NUMBER, 3), 'yyyy-mm-dd-hh')" } file_format = "TYPE = CSV" location = "@${snowflake_database.d.name}.${snowflake_schema.s.name}.${snowflake_stage.s.name}" } - data snowflake_tables "t" { - database = snowflake_table.t.database - schema = snowflake_table.t.schema + data snowflake_tables "in_schema" { depends_on = [snowflake_table.t, snowflake_external_table.et] + in { + schema = snowflake_schema.s.fully_qualified_name + } + } + + data snowflake_tables "filtering" { + depends_on = [snowflake_table.t, snowflake_external_table.et] + in { + database = snowflake_schema.s.database + } + like = "%v" + starts_with = trimsuffix("%v", "%%") } - `, databaseName, schemaName, tableName, stageName, externalTableName) + `, databaseName, schemaName, tableName, stageName, externalTableName, tableName+"%", tableName+"%") } diff --git a/pkg/sdk/testint/tables_integration_test.go b/pkg/sdk/testint/tables_integration_test.go index 19d7e8732f..18dabab375 100644 --- a/pkg/sdk/testint/tables_integration_test.go +++ b/pkg/sdk/testint/tables_integration_test.go @@ -878,7 +878,9 @@ func TestInt_Table(t *testing.T) { table, tableCleanup := testClientHelper().Table.Create(t) t.Cleanup(tableCleanup) - tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithTerse(sdk.Bool(true)).WithLikePattern(table.ID().Name())) + tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithTerse(*sdk.Bool(true)).WithLike(sdk.Like{ + Pattern: sdk.String(table.Name), + })) require.NoError(t, err) assert.Equal(t, 1, len(tables)) @@ -889,7 +891,7 @@ func TestInt_Table(t *testing.T) { table, tableCleanup := testClientHelper().Table.Create(t) t.Cleanup(tableCleanup) - tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithStartsWith(sdk.String(table.Name))) + tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithStartsWith(*sdk.String(table.Name))) require.NoError(t, err) assert.Equal(t, 1, len(tables)) @@ -897,7 +899,9 @@ func TestInt_Table(t *testing.T) { }) t.Run("when searching a non-existent table", func(t *testing.T) { - tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithLikePattern("non-existent")) + tables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithLike(sdk.Like{ + Pattern: sdk.String("non-existent"), + })) require.NoError(t, err) assert.Equal(t, 0, len(tables)) })