diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/describe_database_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/objectassert/describe_database_snowflake_ext.go new file mode 100644 index 00000000000..e92a949e903 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/describe_database_snowflake_ext.go @@ -0,0 +1,46 @@ +package objectassert + +import ( + "fmt" + "slices" + "testing" + + acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" +) + +// TODO [SNOW-1501905]: this file should be fully regenerated when adding and option to assert the results of describe +type DatabaseDescribeAssert struct { + *assert.SnowflakeObjectAssert[sdk.DatabaseDetails, sdk.AccountObjectIdentifier] +} + +func DatabaseDescribe(t *testing.T, id sdk.AccountObjectIdentifier) *DatabaseDescribeAssert { + t.Helper() + return &DatabaseDescribeAssert{ + assert.NewSnowflakeObjectAssertWithProvider(sdk.ObjectType("DATABASE_DETAILS"), id, acc.TestClient().Database.Describe), + } +} + +func (d *DatabaseDescribeAssert) DoesNotContainPublicSchema() *DatabaseDescribeAssert { + d.AddAssertion(func(t *testing.T, o *sdk.DatabaseDetails) error { + t.Helper() + if slices.ContainsFunc(o.Rows, func(row sdk.DatabaseDetailsRow) bool { return row.Name == "PUBLIC" && row.Kind == "SCHEMA" }) { + return fmt.Errorf("expected database %s to not contain public schema", d.GetId()) + } + return nil + }) + return d +} + +func (d *DatabaseDescribeAssert) ContainsPublicSchema() *DatabaseDescribeAssert { + d.AddAssertion(func(t *testing.T, o *sdk.DatabaseDetails) error { + t.Helper() + if !slices.ContainsFunc(o.Rows, func(row sdk.DatabaseDetailsRow) bool { return row.Name == "PUBLIC" && row.Kind == "SCHEMA" }) { + return fmt.Errorf("expected database %s to contain public schema", d.GetId()) + } + return nil + }) + return d +} diff --git a/pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go index 401a1284fa8..801c41aa0d1 100644 --- a/pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go +++ b/pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go @@ -52,6 +52,10 @@ func (s *SnowflakeObjectAssert[T, I]) AddAssertion(assertion assertSdk[*T]) { s.assertions = append(s.assertions, assertion) } +func (s *SnowflakeObjectAssert[T, I]) GetId() I { + return s.id +} + // ToTerraformTestCheckFunc implements TestCheckFuncProvider to allow easier creation of new Snowflake object assertions. // It goes through all the assertion accumulated earlier and gathers the results of the checks. func (s *SnowflakeObjectAssert[_, _]) ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc { diff --git a/pkg/acceptance/helpers/database_client.go b/pkg/acceptance/helpers/database_client.go index e0dc584dfdb..1011336f7ae 100644 --- a/pkg/acceptance/helpers/database_client.go +++ b/pkg/acceptance/helpers/database_client.go @@ -147,12 +147,9 @@ func (c *DatabaseClient) Show(t *testing.T, id sdk.AccountObjectIdentifier) (*sd return c.client().ShowByID(ctx, id) } -func (c *DatabaseClient) Describe(t *testing.T, id sdk.AccountObjectIdentifier) *sdk.DatabaseDetails { +func (c *DatabaseClient) Describe(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.DatabaseDetails, error) { t.Helper() ctx := context.Background() - details, err := c.client().Describe(ctx, id) - require.NoError(t, err) - - return details + return c.client().Describe(ctx, id) } diff --git a/pkg/acceptance/snowflakechecks/database.go b/pkg/acceptance/snowflakechecks/database.go deleted file mode 100644 index e8f01c43c92..00000000000 --- a/pkg/acceptance/snowflakechecks/database.go +++ /dev/null @@ -1,32 +0,0 @@ -package snowflakechecks - -import ( - "fmt" - "slices" - "testing" - - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/terraform" -) - -func DoesNotContainPublicSchema(t *testing.T, id sdk.AccountObjectIdentifier) resource.TestCheckFunc { - t.Helper() - return func(state *terraform.State) error { - if slices.ContainsFunc(acc.TestClient().Database.Describe(t, id).Rows, func(row sdk.DatabaseDetailsRow) bool { return row.Name == "PUBLIC" && row.Kind == "SCHEMA" }) { - return fmt.Errorf("expected database %s to not contain public schema", id.FullyQualifiedName()) - } - return nil - } -} - -func ContainsPublicSchema(t *testing.T, id sdk.AccountObjectIdentifier) resource.TestCheckFunc { - t.Helper() - return func(state *terraform.State) error { - if !slices.ContainsFunc(acc.TestClient().Database.Describe(t, id).Rows, func(row sdk.DatabaseDetailsRow) bool { return row.Name == "PUBLIC" && row.Kind == "SCHEMA" }) { - return fmt.Errorf("expected database %s to contain public schema", id.FullyQualifiedName()) - } - return nil - } -} diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index 3c3cb7e616b..9debdb1a3a7 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -2,22 +2,21 @@ package resources_test import ( "fmt" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectparametersassert" "strconv" "testing" - resourcehelpers "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + resourcehelpers "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" r "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" tfjson "github.com/hashicorp/terraform-json" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectassert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectparametersassert" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/importchecks" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/planchecks" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/snowflakechecks" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" @@ -1191,9 +1190,9 @@ func TestAcc_Database_WithoutPublicSchema(t *testing.T) { Steps: []resource.TestStep{ { Config: databaseWithDropPublicSchemaConfig(id, true), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name()), - snowflakechecks.DoesNotContainPublicSchema(t, id), + Check: assert.AssertThat(t, + assert.Check(resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name())), + objectassert.DatabaseDescribe(t, id).DoesNotContainPublicSchema(), ), }, // Change in parameter shouldn't change the state Snowflake @@ -1204,9 +1203,9 @@ func TestAcc_Database_WithoutPublicSchema(t *testing.T) { }, }, Config: databaseWithDropPublicSchemaConfig(id, false), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name()), - snowflakechecks.DoesNotContainPublicSchema(t, id), + Check: assert.AssertThat(t, + assert.Check(resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name())), + objectassert.DatabaseDescribe(t, id).DoesNotContainPublicSchema(), ), }, }, @@ -1226,9 +1225,9 @@ func TestAcc_Database_WithPublicSchema(t *testing.T) { Steps: []resource.TestStep{ { Config: databaseWithDropPublicSchemaConfig(id, false), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name()), - snowflakechecks.ContainsPublicSchema(t, id), + Check: assert.AssertThat(t, + assert.Check(resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name())), + objectassert.DatabaseDescribe(t, id).ContainsPublicSchema(), ), }, // Change in parameter shouldn't change the state Snowflake @@ -1239,9 +1238,9 @@ func TestAcc_Database_WithPublicSchema(t *testing.T) { }, }, Config: databaseWithDropPublicSchemaConfig(id, true), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name()), - snowflakechecks.ContainsPublicSchema(t, id), + Check: assert.AssertThat(t, + assert.Check(resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name())), + objectassert.DatabaseDescribe(t, id).ContainsPublicSchema(), ), }, },