Commit 72be6aac authored by gwenn's avatar gwenn

Clean test (continued).

parent ce883c04
......@@ -19,35 +19,27 @@ const (
func sqlOpen(t *testing.T) *sql.DB {
db, err := sql.Open("sqlite3", "")
if err != nil {
t.Fatalf("Error opening database: %s", err)
}
checkNoError(t, err, "Error opening database: %s")
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)
}
checkNoError(t, err, "Error creating table: %s")
return db
}
func TestSqlOpen(t *testing.T) {
db := sqlOpen(t)
if err := db.Close(); err != nil {
t.Fatalf("Error closing database: %s", err)
}
checkNoError(t, db.Close(), "Error closing database: %s")
}
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)
}
checkNoError(t, err, "Error creating table: %s")
_, err = result.LastInsertId() // FIXME Error expected
if err == nil {
t.Logf("Error expected when calling LastInsertId after DDL")
......@@ -62,20 +54,14 @@ 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)
}
checkNoError(t, err, "Error updating data: %s")
id, err := result.LastInsertId()
if err != nil {
t.Errorf("Error while calling LastInsertId: %s", err)
}
checkNoError(t, err, "Error while calling LastInsertId: %s")
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)
}
checkNoError(t, err, "Error while calling RowsAffected: %s")
if changes != 0 {
t.Errorf("Expected %d got %d RowsAffected", 0, changes)
}
......@@ -85,20 +71,14 @@ 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)
}
checkNoError(t, err, "Error updating data: %s")
id, err := result.LastInsertId()
if err != nil {
t.Errorf("Error while calling LastInsertId: %s", err)
}
checkNoError(t, err, "Error while calling LastInsertId: %s")
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)
}
checkNoError(t, err, "Error while calling RowsAffected: %s")
if changes != 1 {
t.Errorf("Expected %d got %d RowsAffected", 0, changes)
}
......@@ -124,9 +104,7 @@ func TestSqlQuery(t *testing.T) {
var name string
for rows.Next() {
err = rows.Scan(&id, &name)
if err != nil {
t.Errorf("Error while scanning: %s", err)
}
checkNoError(t, err, "Error while scanning: %s")
}
// FIXME Dangling statement
}
......@@ -136,13 +114,9 @@ func TestSqlTx(t *testing.T) {
defer db.Close()
tx, err := db.Begin()
if err != nil {
t.Errorf("Error while begining tx: %s", err)
}
checkNoError(t, err, "Error while begining tx: %s")
err = tx.Rollback()
if err != nil {
t.Errorf("Error while rollbacking tx: %s", err)
}
checkNoError(t, err, "Error while rollbacking tx: %s")
}
func TestSqlPrepare(t *testing.T) {
......@@ -150,12 +124,8 @@ func TestSqlPrepare(t *testing.T) {
defer db.Close()
stmt, err := db.Prepare(insert)
if err != nil {
t.Errorf("Error while preparing stmt: %s", err)
}
checkNoError(t, err, "Error while preparing stmt: %s")
defer stmt.Close()
_, err = stmt.Exec("Bart")
if err != nil {
t.Errorf("Error while executing stmt: %s", err)
}
checkNoError(t, err, "Error while executing stmt: %s")
}
......@@ -18,23 +18,17 @@ func half(ctx *Context, nArg int) {
func TestScalarFunction(t *testing.T) {
db, err := Open("")
if err != nil {
t.Fatalf("couldn't open database file: %s", err)
}
checkNoError(t, err, "couldn't open database file: %s")
defer db.Close()
if err = db.CreateScalarFunction("half", 1, nil, half, nil); err != nil {
t.Fatalf("couldn't create function: %s", err)
}
err = db.CreateScalarFunction("half", 1, nil, half, nil)
checkNoError(t, err, "couldn't create function: %s")
d, err := db.OneValue("select half(6)")
if err != nil {
t.Fatalf("couldn't retrieve result: %s", err)
}
checkNoError(t, err, "couldn't retrieve result: %s")
if d != 3.0 {
t.Errorf("Expected %f but got %f", 3.0, d)
}
if err = db.CreateScalarFunction("half", 1, nil, nil, nil); err != nil {
t.Errorf("couldn't destroy function: %s", err)
}
err = db.CreateScalarFunction("half", 1, nil, nil, nil)
checkNoError(t, err, "couldn't destroy function: %s")
}
func re(ctx *Context, nArg int) {
......@@ -69,25 +63,18 @@ func reDestroy(ad interface{}) {
func TestRegexpFunction(t *testing.T) {
db, err := Open("")
if err != nil {
t.Fatalf("couldn't open database file: %s", err)
}
checkNoError(t, err, "couldn't open database file: %s")
defer db.Close()
if err = db.CreateScalarFunction("regexp", 2, nil, re, reDestroy); err != nil {
t.Fatalf("couldn't create function: %s", err)
}
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' as name)")
if err != nil {
t.Fatalf("couldn't prepare statement: %s", err)
}
checkNoError(t, err, "couldn't prepare statement: %s")
defer s.Finalize()
if b := Must(s.Next()); !b {
t.Fatalf("No result")
}
i, _, err := s.ScanInt(0)
if err != nil {
t.Fatalf("couldn't scan result: %s", err)
}
checkNoError(t, err, "couldn't scan result: %s")
if i != 1 {
t.Errorf("Expected %d but got %d", 1, i)
}
......@@ -95,9 +82,7 @@ func TestRegexpFunction(t *testing.T) {
t.Fatalf("No result")
}
i, _, err = s.ScanInt(0)
if err != nil {
t.Fatalf("couldn't scan result: %s", err)
}
checkNoError(t, err, "couldn't scan result: %s")
if i != 0 {
t.Errorf("Expected %d but got %d", 0, i)
}
......@@ -127,17 +112,12 @@ func sumFinal(ctx *Context) {
/*
func TestSumFunction(t *testing.T) {
db, err := Open("")
if err != nil {
t.Fatalf("couldn't open database file: %s", err)
}
checkNoError(t, err, "couldn't open database file: %s")
defer db.Close()
if err = db.CreateAggregateFunction("sum", 1, nil, sumStep, sumFinal, nil); err != nil {
t.Fatalf("couldn't create function: %s", err)
}
err = db.CreateAggregateFunction("sum", 1, nil, sumStep, sumFinal, nil)
checkNoError(t, err, "couldn't create function: %s")
i, err := db.OneValue("select sum(i) from (select 2 as i union all select 2 as i)")
if err != nil {
t.Fatalf("couldn't execute statement: %s", err)
}
checkNoError(t, err, "couldn't execute statement: %s")
if i != 4 {
t.Errorf("Expected %d but got %d", 4, i)
}
......
......@@ -8,9 +8,7 @@ import (
func createIndex(db *Conn, t *testing.T) {
err := db.Exec("DROP INDEX IF EXISTS test_index;" +
"CREATE INDEX test_index on test(a_string)")
if err != nil {
t.Fatalf("error creating index: %s", err)
}
checkNoError(t, err, "error creating index: %s")
}
func TestDatabases(t *testing.T) {
......@@ -18,9 +16,7 @@ func TestDatabases(t *testing.T) {
defer db.Close()
databases, err := db.Databases()
if err != nil {
t.Fatalf("error looking for databases: %s", err)
}
checkNoError(t, err, "error looking for databases: %s")
if len(databases) != 1 {
t.Errorf("Expected one database but got %d\n", len(databases))
}
......@@ -34,17 +30,13 @@ func TestTables(t *testing.T) {
defer db.Close()
tables, err := db.Tables()
if err != nil {
t.Fatalf("error looking for tables: %s", err)
}
checkNoError(t, err, "error looking for tables: %s")
if len(tables) != 0 {
t.Errorf("Expected no table but got %d\n", len(tables))
}
createTable(db, t)
tables, err = db.Tables()
if err != nil {
t.Fatalf("error looking for tables: %s", err)
}
checkNoError(t, err, "error looking for tables: %s")
if len(tables) != 1 {
t.Errorf("Expected one table but got %d\n", len(tables))
}
......@@ -59,9 +51,7 @@ func TestColumns(t *testing.T) {
createTable(db, t)
columns, err := db.Columns("test")
if err != nil {
t.Fatalf("error listing columns: %s", err)
}
checkNoError(t, err, "error listing columns: %s")
if len(columns) != 4 {
t.Fatalf("Expected 4 columns <> %d", len(columns))
}
......@@ -78,13 +68,9 @@ func TestForeignKeys(t *testing.T) {
err := db.Exec("CREATE TABLE parent (id INTEGER PRIMARY KEY);" +
"CREATE TABLE child (id INTEGER PRIMARY KEY, parentId INTEGER, " +
"FOREIGN KEY (parentId) REFERENCES parent(id));")
if err != nil {
t.Fatalf("error creating tables: %s", err)
}
checkNoError(t, err, "error creating tables: %s")
fks, err := db.ForeignKeys("child")
if err != nil {
t.Fatalf("error listing FKs: %s", err)
}
checkNoError(t, err, "error listing FKs: %s")
if len(fks) != 1 {
t.Fatalf("Expected 1 FK <> %d", len(fks))
}
......@@ -101,9 +87,7 @@ func TestIndexes(t *testing.T) {
createIndex(db, t)
indexes, err := db.Indexes("test")
if err != nil {
t.Fatalf("error listing indexes: %s", err)
}
checkNoError(t, err, "error listing indexes: %s")
if len(indexes) != 1 {
t.Fatalf("Expected one index <> %d", len(indexes))
}
......@@ -116,9 +100,7 @@ func TestIndexes(t *testing.T) {
}
columns, err := db.IndexColumns("test_index")
if err != nil {
t.Fatalf("error listing index columns: %s", err)
}
checkNoError(t, err, "error listing index columns: %s")
if len(columns) != 1 {
t.Fatalf("Expected one column <> %d", len(columns))
}
......
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