yvbolt/sqliteclidriver/sqliteclidriver_test.go

79 lines
1.8 KiB
Go
Raw Permalink Normal View History

2024-06-28 23:56:45 +00:00
package sqliteclidriver
import (
"database/sql"
"testing"
"github.com/stretchr/testify/require"
)
func TestSqliteCliDriver(t *testing.T) {
db, err := sql.Open("sqliteclidriver", ":memory:")
require.NoError(t, err)
_, err = db.Exec(`CREATE TABLE my_test_table ( id INTEGER PRIMARY KEY, extra TEXT NOT NULL );`)
require.NoError(t, err)
_, err = db.Exec(`INSERT INTO my_test_table (id, extra) VALUES ( ? , ? ), ( ? , ? );`, 1337, "abcdef", 9001, "whoop")
2024-06-28 23:56:45 +00:00
require.NoError(t, err)
// Repeat this part to ensure we can make followup queries on the same connection
for i := 0; i < 3; i++ {
2024-06-28 23:56:45 +00:00
res, err := db.Query(`SELECT * FROM my_test_table ORDER BY id ASC;`)
require.NoError(t, err)
2024-06-28 23:56:45 +00:00
cols, err := res.Columns()
require.NoError(t, err)
require.EqualValues(t, cols, []string{"id", "extra"})
2024-06-28 23:56:45 +00:00
var rowCount int = 0
2024-06-28 23:56:45 +00:00
for res.Next() {
rowCount++
var idVal int
var extraVal string
err = res.Scan(&idVal, &extraVal)
if err != nil {
t.Fatal(err)
}
2024-06-28 23:56:45 +00:00
switch rowCount {
case 1:
require.EqualValues(t, 1337, idVal)
require.EqualValues(t, "abcdef", extraVal)
2024-06-28 23:56:45 +00:00
case 2:
require.EqualValues(t, 9001, idVal)
require.EqualValues(t, "whoop", extraVal)
2024-06-28 23:56:45 +00:00
}
2024-06-28 23:56:45 +00:00
}
require.Equal(t, rowCount, 2)
}
2024-06-28 23:56:45 +00:00
}
func TestSqliteCliDriverNoResults(t *testing.T) {
db, err := sql.Open("sqliteclidriver", ":memory:")
require.NoError(t, err)
// Repeat this part to ensure we can make followup queries on the same connection
for i := 0; i < 3; i++ {
_, err = db.Query(`SELECT 1 AS expect_no_result WHERE 1=2`)
require.NoError(t, err)
// Mix of results and no-results
rr := db.QueryRow(`SELECT 1 AS expect_result WHERE 1=1`)
require.NoError(t, rr.Err())
var result int64
err = rr.Scan(&result)
require.NoError(t, err)
require.EqualValues(t, result, 1)
}
}