diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-01-13 10:35:04 -0700 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-01-13 10:35:04 -0700 |
commit | 79d9b6bb5aa5232df4197932859307359a6ae39a (patch) | |
tree | 425896032d7156508a1f245d7d8af100618bb163 /db_test.go | |
parent | Finish open coverage. (diff) | |
download | dedo-79d9b6bb5aa5232df4197932859307359a6ae39a.tar.gz dedo-79d9b6bb5aa5232df4197932859307359a6ae39a.tar.xz |
Begin Transaction.Cursor().
Diffstat (limited to 'db_test.go')
-rw-r--r-- | db_test.go | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -180,6 +180,41 @@ func TestDBCorruptMeta1(t *testing.T) { }) } +//-------------------------------------- +// Transaction() +//-------------------------------------- + +// Ensure that a database cannot open a transaction when it's not open. +func TestDBTransactionDatabaseNotOpenError(t *testing.T) { + withDB(func(db *DB, path string) { + txn, err := db.Transaction(false) + assert.Nil(t, txn) + assert.Equal(t, err, DatabaseNotOpenError) + }) +} + +// Ensure that a database cannot open a writable transaction while one is in progress. +func TestDBTransactionInProgressError(t *testing.T) { + withOpenDB(func(db *DB, path string) { + db.Transaction(true) + txn, err := db.Transaction(true) + assert.Nil(t, txn) + assert.Equal(t, err, TransactionInProgressError) + }) +} + +// Ensure that a database can create a new writable transaction. +func TestDBTransactionWriter(t *testing.T) { + withOpenDB(func(db *DB, path string) { + txn, err := db.Transaction(true) + if assert.NotNil(t, txn) { + assert.Equal(t, txn.db, db) + assert.Equal(t, txn.writable, true) + } + assert.NoError(t, err) + }) +} + // withDB executes a function with a database reference. func withDB(fn func(*DB, string)) { f, _ := ioutil.TempFile("", "bolt-") @@ -200,3 +235,14 @@ func withMockDB(fn func(*DB, *mockos, *mocksyscall, string)) { db.syscall = syscall fn(db, os, syscall, "/mock/db") } + +// withOpenDB executes a function with an already opened database. +func withOpenDB(fn func(*DB, string)) { + withDB(func(db *DB, path string) { + if err := db.Open(path, 0666); err != nil { + panic("cannot open db: " + err.Error()) + } + defer db.Close() + fn(db, path) + }) +} |