aboutsummaryrefslogtreecommitdiff
path: root/db_test.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-03-08 20:25:37 -0700
committerBen Johnson <benbjohnson@yahoo.com>2014-03-08 20:40:48 -0700
commitc551e45a4722f58dc4c19f9d1b80b0b8db3ad039 (patch)
tree93ac43914f9cd20c8eea13f18105e55026a7ea8d /db_test.go
parentRename Transaction to Tx. (diff)
downloaddedo-c551e45a4722f58dc4c19f9d1b80b0b8db3ad039.tar.gz
dedo-c551e45a4722f58dc4c19f9d1b80b0b8db3ad039.tar.xz
Consolidate Tx and RWTx.
Diffstat (limited to 'db_test.go')
-rw-r--r--db_test.go46
1 files changed, 33 insertions, 13 deletions
diff --git a/db_test.go b/db_test.go
index 029d248..e35502c 100644
--- a/db_test.go
+++ b/db_test.go
@@ -158,8 +158,8 @@ func TestDBCorruptMeta0(t *testing.T) {
// Ensure that a database cannot open a transaction when it's not open.
func TestDBTxErrDatabaseNotOpen(t *testing.T) {
withDB(func(db *DB, path string) {
- txn, err := db.Tx()
- assert.Nil(t, txn)
+ tx, err := db.Tx()
+ assert.Nil(t, tx)
assert.Equal(t, err, ErrDatabaseNotOpen)
})
}
@@ -172,12 +172,32 @@ func TestDBDeleteFromMissingBucket(t *testing.T) {
})
}
+// Ensure that a read-write transaction can be retrieved.
+func TestDBRWTx(t *testing.T) {
+ withOpenDB(func(db *DB, path string) {
+ tx, err := db.RWTx()
+ assert.NotNil(t, tx)
+ assert.NoError(t, err)
+ assert.Equal(t, tx.DB(), db)
+ assert.Equal(t, tx.Writable(), true)
+ })
+}
+
+// Ensure that opening a transaction while the DB is closed returns an error.
+func TestDBRWTxOpenWithClosedDB(t *testing.T) {
+ withDB(func(db *DB, path string) {
+ tx, err := db.RWTx()
+ assert.Equal(t, err, ErrDatabaseNotOpen)
+ assert.Nil(t, tx)
+ })
+}
+
// Ensure a database can provide a transactional block.
func TestDBTxBlock(t *testing.T) {
withOpenDB(func(db *DB, path string) {
- err := db.Do(func(txn *RWTx) error {
- txn.CreateBucket("widgets")
- b := txn.Bucket("widgets")
+ err := db.Do(func(tx *Tx) error {
+ tx.CreateBucket("widgets")
+ b := tx.Bucket("widgets")
b.Put([]byte("foo"), []byte("bar"))
b.Put([]byte("baz"), []byte("bat"))
b.Delete([]byte("foo"))
@@ -194,8 +214,8 @@ func TestDBTxBlock(t *testing.T) {
// Ensure a closed database returns an error while running a transaction block
func TestDBTxBlockWhileClosed(t *testing.T) {
withDB(func(db *DB, path string) {
- err := db.Do(func(txn *RWTx) error {
- txn.CreateBucket("widgets")
+ err := db.Do(func(tx *Tx) error {
+ tx.CreateBucket("widgets")
return nil
})
assert.Equal(t, err, ErrDatabaseNotOpen)
@@ -276,9 +296,9 @@ func TestDBCopyFile(t *testing.T) {
// Ensure the database can return stats about itself.
func TestDBStat(t *testing.T) {
withOpenDB(func(db *DB, path string) {
- db.Do(func(txn *RWTx) error {
- txn.CreateBucket("widgets")
- b := txn.Bucket("widgets")
+ db.Do(func(tx *Tx) error {
+ tx.CreateBucket("widgets")
+ b := tx.Bucket("widgets")
for i := 0; i < 10000; i++ {
b.Put([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
@@ -293,7 +313,7 @@ func TestDBStat(t *testing.T) {
t0, _ := db.Tx()
t1, _ := db.Tx()
t2, _ := db.Tx()
- t2.Close()
+ t2.Rollback()
// Obtain stats.
stat, err := db.Stat()
@@ -305,8 +325,8 @@ func TestDBStat(t *testing.T) {
assert.Equal(t, stat.TxCount, 2)
// Close readers.
- t0.Close()
- t1.Close()
+ t0.Rollback()
+ t1.Rollback()
})
}