Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactored Backup tests, removed irrelevant tests for ResultColumn an…

…d added content verification to QueryParameter test.
  • Loading branch information...
commit 74091eb0d6dcec664563966f78d0fe91e254dfda 1 parent bdcc4cd
@feyeleanor feyeleanor authored
View
25 database.go
@@ -2,6 +2,9 @@ package sqlite3
// #include <sqlite3.h>
// #include <stdlib.h>
+// int gosqlite3_prepare_v2(sqlite3* db, const char* zSql, int nByte, sqlite3_stmt **ppStmt) {
+// return sqlite3_prepare_v2(db, zSql, nByte, ppStmt, NULL);
+// }
import "C"
import (
"fmt"
@@ -180,7 +183,7 @@ func (db *Database) Prepare(sql string, values ...interface{}) (s *Statement, e
s = &Statement{db: db, timestamp: time.Now().UnixNano()}
cs := C.CString(sql)
defer C.free(unsafe.Pointer(cs))
- if e = SQLiteError(C.sqlite3_prepare_v2(db.handle, cs, -1, &s.cptr, nil)); e != nil {
+ if e = SQLiteError(C.gosqlite3_prepare_v2(db.handle, cs, -1, &s.cptr)); e != nil {
s = nil
} else {
if len(values) > 0 {
@@ -313,10 +316,11 @@ func (db *Database) Save(target *Database, dbname string) (e error) {
type Reporter chan *ProgressReport
type BackupParameters struct {
- Target string
- PagesPerStep int
- QueueLength int
- Interval time.Duration
+ Target string
+ PagesPerStep int
+ QueueLength int
+ Interval time.Duration
+ Verbose bool
}
// Backup creates a copy (backup) of the current database to the target file
@@ -331,11 +335,12 @@ func (db *Database) Backup(p BackupParameters) (r Reporter, e error) {
defer close(r)
for {
report := &ProgressReport{
- Source: db.Filename,
- Target: p.Target,
- Error: backup.Step(p.PagesPerStep),
- Total: backup.PageCount(),
- Remaining: backup.Remaining(),
+ Source: db.Filename,
+ Target: p.Target,
+ Error: backup.Step(p.PagesPerStep),
+ Total: backup.PageCount(),
+ Remaining: backup.Remaining(),
+ Verbose: p.Verbose,
}
r <- report
if e := report.Error; !(e == nil || e == BUSY || e == LOCKED) {
View
65 database_test.go
@@ -65,8 +65,12 @@ func (r *Reporter) finished(t *testing.T) bool {
report, ok := <- (*r)
if report != nil {
switch e := report.Error; {
- case e == nil: t.Logf("Backup still has %v pages of %v to copy to %v", report.Remaining, report.Total, report.Target)
- case e != DONE: t.Fatalf("Backup error %v", e)
+ case e == nil:
+ if report.Verbose {
+ t.Logf("Backup still has %v pages of %v to copy to %v", report.Remaining, report.Total, report.Target)
+ }
+ case e != DONE:
+ t.Fatalf("Backup error %v", e)
}
}
return !ok
@@ -79,30 +83,51 @@ func TestBackup(t *testing.T) {
db.createTestTables(t, FOO, BAR)
db.createTestData(t, 1000)
- if sync_reporter, e := db.Backup(BackupParameters{Target: "sync.db", PagesPerStep: 3, QueueLength: 1}); e == nil {
- d := time.Now().UnixNano()
- for messages = 0; !sync_reporter.finished(t); messages++ {}
- t.Logf("backup of %v generated %v synchronous messages and took %vns", db.Filename, messages, time.Now().UnixNano() - d)
+ TestSyncReporter := func(pages int, interval time.Duration) {
+ if reporter, e := db.Backup(BackupParameters{Target: "sync.db", PagesPerStep: pages, QueueLength: 1, Interval: interval, Verbose: false}); e == nil {
+ d := time.Now().UnixNano()
+ for messages = 0; !reporter.finished(t); messages++ {}
+ if interval == 0 {
+ t.Logf("backup of %v generated %v synchronous messages and took %vns", db.Filename, messages, time.Now().UnixNano() - d)
+ } else {
+ t.Logf("backup of %v generated %v synchronous messages and took %vns with interval %v", db.Filename, messages, time.Now().UnixNano() - d, interval)
+ }
+ }
}
- if sync_reporter, e := db.Backup(BackupParameters{Target: "sync.db", PagesPerStep: 3, QueueLength: 1, Interval: 100000}); e == nil {
- d := time.Now().UnixNano()
- for messages = 0; !sync_reporter.finished(t); messages++ {}
- t.Logf("backup of %v generated %v synchronous messages and took %vns with interval %v", db.Filename, messages, time.Now().UnixNano() - d, 100000)
- }
+ TestSyncReporter(3, 0)
+ TestSyncReporter(3, 100000)
- if async_reporter, e := db.Backup(BackupParameters{Target: "async.db", PagesPerStep: 3, QueueLength: 8}); e == nil {
- d := time.Now().UnixNano()
- for messages = 0; !async_reporter.finished(t); messages++ {}
- t.Logf("backup of %v generated %v asynchronous messages and took %vns", db.Filename, messages, time.Now().UnixNano() - d)
- }
+ TestSyncReporter(10, 0)
+ TestSyncReporter(10, 100000)
- if async_reporter, e := db.Backup(BackupParameters{Target: "async.db", PagesPerStep: 3, QueueLength: 8}); e == nil {
- d := time.Now().UnixNano()
- for messages = 0; !async_reporter.finished(t); messages++ {}
- t.Logf("backup of %v generated %v asynchronous messages and took %vns with interval %v", db.Filename, messages, time.Now().UnixNano() - d, 100000)
+ TestASyncReporter := func(queuelength, pages int, interval time.Duration) {
+ if reporter, e := db.Backup(BackupParameters{Target: "sync.db", PagesPerStep: pages, QueueLength: 1, Interval: interval, Verbose: false}); e == nil {
+ d := time.Now().UnixNano()
+ for messages = 0; !reporter.finished(t); messages++ {}
+ if interval == 0 {
+ t.Logf("backup of %v generated %v synchronous messages and took %vns", db.Filename, messages, time.Now().UnixNano() - d)
+ } else {
+ t.Logf("backup of %v generated %v synchronous messages and took %vns with interval %v", db.Filename, messages, time.Now().UnixNano() - d, interval)
+ }
+ }
}
+
+ TestASyncReporter(1, 3, 0)
+ TestASyncReporter(1, 10, 0)
+ TestASyncReporter(1, 3, 100000)
+ TestASyncReporter(1, 10, 100000)
+
+ TestASyncReporter(8, 3, 0)
+ TestASyncReporter(8, 10, 0)
+ TestASyncReporter(8, 3, 100000)
+ TestASyncReporter(8, 10, 100000)
+
+ TestASyncReporter(12, 3, 0)
+ TestASyncReporter(12, 10, 0)
+ TestASyncReporter(12, 3, 100000)
+ TestASyncReporter(12, 10, 100000)
})
}
View
10 helpers_test.go
@@ -35,7 +35,7 @@ func fatalOnSuccess(t *testing.T, e error, message string, parameters... interfa
}
}
-func (db *Database) stepThroughRows(t *testing.T, table *Table) (c int) {
+func (db *Database) stepThroughRows(t *testing.T, table *Table, verbose... bool) (c int) {
var e error
sql := fmt.Sprintf("SELECT * from %v;", table.Name)
c, e = db.Execute(sql, func(st *Statement, values ...interface{}) {
@@ -44,9 +44,13 @@ func (db *Database) stepThroughRows(t *testing.T, table *Table) (c int) {
case *gob.Decoder:
blob := &TwoItems{}
data.Decode(blob)
- t.Logf("BLOB => %v: %v, %v: %v\n", ResultColumn(0).Name(st), ResultColumn(0).Value(st), st.ColumnName(1), blob)
+ if len(verbose) > 0 && verbose[0] {
+ t.Logf("BLOB => %v: %v, %v: %v\n", ResultColumn(0).Name(st), ResultColumn(0).Value(st), st.ColumnName(1), blob)
+ }
default:
- t.Logf("TEXT => %v: %v, %v: %v\n", ResultColumn(0).Name(st), ResultColumn(0).Value(st), st.ColumnName(1), st.Column(1))
+ if len(verbose) > 0 && verbose[0] {
+ t.Logf("TEXT => %v: %v, %v: %v\n", ResultColumn(0).Name(st), ResultColumn(0).Value(st), st.ColumnName(1), st.Column(1))
+ }
}
})
fatalOnError(t, e, "%v failed on step %v", sql, c)
View
1  progress.go
@@ -9,4 +9,5 @@ type ProgressReport struct {
Remaining int
Source string
Target string
+ Verbose bool
}
View
12 query_parameter_test.go
@@ -8,9 +8,9 @@ func TestQueryParameterBinding(t *testing.T) {
t.Fatal("unable to acquire DB handle")
}
BAR.Create(db)
- sql := "INSERT INTO bar values (?, ?);"
- st, e := db.Prepare(sql)
- fatalOnError(t, e, "unable to prepare query: %v", sql)
+ SQL := "INSERT INTO bar values (?, ?);"
+ st, e := db.Prepare(SQL)
+ fatalOnError(t, e, "unable to prepare query: %v", SQL)
fatalOnError(t, QueryParameter(1).Bind(st, nil), "unable to bind NULL to column 1")
for _, v := range []interface{}{1.1, "hello", TwoItems{ "a", "b" }, []int{13, 27} } {
@@ -20,8 +20,12 @@ func TestQueryParameterBinding(t *testing.T) {
fatalOnError(t, QueryParameter(1).Bind(st, 1), "unable to bind integer to column 1")
fatalOnError(t, QueryParameter(2).Bind(st, TwoItems{ "a", "b" }), "unable to bind blob to column 2")
- st, e = db.Prepare("INSERT INTO bar values (1, ?)")
+ st, e = db.Prepare(SQL)
fatalOnError(t, e, "unable to prepare query: %v", st.SQLSource())
fatalOnError(t, QueryParameter(1).Bind(st, TwoItems{ "a", "b" }), "unable to bind blob to column 2")
+
+ t.Logf("test case for issue #12")
+ db.runQuery(t, SQL, 1, TwoItems{ "a", "b" })
+ db.stepThroughRows(t, BAR)
})
}
View
10 result_column_test.go
@@ -5,14 +5,6 @@ import "testing"
func TestResultColumn(t *testing.T) {
Session("test.db", func(db *Database) {
BAR.Create(db)
- st, e := db.Prepare("INSERT INTO bar values (?, ?)")
- fatalOnError(t, e, "unable to prepare query: %v", st.SQLSource())
-
- fatalOnError(t, QueryParameter(1).Bind(st, nil), "unable to bind NULL to column 1")
- for _, v := range []interface{}{1.1, "hello", TwoItems{ "a", "b" }, []int{13, 27} } {
- fatalOnError(t, QueryParameter(1).Bind(st, v), "erroneously bound %v to column 1", v)
- }
- fatalOnError(t, QueryParameter(1).Bind(st, 1), "unable to bind integer to column 1")
- fatalOnError(t, QueryParameter(2).Bind(st, TwoItems{ "a", "b" }), "unable to bind blob to column 2")
+ t.Logf("Test cases needed for ResultColumn")
})
}
Please sign in to comment.
Something went wrong with that request. Please try again.