From 79d9b6bb5aa5232df4197932859307359a6ae39a Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Mon, 13 Jan 2014 10:35:04 -0700 Subject: Begin Transaction.Cursor(). --- db_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'db_test.go') diff --git a/db_test.go b/db_test.go index cf36c37..b9c4afe 100644 --- a/db_test.go +++ b/db_test.go @@ -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) + }) +} -- cgit v1.2.3