mirror of https://github.com/VinGarcia/ksql.git
Improve ScanRowsTest() so its decoupled from the adapters
parent
492dd478e0
commit
ea4f56fadd
|
@ -2124,19 +2124,16 @@ func ScanRowsTest(
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should ignore extra columns from query", func(t *testing.T) {
|
t.Run("should ignore extra columns from query", func(t *testing.T) {
|
||||||
err := createTables("sqlite3")
|
err := createTables(config.driver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("could not create test table!, reason:", err.Error())
|
t.Fatal("could not create test table!, reason:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
dialect := supportedDialects["sqlite3"]
|
dialect := supportedDialects[config.driver]
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
db, closer := connectDB(t, testConfig{
|
db, closer := newDBAdapter(t)
|
||||||
driver: "sqlite3",
|
|
||||||
adapterName: "sql",
|
|
||||||
})
|
|
||||||
defer closer.Close()
|
defer closer.Close()
|
||||||
c := newTestDB(db, "sqlite3")
|
c := newTestDB(db, config.driver)
|
||||||
_ = c.Insert(ctx, UsersTable, &User{Name: "User1", Age: 22})
|
_ = c.Insert(ctx, UsersTable, &User{Name: "User1", Age: 22})
|
||||||
|
|
||||||
rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE name='User1'")
|
rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE name='User1'")
|
||||||
|
@ -2159,17 +2156,14 @@ func ScanRowsTest(
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should report error for closed rows", func(t *testing.T) {
|
t.Run("should report error for closed rows", func(t *testing.T) {
|
||||||
err := createTables("sqlite3")
|
err := createTables(config.driver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("could not create test table!, reason:", err.Error())
|
t.Fatal("could not create test table!, reason:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
dialect := supportedDialects["sqlite3"]
|
dialect := supportedDialects[config.driver]
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
db, closer := connectDB(t, testConfig{
|
db, closer := newDBAdapter(t)
|
||||||
driver: "sqlite3",
|
|
||||||
adapterName: "sql",
|
|
||||||
})
|
|
||||||
defer closer.Close()
|
defer closer.Close()
|
||||||
|
|
||||||
rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE name='User2'")
|
rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE name='User2'")
|
||||||
|
@ -2183,47 +2177,51 @@ func ScanRowsTest(
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("should report if record is not a pointer", func(t *testing.T) {
|
t.Run("should report if record is not a pointer", func(t *testing.T) {
|
||||||
err := createTables("sqlite3")
|
err := createTables(config.driver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("could not create test table!, reason:", err.Error())
|
t.Fatal("could not create test table!, reason:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
dialect := supportedDialects["sqlite3"]
|
dialect := supportedDialects[config.driver]
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
db, closer := connectDB(t, testConfig{
|
db, closer := newDBAdapter(t)
|
||||||
driver: "sqlite3",
|
|
||||||
adapterName: "sql",
|
|
||||||
})
|
|
||||||
defer closer.Close()
|
|
||||||
|
|
||||||
rows, err := db.QueryContext(ctx, "select * from users where name='User2'")
|
|
||||||
assert.Equal(t, nil, err)
|
|
||||||
|
|
||||||
var u User
|
|
||||||
err = scanRows(dialect, rows, u)
|
|
||||||
assert.NotEqual(t, nil, err)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("should report if record is not a pointer to struct", func(t *testing.T) {
|
|
||||||
err := createTables("sqlite3")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("could not create test table!, reason:", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
dialect := supportedDialects["sqlite3"]
|
|
||||||
ctx := context.TODO()
|
|
||||||
db, closer := connectDB(t, testConfig{
|
|
||||||
driver: "sqlite3",
|
|
||||||
adapterName: "sql",
|
|
||||||
})
|
|
||||||
defer closer.Close()
|
defer closer.Close()
|
||||||
|
|
||||||
rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE name='User2'")
|
rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE name='User2'")
|
||||||
assert.Equal(t, nil, err)
|
tt.AssertNoErr(t, err)
|
||||||
|
// Some drivers will hang forever if Next() is not called:
|
||||||
|
defer func() {
|
||||||
|
for rows.Next() {
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var u User
|
||||||
|
err = scanRows(dialect, rows, u)
|
||||||
|
tt.AssertErrContains(t, err, "ksql", "expected", "pointer to struct", "User")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should report if record is not a pointer to struct", func(t *testing.T) {
|
||||||
|
err := createTables(config.driver)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("could not create test table!, reason:", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
dialect := supportedDialects[config.driver]
|
||||||
|
ctx := context.TODO()
|
||||||
|
db, closer := newDBAdapter(t)
|
||||||
|
defer closer.Close()
|
||||||
|
|
||||||
|
rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE name='User2'")
|
||||||
|
tt.AssertNoErr(t, err)
|
||||||
|
// Some drivers will hang forever if Next() is not called:
|
||||||
|
defer func() {
|
||||||
|
for rows.Next() {
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
var u map[string]interface{}
|
var u map[string]interface{}
|
||||||
err = scanRows(dialect, rows, &u)
|
err = scanRows(dialect, rows, &u)
|
||||||
assert.NotEqual(t, nil, err)
|
tt.AssertErrContains(t, err, "ksql", "expected", "pointer to struct", "map[string]interface")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ func startPostgresDB(dbName string) (databaseURL string, closer func()) {
|
||||||
|
|
||||||
fmt.Println("Connecting to postgres on url: ", databaseUrl)
|
fmt.Println("Connecting to postgres on url: ", databaseUrl)
|
||||||
|
|
||||||
resource.Expire(40) // Tell docker to hard kill the container in 20 seconds
|
resource.Expire(40) // Tell docker to hard kill the container in 40 seconds
|
||||||
|
|
||||||
var sqlDB *sql.DB
|
var sqlDB *sql.DB
|
||||||
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
||||||
|
@ -133,7 +133,7 @@ func startMySQLDB(dbName string) (databaseURL string, closer func()) {
|
||||||
|
|
||||||
fmt.Println("Connecting to mysql on url: ", databaseUrl)
|
fmt.Println("Connecting to mysql on url: ", databaseUrl)
|
||||||
|
|
||||||
resource.Expire(40) // Tell docker to hard kill the container in 20 seconds
|
resource.Expire(40) // Tell docker to hard kill the container in 40 seconds
|
||||||
|
|
||||||
var sqlDB *sql.DB
|
var sqlDB *sql.DB
|
||||||
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
||||||
|
@ -189,7 +189,7 @@ func startSQLServerDB(dbName string) (databaseURL string, closer func()) {
|
||||||
|
|
||||||
fmt.Println("Connecting to sqlserver on url: ", databaseUrl)
|
fmt.Println("Connecting to sqlserver on url: ", databaseUrl)
|
||||||
|
|
||||||
resource.Expire(40) // Tell docker to hard kill the container in 20 seconds
|
resource.Expire(40) // Tell docker to hard kill the container in 40 seconds
|
||||||
|
|
||||||
var sqlDB *sql.DB
|
var sqlDB *sql.DB
|
||||||
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
|
||||||
|
|
Loading…
Reference in New Issue