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)
}
} }
...@@ -48,9 +48,7 @@ func TestEnableFKey(t *testing.T) { ...@@ -48,9 +48,7 @@ func TestEnableFKey(t *testing.T) {
b := Must(db.IsFKeyEnabled()) b := Must(db.IsFKeyEnabled())
if !b { if !b {
b = Must(db.EnableFKey(true)) b = Must(db.EnableFKey(true))
if !b { assert(t, "cannot enabled FK", b)
t.Error("cannot enabled FK")
}
} }
} }
...@@ -89,13 +87,9 @@ func TestExists(t *testing.T) { ...@@ -89,13 +87,9 @@ func TestExists(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer db.Close()
b := Must(db.Exists("SELECT 1 where 1 = 0")) b := Must(db.Exists("SELECT 1 where 1 = 0"))
if b { assert(t, "No row expected", !b)
t.Error("No row expected")
}
b = Must(db.Exists("SELECT 1 where 1 = 1")) b = Must(db.Exists("SELECT 1 where 1 = 1"))
if !b { assert(t, "One row expected", b)
t.Error("One row expected")
}
} }
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
...@@ -107,43 +101,31 @@ func TestInsert(t *testing.T) { ...@@ -107,43 +101,31 @@ func TestInsert(t *testing.T) {
ierr := db.Exec("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)", float64(i)*float64(3.14), i, "hello") ierr := db.Exec("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)", float64(i)*float64(3.14), i, "hello")
checkNoError(t, ierr, "insert error: %s") checkNoError(t, ierr, "insert error: %s")
c := db.Changes() c := db.Changes()
if c != 1 { assertEquals(t, "insert error: expected %d changes but got %d", 1, c)
t.Errorf("insert error: %d but got 1", c)
}
} }
checkNoError(t, db.Commit(), "Error: %s") checkNoError(t, db.Commit(), "Error: %s")
lastId := db.LastInsertRowid() lastId := db.LastInsertRowid()
if lastId != 1000 { assertEquals(t, "last insert row id error: expected %d but got %d", int64(1000), lastId)
t.Errorf("last insert row id error: %d but got 1000", lastId)
}
cs, _ := db.Prepare("SELECT COUNT(*) FROM test") cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
defer cs.Finalize() defer cs.Finalize()
paramCount := cs.BindParameterCount() paramCount := cs.BindParameterCount()
if paramCount != 0 { assertEquals(t, "bind parameter count error: expected %d but got %d", 0, paramCount)
t.Errorf("bind parameter count error: %d but got 0", paramCount)
}
columnCount := cs.ColumnCount() columnCount := cs.ColumnCount()
if columnCount != 1 { assertEquals(t, "column count error: expected %d but got %d", 1, columnCount)
t.Errorf("column count error: %d but got 1", columnCount)
}
if !Must(cs.Next()) { if !Must(cs.Next()) {
t.Fatal("no result for count") t.Fatal("no result for count")
} }
var i int var i int
checkNoError(t, cs.Scan(&i), "error scanning count: %s") checkNoError(t, cs.Scan(&i), "error scanning count: %s")
if i != 1000 { assertEquals(t, "count should be %d, but it is %d", 1000, i)
t.Errorf("count should be 1000, but it is %d", i)
}
if Must(cs.Next()) { if Must(cs.Next()) {
t.Fatal("Only one row expected") t.Fatal("Only one row expected")
} }
if cs.Busy() { assert(t, "Statement not reset", !cs.Busy())
t.Error("Statement not reset")
}
} }
func TestInsertWithStatement(t *testing.T) { func TestInsertWithStatement(t *testing.T) {
...@@ -157,101 +139,67 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -157,101 +139,67 @@ func TestInsertWithStatement(t *testing.T) {
} }
defer s.Finalize() defer s.Finalize()
if s.ReadOnly() { assert(t, "update statement should not be readonly", !s.ReadOnly())
t.Errorf("update statement should not be readonly")
}
paramCount := s.BindParameterCount() paramCount := s.BindParameterCount()
if paramCount != 3 { assertEquals(t, "bind parameter count error: expected %d but got %d", 3, paramCount)
t.Errorf("bind parameter count error: %d but got 3", paramCount)
}
firstParamName, berr := s.BindParameterName(1) firstParamName, berr := s.BindParameterName(1)
if firstParamName != ":f" { checkNoError(t, berr, "error binding: %s")
t.Errorf("bind parameter name error: %s but got ':f' (%s)", firstParamName, berr) assertEquals(t, "bind parameter name error: expected %s but got %s", ":f", firstParamName /*, berr*/)
}
lastParamIndex, berr := s.BindParameterIndex(":s") lastParamIndex, berr := s.BindParameterIndex(":s")
if lastParamIndex != 3 { checkNoError(t, berr, "error binding: %s")
t.Errorf("bind parameter name error: %d but got 3 (%s)", lastParamIndex, berr) assertEquals(t, "bind parameter index error: expected %d but got %d", 3, lastParamIndex /*, berr*/)
} columnCount := s.ColumnCount()
assertEquals(t, "column count error: expected %d but got %d", 0, columnCount)
db.Begin() db.Begin()
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
c, ierr := s.ExecDml(float64(i)*float64(3.14), i, "hello") c, ierr := s.ExecDml(float64(i)*float64(3.14), i, "hello")
checkNoError(t, ierr, "insert error: %s") checkNoError(t, ierr, "insert error: %s")
if c != 1 { assertEquals(t, "insert error: expected %d changes but got %d", 1, c)
t.Errorf("insert error: %d but got 1", c) assert(t, "Statement not reset", !s.Busy())
}
if s.Busy() {
t.Errorf("Statement not reset")
}
} }
checkNoError(t, db.Commit(), "Error: %s") checkNoError(t, db.Commit(), "Error: %s")
cs, _ := db.Prepare("SELECT COUNT(*) FROM test") cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
defer cs.Finalize() defer cs.Finalize()
if !cs.ReadOnly() { assert(t, "select statement should be readonly", cs.ReadOnly())
t.Errorf("select statement should be readonly")
}
if !Must(cs.Next()) { if !Must(cs.Next()) {
t.Fatal("no result for count") t.Fatal("no result for count")
} }
var i int var i int
checkNoError(t, cs.Scan(&i), "error scanning count: %s") checkNoError(t, cs.Scan(&i), "error scanning count: %s")
if i != 1000 { assertEquals(t, "count should be %d, but it is %d", 1000, i)
t.Errorf("count should be 1000, but it is %d", 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 rs.Finalize() defer rs.Finalize()
columnCount := rs.ColumnCount() columnCount = rs.ColumnCount()
if columnCount != 3 { assertEquals(t, "column count error: expected %d but got %d", 3, columnCount)
t.Errorf("column count error: %d but got 3", columnCount)
}
secondColumnName := rs.ColumnName(1) secondColumnName := rs.ColumnName(1)
if secondColumnName != "int_num" { assertEquals(t, "column name error: expected %s but got %s", "int_num", secondColumnName)
t.Errorf("column name error: %s but got 'int_num'", secondColumnName)
}
if Must(rs.Next()) { if Must(rs.Next()) {
var fnum float64 var fnum float64
var inum int64 var inum int64
var sstr string var sstr string
rs.Scan(&fnum, &inum, &sstr) rs.Scan(&fnum, &inum, &sstr)
if fnum != 0 { assertEquals(t, "expected %f but got %f", float64(0), fnum)
t.Errorf("Expected 0 but got %f\n", fnum) assertEquals(t, "expected %d but got %d", int64(0), inum)
} assertEquals(t, "expected %q but got %q", "hello", sstr)
if inum != 0 {
t.Errorf("Expected 0 but got %d\n", inum)
}
if sstr != "hello" {
t.Errorf("Expected 'hello' but got %s\n", sstr)
}
} }
if Must(rs.Next()) { if Must(rs.Next()) {
var fnum float64 var fnum float64
var inum int64 var inum int64
var sstr string var sstr string
rs.NamedScan("a_string", &sstr, "float_num", &fnum, "int_num", &inum) rs.NamedScan("a_string", &sstr, "float_num", &fnum, "int_num", &inum)
if fnum != 3.14 { assertEquals(t, "expected %f but got %f", float64(3.14), fnum)
t.Errorf("Expected 3.14 but got %f\n", fnum) assertEquals(t, "expected %d but got %d", int64(1), inum)
} assertEquals(t, "expected %q but got %q", "hello", sstr)
if inum != 1 {
t.Errorf("Expected 1 but got %d\n", inum)
}
if sstr != "hello" {
t.Errorf("Expected 'hello' but got %s\n", sstr)
}
}
if 999 != rs.Status(STMTSTATUS_FULLSCAN_STEP, false) {
t.Errorf("Expected full scan")
}
if 1 != rs.Status(STMTSTATUS_SORT, false) {
t.Errorf("Expected one sort")
}
if 0 != rs.Status(STMTSTATUS_AUTOINDEX, false) {
t.Errorf("Expected no auto index")
} }
assert(t, "expected full scan", 999 == rs.Status(STMTSTATUS_FULLSCAN_STEP, false))
assert(t, "expected one sort", 1 == rs.Status(STMTSTATUS_SORT, false))
assert(t, "expected no auto index", 0 == rs.Status(STMTSTATUS_AUTOINDEX, false))
} }
func TestScanColumn(t *testing.T) { func TestScanColumn(t *testing.T) {
...@@ -266,23 +214,14 @@ func TestScanColumn(t *testing.T) { ...@@ -266,23 +214,14 @@ func TestScanColumn(t *testing.T) {
} }
var i1, i2, i3 int var i1, i2, i3 int
null := Must(s.ScanByIndex(0, &i1)) null := Must(s.ScanByIndex(0, &i1))
if null { assert(t, "expected not null value", !null)
t.Errorf("Expected not null value") assertEquals(t, "expected %d but got %d", 1, i1)
} else if i1 != 1 {
t.Errorf("Expected 1 but got %d\n", i1)
}
null = Must(s.ScanByIndex(1, &i2)) null = Must(s.ScanByIndex(1, &i2))
if !null { assert(t, "expected null value", null)
t.Errorf("Expected null value") assertEquals(t, "expected %d but got %d", 0, i2)
} else if i2 != 0 {
t.Errorf("Expected 0 but got %d\n", i2)
}
null = Must(s.ScanByIndex(2, &i3)) null = Must(s.ScanByIndex(2, &i3))
if null { assert(t, "expected not null value", !null)
t.Errorf("Expected not null value") assertEquals(t, "expected %d but got %d", 0, i3)
} else if i3 != 0 {
t.Errorf("Expected 0 but got %d\n", i3)
}
} }
func TestNamedScanColumn(t *testing.T) { func TestNamedScanColumn(t *testing.T) {
...@@ -297,23 +236,14 @@ func TestNamedScanColumn(t *testing.T) { ...@@ -297,23 +236,14 @@ func TestNamedScanColumn(t *testing.T) {
} }
var i1, i2, i3 int var i1, i2, i3 int
null := Must(s.ScanByName("i1", &i1)) null := Must(s.ScanByName("i1", &i1))
if null { assert(t, "expected not null value", !null)
t.Errorf("Expected not null value") assertEquals(t, "expected %d but got %d", 1, i1)
} else if i1 != 1 {
t.Errorf("Expected 1 but got %d\n", i1)
}
null = Must(s.ScanByName("i2", &i2)) null = Must(s.ScanByName("i2", &i2))
if !null { assert(t, "expected null value", null)
t.Errorf("Expected null value") assertEquals(t, "expected %d but got %d", 0, i2)
} else if i2 != 0 {
t.Errorf("Expected 0 but got %d\n", i2)
}
null = Must(s.ScanByName("i3", &i3)) null = Must(s.ScanByName("i3", &i3))
if null { assert(t, "expected not null value", !null)
t.Errorf("Expected not null value") assertEquals(t, "expected %d but got %d", 0, i3)
} else if i3 != 0 {
t.Errorf("Expected 0 but got %d\n", i3)
}
} }
func TestScanCheck(t *testing.T) { func TestScanCheck(t *testing.T) {
...@@ -329,15 +259,9 @@ func TestScanCheck(t *testing.T) { ...@@ -329,15 +259,9 @@ func TestScanCheck(t *testing.T) {
var i int var i int
_, err = s.ScanByIndex(0, &i) _, err = s.ScanByIndex(0, &i)
if serr, ok := err.(*StmtError); ok { if serr, ok := err.(*StmtError); ok {
if serr.Filename() != "" { assertEquals(t, "expected %q but got %q", "", serr.Filename())
t.Errorf("Expected '' but got '%s'", serr.Filename()) assertEquals(t, "expected %q but got %q", ErrSpecific, serr.Code())
} assertEquals(t, "expected %q but got %q", s.SQL(), serr.SQL())
if serr.Code() != ErrSpecific {
t.Errorf("Expected %s but got %s", ErrSpecific, serr.Code())
}
if serr.SQL() != s.SQL() {
t.Errorf("Expected %s but got %s", s.SQL(), serr.SQL())
}
} else { } else {
t.Errorf("Expected StmtError but got %s", reflect.TypeOf(err)) t.Errorf("Expected StmtError but got %s", reflect.TypeOf(err))
} }
...@@ -366,18 +290,12 @@ func TestScanNull(t *testing.T) { ...@@ -366,18 +290,12 @@ func TestScanNull(t *testing.T) {
} }
var pi *int var pi *int
null := Must(s.ScanByIndex(0, &pi)) null := Must(s.ScanByIndex(0, &pi))
if !null { assert(t, "expected null value", null)
t.Errorf("Expected null value") assertEquals(t, "expected nil (%p) but got %p", (*int)(nil), pi)
} else if pi != nil {
t.Errorf("Expected nil but got %p\n", pi)
}
var ps *string var ps *string
null = Must(s.ScanByIndex(0, &ps)) null = Must(s.ScanByIndex(0, &ps))
if !null { assert(t, "expected null value", null)
t.Errorf("Expected null value") assertEquals(t, "expected nil (%p) but got %p", (*string)(nil), ps)
} else if ps != nil {
t.Errorf("Expected nil but got %p\n", ps)
}
} }
func TestCloseTwice(t *testing.T) { func TestCloseTwice(t *testing.T) {
...@@ -399,11 +317,18 @@ func TestStmtMisuse(t *testing.T) { ...@@ -399,11 +317,18 @@ func TestStmtMisuse(t *testing.T) {
defer db.Close() defer db.Close()
s, err := db.Prepare("MISUSE") s, err := db.Prepare("MISUSE")
if s != nil || err == nil { assert(t, "error expected", s == nil && err != nil)
t.Error("error expected")
}
err = s.Finalize() err = s.Finalize()
if err == nil { assert(t, "error expected", err != nil)
t.Error("error expected") }
func assertEquals(t *testing.T, format string, expected, actual interface{}) {
if expected != actual {
t.Errorf(format, expected, actual)
}
}
func assert(t *testing.T, msg string, actual bool) {
if !actual {
t.Error(msg)
} }
} }
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