Commit 33e931b3 authored by gwenn's avatar gwenn

Introduce helper methods in tests.

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