Skip to content

Commit

Permalink
Implement half of alter integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-asawicki committed Jan 23, 2024
1 parent 6483219 commit 6934366
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 37 deletions.
8 changes: 4 additions & 4 deletions pkg/sdk/materialized_views_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ var materializedViewClusterBy = g.NewQueryStruct("MaterializedViewClusterBy").
var materializedViewSet = g.NewQueryStruct("MaterializedViewSet").
OptionalSQL("SECURE").
OptionalComment().
WithValidation(g.AtLeastOneValueSet, "Secure", "Comment")
WithValidation(g.ExactlyOneValueSet, "Secure", "Comment")

var materializedViewUnset = g.NewQueryStruct("MaterializedViewUnset").
OptionalSQL("SECURE").
OptionalSQL("COMMENT").
WithValidation(g.AtLeastOneValueSet, "Secure", "Comment")
WithValidation(g.ExactlyOneValueSet, "Secure", "Comment")

var materializedViewDbRow = g.DbStruct("materializedViewDBRow").
Text("created_on").
Expand Down Expand Up @@ -69,7 +69,7 @@ var materializedView = g.PlainStruct("MaterializedView").
OptionalText("Reserved").
Text("DatabaseName").
Text("SchemaName").
OptionalText("ClusterBy").
Text("ClusterBy").
Number("Rows").
Number("Bytes").
Text("SourceDatabaseName").
Expand Down Expand Up @@ -152,7 +152,7 @@ var MaterializedViewsDef = g.NewInterface(
OptionalSQL("SUSPEND").
OptionalSQL("RESUME").
OptionalQueryStructField("Set", materializedViewSet, g.KeywordOptions().SQL("SET")).
OptionalQueryStructField("Unset", materializedViewUnset, g.ListOptions().NoParentheses().SQL("UNSET")).
OptionalQueryStructField("Unset", materializedViewUnset, g.KeywordOptions().SQL("UNSET")).
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ExactlyOneValueSet, "RenameTo", "ClusterBy", "DropClusteringKey", "SuspendRecluster", "ResumeRecluster", "Suspend", "Resume", "Set", "Unset"),
).
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/materialized_views_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type AlterMaterializedViewOptions struct {
Suspend *bool `ddl:"keyword" sql:"SUSPEND"`
Resume *bool `ddl:"keyword" sql:"RESUME"`
Set *MaterializedViewSet `ddl:"keyword" sql:"SET"`
Unset *MaterializedViewUnset `ddl:"list,no_parentheses" sql:"UNSET"`
Unset *MaterializedViewUnset `ddl:"keyword" sql:"UNSET"`
}

type MaterializedViewSet struct {
Expand Down Expand Up @@ -134,7 +134,7 @@ type MaterializedView struct {
Reserved *string
DatabaseName string
SchemaName string
ClusterBy *string
ClusterBy string
Rows int
Bytes int
SourceDatabaseName string
Expand Down
44 changes: 22 additions & 22 deletions pkg/sdk/materialized_views_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,34 @@ func TestMaterializedViews_Alter(t *testing.T) {
assertOptsInvalidJoinedErrors(t, opts, errNotSet("AlterMaterializedViewOptions.ClusterBy", "Expressions"))
})

t.Run("validation: at least one of the fields [opts.Set.Secure opts.Set.Comment] should be set", func(t *testing.T) {
t.Run("validation: exactly one field from [opts.Set.Secure opts.Set.Comment] should be set", func(t *testing.T) {
opts := defaultOpts()
opts.Set = &MaterializedViewSet{}
assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterMaterializedViewOptions.Set", "Secure", "Comment"))
assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterMaterializedViewOptions.Set", "Secure", "Comment"))
})

t.Run("validation: at least one of the fields [opts.Unset.Secure opts.Unset.Comment] should be set", func(t *testing.T) {
t.Run("validation: exactly one field from [opts.Set.Secure opts.Set.Comment] should be set - more present", func(t *testing.T) {
opts := defaultOpts()
opts.Set = &MaterializedViewSet{
Secure: Bool(true),
Comment: String("comment"),
}
assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterMaterializedViewOptions.Set", "Secure", "Comment"))
})

t.Run("validation: exactly one field from [opts.Unset.Secure opts.Unset.Comment] should be set", func(t *testing.T) {
opts := defaultOpts()
opts.Unset = &MaterializedViewUnset{}
assertOptsInvalidJoinedErrors(t, opts, errAtLeastOneOf("AlterMaterializedViewOptions.Unset", "Secure", "Comment"))
assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterMaterializedViewOptions.Unset", "Secure", "Comment"))
})

t.Run("validation: exactly one field from [opts.Unset.Secure opts.Unset.Comment] should be set - more present", func(t *testing.T) {
opts := defaultOpts()
opts.Unset = &MaterializedViewUnset{
Secure: Bool(true),
Comment: Bool(true),
}
assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterMaterializedViewOptions.Unset", "Secure", "Comment"))
})

t.Run("rename", func(t *testing.T) {
Expand Down Expand Up @@ -205,31 +223,13 @@ func TestMaterializedViews_Alter(t *testing.T) {
assertOptsValidAndSQLEquals(t, opts, "ALTER MATERIALIZED VIEW %s SET SECURE", id.FullyQualifiedName())
})

t.Run("set multiple", func(t *testing.T) {
opts := defaultOpts()
opts.Set = &MaterializedViewSet{
Secure: Bool(true),
Comment: String("some comment"),
}
assertOptsValidAndSQLEquals(t, opts, "ALTER MATERIALIZED VIEW %s SET SECURE COMMENT = 'some comment'", id.FullyQualifiedName())
})

t.Run("unset single", func(t *testing.T) {
opts := defaultOpts()
opts.Unset = &MaterializedViewUnset{
Secure: Bool(true),
}
assertOptsValidAndSQLEquals(t, opts, "ALTER MATERIALIZED VIEW %s UNSET SECURE", id.FullyQualifiedName())
})

t.Run("unset multiple", func(t *testing.T) {
opts := defaultOpts()
opts.Unset = &MaterializedViewUnset{
Secure: Bool(true),
Comment: Bool(true),
}
assertOptsValidAndSQLEquals(t, opts, "ALTER MATERIALIZED VIEW %s UNSET SECURE, COMMENT", id.FullyQualifiedName())
})
}

func TestMaterializedViews_Drop(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/materialized_views_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (r materializedViewDBRow) convert() *MaterializedView {
materializedView.Reserved = &r.Reserved.String
}
if r.ClusterBy.Valid {
materializedView.ClusterBy = &r.ClusterBy.String
materializedView.ClusterBy = r.ClusterBy.String
}
if r.InvalidReason.Valid {
materializedView.InvalidReason = r.InvalidReason.String
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/materialized_views_validations_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (opts *AlterMaterializedViewOptions) validate() error {
}
}
if valueSet(opts.Set) {
if !anyValueSet(opts.Set.Secure, opts.Set.Comment) {
errs = append(errs, errAtLeastOneOf("AlterMaterializedViewOptions.Set", "Secure", "Comment"))
if !exactlyOneValueSet(opts.Set.Secure, opts.Set.Comment) {
errs = append(errs, errExactlyOneOf("AlterMaterializedViewOptions.Set", "Secure", "Comment"))
}
}
if valueSet(opts.Unset) {
if !anyValueSet(opts.Unset.Secure, opts.Unset.Comment) {
errs = append(errs, errAtLeastOneOf("AlterMaterializedViewOptions.Unset", "Secure", "Comment"))
if !exactlyOneValueSet(opts.Unset.Secure, opts.Unset.Comment) {
errs = append(errs, errExactlyOneOf("AlterMaterializedViewOptions.Unset", "Secure", "Comment"))
}
}
return JoinErrors(errs...)
Expand Down
87 changes: 83 additions & 4 deletions pkg/sdk/testint/materialized_views_gen_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestInt_MaterializedViews(t *testing.T) {
assert.Empty(t, view.Reserved)
assert.Equal(t, testDb(t).Name, view.DatabaseName)
assert.Equal(t, testSchema(t).Name, view.SchemaName)
assert.Equal(t, clusterBy, *view.ClusterBy)
assert.Equal(t, clusterBy, view.ClusterBy)
assert.Equal(t, 0, view.Rows)
assert.Equal(t, 0, view.Bytes)
assert.Equal(t, testDb(t).Name, view.SourceDatabaseName)
Expand Down Expand Up @@ -169,11 +169,45 @@ func TestInt_MaterializedViews(t *testing.T) {
})

t.Run("alter materialized view: rename", func(t *testing.T) {
// TODO: fill me
createRequest := createMaterializedViewBasicRequest(t)
id := createRequest.GetName()

err := client.MaterializedViews.Create(ctx, createRequest)
require.NoError(t, err)

newName := random.String()
newId := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, newName)
alterRequest := sdk.NewAlterMaterializedViewRequest(id).WithRenameTo(&newId)

err = client.MaterializedViews.Alter(ctx, alterRequest)
if err != nil {
t.Cleanup(cleanupMaterializedViewProvider(id))
} else {
t.Cleanup(cleanupMaterializedViewProvider(newId))
}
require.NoError(t, err)

_, err = client.MaterializedViews.ShowByID(ctx, id)
assert.ErrorIs(t, err, collections.ErrObjectNotFound)

view, err := client.MaterializedViews.ShowByID(ctx, newId)
require.NoError(t, err)

assertMaterializedView(t, view, newId)
})

t.Run("alter materialized view: set cluster by", func(t *testing.T) {
// TODO: fill me
view := createMaterializedView(t)
id := view.ID()

alterRequest := sdk.NewAlterMaterializedViewRequest(id).WithClusterBy(sdk.NewMaterializedViewClusterByRequest().WithExpressions([]sdk.MaterializedViewClusterByExpressionRequest{{"ID"}}))
err := client.MaterializedViews.Alter(ctx, alterRequest)
require.NoError(t, err)

alteredView, err := client.MaterializedViews.ShowByID(ctx, id)
require.NoError(t, err)

assert.Equal(t, fmt.Sprintf(`LINEAR("%s")`, "ID"), alteredView.ClusterBy)
})

t.Run("alter materialized view: recluster suspend and resume", func(t *testing.T) {
Expand All @@ -185,7 +219,52 @@ func TestInt_MaterializedViews(t *testing.T) {
})

t.Run("alter materialized view: set and unset values", func(t *testing.T) {
// TODO: fill me
view := createMaterializedView(t)
id := view.ID()

alterRequest := sdk.NewAlterMaterializedViewRequest(id).WithSet(
sdk.NewMaterializedViewSetRequest().WithSecure(sdk.Bool(true)),
)
err := client.MaterializedViews.Alter(ctx, alterRequest)
require.NoError(t, err)

alteredView, err := client.MaterializedViews.ShowByID(ctx, id)
require.NoError(t, err)

assert.Equal(t, true, alteredView.IsSecure)

alterRequest = sdk.NewAlterMaterializedViewRequest(id).WithSet(
sdk.NewMaterializedViewSetRequest().WithComment(sdk.String("comment")),
)
err = client.MaterializedViews.Alter(ctx, alterRequest)
require.NoError(t, err)

alteredView, err = client.MaterializedViews.ShowByID(ctx, id)
require.NoError(t, err)

assert.Equal(t, "comment", alteredView.Comment)

alterRequest = sdk.NewAlterMaterializedViewRequest(id).WithUnset(
sdk.NewMaterializedViewUnsetRequest().WithComment(sdk.Bool(true)),
)
err = client.MaterializedViews.Alter(ctx, alterRequest)
require.NoError(t, err)

alteredView, err = client.MaterializedViews.ShowByID(ctx, id)
require.NoError(t, err)

assert.Equal(t, "", alteredView.Comment)

alterRequest = sdk.NewAlterMaterializedViewRequest(id).WithUnset(
sdk.NewMaterializedViewUnsetRequest().WithSecure(sdk.Bool(true)),
)
err = client.MaterializedViews.Alter(ctx, alterRequest)
require.NoError(t, err)

alteredView, err = client.MaterializedViews.ShowByID(ctx, id)
require.NoError(t, err)

assert.Equal(t, false, alteredView.IsSecure)
})

t.Run("show materialized view: default", func(t *testing.T) {
Expand Down

0 comments on commit 6934366

Please sign in to comment.