Skip to content

Commit

Permalink
SNOW-859548 Reuse connection in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pfus committed Jul 19, 2023
1 parent 07a0e35 commit 8ca63c5
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 131 deletions.
24 changes: 14 additions & 10 deletions arrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

func TestArrowBigInt(t *testing.T) {
db := openDB(t)
dbt := &DBTest{t, db}
conn := openConn(t)
defer conn.Close()
dbt := &DBTest{t, conn}

testcases := []struct {
num string
Expand All @@ -24,7 +25,7 @@ func TestArrowBigInt(t *testing.T) {
}{
{"10000000000000000000000000000000000000", 38, 0},
{"-10000000000000000000000000000000000000", 38, 0},
{"12345678901234567890123456789012345678", 38, 0},
{"12345678901234567890123456789012345678", 38, 0}, // #pragma: allowlist secret
{"-12345678901234567890123456789012345678", 38, 0},
{"99999999999999999999999999999999999999", 38, 0},
{"-99999999999999999999999999999999999999", 38, 0},
Expand Down Expand Up @@ -53,8 +54,9 @@ func TestArrowBigInt(t *testing.T) {
}

func TestArrowBigFloat(t *testing.T) {
db := openDB(t)
dbt := &DBTest{t, db}
conn := openConn(t)
defer conn.Close()
dbt := &DBTest{t, conn}

testcases := []struct {
num string
Expand Down Expand Up @@ -94,8 +96,9 @@ func TestArrowBigFloat(t *testing.T) {
}

func TestArrowIntPrecision(t *testing.T) {
db := openDB(t)
dbt := &DBTest{t, db}
conn := openConn(t)
defer conn.Close()
dbt := &DBTest{t, conn}

intTestcases := []struct {
num string
Expand All @@ -104,7 +107,7 @@ func TestArrowIntPrecision(t *testing.T) {
}{
{"10000000000000000000000000000000000000", 38, 0},
{"-10000000000000000000000000000000000000", 38, 0},
{"12345678901234567890123456789012345678", 38, 0},
{"12345678901234567890123456789012345678", 38, 0}, // pragma: allowlist secret
{"-12345678901234567890123456789012345678", 38, 0},
{"99999999999999999999999999999999999999", 38, 0},
{"-99999999999999999999999999999999999999", 38, 0},
Expand Down Expand Up @@ -183,8 +186,9 @@ func TestArrowIntPrecision(t *testing.T) {
// rows.Scan() method. Note that for lower precision types we do not attempt
// to check the value as precision could be lost.
func TestArrowFloatPrecision(t *testing.T) {
db := openDB(t)
dbt := &DBTest{t, db}
conn := openConn(t)
defer conn.Close()
dbt := &DBTest{t, conn}

fltTestcases := []struct {
num string
Expand Down
6 changes: 3 additions & 3 deletions async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ func retrieveRows(rows *RowsExtended, ch chan string) {
}

func TestLongRunningAsyncQuery(t *testing.T) {
db := openDB(t)
defer db.Close()
conn := openConn(t)
defer conn.Close()

ctx, _ := WithMultiStatement(context.Background(), 0)
query := "CALL SYSTEM$WAIT(50, 'SECONDS');use snowflake_sample_data"

rows, err := db.QueryContext(WithAsyncMode(ctx), query)
rows, err := conn.QueryContext(WithAsyncMode(ctx), query)
if err != nil {
t.Fatalf("failed to run a query. %v, err: %v", query, err)
}
Expand Down
26 changes: 13 additions & 13 deletions bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestBindingUint64(t *testing.T) {
expected := uint64(18446744073709551615)
for _, v := range types {
dbt.mustExec(fmt.Sprintf("CREATE TABLE test (id int, value %v)", v))
if _, err := dbt.db.Exec("INSERT INTO test VALUES (1, ?)", expected); err == nil {
if _, err := dbt.exec("INSERT INTO test VALUES (1, ?)", expected); err == nil {
dbt.Fatal("should fail as uint64 values with high bit set are not supported.")
} else {
logger.Infof("expected err: %v", err)
Expand All @@ -84,7 +84,7 @@ func TestBindingDateTimeTimestamp(t *testing.T) {
expected := time.Now()
dbt.mustExec(
"CREATE OR REPLACE TABLE tztest (id int, ntz timestamp_ntz, ltz timestamp_ltz, dt date, tm time)")
stmt, err := dbt.db.Prepare("INSERT INTO tztest(id,ntz,ltz,dt,tm) VALUES(1,?,?,?,?)")
stmt, err := dbt.prepare("INSERT INTO tztest(id,ntz,ltz,dt,tm) VALUES(1,?,?,?,?)")
if err != nil {
dbt.Fatal(err.Error())
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestBindingTimestampTZ(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
expected := time.Now()
dbt.mustExec("CREATE OR REPLACE TABLE tztest (id int, tz timestamp_tz)")
stmt, err := dbt.db.Prepare("INSERT INTO tztest(id,tz) VALUES(1, ?)")
stmt, err := dbt.prepare("INSERT INTO tztest(id,tz) VALUES(1, ?)")
if err != nil {
dbt.Fatal(err.Error())
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestBindingTimePtrInStruct(t *testing.T) {
runInsertQuery := false
for i := 0; i < 2; i++ {
if !runInsertQuery {
_, err := dbt.db.Exec("INSERT INTO timeStructTest(id,tz) VALUES(?, ?)", testStruct.id, testStruct.timeVal)
_, err := dbt.exec("INSERT INTO timeStructTest(id,tz) VALUES(?, ?)", testStruct.id, testStruct.timeVal)
if err != nil {
dbt.Fatal(err.Error())
}
Expand All @@ -223,7 +223,7 @@ func TestBindingTimePtrInStruct(t *testing.T) {
// Update row with a new time value
expectedTime = time.Now().Add(1)
testStruct.timeVal = &expectedTime
_, err := dbt.db.Exec("UPDATE timeStructTest SET tz = ? where id = ?", testStruct.timeVal, testStruct.id)
_, err := dbt.exec("UPDATE timeStructTest SET tz = ? where id = ?", testStruct.timeVal, testStruct.id)
if err != nil {
dbt.Fatal(err.Error())
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestBindingTimeInStruct(t *testing.T) {
runInsertQuery := false
for i := 0; i < 2; i++ {
if !runInsertQuery {
_, err := dbt.db.Exec("INSERT INTO timeStructTest(id,tz) VALUES(?, ?)", testStruct.id, testStruct.timeVal)
_, err := dbt.exec("INSERT INTO timeStructTest(id,tz) VALUES(?, ?)", testStruct.id, testStruct.timeVal)
if err != nil {
dbt.Fatal(err.Error())
}
Expand All @@ -270,7 +270,7 @@ func TestBindingTimeInStruct(t *testing.T) {
// Update row with a new time value
expectedTime = time.Now().Add(1)
testStruct.timeVal = expectedTime
_, err := dbt.db.Exec("UPDATE timeStructTest SET tz = ? where id = ?", testStruct.timeVal, testStruct.id)
_, err := dbt.exec("UPDATE timeStructTest SET tz = ? where id = ?", testStruct.timeVal, testStruct.id)
if err != nil {
dbt.Fatal(err.Error())
}
Expand Down Expand Up @@ -630,7 +630,7 @@ func testBindingArray(t *testing.T, bulk bool) {
dbt.mustExec(createTableSQL)
defer dbt.mustExec(deleteTableSQL)
if bulk {
if _, err := dbt.db.Exec("ALTER SESSION SET CLIENT_STAGE_ARRAY_BINDING_THRESHOLD = 1"); err != nil {
if _, err := dbt.exec("ALTER SESSION SET CLIENT_STAGE_ARRAY_BINDING_THRESHOLD = 1"); err != nil {
t.Error(err)
}
}
Expand Down Expand Up @@ -778,7 +778,7 @@ func TestBulkArrayMultiPartBindingInt(t *testing.T) {
for i := startNum; i < endNum; i++ {
intArr[i-startNum] = i
}
_, err := dbt.db.Exec("insert into binding_test values (?)", Array(&intArr))
_, err := dbt.exec("insert into binding_test values (?)", Array(&intArr))
if err != nil {
t.Errorf("Should have succeeded to insert. err: %v", err)
}
Expand Down Expand Up @@ -825,7 +825,7 @@ func TestBulkArrayMultiPartBindingWithNull(t *testing.T) {
stringArr[2] = nil
stringArr[3] = nil

_, err := dbt.db.Exec("insert into binding_test values (?, ?)", Array(&intArr), Array(&stringArr))
_, err := dbt.exec("insert into binding_test values (?, ?)", Array(&intArr), Array(&stringArr))
if err != nil {
t.Errorf("Should have succeeded to insert. err: %v", err)
}
Expand Down Expand Up @@ -903,7 +903,7 @@ func TestFunctionParameters(t *testing.T) {
LANGUAGE SQL
AS 'select param1';`, tc.paramType, tc.paramType)
dbt.mustExec(query)
if rows, err := dbt.db.Query("select * from table(NULLPARAMFUNCTION(?))", tc.input); err != nil {
if rows, err := dbt.query("select * from table(NULLPARAMFUNCTION(?))", tc.input); err != nil {
t.Fatal(err)
} else {
if rows.Err() != nil {
Expand Down Expand Up @@ -987,15 +987,15 @@ func TestVariousBindingModes(t *testing.T) {
t.Run(tc.testDesc+" "+bindingMode.param, func(t *testing.T) {
query := fmt.Sprintf(`CREATE OR REPLACE TABLE BINDING_MODES(param1 %v)`, tc.paramType)
dbt.mustExec(query)
if _, err := dbt.db.Exec(fmt.Sprintf("INSERT INTO BINDING_MODES VALUES (%v)", bindingMode.param), bindingMode.transform(tc.input)); err != nil {
if _, err := dbt.exec(fmt.Sprintf("INSERT INTO BINDING_MODES VALUES (%v)", bindingMode.param), bindingMode.transform(tc.input)); err != nil {
t.Fatal(err)
}
if tc.isNil {
query = "SELECT * FROM BINDING_MODES WHERE param1 IS NULL"
} else {
query = fmt.Sprintf("SELECT * FROM BINDING_MODES WHERE param1 = %v", bindingMode.param)
}
rows, err := dbt.db.Query(query, bindingMode.transform(tc.input))
rows, err := dbt.query(query, bindingMode.transform(tc.input))
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ const (
)

func TestInvalidConnection(t *testing.T) {
db := openDB(t)
if err := db.Close(); err != nil {
conn := openConn(t)
if err := conn.Close(); err != nil {
t.Error("should not cause error in Close")
}
if err := db.Close(); err != nil {
if err := conn.Close(); err != nil {
t.Error("should not cause error in the second call of Close")
}
if _, err := db.Exec("CREATE TABLE OR REPLACE test0(c1 int)"); err == nil {
if _, err := conn.ExecContext(context.Background(), "CREATE TABLE OR REPLACE test0(c1 int)"); err == nil {
t.Error("should fail to run Exec")
}
if _, err := db.Query("SELECT CURRENT_TIMESTAMP()"); err == nil {
if _, err := conn.QueryContext(context.Background(), "SELECT CURRENT_TIMESTAMP()"); err == nil {
t.Error("should fail to run Query")
}
if _, err := db.Begin(); err == nil {
if _, err := conn.BeginTx(context.Background(), nil); err == nil {
t.Error("should fail to run Begin")
}
}
Expand Down
Loading

0 comments on commit 8ca63c5

Please sign in to comment.