Skip to content

Commit

Permalink
chore: Integration tests setup db once (#2118)
Browse files Browse the repository at this point in the history
* WIP

* Prepare setup

* Use created test db in one of the int tests

* Use test db in all (almost) all integration tests

* Fix linter complaints
  • Loading branch information
sfc-gh-asawicki committed Oct 13, 2023
1 parent 3f528a8 commit f533368
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 421 deletions.
20 changes: 7 additions & 13 deletions pkg/sdk/testint/accounts_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,9 @@ func TestInt_AccountAlter(t *testing.T) {
})

t.Run("set and unset password policy", func(t *testing.T) {
databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)
schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)
passwordPolicyTest, passwordPolicyCleanup := createPasswordPolicy(t, client, databaseTest, schemaTest)
passwordPolicyTest, passwordPolicyCleanup := createPasswordPolicy(t, client, testDb(t), schemaTest)
t.Cleanup(passwordPolicyCleanup)
opts := &sdk.AlterAccountOptions{
Set: &sdk.AccountSet{
Expand All @@ -259,11 +257,9 @@ func TestInt_AccountAlter(t *testing.T) {
})

t.Run("set and unset session policy", func(t *testing.T) {
databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)
schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)
sessionPolicyTest, sessionPolicyCleanup := createSessionPolicy(t, client, databaseTest, schemaTest)
sessionPolicyTest, sessionPolicyCleanup := createSessionPolicy(t, client, testDb(t), schemaTest)
t.Cleanup(sessionPolicyCleanup)
opts := &sdk.AlterAccountOptions{
Set: &sdk.AccountSet{
Expand All @@ -284,13 +280,11 @@ func TestInt_AccountAlter(t *testing.T) {
})

t.Run("set and unset tag", func(t *testing.T) {
databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)
schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)
tagTest1, tagCleanup1 := createTag(t, client, databaseTest, schemaTest)
tagTest1, tagCleanup1 := createTag(t, client, testDb(t), schemaTest)
t.Cleanup(tagCleanup1)
tagTest2, tagCleanup2 := createTag(t, client, databaseTest, schemaTest)
tagTest2, tagCleanup2 := createTag(t, client, testDb(t), schemaTest)
t.Cleanup(tagCleanup2)

opts := &sdk.AlterAccountOptions{
Expand Down
52 changes: 19 additions & 33 deletions pkg/sdk/testint/alerts_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ func TestInt_AlertsShow(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)

schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)

testWarehouse, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)

alertTest, alertCleanup := createAlert(t, client, databaseTest, schemaTest, testWarehouse)
alertTest, alertCleanup := createAlert(t, client, testDb(t), schemaTest, testWarehouse)
t.Cleanup(alertCleanup)

alert2Test, alert2Cleanup := createAlert(t, client, databaseTest, schemaTest, testWarehouse)
alert2Test, alert2Cleanup := createAlert(t, client, testDb(t), schemaTest, testWarehouse)
t.Cleanup(alert2Cleanup)

t.Run("without show options", func(t *testing.T) {
Expand Down Expand Up @@ -54,7 +51,7 @@ func TestInt_AlertsShow(t *testing.T) {
Pattern: sdk.String(alertTest.Name),
},
In: &sdk.In{
Database: databaseTest.ID(),
Database: testDb(t).ID(),
},
}
alerts, err := client.Alerts.Show(ctx, showOptions)
Expand Down Expand Up @@ -90,10 +87,8 @@ func TestInt_AlertsShow(t *testing.T) {
func TestInt_AlertCreate(t *testing.T) {
client := testClient(t)
ctx := testContext(t)
databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)

schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)

testWarehouse, warehouseCleanup := createWarehouse(t, client)
Expand All @@ -105,7 +100,7 @@ func TestInt_AlertCreate(t *testing.T) {
condition := "SELECT 1"
action := "SELECT 1"
comment := random.Comment()
id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name)
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, schemaTest.Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, &sdk.CreateAlertOptions{
OrReplace: sdk.Bool(true),
IfNotExists: sdk.Bool(false),
Expand Down Expand Up @@ -141,7 +136,7 @@ func TestInt_AlertCreate(t *testing.T) {
condition := "SELECT 1"
action := "SELECT 1"
comment := random.Comment()
id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name)
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, schemaTest.Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, &sdk.CreateAlertOptions{
OrReplace: sdk.Bool(false),
IfNotExists: sdk.Bool(true),
Expand Down Expand Up @@ -176,7 +171,7 @@ func TestInt_AlertCreate(t *testing.T) {
schedule := "USING CRON * * * * TUE,THU UTC"
condition := "SELECT 1"
action := "SELECT 1"
id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name)
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, schemaTest.Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, nil)
require.NoError(t, err)
alertDetails, err := client.Alerts.Describe(ctx, id)
Expand Down Expand Up @@ -214,7 +209,7 @@ func TestInt_AlertCreate(t *testing.T) {
2
end
`
id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name)
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, schemaTest.Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, nil)
require.NoError(t, err)
alertDetails, err := client.Alerts.Describe(ctx, id)
Expand Down Expand Up @@ -244,16 +239,13 @@ func TestInt_AlertDescribe(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)

schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)

warehouseTest, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)

alert, alertCleanup := createAlert(t, client, databaseTest, schemaTest, warehouseTest)
alert, alertCleanup := createAlert(t, client, testDb(t), schemaTest, warehouseTest)
t.Cleanup(alertCleanup)

t.Run("when alert exists", func(t *testing.T) {
Expand All @@ -263,7 +255,7 @@ func TestInt_AlertDescribe(t *testing.T) {
})

t.Run("when alert does not exist", func(t *testing.T) {
id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, "does_not_exist")
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, schemaTest.Name, "does_not_exist")
_, err := client.Alerts.Describe(ctx, id)
assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized)
})
Expand All @@ -273,17 +265,14 @@ func TestInt_AlertAlter(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)

schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)

warehouseTest, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)

t.Run("when setting and unsetting a value", func(t *testing.T) {
alert, alertCleanup := createAlert(t, client, databaseTest, schemaTest, warehouseTest)
alert, alertCleanup := createAlert(t, client, testDb(t), schemaTest, warehouseTest)
t.Cleanup(alertCleanup)
newSchedule := "USING CRON * * * * TUE,FRI GMT"

Expand All @@ -309,7 +298,7 @@ func TestInt_AlertAlter(t *testing.T) {
})

t.Run("when modifying condition and action", func(t *testing.T) {
alert, alertCleanup := createAlert(t, client, databaseTest, schemaTest, warehouseTest)
alert, alertCleanup := createAlert(t, client, testDb(t), schemaTest, warehouseTest)
t.Cleanup(alertCleanup)
newCondition := "select * from DUAL where false"

Expand Down Expand Up @@ -353,7 +342,7 @@ func TestInt_AlertAlter(t *testing.T) {
})

t.Run("resume and then suspend", func(t *testing.T) {
alert, alertCleanup := createAlert(t, client, databaseTest, schemaTest, warehouseTest)
alert, alertCleanup := createAlert(t, client, testDb(t), schemaTest, warehouseTest)
t.Cleanup(alertCleanup)

alterOptions := &sdk.AlterAlertOptions{
Expand Down Expand Up @@ -398,17 +387,14 @@ func TestInt_AlertDrop(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

databaseTest, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)

schemaTest, schemaCleanup := createSchema(t, client, databaseTest)
schemaTest, schemaCleanup := createSchema(t, client, testDb(t))
t.Cleanup(schemaCleanup)

warehouseTest, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)

t.Run("when alert exists", func(t *testing.T) {
alert, _ := createAlert(t, client, databaseTest, schemaTest, warehouseTest)
alert, _ := createAlert(t, client, testDb(t), schemaTest, warehouseTest)
id := alert.ID()
err := client.Alerts.Drop(ctx, id)
require.NoError(t, err)
Expand All @@ -417,7 +403,7 @@ func TestInt_AlertDrop(t *testing.T) {
})

t.Run("when alert does not exist", func(t *testing.T) {
id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, "does_not_exist")
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, schemaTest.Name, "does_not_exist")
err := client.Alerts.Drop(ctx, id)
assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized)
})
Expand Down
37 changes: 17 additions & 20 deletions pkg/sdk/testint/database_role_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ func TestInt_DatabaseRoles(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

database, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)

assertDatabaseRole := func(t *testing.T, databaseRole *sdk.DatabaseRole, expectedName string, expectedComment string) {
t.Helper()
assert.NotEmpty(t, databaseRole.CreatedOn)
Expand All @@ -38,7 +35,7 @@ func TestInt_DatabaseRoles(t *testing.T) {
createDatabaseRole := func(t *testing.T) *sdk.DatabaseRole {
t.Helper()
name := random.String()
id := sdk.NewDatabaseObjectIdentifier(database.Name, name)
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, name)

err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id))
require.NoError(t, err)
Expand All @@ -52,7 +49,7 @@ func TestInt_DatabaseRoles(t *testing.T) {

t.Run("create database_role: complete case", func(t *testing.T) {
name := random.String()
id := sdk.NewDatabaseObjectIdentifier(database.Name, name)
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, name)
comment := random.Comment()

request := sdk.NewCreateDatabaseRoleRequest(id).WithComment(&comment).WithIfNotExists(true)
Expand All @@ -68,7 +65,7 @@ func TestInt_DatabaseRoles(t *testing.T) {

t.Run("create database_role: no optionals", func(t *testing.T) {
name := random.String()
id := sdk.NewDatabaseObjectIdentifier(database.Name, name)
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, name)

err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id))
require.NoError(t, err)
Expand All @@ -82,7 +79,7 @@ func TestInt_DatabaseRoles(t *testing.T) {

t.Run("drop database_role: existing", func(t *testing.T) {
name := random.String()
id := sdk.NewDatabaseObjectIdentifier(database.Name, name)
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, name)

err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id))
require.NoError(t, err)
Expand All @@ -95,15 +92,15 @@ func TestInt_DatabaseRoles(t *testing.T) {
})

t.Run("drop database_role: non-existing", func(t *testing.T) {
id := sdk.NewDatabaseObjectIdentifier(database.Name, "does_not_exist")
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, "does_not_exist")

err := client.DatabaseRoles.Drop(ctx, sdk.NewDropDatabaseRoleRequest(id))
assert.ErrorIs(t, err, sdk.ErrObjectNotExistOrAuthorized)
})

t.Run("alter database_role: set value and unset value", func(t *testing.T) {
name := random.String()
id := sdk.NewDatabaseObjectIdentifier(database.Name, name)
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, name)

err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id))
require.NoError(t, err)
Expand All @@ -130,13 +127,13 @@ func TestInt_DatabaseRoles(t *testing.T) {

t.Run("alter database_role: rename", func(t *testing.T) {
name := random.String()
id := sdk.NewDatabaseObjectIdentifier(database.Name, name)
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, name)

err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id))
require.NoError(t, err)

newName := random.String()
newId := sdk.NewDatabaseObjectIdentifier(database.Name, newName)
newId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, newName)
alterRequest := sdk.NewAlterDatabaseRoleRequest(id).WithRename(newId)

err = client.DatabaseRoles.Alter(ctx, alterRequest)
Expand All @@ -161,7 +158,7 @@ func TestInt_DatabaseRoles(t *testing.T) {
t.Cleanup(secondDatabaseCleanup)

name := random.String()
id := sdk.NewDatabaseObjectIdentifier(database.Name, name)
id := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, name)

err := client.DatabaseRoles.Create(ctx, sdk.NewCreateDatabaseRoleRequest(id))
require.NoError(t, err)
Expand All @@ -179,7 +176,7 @@ func TestInt_DatabaseRoles(t *testing.T) {
role1 := createDatabaseRole(t)
role2 := createDatabaseRole(t)

showRequest := sdk.NewShowDatabaseRoleRequest(database.ID())
showRequest := sdk.NewShowDatabaseRoleRequest(testDb(t).ID())
returnedDatabaseRoles, err := client.DatabaseRoles.Show(ctx, showRequest)
require.NoError(t, err)

Expand All @@ -192,7 +189,7 @@ func TestInt_DatabaseRoles(t *testing.T) {
role1 := createDatabaseRole(t)
role2 := createDatabaseRole(t)

showRequest := sdk.NewShowDatabaseRoleRequest(database.ID()).WithLike(role1.Name)
showRequest := sdk.NewShowDatabaseRoleRequest(testDb(t).ID()).WithLike(role1.Name)
returnedDatabaseRoles, err := client.DatabaseRoles.Show(ctx, showRequest)

require.NoError(t, err)
Expand All @@ -202,7 +199,7 @@ func TestInt_DatabaseRoles(t *testing.T) {
})

t.Run("show database_role: no matches", func(t *testing.T) {
showRequest := sdk.NewShowDatabaseRoleRequest(database.ID()).WithLike("non-existent")
showRequest := sdk.NewShowDatabaseRoleRequest(testDb(t).ID()).WithLike("non-existent")
returnedDatabaseRoles, err := client.DatabaseRoles.Show(ctx, showRequest)

require.NoError(t, err)
Expand All @@ -211,9 +208,9 @@ func TestInt_DatabaseRoles(t *testing.T) {

t.Run("grant and revoke database_role: to database role", func(t *testing.T) {
role1 := createDatabaseRole(t)
id1 := sdk.NewDatabaseObjectIdentifier(database.Name, role1.Name)
id1 := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, role1.Name)
role2 := createDatabaseRole(t)
id2 := sdk.NewDatabaseObjectIdentifier(database.Name, role2.Name)
id2 := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, role2.Name)

grantRequest := sdk.NewGrantDatabaseRoleRequest(id1).WithDatabaseRole(id2)
err := client.DatabaseRoles.Grant(ctx, grantRequest)
Expand All @@ -238,7 +235,7 @@ func TestInt_DatabaseRoles(t *testing.T) {

t.Run("grant and revoke database_role: to account role", func(t *testing.T) {
role := createDatabaseRole(t)
roleId := sdk.NewDatabaseObjectIdentifier(database.Name, role.Name)
roleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, role.Name)

accountRole, accountRoleCleanup := createRole(t, client)
t.Cleanup(accountRoleCleanup)
Expand All @@ -260,12 +257,12 @@ func TestInt_DatabaseRoles(t *testing.T) {

t.Run("grant and revoke database_role: to share", func(t *testing.T) {
role := createDatabaseRole(t)
roleId := sdk.NewDatabaseObjectIdentifier(database.Name, role.Name)
roleId := sdk.NewDatabaseObjectIdentifier(testDb(t).Name, role.Name)

share, shareCleanup := createShare(t, client)
t.Cleanup(shareCleanup)

err := client.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{Database: database.ID()}, share.ID())
err := client.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{Database: testDb(t).ID()}, share.ID())
require.NoError(t, err)

grantRequest := sdk.NewGrantDatabaseRoleToShareRequest(roleId, share.ID())
Expand Down
Loading

0 comments on commit f533368

Please sign in to comment.