Skip to content

Commit

Permalink
Add database describe object assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-asawicki committed Sep 2, 2024
1 parent 260351a commit a8256e5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions pkg/acceptance/helpers/database_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
32 changes: 0 additions & 32 deletions pkg/acceptance/snowflakechecks/database.go

This file was deleted.

33 changes: 16 additions & 17 deletions pkg/resources/database_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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(),
),
},
},
Expand All @@ -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
Expand All @@ -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(),
),
},
},
Expand Down

0 comments on commit a8256e5

Please sign in to comment.