diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-28 00:07:05 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-29 14:22:32 -0600 |
commit | 7f2de9f17a8c6113176ecb5a3eb6ecc0772a9ec1 (patch) | |
tree | d8381077fb0d78b86219491d5331589cf470042a /db_test.go | |
parent | Merge pull request #97 from benbjohnson/cli (diff) | |
download | dedo-7f2de9f17a8c6113176ecb5a3eb6ecc0772a9ec1.tar.gz dedo-7f2de9f17a8c6113176ecb5a3eb6ecc0772a9ec1.tar.xz |
Add DB.Check().
Diffstat (limited to 'db_test.go')
-rw-r--r-- | db_test.go | 23 |
1 files changed, 22 insertions, 1 deletions
@@ -78,7 +78,8 @@ func TestDBMetaInitWriteError(t *testing.T) { // Ensure that a database that is too small returns an error. func TestDBFileTooSmall(t *testing.T) { - withOpenDB(func(db *DB, path string) { + withDB(func(db *DB, path string) { + assert.NoError(t, db.Open(path, 0666)) db.Close() // corrupt the database @@ -130,6 +131,7 @@ func TestDBBeginRW(t *testing.T) { assert.NoError(t, err) assert.Equal(t, tx.DB(), db) assert.Equal(t, tx.Writable(), true) + assert.NoError(t, tx.Commit()) }) } @@ -382,9 +384,28 @@ func withOpenDB(fn func(*DB, string)) { } defer db.Close() fn(db, path) + + // Check database consistency after every test. + mustCheck(db) }) } +// mustCheck runs a consistency check on the database and panics if any errors are found. +func mustCheck(db *DB) { + if err := db.Check(); err != nil { + // Copy db off first. + db.CopyFile("/tmp/check.db", 0600) + + if errors, ok := err.(ErrorList); ok { + for _, err := range errors { + warn(err) + } + } + warn(err) + panic("check failure: see /tmp/check.db") + } +} + func trunc(b []byte, length int) []byte { if length < len(b) { return b[:length] |