Commit a8febe56 authored by gwenn's avatar gwenn

Improves tests coverage.

parent 10edd7e3
......@@ -22,7 +22,7 @@ func TestJulianDay(t *testing.T) {
}
}
func TestBind(t *testing.T) {
func TestBindTime(t *testing.T) {
db := open(t)
defer checkClose(db, t)
var delta int
......
......@@ -13,12 +13,12 @@ const (
ddl = "DROP TABLE IF EXISTS test;" +
"CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL," +
" name TEXT);"
dml = "INSERT INTO test (name) values ('Bart');" +
"INSERT INTO test (name) values ('Lisa');" +
"UPDATE test set name = 'El Barto' where name = 'Bart';" +
"DELETE from test where name = 'Bart';"
insert = "INSERT into test (name) values (?)"
query = "SELECT * FROM test where name like ?"
dml = "INSERT INTO test (name) VALUES ('Bart');" +
"INSERT INTO test (name) VALUES ('Lisa');" +
"UPDATE test SET name = 'El Barto' WHERE name = 'Bart';" +
"DELETE FROM test WHERE name = 'Bart';"
insert = "INSERT INTO test (name) VALUES (?)"
query = "SELECT * FROM test WHERE name LIKE ?"
)
func sqlOpen(t *testing.T) *sql.DB {
......@@ -133,8 +133,14 @@ func TestSqlPrepare(t *testing.T) {
stmt, err := db.Prepare(insert)
checkNoError(t, err, "Error while preparing stmt: %s")
defer checkSqlStmtClose(stmt, t)
_, err = stmt.Exec("Bart")
result, err := stmt.Exec("Bart")
checkNoError(t, err, "Error while executing stmt: %s")
id, err := result.LastInsertId()
checkNoError(t, err, "Error while calling LastInsertId: %s")
assertEquals(t, "expected %d got %d LastInsertId", int64(3), id)
changes, err := result.RowsAffected()
checkNoError(t, err, "Error while calling RowsAffected: %s")
assertEquals(t, "expected %d got %d RowsAffected", int64(1), changes)
}
func TestRowsWithStmtClosed(t *testing.T) {
......
......@@ -7,6 +7,7 @@ package sqlite_test
import (
"fmt"
"github.com/gwenn/gosqlite"
"sync"
)
func check(err error) {
......@@ -26,7 +27,7 @@ func Example() {
defer ins.Finalize()
_, err = ins.Insert("gosqlite driver")
check(err)
s, err := db.Prepare("SELECT name from test WHERE name like ?", "%go%")
s, err := db.Prepare("SELECT name FROM test WHERE name LIKE ?", "%go%")
check(err)
defer s.Finalize()
var name string
......@@ -68,7 +69,7 @@ func ExampleStmt_ExecDml() {
err = db.Exec("CREATE TABLE test (content TEXT); INSERT INTO test VALUES ('Go'); INSERT INTO test VALUES ('SQLite')")
check(err)
s, err := db.Prepare("UPDATE test SET content = content || 'lang' where content like ?")
s, err := db.Prepare("UPDATE test SET content = content || 'lang' WHERE content LIKE ?")
check(err)
defer s.Finalize()
changes, err := s.ExecDml("%o")
......@@ -103,7 +104,7 @@ func ExampleStmt_NamedScan() {
check(err)
defer db.Close()
s, err := db.Prepare("SELECT 1 as id, 'Go' as name UNION SELECT 2, 'SQLite'")
s, err := db.Prepare("SELECT 1 AS id, 'Go' AS name UNION SELECT 2, 'SQLite'")
check(err)
defer s.Finalize()
......@@ -125,7 +126,7 @@ func ExampleStmt_Scan() {
check(err)
defer db.Close()
s, err := db.Prepare("SELECT 1 as id, 'Go' as name, 'Y' as status UNION SELECT 2, 'SQLite', 'yes'")
s, err := db.Prepare("SELECT 1 AS id, 'Go' AS name, 'Y' AS status UNION SELECT 2, 'SQLite', 'yes'")
check(err)
defer s.Finalize()
......@@ -165,13 +166,13 @@ func ExampleNewBackup() {
cbs := make(chan sqlite.BackupStatus)
defer close(cbs)
ack := make(chan bool)
defer close(ack)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for {
s := <-cbs
fmt.Printf("backup progress (remaining: %d)\n", s.Remaining)
ack <- true
wg.Done()
if s.Remaining == 0 {
break
}
......@@ -179,7 +180,7 @@ func ExampleNewBackup() {
}()
err = bck.Run(100, 250000, cbs)
check(err)
<-ack
wg.Wait()
// Output: backup progress (remaining: 0)
}
......
......@@ -27,7 +27,7 @@ func TestScalarFunction(t *testing.T) {
err := db.CreateScalarFunction("half", 1, nil, half, nil)
checkNoError(t, err, "couldn't create function: %s")
var d float64
err = db.OneValue("select half(6)", &d)
err = db.OneValue("SELECT half(6)", &d)
checkNoError(t, err, "couldn't retrieve result: %s")
assertEquals(t, "Expected %f but got %f", 3.0, d)
err = db.CreateScalarFunction("half", 1, nil, nil, nil)
......@@ -73,7 +73,7 @@ func TestRegexpFunction(t *testing.T) {
defer checkClose(db, t)
err := db.CreateScalarFunction("regexp", 2, nil, re, reDestroy)
checkNoError(t, err, "couldn't create function: %s")
s, err := db.Prepare("select regexp('l.s[aeiouy]', name) from (select 'lisa' as name union all select 'bart')")
s, err := db.Prepare("SELECT regexp('l.s[aeiouy]', name) from (SELECT 'lisa' AS name UNION ALL SELECT 'bart')")
checkNoError(t, err, "couldn't prepare statement: %s")
defer checkFinalize(s, t)
......@@ -108,7 +108,7 @@ func TestUserFunction(t *testing.T) {
err := db.CreateScalarFunction("user", 0, nil, user, nil)
checkNoError(t, err, "couldn't create function: %s")
var name string
err = db.OneValue("select user()", &name)
err = db.OneValue("SELECT user()", &name)
checkNoError(t, err, "couldn't retrieve result: %s")
assert(t, "unexpected user name: %q", len(name) > 0)
err = db.CreateScalarFunction("user", 1, nil, nil, nil)
......@@ -142,7 +142,7 @@ func TestSumFunction(t *testing.T) {
err := db.CreateAggregateFunction("mysum", 1, nil, sumStep, sumFinal, nil)
checkNoError(t, err, "couldn't create function: %s")
var i int
err = db.OneValue("select mysum(i) from (select 2 as i union all select 2)", &i)
err = db.OneValue("SELECT mysum(i) FROM (SELECT 2 AS i UNION ALL SELECT 2)", &i)
checkNoError(t, err, "couldn't execute statement: %s")
assertEquals(t, "expected %d but got %v", 4, i)
......@@ -170,7 +170,7 @@ func BenchmarkLike(b *testing.B) {
db, _ := Open(":memory:")
defer db.Close()
randomFill(db, 1)
cs, _ := db.Prepare("SELECT count(1) FROM test where name like 'lisa'")
cs, _ := db.Prepare("SELECT count(1) FROM test WHERE name LIKE 'lisa'")
defer cs.Finalize()
b.StartTimer()
......@@ -186,7 +186,7 @@ func BenchmarkHalf(b *testing.B) {
defer db.Close()
randomFill(db, 1)
db.CreateScalarFunction("half", 1, nil, half, nil)
cs, _ := db.Prepare("SELECT count(1) FROM test where half(rank) > 20")
cs, _ := db.Prepare("SELECT count(1) FROM test WHERE half(rank) > 20")
defer cs.Finalize()
b.StartTimer()
......@@ -202,7 +202,7 @@ func BenchmarkRegexp(b *testing.B) {
defer db.Close()
randomFill(db, 1)
db.CreateScalarFunction("regexp", 2, nil, re, reDestroy)
cs, _ := db.Prepare("SELECT count(1) FROM test where name regexp '(?i)\\blisa\\b'")
cs, _ := db.Prepare("SELECT count(1) FROM test WHERE name regexp '(?i)\\blisa\\b'")
defer cs.Finalize()
b.StartTimer()
......
......@@ -119,9 +119,9 @@ func TestSavepoint(t *testing.T) {
func TestExists(t *testing.T) {
db := open(t)
defer checkClose(db, t)
b := Must(db.Exists("SELECT 1 where 1 = 0"))
b := Must(db.Exists("SELECT 1 WHERE 1 = 0"))
assert(t, "No row expected", !b)
b = Must(db.Exists("SELECT 1 where 1 = 1"))
b = Must(db.Exists("SELECT 1 WHERE 1 = 1"))
assert(t, "One row expected", b)
}
......@@ -183,7 +183,7 @@ func TestOpenSameMemoryDb(t *testing.T) {
db2, err := Open("file:dummy.db?mode=memory&cache=shared", OpenUri, OpenReadWrite, OpenCreate, OpenFullMutex)
checkNoError(t, err, "open error: %s")
defer checkClose(db2, t)
_, err = db2.Exists("SELECT 1 from test")
_, err = db2.Exists("SELECT 1 FROM test")
checkNoError(t, err, "exists error: %s")
}
......@@ -191,7 +191,7 @@ func TestConnExecWithSelect(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("select 1")
err := db.Exec("SELECT 1")
assert(t, "error expected", err != nil)
if serr, ok := err.(*StmtError); ok {
assertEquals(t, "expected %q but got %q", Row, serr.Code())
......@@ -232,7 +232,15 @@ func TestConnSettings(t *testing.T) {
}
func TestComplete(t *testing.T) {
assert(t, "expected complete statement", Complete("select 1;"))
assert(t, "expected complete statement", Complete("SELECT 1;"))
}
func TestExecMisuse(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
err := db.Exec("INSERT INTO test VALUES (?, ?, ?, ?); INSERT INTO test VALUES (?, ?, ?, ?)", 0, 273.1, 1, "test")
assert(t, "exec misuse expected", err != nil)
}
func assertEquals(t *testing.T, format string, expected, actual interface{}) {
......
......@@ -50,7 +50,7 @@ func TestInsertWithStatement(t *testing.T) {
cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
defer checkFinalize(cs, t)
assert(t, "select statement should be readonly", cs.ReadOnly())
assert(t, "SELECT statement should be readonly", cs.ReadOnly())
if !Must(cs.Next()) {
t.Fatal("no result for count")
}
......@@ -58,7 +58,7 @@ func TestInsertWithStatement(t *testing.T) {
checkNoError(t, cs.Scan(&i), "error scanning count: %s")
assertEquals(t, "count should be %d, but it is %d", 1000, i)
rs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test where a_string like ? ORDER BY int_num LIMIT 2", "hel%")
rs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test WHERE a_string LIKE ? ORDER BY int_num LIMIT 2", "hel%")
defer checkFinalize(rs, t)
columnCount = rs.ColumnCount()
assertEquals(t, "column count error: expected %d but got %d", 3, columnCount)
......@@ -92,7 +92,7 @@ func TestScanColumn(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("select 1, null, 0")
s, err := db.Prepare("SELECT 1, null, 0")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
......@@ -114,7 +114,7 @@ func TestNamedScanColumn(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("select 1 as i1, null as i2, 0 as i3")
s, err := db.Prepare("SELECT 1 AS i1, null AS i2, 0 AS i3")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
......@@ -130,13 +130,16 @@ func TestNamedScanColumn(t *testing.T) {
null = Must(s.ScanByName("i3", &i3))
assert(t, "expected not null value", !null)
assertEquals(t, "expected %d but got %d", 0, i3)
_, err = s.ScanByName("invalid", &i1)
assert(t, "expected invalid name", err != nil)
}
func TestScanCheck(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("select 'hello'")
s, err := db.Prepare("SELECT 'hello'")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
......@@ -157,7 +160,7 @@ func TestScanNull(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("select null")
s, err := db.Prepare("SELECT null")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
......@@ -177,7 +180,7 @@ func TestScanNotNull(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("select 1")
s, err := db.Prepare("SELECT 1")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
......@@ -198,7 +201,7 @@ func TestScanError(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("select 1")
s, err := db.Prepare("SELECT 1")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
......@@ -239,7 +242,7 @@ func TestStmtWithClosedDb(t *testing.T) {
db.SetCacheSize(0)
s, err := db.Prepare("select 1")
s, err := db.Prepare("SELECT 1")
checkNoError(t, err, "prepare error: %s")
assertEquals(t, "expected Conn: %p, actual: %p", db, s.Conn())
defer s.Finalize()
......@@ -256,7 +259,7 @@ func TestStmtExecWithSelect(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("select 1")
s, err := db.Prepare("SELECT 1")
checkNoError(t, err, "prepare error: %s")
defer s.Finalize()
......@@ -280,7 +283,7 @@ func TestStmtSelectWithInsert(t *testing.T) {
defer s.Finalize()
exists, err := s.SelectOneRow()
checkNoError(t, err, "select error: %s")
checkNoError(t, err, "SELECT error: %s")
assert(t, "no row expected", !exists)
}
......@@ -305,9 +308,16 @@ func TestNamedBind(t *testing.T) {
checkNoError(t, err, "named bind error: %s")
_, err = is.Next()
checkNoError(t, err, "named bind step error: %s")
err = is.NamedBind(":b", byt, ":invalid", nil)
assert(t, "invalid param name expected", err != nil)
err = is.NamedBind(":b")
assert(t, "missing params", err != nil)
err = is.NamedBind(byt, ":b")
assert(t, "invalid param name", err != nil)
checkFinalize(is, t)
s, err := db.Prepare("SELECT data as bs, byte as b FROM test")
s, err := db.Prepare("SELECT data AS bs, byte AS b FROM test")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
......@@ -319,4 +329,41 @@ func TestNamedBind(t *testing.T) {
checkNoError(t, err, "named scan error: %s")
assertEquals(t, "expected blob: %v, actual: %s", len(blob), len(bs))
assertEquals(t, "expected byte: %c, actual: %c", byt, b)
err = s.NamedScan("b")
assert(t, "missing params", err != nil)
err = s.NamedScan(&b, "b")
assert(t, "invalid param name", err != nil)
}
func TestBind(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (data TEXT, bool INT)")
checkNoError(t, err, "exec error: %s")
is, err := db.Prepare("INSERT INTO test (data, bool) VALUES (?, ?)")
defer checkFinalize(is, t)
checkNoError(t, err, "prepare error: %s")
err = is.Bind(nil, true)
checkNoError(t, err, "bind error: %s")
_, err = is.Next()
checkNoError(t, err, "step error: %s")
err = is.Bind(nil, db)
assert(t, "unsupported type error expected", err != nil)
}
func TestInsertMisuse(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (data TEXT, bool INT)")
checkNoError(t, err, "exec error: %s")
is, err := db.Prepare("INSERT INTO test (data, bool) VALUES (?, ?)")
defer checkFinalize(is, t)
checkNoError(t, err, "prepare error: %s")
_, err = is.Insert()
assert(t, "missing bind parameters expected", err != nil)
}
......@@ -105,16 +105,15 @@ func TestCreateModule(t *testing.T) {
err = db.Exec("CREATE VIRTUAL TABLE vtab USING test('1', 2, three)")
checkNoError(t, err, "couldn't create virtual table: %s")
s, err := db.Prepare("SELECT * from vtab")
s, err := db.Prepare("SELECT rowid, * FROM vtab")
checkNoError(t, err, "couldn't select from virtual table: %s")
defer checkFinalize(s, t)
var i, value int
err = s.Select(func(s *Stmt) (err error) {
if err = s.Scan(&value); err != nil {
if err = s.Scan(&i, &value); err != nil {
return
}
assertEquals(t, "Expected '%d' but got '%d'", intarray[i], value)
i++
return
})
checkNoError(t, err, "couldn't select from virtual table: %s")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment