Skip to content

Commit

Permalink
chore: Set up a single warehouse for the SDK integration tests (#2141)
Browse files Browse the repository at this point in the history
* WH: Prepare common warehouse for integration tests

* WH: Use created warehouse in almost every test
  • Loading branch information
sfc-gh-asawicki authored Oct 20, 2023
1 parent de23f2b commit 16022ef
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 75 deletions.
45 changes: 15 additions & 30 deletions pkg/sdk/testint/alerts_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ func TestInt_AlertsShow(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

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

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

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

t.Run("without show options", func(t *testing.T) {
Expand Down Expand Up @@ -85,17 +82,14 @@ func TestInt_AlertCreate(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

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

t.Run("test complete case", func(t *testing.T) {
name := random.String()
schedule := "USING CRON * * * * TUE,THU UTC"
condition := "SELECT 1"
action := "SELECT 1"
comment := random.Comment()
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, &sdk.CreateAlertOptions{
err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, &sdk.CreateAlertOptions{
OrReplace: sdk.Bool(true),
IfNotExists: sdk.Bool(false),
Comment: sdk.String(comment),
Expand All @@ -104,7 +98,7 @@ func TestInt_AlertCreate(t *testing.T) {
alertDetails, err := client.Alerts.Describe(ctx, id)
require.NoError(t, err)
assert.Equal(t, name, alertDetails.Name)
assert.Equal(t, testWarehouse.Name, alertDetails.Warehouse)
assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse)
assert.Equal(t, schedule, alertDetails.Schedule)
assert.Equal(t, comment, *alertDetails.Comment)
assert.Equal(t, condition, alertDetails.Condition)
Expand All @@ -131,7 +125,7 @@ func TestInt_AlertCreate(t *testing.T) {
action := "SELECT 1"
comment := random.Comment()
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, &sdk.CreateAlertOptions{
err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, &sdk.CreateAlertOptions{
OrReplace: sdk.Bool(false),
IfNotExists: sdk.Bool(true),
Comment: sdk.String(comment),
Expand All @@ -140,7 +134,7 @@ func TestInt_AlertCreate(t *testing.T) {
alertDetails, err := client.Alerts.Describe(ctx, id)
require.NoError(t, err)
assert.Equal(t, name, alertDetails.Name)
assert.Equal(t, testWarehouse.Name, alertDetails.Warehouse)
assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse)
assert.Equal(t, schedule, alertDetails.Schedule)
assert.Equal(t, comment, *alertDetails.Comment)
assert.Equal(t, condition, alertDetails.Condition)
Expand All @@ -166,12 +160,12 @@ func TestInt_AlertCreate(t *testing.T) {
condition := "SELECT 1"
action := "SELECT 1"
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, nil)
err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, nil)
require.NoError(t, err)
alertDetails, err := client.Alerts.Describe(ctx, id)
require.NoError(t, err)
assert.Equal(t, name, alertDetails.Name)
assert.Equal(t, testWarehouse.Name, alertDetails.Warehouse)
assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse)
assert.Equal(t, schedule, alertDetails.Schedule)
assert.Equal(t, condition, alertDetails.Condition)
assert.Equal(t, action, alertDetails.Action)
Expand Down Expand Up @@ -204,12 +198,12 @@ func TestInt_AlertCreate(t *testing.T) {
end
`
id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name)
err := client.Alerts.Create(ctx, id, testWarehouse.ID(), schedule, condition, action, nil)
err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, nil)
require.NoError(t, err)
alertDetails, err := client.Alerts.Describe(ctx, id)
require.NoError(t, err)
assert.Equal(t, name, alertDetails.Name)
assert.Equal(t, testWarehouse.Name, alertDetails.Warehouse)
assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse)
assert.Equal(t, schedule, alertDetails.Schedule)
assert.Equal(t, condition, alertDetails.Condition)
assert.Equal(t, strings.TrimSpace(action), alertDetails.Action)
Expand All @@ -233,10 +227,7 @@ func TestInt_AlertDescribe(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

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

alert, alertCleanup := createAlert(t, client, testDb(t), testSchema(t), warehouseTest)
alert, alertCleanup := createAlert(t, client, testDb(t), testSchema(t), testWarehouse(t))
t.Cleanup(alertCleanup)

t.Run("when alert exists", func(t *testing.T) {
Expand All @@ -256,11 +247,8 @@ func TestInt_AlertAlter(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

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, testDb(t), testSchema(t), warehouseTest)
alert, alertCleanup := createAlert(t, client, testDb(t), testSchema(t), testWarehouse(t))
t.Cleanup(alertCleanup)
newSchedule := "USING CRON * * * * TUE,FRI GMT"

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

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

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

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

alterOptions := &sdk.AlterAlertOptions{
Expand Down Expand Up @@ -375,11 +363,8 @@ func TestInt_AlertDrop(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

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

t.Run("when alert exists", func(t *testing.T) {
alert, _ := createAlert(t, client, testDb(t), testSchema(t), warehouseTest)
alert, _ := createAlert(t, client, testDb(t), testSchema(t), testWarehouse(t))
id := alert.ID()
err := client.Alerts.Drop(ctx, id)
require.NoError(t, err)
Expand Down
7 changes: 2 additions & 5 deletions pkg/sdk/testint/comments_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ func TestInt_Comment(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

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

t.Run("set", func(t *testing.T) {
comment := random.Comment()
err := client.Comments.Set(ctx, &sdk.SetCommentOptions{
ObjectType: sdk.ObjectTypeWarehouse,
ObjectName: testWarehouse.ID(),
ObjectName: testWarehouse(t).ID(),
Value: sdk.String(comment),
})
require.NoError(t, err)
wh, err := client.Warehouses.ShowByID(ctx, testWarehouse.ID())
wh, err := client.Warehouses.ShowByID(ctx, testWarehouse(t).ID())
require.NoError(t, err)
assert.Equal(t, comment, wh.Comment)
})
Expand Down
1 change: 1 addition & 0 deletions pkg/sdk/testint/context_functions_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestInt_CurrentWarehouse(t *testing.T) {
client := testClient(t)
ctx := testContext(t)

// new warehouse created on purpose
warehouseTest, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)
err := client.Sessions.UseWarehouse(ctx, warehouseTest.ID())
Expand Down
2 changes: 2 additions & 0 deletions pkg/sdk/testint/conversion_functions_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestInt_ToTimestampLTZ(t *testing.T) {
})
require.NoError(t, err)
})
// new warehouse created on purpose
warehouseTest, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)
err = client.Sessions.UseWarehouse(ctx, warehouseTest.ID())
Expand Down Expand Up @@ -74,6 +75,7 @@ func TestInt_ToTimestampNTZ(t *testing.T) {
})
require.NoError(t, err)
})
// new warehouse created on purpose
warehouseTest, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)
err = client.Sessions.UseWarehouse(ctx, warehouseTest.ID())
Expand Down
10 changes: 4 additions & 6 deletions pkg/sdk/testint/dynamic_table_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
func TestInt_DynamicTableCreateAndDrop(t *testing.T) {
client := testClient(t)

warehouseTest, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)
tableTest, tableCleanup := createTable(t, client, testDb(t), testSchema(t))
t.Cleanup(tableCleanup)

Expand All @@ -26,7 +24,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {
}
query := "select id from " + tableTest.ID().FullyQualifiedName()
comment := random.Comment()
err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, warehouseTest.ID(), targetLag, query).WithOrReplace(true).WithComment(&comment))
err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, testWarehouse(t).ID(), targetLag, query).WithOrReplace(true).WithComment(&comment))
require.NoError(t, err)
t.Cleanup(func() {
err = client.DynamicTables.Drop(ctx, sdk.NewDropDynamicTableRequest(name))
Expand All @@ -38,7 +36,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {

entity := entities[0]
require.Equal(t, name.Name(), entity.Name)
require.Equal(t, warehouseTest.ID().Name(), entity.Warehouse)
require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse)
require.Equal(t, *targetLag.MaximumDuration, entity.TargetLag)
})

Expand All @@ -49,7 +47,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {
}
query := "select id from " + tableTest.ID().FullyQualifiedName()
comment := random.Comment()
err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, warehouseTest.ID(), targetLag, query).WithOrReplace(true).WithComment(&comment))
err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, testWarehouse(t).ID(), targetLag, query).WithOrReplace(true).WithComment(&comment))
require.NoError(t, err)
t.Cleanup(func() {
err = client.DynamicTables.Drop(ctx, sdk.NewDropDynamicTableRequest(name))
Expand All @@ -61,7 +59,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {

entity := entities[0]
require.Equal(t, name.Name(), entity.Name)
require.Equal(t, warehouseTest.ID().Name(), entity.Warehouse)
require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse)
require.Equal(t, "DOWNSTREAM", entity.TargetLag)
})
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/sdk/testint/external_tables_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ func TestInt_ExternalTables(t *testing.T) {

t.Run("Create: infer schema", func(t *testing.T) {
fileFormat, _ := createFileFormat(t, client, testSchema(t).ID())
warehouse, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)

err := client.Sessions.UseWarehouse(ctx, warehouse.ID())
err := client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID())
require.NoError(t, err)

name := random.AlphanumericN(32)
Expand Down
5 changes: 1 addition & 4 deletions pkg/sdk/testint/grants_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,6 @@ func TestInt_GrantOwnership(t *testing.T) {
})

t.Run("on account level object to role", func(t *testing.T) {
warehouse, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)

// role is deliberately created after warehouse, so that cleanup is done in reverse
// because after ownership grant we lose privilege to drop object
// with first dropping the role, we reacquire rights to do it - a little hacky trick
Expand All @@ -557,7 +554,7 @@ func TestInt_GrantOwnership(t *testing.T) {
on := sdk.OwnershipGrantOn{
Object: &sdk.Object{
ObjectType: sdk.ObjectTypeWarehouse,
Name: warehouse.ID(),
Name: testWarehouse(t).ID(),
},
}
to := sdk.OwnershipGrantTo{
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/testint/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ func testClientFromProfile(t *testing.T, profile string) (*sdk.Client, error) {
func useWarehouse(t *testing.T, client *sdk.Client, warehouseID sdk.AccountObjectIdentifier) func() {
t.Helper()
ctx := context.Background()
orgWarehouse, err := client.ContextFunctions.CurrentWarehouse(ctx)
require.NoError(t, err)
err = client.Sessions.UseWarehouse(ctx, warehouseID)
err := client.Sessions.UseWarehouse(ctx, warehouseID)
require.NoError(t, err)
return func() {
err := client.Sessions.UseWarehouse(ctx, sdk.NewAccountObjectIdentifier(orgWarehouse))
err = client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID())
require.NoError(t, err)
}
}
Expand Down Expand Up @@ -137,6 +135,8 @@ func createWarehouseWithOptions(t *testing.T, client *sdk.Client, opts *sdk.Crea
}, func() {
err := client.Warehouses.Drop(ctx, id, nil)
require.NoError(t, err)
err = client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID())
require.NoError(t, err)
}
}

Expand Down
32 changes: 18 additions & 14 deletions pkg/sdk/testint/sessions_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,19 @@ func TestInt_ShowUserParameter(t *testing.T) {
func TestInt_UseWarehouse(t *testing.T) {
client := testClient(t)
ctx := testContext(t)
originalWH, err := client.ContextFunctions.CurrentWarehouse(ctx)
require.NoError(t, err)

t.Cleanup(func() {
originalWHIdentifier := sdk.NewAccountObjectIdentifier(originalWH)
if !sdk.ValidObjectIdentifier(originalWHIdentifier) {
return
}
err := client.Sessions.UseWarehouse(ctx, originalWHIdentifier)
err := client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID())
require.NoError(t, err)
})
warehouseTest, warehouseCleanup := createWarehouse(t, client)
// new warehouse created on purpose
warehouse, warehouseCleanup := createWarehouse(t, client)
t.Cleanup(warehouseCleanup)
err = client.Sessions.UseWarehouse(ctx, warehouseTest.ID())
err := client.Sessions.UseWarehouse(ctx, warehouse.ID())
require.NoError(t, err)
actual, err := client.ContextFunctions.CurrentWarehouse(ctx)
require.NoError(t, err)
expected := warehouseTest.Name
expected := warehouse.Name
assert.Equal(t, expected, actual)
}

Expand All @@ -127,11 +123,14 @@ func TestInt_UseDatabase(t *testing.T) {
err := client.Sessions.UseSchema(ctx, testSchema(t).ID())
require.NoError(t, err)
})
err := client.Sessions.UseDatabase(ctx, testDb(t).ID())
// new database created on purpose
database, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)
err := client.Sessions.UseDatabase(ctx, database.ID())
require.NoError(t, err)
actual, err := client.ContextFunctions.CurrentDatabase(ctx)
require.NoError(t, err)
expected := testDb(t).Name
expected := database.Name
assert.Equal(t, expected, actual)
}

Expand All @@ -143,10 +142,15 @@ func TestInt_UseSchema(t *testing.T) {
err := client.Sessions.UseSchema(ctx, testSchema(t).ID())
require.NoError(t, err)
})
err := client.Sessions.UseSchema(ctx, testSchema(t).ID())
// new database and schema created on purpose
database, databaseCleanup := createDatabase(t, client)
t.Cleanup(databaseCleanup)
schema, schemaCleanup := createSchema(t, client, database)
t.Cleanup(schemaCleanup)
err := client.Sessions.UseSchema(ctx, schema.ID())
require.NoError(t, err)
actual, err := client.ContextFunctions.CurrentSchema(ctx)
require.NoError(t, err)
expected := testSchema(t).Name
expected := schema.Name
assert.Equal(t, expected, actual)
}
Loading

0 comments on commit 16022ef

Please sign in to comment.