Commit 33e931b3 authored by gwenn's avatar gwenn

Introduce helper methods in tests.

parent c3df08aa
...@@ -34,11 +34,7 @@ func TestBackupMisuse(t *testing.T) { ...@@ -34,11 +34,7 @@ func TestBackupMisuse(t *testing.T) {
defer db.Close() defer db.Close()
bck, err := NewBackup(db, "", db, "") bck, err := NewBackup(db, "", db, "")
if bck != nil || err == nil { assert(t, "source and destination must be distinct", bck == nil && err != nil)
t.Error("source and destination must be distinct")
}
err = bck.Run(10, 0, nil) err = bck.Run(10, 0, nil)
if err == nil { assert(t, "misuse expected", err != nil)
t.Error("Misuse expected")
}
} }
...@@ -36,9 +36,7 @@ func TestBlob(t *testing.T) { ...@@ -36,9 +36,7 @@ func TestBlob(t *testing.T) {
content = make([]byte, size) content = make([]byte, size)
n, err = br.Read(content) n, err = br.Read(content)
checkNoError(t, err, "blob read error: %s") checkNoError(t, err, "blob read error: %s")
if n != 10 { assertEquals(t, "expected %d bytes but got %d", 10, n)
t.Fatalf("Expected 10 bytes but got %d", n)
}
//fmt.Printf("%#v\n", content) //fmt.Printf("%#v\n", content)
br.Close() br.Close()
} }
...@@ -48,11 +46,7 @@ func TestBlobMisuse(t *testing.T) { ...@@ -48,11 +46,7 @@ func TestBlobMisuse(t *testing.T) {
defer db.Close() defer db.Close()
bw, err := db.NewBlobReadWriter("main", "test", "content", 0) bw, err := db.NewBlobReadWriter("main", "test", "content", 0)
if bw != nil || err == nil { assert(t, "error expected", bw == nil && err != nil)
t.Errorf("error expected")
}
err = bw.Close() err = bw.Close()
if err == nil { assert(t, "error expected", err != nil)
t.Errorf("error expected")
}
} }
...@@ -102,7 +102,5 @@ func TestBusyHandler(t *testing.T) { ...@@ -102,7 +102,5 @@ func TestBusyHandler(t *testing.T) {
_, err = db2.SchemaVersion() _, err = db2.SchemaVersion()
checkNoError(t, err, "couldn't query schema version: %#v") checkNoError(t, err, "couldn't query schema version: %#v")
if !called { assert(t, "busy handler not called!", called)
t.Fatalf("Busy handler not called!")
}
} }
...@@ -57,14 +57,10 @@ func TestSqlDml(t *testing.T) { ...@@ -57,14 +57,10 @@ func TestSqlDml(t *testing.T) {
checkNoError(t, err, "Error updating data: %s") checkNoError(t, err, "Error updating data: %s")
id, err := result.LastInsertId() id, err := result.LastInsertId()
checkNoError(t, err, "Error while calling LastInsertId: %s") checkNoError(t, err, "Error while calling LastInsertId: %s")
if id != 2 { assertEquals(t, "expected %d got %d LastInsertId", int64(2), id)
t.Errorf("Expected %d got %d LastInsertId", 2, id)
}
changes, err := result.RowsAffected() changes, err := result.RowsAffected()
checkNoError(t, err, "Error while calling RowsAffected: %s") checkNoError(t, err, "Error while calling RowsAffected: %s")
if changes != 0 { assertEquals(t, "expected %d got %d RowsAffected", int64(0), changes)
t.Errorf("Expected %d got %d RowsAffected", 0, changes)
}
} }
func TestSqlInsert(t *testing.T) { func TestSqlInsert(t *testing.T) {
...@@ -74,14 +70,10 @@ func TestSqlInsert(t *testing.T) { ...@@ -74,14 +70,10 @@ func TestSqlInsert(t *testing.T) {
checkNoError(t, err, "Error updating data: %s") checkNoError(t, err, "Error updating data: %s")
id, err := result.LastInsertId() id, err := result.LastInsertId()
checkNoError(t, err, "Error while calling LastInsertId: %s") checkNoError(t, err, "Error while calling LastInsertId: %s")
if id != 1 { assertEquals(t, "expected %d got %d LastInsertId", int64(1), id)
t.Errorf("Expected %d got %d LastInsertId", 2, id)
}
changes, err := result.RowsAffected() changes, err := result.RowsAffected()
checkNoError(t, err, "Error while calling RowsAffected: %s") checkNoError(t, err, "Error while calling RowsAffected: %s")
if changes != 1 { assertEquals(t, "expected %d got %d RowsAffected", int64(1), changes)
t.Errorf("Expected %d got %d RowsAffected", 0, changes)
}
} }
func TestSqlExecWithIllegalCmd(t *testing.T) { func TestSqlExecWithIllegalCmd(t *testing.T) {
......
...@@ -25,9 +25,7 @@ func TestScalarFunction(t *testing.T) { ...@@ -25,9 +25,7 @@ func TestScalarFunction(t *testing.T) {
var d float64 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") checkNoError(t, err, "couldn't retrieve result: %s")
if d != 3.0 { assertEquals(t, "Expected %f but got %f", 3.0, d)
t.Errorf("Expected %f but got %f", 3.0, d)
}
err = db.CreateScalarFunction("half", 1, nil, nil, nil) err = db.CreateScalarFunction("half", 1, nil, nil, nil)
checkNoError(t, err, "couldn't destroy function: %s") checkNoError(t, err, "couldn't destroy function: %s")
} }
...@@ -81,24 +79,16 @@ func TestRegexpFunction(t *testing.T) { ...@@ -81,24 +79,16 @@ func TestRegexpFunction(t *testing.T) {
} }
i, _, err := s.ScanInt(0) i, _, err := s.ScanInt(0)
checkNoError(t, err, "couldn't scan result: %s") checkNoError(t, err, "couldn't scan result: %s")
if i != 1 { assertEquals(t, "expected %d but got %d", 1, i)
t.Errorf("Expected %d but got %d", 1, i) assert(t, "unexpected reused state", !reused)
}
if reused {
t.Errorf("unexpected reused state")
}
if b := Must(s.Next()); !b { if b := Must(s.Next()); !b {
t.Fatalf("No result") t.Fatalf("No result")
} }
i, _, err = s.ScanInt(0) i, _, err = s.ScanInt(0)
checkNoError(t, err, "couldn't scan result: %s") checkNoError(t, err, "couldn't scan result: %s")
if i != 0 { assertEquals(t, "expected %d but got %d", 0, i)
t.Errorf("Expected %d but got %d", 0, i) assert(t, "unexpected reused state", reused)
}
if !reused {
t.Errorf("unexpected reused state")
}
} }
func sumStep(ctx *AggregateContext, nArg int) { func sumStep(ctx *AggregateContext, nArg int) {
...@@ -131,9 +121,7 @@ func TestSumFunction(t *testing.T) { ...@@ -131,9 +121,7 @@ func TestSumFunction(t *testing.T) {
var i int 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") checkNoError(t, err, "couldn't execute statement: %s")
if i != 4 { assertEquals(t, "expected %d but got %v", 4, i)
t.Errorf("Expected %d but got %v", 4, i)
}
} }
func randomFill(db *Conn, n int) { func randomFill(db *Conn, n int) {
......
...@@ -31,18 +31,12 @@ func TestTables(t *testing.T) { ...@@ -31,18 +31,12 @@ func TestTables(t *testing.T) {
tables, err := db.Tables() tables, err := db.Tables()
checkNoError(t, err, "error looking for tables: %s") checkNoError(t, err, "error looking for tables: %s")
if len(tables) != 0 { assertEquals(t, "expected %d table but got %d", 0, len(tables))
t.Errorf("Expected no table but got %d\n", len(tables))
}
createTable(db, t) createTable(db, t)
tables, err = db.Tables() tables, err = db.Tables()
checkNoError(t, err, "error looking for tables: %s") checkNoError(t, err, "error looking for tables: %s")
if len(tables) != 1 { assertEquals(t, "expected %d table but got %d", 1, len(tables))
t.Errorf("Expected one table but got %d\n", len(tables)) assertEquals(t, "wrong table name: %q <> %q", "test", tables[0])
}
if tables[0] != "test" {
t.Errorf("Wrong table name: 'test' <> %s\n", tables[0])
}
} }
func TestColumns(t *testing.T) { func TestColumns(t *testing.T) {
...@@ -56,9 +50,7 @@ func TestColumns(t *testing.T) { ...@@ -56,9 +50,7 @@ func TestColumns(t *testing.T) {
t.Fatalf("Expected 4 columns <> %d", len(columns)) t.Fatalf("Expected 4 columns <> %d", len(columns))
} }
column := columns[2] column := columns[2]
if column.Name != "int_num" { assertEquals(t, "wrong column name: %q <> %q", "int_num", column.Name)
t.Errorf("Wrong column name: 'int_num' <> %s", column.Name)
}
} }
func TestColumn(t *testing.T) { func TestColumn(t *testing.T) {
...@@ -68,15 +60,9 @@ func TestColumn(t *testing.T) { ...@@ -68,15 +60,9 @@ func TestColumn(t *testing.T) {
column, err := db.Column("", "test", "id") column, err := db.Column("", "test", "id")
checkNoError(t, err, "error getting column metadata: %s") checkNoError(t, err, "error getting column metadata: %s")
if column.Name != "id" { assertEquals(t, "wrong column name: %q <> %q", "id", column.Name)
t.Errorf("Wrong column name: 'id' <> %s", column.Name) assert(t, "expecting primary key flag to be true", column.Pk)
} assert(t, "expecting autoinc flag to be false", !column.Autoinc)
if !column.Pk {
t.Errorf("Expecting primary key flag to be true")
}
if column.Autoinc {
t.Errorf("Expecting autoinc flag to be false")
}
} }
func TestForeignKeys(t *testing.T) { func TestForeignKeys(t *testing.T) {
...@@ -90,11 +76,11 @@ func TestForeignKeys(t *testing.T) { ...@@ -90,11 +76,11 @@ func TestForeignKeys(t *testing.T) {
fks, err := db.ForeignKeys("child") fks, err := db.ForeignKeys("child")
checkNoError(t, err, "error listing FKs: %s") checkNoError(t, err, "error listing FKs: %s")
if len(fks) != 1 { if len(fks) != 1 {
t.Fatalf("Expected 1 FK <> %d", len(fks)) t.Fatalf("expected 1 FK <> %d", len(fks))
} }
fk := fks[0] fk := fks[0]
if fk.From[0] != "parentId" || fk.Table != "parent" || fk.To[0] != "id" { if fk.From[0] != "parentId" || fk.Table != "parent" || fk.To[0] != "id" {
t.Errorf("Unexpected FK data: %#v", fk) t.Errorf("unexpected FK data: %#v", fk)
} }
} }
...@@ -110,20 +96,14 @@ func TestIndexes(t *testing.T) { ...@@ -110,20 +96,14 @@ func TestIndexes(t *testing.T) {
t.Fatalf("Expected one index <> %d", len(indexes)) t.Fatalf("Expected one index <> %d", len(indexes))
} }
index := indexes[0] index := indexes[0]
if index.Name != "test_index" { assertEquals(t, "wrong index name: %q <> %q", "test_index", index.Name)
t.Errorf("Wrong index name: 'test_index' <> %s", index.Name) assert(t, "index 'test_index' is not unique", !index.Unique)
}
if index.Unique {
t.Errorf("Index 'test_index' is not unique")
}
columns, err := db.IndexColumns("test_index") columns, err := db.IndexColumns("test_index")
checkNoError(t, err, "error listing index columns: %s") checkNoError(t, err, "error listing index columns: %s")
if len(columns) != 1 { if len(columns) != 1 {
t.Fatalf("Expected one column <> %d", len(columns)) t.Fatalf("expected one column <> %d", len(columns))
} }
column := columns[0] column := columns[0]
if column.Name != "a_string" { assertEquals(t, "Wrong column name: %q <> %q", "a_string", column.Name)
t.Errorf("Wrong column name: 'a_string' <> %s", column.Name)
}
} }
...@@ -15,9 +15,7 @@ func TestEncoding(t *testing.T) { ...@@ -15,9 +15,7 @@ func TestEncoding(t *testing.T) {
defer db.Close() defer db.Close()
encoding, err := db.Encoding() encoding, err := db.Encoding()
checkNoError(t, err, "Error reading encoding of database: %s") checkNoError(t, err, "Error reading encoding of database: %s")
if encoding != "UTF-8" { assertEquals(t, "Expecting %s but got %s", "UTF-8", encoding)
t.Errorf("Expecting %s but got %s", "UTF-8", encoding)
}
} }
func TestSchemaVersion(t *testing.T) { func TestSchemaVersion(t *testing.T) {
...@@ -25,7 +23,5 @@ func TestSchemaVersion(t *testing.T) { ...@@ -25,7 +23,5 @@ func TestSchemaVersion(t *testing.T) {
defer db.Close() defer db.Close()
version, err := db.SchemaVersion() version, err := db.SchemaVersion()
checkNoError(t, err, "Error reading schema version of database: %s") checkNoError(t, err, "Error reading schema version of database: %s")
if version != 0 { assertEquals(t, "expecting %d but got %d", 0, version)
t.Errorf("Expecting %d but got %d", 0, version)
}
} }
This diff is collapsed.
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