From 482f00fdfc52f0b4592014b37dc9e1749a7cfd6d Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Sun, 23 Mar 2014 12:17:30 -0600 Subject: Add ErrTxClosed error. Commit/Rollback and mutable calls on Tx and Bucket now return ErrTxClosed if the transaction has already been committed or rolled back. Non-mutable calls have added an assertion to check if the transaction is closed which will cause a panic. I don't want to introduce an error return for accessor methods that are being used improperly so I think the panic is appropriate. --- tx_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'tx_test.go') diff --git a/tx_test.go b/tx_test.go index afdbb02..fe33869 100644 --- a/tx_test.go +++ b/tx_test.go @@ -13,6 +13,33 @@ import ( "github.com/stretchr/testify/assert" ) +// Ensure that committing a closed transaction returns an error. +func TestTxCommitClosed(t *testing.T) { + withOpenDB(func(db *DB, path string) { + tx, _ := db.RWTx() + tx.CreateBucket("foo") + assert.NoError(t, tx.Commit()) + assert.Equal(t, tx.Commit(), ErrTxClosed) + }) +} + +// Ensure that rolling back a closed transaction returns an error. +func TestTxRollbackClosed(t *testing.T) { + withOpenDB(func(db *DB, path string) { + tx, _ := db.RWTx() + assert.NoError(t, tx.Rollback()) + assert.Equal(t, tx.Rollback(), ErrTxClosed) + }) +} + +// Ensure that committing a read-only transaction returns an error. +func TestTxCommitReadOnly(t *testing.T) { + withOpenDB(func(db *DB, path string) { + tx, _ := db.Tx() + assert.Equal(t, tx.Commit(), ErrTxNotWritable) + }) +} + // Ensure that the database can retrieve a list of buckets. func TestTxBuckets(t *testing.T) { withOpenDB(func(db *DB, path string) { @@ -41,6 +68,15 @@ func TestTxCreateBucketReadOnly(t *testing.T) { }) } +// Ensure that creating a bucket on a closed transaction returns an error. +func TestTxCreateBucketClosed(t *testing.T) { + withOpenDB(func(db *DB, path string) { + tx, _ := db.RWTx() + tx.Commit() + assert.Equal(t, tx.CreateBucket("foo"), ErrTxClosed) + }) +} + // Ensure that a Tx can retrieve a bucket. func TestTxBucket(t *testing.T) { withOpenDB(func(db *DB, path string) { @@ -204,6 +240,15 @@ func TestTxDeleteBucket(t *testing.T) { }) } +// Ensure that deleting a bucket on a closed transaction returns an error. +func TestTxDeleteBucketClosed(t *testing.T) { + withOpenDB(func(db *DB, path string) { + tx, _ := db.RWTx() + tx.Commit() + assert.Equal(t, tx.DeleteBucket("foo"), ErrTxClosed) + }) +} + // Ensure that deleting a bucket with a read-only transaction returns an error. func TestTxDeleteBucketReadOnly(t *testing.T) { withOpenDB(func(db *DB, path string) { -- cgit v1.2.3