Commit 412216e2 authored by gwenn's avatar gwenn

Add tests to the exp/sql driver implementation.

parent a13f41c5
...@@ -32,7 +32,6 @@ func (d *Driver) Open(name string) (driver.Conn, error) { ...@@ -32,7 +32,6 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
return &connImpl{c}, nil return &connImpl{c}, nil
} }
/*
func (c *connImpl) Exec(query string, args []interface{}) (driver.Result, error) { func (c *connImpl) Exec(query string, args []interface{}) (driver.Result, error) {
if err := c.c.Exec(query, args...); err != nil { if err := c.c.Exec(query, args...); err != nil {
return nil, err return nil, err
...@@ -49,7 +48,6 @@ func (c *connImpl) LastInsertId() (int64, error) { ...@@ -49,7 +48,6 @@ func (c *connImpl) LastInsertId() (int64, error) {
func (c *connImpl) RowsAffected() (int64, error) { func (c *connImpl) RowsAffected() (int64, error) {
return int64(c.c.Changes()), nil return int64(c.c.Changes()), nil
} }
*/
func (c *connImpl) Prepare(query string) (driver.Stmt, error) { func (c *connImpl) Prepare(query string) (driver.Stmt, error) {
s, err := c.c.Prepare(query) s, err := c.c.Prepare(query)
...@@ -60,7 +58,7 @@ func (c *connImpl) Prepare(query string) (driver.Stmt, error) { ...@@ -60,7 +58,7 @@ func (c *connImpl) Prepare(query string) (driver.Stmt, error) {
} }
func (c *connImpl) Close() error { func (c *connImpl) Close() error {
return c.c.Close() // TODO c.c = nil ? return c.c.Close()
} }
func (c *connImpl) Begin() (driver.Tx, error) { func (c *connImpl) Begin() (driver.Tx, error) {
...@@ -128,5 +126,8 @@ func (s *stmtImpl) Next(dest []interface{}) error { ...@@ -128,5 +126,8 @@ func (s *stmtImpl) Next(dest []interface{}) error {
if !ok { if !ok {
return io.EOF return io.EOF
} }
return s.s.Scan(dest...) for i := range dest {
dest[i] = s.s.ScanValue(i)
}
return nil
} }
package sqlite_test
import (
"exp/sql"
"testing"
)
const (
ddl = "DROP TABLE IF EXISTS test;" +
"CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT," +
" 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 ?"
)
func sqlOpen(t *testing.T) *sql.DB {
db, err := sql.Open("sqlite3", "")
if err != nil {
t.Fatalf("Error opening database: %s", err)
}
return db
}
func sqlCreate(ddl string, t *testing.T) *sql.DB {
db := sqlOpen(t)
_, err := db.Exec(ddl)
if err != nil {
t.Fatalf("Error creating table: %s", err)
}
return db
}
func TestSqlOpen(t *testing.T) {
db := sqlOpen(t)
if err := db.Close(); err != nil {
t.Fatalf("Error closing database: %s", err)
}
}
func TestSqlDdl(t *testing.T) {
db := sqlOpen(t)
defer db.Close()
result, err := db.Exec(ddl)
if err != nil {
t.Fatalf("Error creating table: %s", err)
}
_, err = result.LastInsertId() // FIXME Error expected
if err == nil {
t.Logf("Error expected when calling LastInsertId after DDL")
}
_, err = result.RowsAffected() // FIXME Error expected
if err == nil {
t.Logf("Error expected when calling RowsAffected after DDL")
}
}
func TestSqlDml(t *testing.T) {
db := sqlCreate(ddl, t)
defer db.Close()
result, err := db.Exec(dml)
if err != nil {
t.Fatalf("Error updating data: %s", err)
}
id, err := result.LastInsertId()
if err != nil {
t.Errorf("Error while calling LastInsertId: %s", err)
}
if id != 2 {
t.Errorf("Expected %d got %d LastInsertId", 2, id)
}
changes, err := result.RowsAffected()
if err != nil {
t.Errorf("Error while calling RowsAffected: %s", err)
}
if changes != 0 {
t.Errorf("Expected %d got %d RowsAffected", 0, changes)
}
}
func TestSqlInsert(t *testing.T) {
db := sqlCreate(ddl, t)
defer db.Close()
result, err := db.Exec(insert, "Bart")
if err != nil {
t.Fatalf("Error updating data: %s", err)
}
id, err := result.LastInsertId()
if err != nil {
t.Errorf("Error while calling LastInsertId: %s", err)
}
if id != 1 {
t.Errorf("Expected %d got %d LastInsertId", 2, id)
}
changes, err := result.RowsAffected()
if err != nil {
t.Errorf("Error while calling RowsAffected: %s", err)
}
if changes != 1 {
t.Errorf("Expected %d got %d RowsAffected", 0, changes)
}
}
func TestSqlExecWithIllegalCmd(t *testing.T) {
db := sqlCreate(ddl+dml, t)
defer db.Close()
_, err := db.Exec(query, "%")
if err == nil {
t.Fatalf("Error expected when calling Exec with a SELECT")
}
}
func TestSqlQuery(t *testing.T) {
db := sqlCreate(ddl+dml, t)
defer db.Close()
rows, err := db.Query(query, "%")
defer rows.Close()
var id int
var name string
for rows.Next() {
err = rows.Scan(&id, &name)
if err != nil {
t.Errorf("Error while scanning: %s", err)
}
}
// FIXME Dangling statement
}
func TestSqlTx(t *testing.T) {
db := sqlCreate(ddl, t)
defer db.Close()
tx, err := db.Begin()
if err != nil {
t.Errorf("Error while begining tx: %s", err)
}
err = tx.Rollback()
if err != nil {
t.Errorf("Error while rollbacking tx: %s", err)
}
}
func TestSqlPrepare(t *testing.T) {
db := sqlCreate(ddl+dml, t)
defer db.Close()
stmt, err := db.Prepare(insert)
if err != nil {
t.Errorf("Error while preparing stmt: %s", err)
}
defer stmt.Close()
_, err = stmt.Exec("Bart")
if err != nil {
t.Errorf("Error while executing stmt: %s", err)
}
}
...@@ -266,3 +266,7 @@ func Log(err /*Errno*/ int, msg string) { ...@@ -266,3 +266,7 @@ func Log(err /*Errno*/ int, msg string) {
defer C.free(unsafe.Pointer(cs)) defer C.free(unsafe.Pointer(cs))
C.my_log(C.int(err), cs) C.my_log(C.int(err), cs)
} }
// TODO sqlite3_config(SQLITE_CONFIG_LOG,...)
// #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
// The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a function with a call signature of void(*)(void*,int,const char*), and a pointer to void.
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