diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-23 12:17:30 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-23 12:20:16 -0600 |
commit | 482f00fdfc52f0b4592014b37dc9e1749a7cfd6d (patch) | |
tree | 7ecbac715fd4556b77cb46ff6a1096a39111fab5 /tx_test.go | |
parent | Merge pull request #78 from benbjohnson/tx-managed (diff) | |
download | dedo-482f00fdfc52f0b4592014b37dc9e1749a7cfd6d.tar.gz dedo-482f00fdfc52f0b4592014b37dc9e1749a7cfd6d.tar.xz |
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.
Diffstat (limited to 'tx_test.go')
-rw-r--r-- | tx_test.go | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -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) { |