Skip to content

Commit

Permalink
Add integration tests for identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Jul 30, 2024
1 parent 80be778 commit 6b74a9f
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 14 deletions.
44 changes: 44 additions & 0 deletions pkg/acceptance/helpers/grant_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package helpers

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

type GrantClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewGrantClient(context *TestClientContext, idsGenerator *IdsGenerator) *GrantClient {
return &GrantClient{
context: context,
ids: idsGenerator,
}
}

func (c *GrantClient) client() sdk.Grants {
return c.context.client.Grants
}

func (c *GrantClient) GrantOnSchemaToAccountRole(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, accountRoleId sdk.AccountObjectIdentifier, privileges ...sdk.SchemaPrivilege) {
t.Helper()
ctx := context.Background()

err := c.client().GrantPrivilegesToAccountRole(
ctx,
&sdk.AccountRoleGrantPrivileges{
SchemaPrivileges: privileges,
},
&sdk.AccountRoleGrantOn{
Schema: &sdk.GrantOnSchema{
Schema: &schemaId,
},
},
accountRoleId,
new(sdk.GrantPrivilegesToAccountRoleOptions),
)
require.NoError(t, err)
}
2 changes: 2 additions & 0 deletions pkg/acceptance/helpers/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type TestClient struct {
ExternalVolume *ExternalVolumeClient
FailoverGroup *FailoverGroupClient
FileFormat *FileFormatClient
Grant *GrantClient
MaskingPolicy *MaskingPolicyClient
MaterializedView *MaterializedViewClient
NetworkPolicy *NetworkPolicyClient
Expand Down Expand Up @@ -77,6 +78,7 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
ExternalVolume: NewExternalVolumeClient(context, idsGenerator),
FailoverGroup: NewFailoverGroupClient(context, idsGenerator),
FileFormat: NewFileFormatClient(context, idsGenerator),
Grant: NewGrantClient(context, idsGenerator),
MaskingPolicy: NewMaskingPolicyClient(context, idsGenerator),
MaterializedView: NewMaterializedViewClient(context, idsGenerator),
NetworkPolicy: NewNetworkPolicyClient(context, idsGenerator),
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/secondary_database_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestAcc_CreateSecondaryDatabase_complete(t *testing.T) {
sdk.NewAccountIdentifierFromAccountLocator(acc.Client(t).GetAccountLocator()),
})
t.Cleanup(func() {
// TODO(SNOW-1562172): Create a better solution for this type of situations
// TODO(SNOW-1562172: Create a better solution for this type of situations
// Have to wait; otherwise the secondary database removal can be not registered yet,
// resulting in an error in the cleanup below.
time.Sleep(time.Second)
Expand Down Expand Up @@ -408,7 +408,7 @@ func TestAcc_CreateSecondaryDatabase_DataRetentionTimeInDays(t *testing.T) {
sdk.NewAccountIdentifierFromAccountLocator(acc.Client(t).GetAccountLocator()),
})
t.Cleanup(func() {
// TODO(SNOW-1562172): Create a better solution for this type of situations
// TODO(SNOW-1562172: Create a better solution for this type of situations
// Have to wait; otherwise the secondary database removal can be not registered yet,
// resulting in an error in the cleanup below.
time.Sleep(time.Second)
Expand Down
5 changes: 5 additions & 0 deletions pkg/sdk/identifier_parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func ParseIdentifierStringWithOpts(identifier string, opts func(*csv.Reader)) ([
if len(lines) != 1 {
return nil, fmt.Errorf("incompatible identifier: %s", identifier)
}
for _, part := range lines[0] {
if strings.Contains(part, `"`) {
return nil, fmt.Errorf(`unable to parse identifier: %s, currently identifiers containing double quotes are not supported in the provider`, identifier)
}
}
return lines[0], nil
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/sdk/identifier_parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,72 +125,72 @@ func Test_IdentifierParsers(t *testing.T) {
{IdentifierType: "AccountObjectIdentifier", Input: "a\nb", Error: "incompatible identifier: a\nb"},
{IdentifierType: "AccountObjectIdentifier", Input: `a"b`, Error: "unable to read identifier: a\"b, err = parse error on line 1, column 2: bare \" in non-quoted-field"},
{IdentifierType: "AccountObjectIdentifier", Input: `abc.cde`, Error: `unexpected number of parts 2 in identifier abc.cde, expected 1 in a form of "<account_object_name>"`},
{IdentifierType: "AccountObjectIdentifier", Input: `""""`, Error: `unable to parse identifier: """", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "AccountObjectIdentifier", Input: `"a""bc"`, Error: `unable to parse identifier: "a""b", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "AccountObjectIdentifier", Input: `""`, Expected: NewAccountObjectIdentifier(``)},
{IdentifierType: "AccountObjectIdentifier", Input: `""""`, Expected: NewAccountObjectIdentifier(`"`)},
{IdentifierType: "AccountObjectIdentifier", Input: `abc`, Expected: NewAccountObjectIdentifier(`abc`)},
{IdentifierType: "AccountObjectIdentifier", Input: `"abc"`, Expected: NewAccountObjectIdentifier(`abc`)},
{IdentifierType: "AccountObjectIdentifier", Input: `"ab.c"`, Expected: NewAccountObjectIdentifier(`ab.c`)},
{IdentifierType: "AccountObjectIdentifier", Input: `"a""bc"`, Expected: NewAccountObjectIdentifier(`a"bc`)},

{IdentifierType: "DatabaseObjectIdentifier", Input: ``, Error: "incompatible identifier: "},
{IdentifierType: "DatabaseObjectIdentifier", Input: "a\nb.cde", Error: "unable to read identifier: a\nb.cde, err = record on line 2: wrong number of fields"},
{IdentifierType: "DatabaseObjectIdentifier", Input: `a"b.cde`, Error: "unable to read identifier: a\"b.cde, err = parse error on line 1, column 2: bare \" in non-quoted-field"},
{IdentifierType: "DatabaseObjectIdentifier", Input: `abc.cde.efg`, Error: `unexpected number of parts 3 in identifier abc.cde.efg, expected 2 in a form of "<database_name>.<database_object_name>"`},
{IdentifierType: "DatabaseObjectIdentifier", Input: `abc`, Error: `unexpected number of parts 1 in identifier abc, expected 2 in a form of "<database_name>.<database_object_name>"`},
{IdentifierType: "DatabaseObjectIdentifier", Input: `"""".""""`, Error: `unable to parse identifier: """"."""", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "DatabaseObjectIdentifier", Input: `"a""bc"."cd""e"`, Error: `unable to parse identifier: "a""bc"."cd""e", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "DatabaseObjectIdentifier", Input: `"".""`, Expected: NewDatabaseObjectIdentifier(``, ``)},
{IdentifierType: "DatabaseObjectIdentifier", Input: `"""".""""`, Expected: NewDatabaseObjectIdentifier(`"`, `"`)},
{IdentifierType: "DatabaseObjectIdentifier", Input: `abc.cde`, Expected: NewDatabaseObjectIdentifier(`abc`, `cde`)},
{IdentifierType: "DatabaseObjectIdentifier", Input: `"abc"."cde"`, Expected: NewDatabaseObjectIdentifier(`abc`, `cde`)},
{IdentifierType: "DatabaseObjectIdentifier", Input: `"ab.c"."cd.e"`, Expected: NewDatabaseObjectIdentifier(`ab.c`, `cd.e`)},
{IdentifierType: "DatabaseObjectIdentifier", Input: `"a""bc"."cd""e"`, Expected: NewDatabaseObjectIdentifier(`a"bc`, `cd"e`)},

{IdentifierType: "SchemaObjectIdentifier", Input: ``, Error: "incompatible identifier: "},
{IdentifierType: "SchemaObjectIdentifier", Input: "a\nb.cde.efg", Error: "unable to read identifier: a\nb.cde.efg, err = record on line 2: wrong number of fields"},
{IdentifierType: "SchemaObjectIdentifier", Input: `a"b.cde.efg`, Error: "unable to read identifier: a\"b.cde.efg, err = parse error on line 1, column 2: bare \" in non-quoted-field"},
{IdentifierType: "SchemaObjectIdentifier", Input: `abc.cde.efg.ghi`, Error: `unexpected number of parts 4 in identifier abc.cde.efg.ghi, expected 3 in a form of "<database_name>.<schema_name>.<schema_object_name>"`},
{IdentifierType: "SchemaObjectIdentifier", Input: `abc.cde`, Error: `unexpected number of parts 2 in identifier abc.cde, expected 3 in a form of "<database_name>.<schema_name>.<schema_object_name>"`},
{IdentifierType: "SchemaObjectIdentifier", Input: `""""."""".""""`, Error: `unable to parse identifier: """".""""."""", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "SchemaObjectIdentifier", Input: `"a""bc"."cd""e"."ef""g"`, Error: `unable to parse identifier: "a""bc"."cd""e"."ef""g", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "SchemaObjectIdentifier", Input: `""."".""`, Expected: NewSchemaObjectIdentifier(``, ``, ``)},
{IdentifierType: "SchemaObjectIdentifier", Input: `""""."""".""""`, Expected: NewSchemaObjectIdentifier(`"`, `"`, `"`)},
{IdentifierType: "SchemaObjectIdentifier", Input: `abc.cde.efg`, Expected: NewSchemaObjectIdentifier(`abc`, `cde`, `efg`)},
{IdentifierType: "SchemaObjectIdentifier", Input: `"abc"."cde"."efg"`, Expected: NewSchemaObjectIdentifier(`abc`, `cde`, `efg`)},
{IdentifierType: "SchemaObjectIdentifier", Input: `"ab.c"."cd.e"."ef.g"`, Expected: NewSchemaObjectIdentifier(`ab.c`, `cd.e`, `ef.g`)},
{IdentifierType: "SchemaObjectIdentifier", Input: `"a""bc"."cd""e"."ef""g"`, Expected: NewSchemaObjectIdentifier(`a"bc`, `cd"e`, `ef"g`)},

{IdentifierType: "TableColumnIdentifier", Input: ``, Error: "incompatible identifier: "},
{IdentifierType: "TableColumnIdentifier", Input: "a\nb.cde.efg.ghi", Error: "unable to read identifier: a\nb.cde.efg.ghi, err = record on line 2: wrong number of fields"},
{IdentifierType: "TableColumnIdentifier", Input: `a"b.cde.efg.ghi`, Error: "unable to read identifier: a\"b.cde.efg.ghi, err = parse error on line 1, column 2: bare \" in non-quoted-field"},
{IdentifierType: "TableColumnIdentifier", Input: `abc.cde.efg.ghi.ijk`, Error: `unexpected number of parts 5 in identifier abc.cde.efg.ghi.ijk, expected 4 in a form of "<database_name>.<schema_name>.<table_name>.<table_column_name>"`},
{IdentifierType: "TableColumnIdentifier", Input: `abc.cde`, Error: `unexpected number of parts 2 in identifier abc.cde, expected 4 in a form of "<database_name>.<schema_name>.<table_name>.<table_column_name>"`},
{IdentifierType: "TableColumnIdentifier", Input: `"""".""""."""".""""`, Error: `unable to parse identifier: """"."""".""""."""", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "TableColumnIdentifier", Input: `"a""bc"."cd""e"."ef""g"."gh""i"`, Error: `unable to parse identifier: "a""bc"."cd""e"."ef""g"."gh""i", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "TableColumnIdentifier", Input: `"".""."".""`, Expected: NewTableColumnIdentifier(``, ``, ``, ``)},
{IdentifierType: "TableColumnIdentifier", Input: `"""".""""."""".""""`, Expected: NewTableColumnIdentifier(`"`, `"`, `"`, `"`)},
{IdentifierType: "TableColumnIdentifier", Input: `abc.cde.efg.ghi`, Expected: NewTableColumnIdentifier(`abc`, `cde`, `efg`, `ghi`)},
{IdentifierType: "TableColumnIdentifier", Input: `"abc"."cde"."efg"."ghi"`, Expected: NewTableColumnIdentifier(`abc`, `cde`, `efg`, `ghi`)},
{IdentifierType: "TableColumnIdentifier", Input: `"ab.c"."cd.e"."ef.g"."gh.i"`, Expected: NewTableColumnIdentifier(`ab.c`, `cd.e`, `ef.g`, `gh.i`)},
{IdentifierType: "TableColumnIdentifier", Input: `"a""bc"."cd""e"."ef""g"."gh""i"`, Expected: NewTableColumnIdentifier(`a"bc`, `cd"e`, `ef"g`, `gh"i`)},

{IdentifierType: "AccountIdentifier", Input: ``, Error: "incompatible identifier: "},
{IdentifierType: "AccountIdentifier", Input: "a\nb.cde", Error: "unable to read identifier: a\nb.cde, err = record on line 2: wrong number of fields"},
{IdentifierType: "AccountIdentifier", Input: `a"b.cde`, Error: "unable to read identifier: a\"b.cde, err = parse error on line 1, column 2: bare \" in non-quoted-field"},
{IdentifierType: "AccountIdentifier", Input: `abc.cde.efg`, Error: `unexpected number of parts 3 in identifier abc.cde.efg, expected 2 in a form of "<organization_name>.<account_name>"`},
{IdentifierType: "AccountIdentifier", Input: `abc`, Error: `unexpected number of parts 1 in identifier abc, expected 2 in a form of "<organization_name>.<account_name>"`},
{IdentifierType: "AccountIdentifier", Input: `"""".""""`, Error: `unable to parse identifier: """"."""", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "AccountIdentifier", Input: `"a""bc"."cd""e"`, Error: `unable to parse identifier: "a""bc"."cd""e", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "AccountIdentifier", Input: `"".""`, Expected: NewAccountIdentifier(``, ``)},
{IdentifierType: "AccountIdentifier", Input: `"""".""""`, Expected: NewAccountIdentifier(`"`, `"`)},
{IdentifierType: "AccountIdentifier", Input: `abc.cde`, Expected: NewAccountIdentifier(`abc`, `cde`)},
{IdentifierType: "AccountIdentifier", Input: `"abc"."cde"`, Expected: NewAccountIdentifier(`abc`, `cde`)},
{IdentifierType: "AccountIdentifier", Input: `"ab.c"."cd.e"`, Expected: NewAccountIdentifier(`ab.c`, `cd.e`)},
{IdentifierType: "AccountIdentifier", Input: `"a""bc"."cd""e"`, Expected: NewAccountIdentifier(`a"bc`, `cd"e`)},

{IdentifierType: "ExternalObjectIdentifier", Input: ``, Error: "incompatible identifier: "},
{IdentifierType: "ExternalObjectIdentifier", Input: "a\nb.cde.efg", Error: "unable to read identifier: a\nb.cde.efg, err = record on line 2: wrong number of fields"},
{IdentifierType: "ExternalObjectIdentifier", Input: `a"b.cde.efg`, Error: "unable to read identifier: a\"b.cde.efg, err = parse error on line 1, column 2: bare \" in non-quoted-field"},
{IdentifierType: "ExternalObjectIdentifier", Input: `abc.cde.efg.ghi`, Error: `unexpected number of parts 4 in identifier abc.cde.efg.ghi, expected 3 in a form of "<organization_name>.<account_name>.<external_object_name>"`},
{IdentifierType: "ExternalObjectIdentifier", Input: `abc.cde`, Error: `unexpected number of parts 2 in identifier abc.cde, expected 3 in a form of "<organization_name>.<account_name>.<external_object_name>"`},
{IdentifierType: "ExternalObjectIdentifier", Input: `""""."""".""""`, Error: `unable to parse identifier: """".""""."""", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "ExternalObjectIdentifier", Input: `"a""bc"."cd""e"."ef""g"`, Error: `unable to parse identifier: "a""bc"."cd""e"."ef""g", currently identifiers containing double quotes are not supported in the provider`},
{IdentifierType: "ExternalObjectIdentifier", Input: `""."".""`, Expected: NewExternalObjectIdentifier(NewAccountIdentifier(``, ``), NewAccountObjectIdentifier(``))},
{IdentifierType: "ExternalObjectIdentifier", Input: `""""."""".""""`, Expected: NewExternalObjectIdentifier(NewAccountIdentifier(`"`, `"`), NewAccountObjectIdentifier(`"`))},
{IdentifierType: "ExternalObjectIdentifier", Input: `abc.cde.efg`, Expected: NewExternalObjectIdentifier(NewAccountIdentifier(`abc`, `cde`), NewAccountObjectIdentifier(`efg`))},
{IdentifierType: "ExternalObjectIdentifier", Input: `"abc"."cde"."efg"`, Expected: NewExternalObjectIdentifier(NewAccountIdentifier(`abc`, `cde`), NewAccountObjectIdentifier(`efg`))},
{IdentifierType: "ExternalObjectIdentifier", Input: `"ab.c"."cd.e"."ef.g"`, Expected: NewExternalObjectIdentifier(NewAccountIdentifier(`ab.c`, `cd.e`), NewAccountObjectIdentifier(`ef.g`))},
{IdentifierType: "ExternalObjectIdentifier", Input: `"a""bc"."cd""e"."ef""g"`, Expected: NewExternalObjectIdentifier(NewAccountIdentifier(`a"bc`, `cd"e`), NewAccountObjectIdentifier(`ef"g`))},
}

for _, testCase := range testCases {
Expand Down
Loading

0 comments on commit 6b74a9f

Please sign in to comment.