-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
147 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package driver_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/api/option" | ||
|
||
"github.com/rudderlabs/rudder-go-kit/testhelper/rand" | ||
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/bigquery/driver" | ||
) | ||
|
||
func TestBigqueryDriver(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.Cleanup(cancel) | ||
|
||
configJSON, ok := os.LookupEnv("BIGQUERY_TEST_ENVIRONMENT_CREDENTIALS") | ||
if !ok { | ||
t.Skip("skipping bigquery driver test due to lack of a test environment") | ||
} | ||
var c config | ||
require.NoError(t, json.Unmarshal([]byte(configJSON), &c)) | ||
|
||
db := sql.OpenDB(driver.NewConnector(c.ProjectID, option.WithCredentialsJSON([]byte(c.CredentialsJSON)))) | ||
t.Cleanup(func() { | ||
require.NoError(t, db.Close(), "it should be able to close the database connection") | ||
}) | ||
|
||
schema := GenerateTestSchema() | ||
|
||
t.Run("Transaction unsupported", func(t *testing.T) { | ||
t.Run("Begin", func(t *testing.T) { | ||
_, err := db.Begin() | ||
require.Error(t, err, "it should not be able to begin a transaction") | ||
}) | ||
|
||
t.Run("BeginTx", func(t *testing.T) { | ||
_, err := db.BeginTx(ctx, nil) | ||
require.Error(t, err, "it should not be able to begin a transaction") | ||
}) | ||
}) | ||
t.Run("Exec", func(t *testing.T) { | ||
_, err := db.Exec(fmt.Sprintf("CREATE SCHEMA `%s`", schema)) | ||
require.NoError(t, err, "it should be able to create a schema") | ||
}) | ||
|
||
t.Run("ExecContext", func(t *testing.T) { | ||
_, err := db.ExecContext(ctx, fmt.Sprintf("CREATE TABLE `%s`.`test_table` (C1 INT)", schema)) | ||
require.NoError(t, err, "it should be able to create a table") | ||
}) | ||
|
||
t.Run("prepared statement", func(t *testing.T) { | ||
t.Run("QueryRow", func(t *testing.T) { | ||
stmt, err := db.Prepare(fmt.Sprintf("SELECT COUNT(*) FROM `%s`.`test_table`", schema)) | ||
require.NoError(t, err, "it should be able to prepare a statement") | ||
defer func() { | ||
require.NoError(t, stmt.Close(), "it should be able to close the prepared statement") | ||
}() | ||
|
||
var count int | ||
err = stmt.QueryRow().Scan(&count) | ||
require.NoError(t, err, "it should be able to execute a prepared statement") | ||
}) | ||
|
||
t.Run("Exec", func(t *testing.T) { | ||
stmt, err := db.Prepare(fmt.Sprintf("INSERT INTO `%s`.`test_table` VALUES (?)", schema)) | ||
require.NoError(t, err, "it should be able to prepare a statement") | ||
defer func() { | ||
require.NoError(t, stmt.Close(), "it should be able to close the prepared statement") | ||
}() | ||
result, err := stmt.Exec(1) | ||
require.NoError(t, err, "it should be able to execute a prepared statement") | ||
rowsAffected, err := result.RowsAffected() | ||
require.NoError(t, err, "it should be able to get rows affected") | ||
require.EqualValues(t, 0, rowsAffected, "rows affected should be 0 (not supported)") | ||
}) | ||
|
||
t.Run("Query", func(t *testing.T) { | ||
stmt, err := db.Prepare(fmt.Sprintf("SELECT C1 FROM `%s`.`test_table` WHERE C1 = ?", schema)) | ||
require.NoError(t, err, "it should be able to prepare a statement") | ||
defer func() { | ||
require.NoError(t, stmt.Close(), "it should be able to close the prepared statement") | ||
}() | ||
rows, err := stmt.Query(1) | ||
require.NoError(t, err, "it should be able to execute a prepared statement") | ||
defer func() { | ||
require.NoError(t, rows.Close(), "it should be able to close the rows") | ||
}() | ||
require.True(t, rows.Next(), "it should be able to get a row") | ||
var c1 int | ||
err = rows.Scan(&c1) | ||
require.NoError(t, err, "it should be able to scan the row") | ||
require.EqualValues(t, 1, c1, "it should be able to get the correct value") | ||
require.False(t, rows.Next(), "it shouldn't have next row") | ||
|
||
require.NoError(t, rows.Err()) | ||
}) | ||
|
||
t.Run("Query with named parameters", func(t *testing.T) { | ||
stmt, err := db.Prepare(fmt.Sprintf("SELECT C1 FROM `%s`.`test_table` WHERE C1 = @c1_value", schema)) | ||
require.NoError(t, err, "it should be able to prepare a statement") | ||
defer func() { | ||
require.NoError(t, stmt.Close(), "it should be able to close the prepared statement") | ||
}() | ||
rows, err := stmt.Query(sql.Named("c1_value", 1)) | ||
require.NoError(t, err, "it should be able to execute a prepared statement") | ||
defer func() { | ||
require.NoError(t, rows.Close(), "it should be able to close the rows") | ||
}() | ||
require.True(t, rows.Next(), "it should be able to get a row") | ||
var c1 int | ||
err = rows.Scan(&c1) | ||
require.NoError(t, err, "it should be able to scan the row") | ||
require.EqualValues(t, 1, c1, "it should be able to get the correct value") | ||
require.False(t, rows.Next(), "it shouldn't have next row") | ||
|
||
require.NoError(t, rows.Err()) | ||
}) | ||
}) | ||
} | ||
|
||
type config struct { | ||
ProjectID string `json:"project"` | ||
CredentialsJSON string `json:"credentials"` | ||
} | ||
|
||
func GenerateTestSchema() string { | ||
return strings.ToLower(fmt.Sprintf("tbqdrv_%s_%d", rand.String(12), time.Now().Unix())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters