Skip to content

Commit 79482ea

Browse files
sfc-gh-sarurshreyas
andauthored
feat: add integration tests for semantic views (#3978)
<!-- Feel free to delete comments as you fill this in --> <!-- summary of changes --> ## Test Plan <!-- detail ways in which this PR has been tested or needs to be tested --> * [ ] acceptance tests <!-- add more below if you think they are relevant --> * [ ] … ## References <!-- issues documentation links, etc --> * --------- Co-authored-by: shreyas <[email protected]>
1 parent 84190e6 commit 79482ea

File tree

11 files changed

+582
-11
lines changed

11 files changed

+582
-11
lines changed

pkg/acceptance/bettertestspoc/assert/objectassert/gen/sdk_object_def.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ var allStructs = []SdkObjectDef{
137137
ObjectType: sdk.ObjectTypeListing,
138138
ObjectStruct: sdk.Listing{},
139139
},
140+
{
141+
IdType: "sdk.SchemaObjectIdentifier",
142+
ObjectType: sdk.ObjectTypeSemanticView,
143+
ObjectStruct: sdk.SemanticView{},
144+
},
140145
}
141146

142147
func GetSdkObjectDetails() []genhelpers.SdkObjectDetails {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package objectassert
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"testing"
7+
8+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"
9+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers"
10+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
11+
)
12+
13+
type SemanticViewDetailsCollection struct {
14+
Details []sdk.SemanticViewDetails
15+
}
16+
type SemanticViewDetailsAssert struct {
17+
*assert.SnowflakeObjectAssert[SemanticViewDetailsCollection, sdk.SchemaObjectIdentifier]
18+
}
19+
20+
func SemanticViewDetails(t *testing.T, id sdk.SchemaObjectIdentifier) *SemanticViewDetailsAssert {
21+
t.Helper()
22+
return &SemanticViewDetailsAssert{
23+
assert.NewSnowflakeObjectAssertWithTestClientObjectProvider(sdk.ObjectTypeSemanticView, id, func(testClient *helpers.TestClient) assert.ObjectProvider[SemanticViewDetailsCollection, sdk.SchemaObjectIdentifier] {
24+
return func(t *testing.T, id sdk.SchemaObjectIdentifier) (*SemanticViewDetailsCollection, error) {
25+
details, err := testClient.SemanticView.Describe(t, id)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return &SemanticViewDetailsCollection{Details: details}, nil
30+
}
31+
}),
32+
}
33+
}
34+
35+
func (s *SemanticViewDetailsAssert) HasDetailsCount(expected int) *SemanticViewDetailsAssert {
36+
s.AddAssertion(func(t *testing.T, o *SemanticViewDetailsCollection) error {
37+
t.Helper()
38+
if len(o.Details) != expected {
39+
return fmt.Errorf("expected %d semantic view details; got: %d", expected, len(o.Details))
40+
}
41+
return nil
42+
})
43+
return s
44+
}
45+
46+
func (s *SemanticViewDetailsAssert) ContainsDetail(expected sdk.SemanticViewDetails) *SemanticViewDetailsAssert {
47+
s.AddAssertion(func(t *testing.T, o *SemanticViewDetailsCollection) error {
48+
t.Helper()
49+
found := slices.ContainsFunc(o.Details, func(detail sdk.SemanticViewDetails) bool {
50+
return detail.ObjectKind == expected.ObjectKind &&
51+
detail.ObjectName == expected.ObjectName &&
52+
detail.Property == expected.Property &&
53+
detail.PropertyValue == expected.PropertyValue
54+
})
55+
if !found {
56+
return fmt.Errorf("expected semantic view to contain a detail row matching %v", expected)
57+
}
58+
return nil
59+
})
60+
return s
61+
}
62+
63+
func NewSemanticViewDetails(
64+
ObjectKind string,
65+
ObjectName string,
66+
ParentEntity *string,
67+
Property string,
68+
PropertyValue string,
69+
) sdk.SemanticViewDetails {
70+
details := sdk.SemanticViewDetails{
71+
ObjectKind: ObjectKind,
72+
ObjectName: ObjectName,
73+
Property: Property,
74+
PropertyValue: PropertyValue,
75+
}
76+
if ParentEntity != nil {
77+
details.ParentEntity = ParentEntity
78+
}
79+
80+
return details
81+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package objectassert
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
8+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
9+
)
10+
11+
func (s *SemanticViewAssert) HasCreatedOnNotEmpty() *SemanticViewAssert {
12+
s.AddAssertion(func(t *testing.T, o *sdk.SemanticView) error {
13+
t.Helper()
14+
if o.CreatedOn == (time.Time{}) {
15+
return fmt.Errorf("expected created_on to be not empty")
16+
}
17+
return nil
18+
})
19+
return s
20+
}
21+
22+
func (s *SemanticViewAssert) HasNoComment() *SemanticViewAssert {
23+
s.AddAssertion(func(t *testing.T, o *sdk.SemanticView) error {
24+
t.Helper()
25+
if o.Comment != nil {
26+
return fmt.Errorf("expected comment to be empty")
27+
}
28+
return nil
29+
})
30+
return s
31+
}

pkg/acceptance/bettertestspoc/assert/objectassert/semantic_view_snowflake_gen.go

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package helpers
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
type SemanticViewClient struct {
12+
context *TestClientContext
13+
ids *IdsGenerator
14+
}
15+
16+
func NewSemanticViewClient(context *TestClientContext, idsGenerator *IdsGenerator) *SemanticViewClient {
17+
return &SemanticViewClient{
18+
context: context,
19+
ids: idsGenerator,
20+
}
21+
}
22+
23+
func (c *SemanticViewClient) client() sdk.SemanticViews {
24+
return c.context.client.SemanticViews
25+
}
26+
27+
func (c *SemanticViewClient) Create(t *testing.T) (*sdk.SemanticView, func()) {
28+
t.Helper()
29+
return c.CreateWithId(t, c.ids.RandomSchemaObjectIdentifier())
30+
}
31+
32+
func (c *SemanticViewClient) CreateWithId(t *testing.T, id sdk.SchemaObjectIdentifier) (*sdk.SemanticView, func()) {
33+
t.Helper()
34+
return c.CreateWithRequest(t, sdk.NewCreateSemanticViewRequest(id, []sdk.LogicalTableRequest{}))
35+
}
36+
37+
func (c *SemanticViewClient) CreateWithRequest(t *testing.T, req *sdk.CreateSemanticViewRequest) (*sdk.SemanticView, func()) {
38+
t.Helper()
39+
ctx := context.Background()
40+
41+
err := c.client().Create(ctx, req)
42+
require.NoError(t, err)
43+
semanticView, err := c.client().ShowByID(ctx, req.GetName())
44+
require.NoError(t, err)
45+
return semanticView, c.DropFunc(t, req.GetName())
46+
}
47+
48+
func (c *SemanticViewClient) DropFunc(t *testing.T, id sdk.SchemaObjectIdentifier) func() {
49+
t.Helper()
50+
ctx := context.Background()
51+
52+
return func() {
53+
err := c.client().Drop(ctx, sdk.NewDropSemanticViewRequest(id).WithIfExists(true))
54+
require.NoError(t, err)
55+
}
56+
}
57+
58+
func (c *SemanticViewClient) Show(t *testing.T, id sdk.SchemaObjectIdentifier) (*sdk.SemanticView, error) {
59+
t.Helper()
60+
ctx := context.Background()
61+
return c.client().ShowByID(ctx, id)
62+
}
63+
64+
func (c *SemanticViewClient) Describe(t *testing.T, id sdk.SchemaObjectIdentifier) ([]sdk.SemanticViewDetails, error) {
65+
t.Helper()
66+
ctx := context.Background()
67+
return c.client().Describe(ctx, id)
68+
}

pkg/acceptance/helpers/test_client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type TestClient struct {
7070
Sequence *SequenceClient
7171
SessionPolicy *SessionPolicyClient
7272
Share *ShareClient
73+
SemanticView *SemanticViewClient
7374
Snapshot *SnapshotClient
7475
Stage *StageClient
7576
StorageIntegration *StorageIntegrationClient
@@ -150,6 +151,7 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
150151
Schema: NewSchemaClient(context, idsGenerator),
151152
Secret: NewSecretClient(context, idsGenerator),
152153
SecurityIntegration: NewSecurityIntegrationClient(context, idsGenerator),
154+
SemanticView: NewSemanticViewClient(context, idsGenerator),
153155
Snapshot: NewSnapshotClient(context, idsGenerator),
154156
Service: NewServiceClient(context, idsGenerator),
155157
Sequence: NewSequenceClient(context, idsGenerator),

pkg/sdk/semantic_view_def.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ var semanticView = g.PlainStruct("SemanticView").
2727
var semanticViewDetailsDbRow = g.DbStruct("semanticViewDetailsRow").
2828
Text("object_kind").
2929
Text("object_name").
30-
Text("parent_entity").
30+
OptionalText("parent_entity").
3131
Text("property").
3232
Text("property_value")
3333

3434
var semanticViewDetails = g.PlainStruct("SemanticViewDetails").
3535
Text("ObjectKind").
3636
Text("ObjectName").
37-
Text("ParentEntity").
37+
OptionalText("ParentEntity").
3838
Text("Property").
3939
Text("PropertyValue")
4040

pkg/sdk/semantic_view_ext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sdk
2+
3+
func (s *CreateSemanticViewRequest) GetName() SchemaObjectIdentifier {
4+
return s.name
5+
}

pkg/sdk/semantic_view_gen.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,17 @@ type DescribeSemanticViewOptions struct {
146146
}
147147

148148
type semanticViewDetailsRow struct {
149-
ObjectKind string `db:"object_kind"`
150-
ObjectName string `db:"object_name"`
151-
ParentEntity string `db:"parent_entity"`
152-
Property string `db:"property"`
153-
PropertyValue string `db:"property_value"`
149+
ObjectKind string `db:"object_kind"`
150+
ObjectName string `db:"object_name"`
151+
ParentEntity sql.NullString `db:"parent_entity"`
152+
Property string `db:"property"`
153+
PropertyValue string `db:"property_value"`
154154
}
155155

156156
type SemanticViewDetails struct {
157157
ObjectKind string
158158
ObjectName string
159-
ParentEntity string
159+
ParentEntity *string
160160
Property string
161161
PropertyValue string
162162
}

0 commit comments

Comments
 (0)