Skip to content

Commit

Permalink
feat: Upgrade view sdk (#2969)
Browse files Browse the repository at this point in the history
<!-- Feel free to delete comments as you fill this in -->
- add acceptance clients for aggregation and projection policies (with
jiras to use proper SDK clients)
- adjust SDK to match snowflake
- change handling of `PolicyReference` to be more generic
- add new sturct `ExtendedIn` to handle SHOWs that can specify
applications and applications packages (will be useful in other
resources)
- add more validations
- add quotes for fields with column names
- update resource and datasource to be compatible with sdk
- update statuses of remaining objects
- NOTE: view behavior is not consistent with docs, that is:
    - adding columns is not working
- columns with options in CREATE are without () in docs - this is not
correct, they need to be wrapped in (), this will be reported on
doc-discuss

## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
* [x] integration tests
<!-- add more below if you think they are relevant -->
* [x] unit tests
## References
<!-- issues documentation links, etc  -->
https://docs.snowflake.com/en/sql-reference/sql/create-view#examples
  • Loading branch information
sfc-gh-jmichalak authored Aug 6, 2024
1 parent f4ae380 commit ef2d50a
Show file tree
Hide file tree
Showing 31 changed files with 2,024 additions and 509 deletions.
4 changes: 2 additions & 2 deletions pkg/acceptance/bettertestspoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (w *WarehouseResourceParametersAssert) HasDefaultMaxConcurrencyLevel() *War

### Adding new Snowflake object assertions
Snowflake object assertions can be generated automatically. For object `abc` do the following:
- add object you want to generate to `allStructs` slice in the `assert/objectassert/gen/main/main.go`
- add object you want to generate to `allStructs` slice in the `assert/objectassert/gen/sdk_object_def.go`
- to add custom (not generated assertions) create file `abc_snowflake_ext.go` in the `objectassert` package. Example would be:
```go
func (w *WarehouseAssert) HasStateOneOf(expected ...sdk.WarehouseState) *WarehouseAssert {
Expand Down Expand Up @@ -77,7 +77,7 @@ func (w *WarehouseParametersAssert) HasDefaultMaxConcurrencyLevel() *WarehousePa
}
```

### Adding new models
### Adding new resource config model builders
Resource config model builders can be generated automatically. For object `abc` do the following:
- add object you want to generate to `allResourceSchemaDefs` slice in the `assert/resourceassert/gen/resource_schema_def.go`
- to add custom (not generated) config builder methods create file `warehouse_model_ext` in the `config/model` package. Example would be:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ var allStructs = []SdkObjectDef{
ObjectType: sdk.ObjectTypeWarehouse,
ObjectStruct: sdk.Warehouse{},
},
{
IdType: "sdk.SchemaObjectIdentifier",
ObjectType: sdk.ObjectTypeView,
ObjectStruct: sdk.View{},
},
}

func GetSdkObjectDetails() []genhelpers.SdkObjectDetails {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package objectassert

import (
"fmt"
"testing"

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

func (w *ViewAssert) HasCreatedOnNotEmpty() *ViewAssert {
w.AddAssertion(func(t *testing.T, o *sdk.View) error {
t.Helper()
if o.CreatedOn == "" {
return fmt.Errorf("expected created on not empty; got: %v", o.CreatedOn)
}
return nil
})
return w
}

func (v *ViewAssert) HasNonEmptyText() *ViewAssert {
v.AddAssertion(func(t *testing.T, o *sdk.View) error {
t.Helper()
if o.Text == "" {
return fmt.Errorf("expected non empty text")
}
return nil
})
return v
}

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.

47 changes: 47 additions & 0 deletions pkg/acceptance/helpers/aggregation_policy_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package helpers

import (
"context"
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

// TODO(SNOW-1564954): change raw sqls to proper client
type AggregationPolicyClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewAggregationPolicyClient(context *TestClientContext, idsGenerator *IdsGenerator) *AggregationPolicyClient {
return &AggregationPolicyClient{
context: context,
ids: idsGenerator,
}
}

func (c *AggregationPolicyClient) client() *sdk.Client {
return c.context.client
}

func (c *AggregationPolicyClient) CreateAggregationPolicy(t *testing.T) (sdk.SchemaObjectIdentifier, func()) {
t.Helper()
ctx := context.Background()

id := c.ids.RandomSchemaObjectIdentifier()
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`CREATE AGGREGATION POLICY %s AS () RETURNS AGGREGATION_CONSTRAINT -> AGGREGATION_CONSTRAINT(MIN_GROUP_SIZE => 5)`, id.Name()))
require.NoError(t, err)
return id, c.DropAggregationPolicyFunc(t, id)
}

func (c *AggregationPolicyClient) DropAggregationPolicyFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() {
t.Helper()
ctx := context.Background()

return func() {
_, err := c.client().ExecForTests(ctx, fmt.Sprintf(`DROP AGGREGATION POLICY IF EXISTS %s`, id.Name()))
require.NoError(t, err)
}
}
Loading

0 comments on commit ef2d50a

Please sign in to comment.