diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-24 11:46:58 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-24 11:46:58 -0600 |
commit | 2bc868c4663468c38aa5d5523138b8837993db8a (patch) | |
tree | f264bf174c0ca41218a9cd40025ab90aeaf56ad4 /tx_test.go | |
parent | Merge pull request #87 from benbjohnson/errors (diff) | |
parent | Make DB/Tx API more consistent. (diff) | |
download | dedo-2bc868c4663468c38aa5d5523138b8837993db8a.tar.gz dedo-2bc868c4663468c38aa5d5523138b8837993db8a.tar.xz |
Merge pull request #88 from benbjohnson/tx-rename
Make DB/Tx API more consistent.
Diffstat (limited to 'tx_test.go')
-rw-r--r-- | tx_test.go | 92 |
1 files changed, 46 insertions, 46 deletions
@@ -16,7 +16,7 @@ import ( // Ensure that committing a closed transaction returns an error. func TestTxCommitClosed(t *testing.T) { withOpenDB(func(db *DB, path string) { - tx, _ := db.RWTx() + tx, _ := db.Begin(true) tx.CreateBucket("foo") assert.NoError(t, tx.Commit()) assert.Equal(t, tx.Commit(), ErrTxClosed) @@ -26,7 +26,7 @@ func TestTxCommitClosed(t *testing.T) { // Ensure that rolling back a closed transaction returns an error. func TestTxRollbackClosed(t *testing.T) { withOpenDB(func(db *DB, path string) { - tx, _ := db.RWTx() + tx, _ := db.Begin(true) assert.NoError(t, tx.Rollback()) assert.Equal(t, tx.Rollback(), ErrTxClosed) }) @@ -35,7 +35,7 @@ func TestTxRollbackClosed(t *testing.T) { // Ensure that committing a read-only transaction returns an error. func TestTxCommitReadOnly(t *testing.T) { withOpenDB(func(db *DB, path string) { - tx, _ := db.Tx() + tx, _ := db.Begin(false) assert.Equal(t, tx.Commit(), ErrTxNotWritable) }) } @@ -43,7 +43,7 @@ func TestTxCommitReadOnly(t *testing.T) { // Ensure that the database can retrieve a list of buckets. func TestTxBuckets(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("foo") tx.CreateBucket("bar") tx.CreateBucket("baz") @@ -61,7 +61,7 @@ func TestTxBuckets(t *testing.T) { // Ensure that creating a bucket with a read-only transaction returns an error. func TestTxCreateBucketReadOnly(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { assert.Equal(t, tx.CreateBucket("foo"), ErrTxNotWritable) return nil }) @@ -71,7 +71,7 @@ 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, _ := db.Begin(true) tx.Commit() assert.Equal(t, tx.CreateBucket("foo"), ErrTxClosed) }) @@ -80,7 +80,7 @@ func TestTxCreateBucketClosed(t *testing.T) { // Ensure that a Tx can retrieve a bucket. func TestTxBucket(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") b := tx.Bucket("widgets") if assert.NotNil(t, b) { @@ -94,7 +94,7 @@ func TestTxBucket(t *testing.T) { // Ensure that a Tx retrieving a non-existent key returns nil. func TestTxGetMissing(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") tx.Bucket("widgets").Put([]byte("foo"), []byte("bar")) value := tx.Bucket("widgets").Get([]byte("no_such_key")) @@ -107,12 +107,12 @@ func TestTxGetMissing(t *testing.T) { // Ensure that retrieving all buckets returns writable buckets. func TestTxWritableBuckets(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") tx.CreateBucket("woojits") return nil }) - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { buckets := tx.Buckets() assert.Equal(t, len(buckets), 2) assert.Equal(t, buckets[0].Name(), "widgets") @@ -121,7 +121,7 @@ func TestTxWritableBuckets(t *testing.T) { buckets[1].Put([]byte("bar"), []byte("0001")) return nil }) - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { assert.Equal(t, []byte("0000"), tx.Bucket("widgets").Get([]byte("foo"))) assert.Equal(t, []byte("0001"), tx.Bucket("woojits").Get([]byte("bar"))) return nil @@ -133,13 +133,13 @@ func TestTxWritableBuckets(t *testing.T) { func TestTxCreateBucket(t *testing.T) { withOpenDB(func(db *DB, path string) { // Create a bucket. - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.NoError(t, tx.CreateBucket("widgets")) return nil }) // Read the bucket through a separate transaction. - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { b := tx.Bucket("widgets") assert.NotNil(t, b) return nil @@ -150,7 +150,7 @@ func TestTxCreateBucket(t *testing.T) { // Ensure that a bucket can be created if it doesn't already exist. func TestTxCreateBucketIfNotExists(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.NoError(t, tx.CreateBucketIfNotExists("widgets")) assert.NoError(t, tx.CreateBucketIfNotExists("widgets")) assert.Equal(t, tx.CreateBucketIfNotExists(""), ErrBucketNameRequired) @@ -158,7 +158,7 @@ func TestTxCreateBucketIfNotExists(t *testing.T) { }) // Read the bucket through a separate transaction. - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { b := tx.Bucket("widgets") assert.NotNil(t, b) return nil @@ -170,13 +170,13 @@ func TestTxCreateBucketIfNotExists(t *testing.T) { func TestTxRecreateBucket(t *testing.T) { withOpenDB(func(db *DB, path string) { // Create a bucket. - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.NoError(t, tx.CreateBucket("widgets")) return nil }) // Create the same bucket again. - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.Equal(t, ErrBucketExists, tx.CreateBucket("widgets")) return nil }) @@ -186,7 +186,7 @@ func TestTxRecreateBucket(t *testing.T) { // Ensure that a bucket is created with a non-blank name. func TestTxCreateBucketWithoutName(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.Equal(t, ErrBucketNameRequired, tx.CreateBucket("")) return nil }) @@ -196,7 +196,7 @@ func TestTxCreateBucketWithoutName(t *testing.T) { // Ensure that a bucket name is not too long. func TestTxCreateBucketWithLongName(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.NoError(t, tx.CreateBucket(strings.Repeat("X", 255))) assert.Equal(t, ErrBucketNameTooLarge, tx.CreateBucket(strings.Repeat("X", 256))) return nil @@ -208,7 +208,7 @@ func TestTxCreateBucketWithLongName(t *testing.T) { func TestTxDeleteBucket(t *testing.T) { withOpenDB(func(db *DB, path string) { // Create a bucket and add a value. - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") tx.Bucket("widgets").Put([]byte("foo"), []byte("bar")) return nil @@ -216,19 +216,19 @@ func TestTxDeleteBucket(t *testing.T) { // Save root page id. var root pgid - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { root = tx.Bucket("widgets").root return nil }) // Delete the bucket and make sure we can't get the value. - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.NoError(t, tx.DeleteBucket("widgets")) assert.Nil(t, tx.Bucket("widgets")) return nil }) - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { // Verify that the bucket's page is free. assert.Equal(t, []pgid{root}, db.freelist.all()) @@ -243,7 +243,7 @@ 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, _ := db.Begin(true) tx.Commit() assert.Equal(t, tx.DeleteBucket("foo"), ErrTxClosed) }) @@ -252,7 +252,7 @@ func TestTxDeleteBucketClosed(t *testing.T) { // Ensure that deleting a bucket with a read-only transaction returns an error. func TestTxDeleteBucketReadOnly(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { assert.Equal(t, tx.DeleteBucket("foo"), ErrTxNotWritable) return nil }) @@ -262,7 +262,7 @@ func TestTxDeleteBucketReadOnly(t *testing.T) { // Ensure that an error is returned when deleting from a bucket that doesn't exist. func TestTxDeleteBucketNotFound(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { assert.Equal(t, ErrBucketNotFound, tx.DeleteBucket("widgets")) return nil }) @@ -272,10 +272,10 @@ func TestTxDeleteBucketNotFound(t *testing.T) { // Ensure that a Tx cursor can iterate over an empty bucket without error. func TestTxCursorEmptyBucket(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { return tx.CreateBucket("widgets") }) - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { c := tx.Bucket("widgets").Cursor() k, v := c.First() assert.Nil(t, k) @@ -288,10 +288,10 @@ func TestTxCursorEmptyBucket(t *testing.T) { // Ensure that a Tx cursor can reverse iterate over an empty bucket without error. func TestCursorEmptyBucketReverse(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { return tx.CreateBucket("widgets") }) - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { c := tx.Bucket("widgets").Cursor() k, v := c.Last() assert.Nil(t, k) @@ -304,14 +304,14 @@ func TestCursorEmptyBucketReverse(t *testing.T) { // Ensure that a Tx cursor can iterate over a single root with a couple elements. func TestTxCursorLeafRoot(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") tx.Bucket("widgets").Put([]byte("baz"), []byte{}) tx.Bucket("widgets").Put([]byte("foo"), []byte{0}) tx.Bucket("widgets").Put([]byte("bar"), []byte{1}) return nil }) - tx, _ := db.Tx() + tx, _ := db.Begin(false) c := tx.Bucket("widgets").Cursor() k, v := c.First() @@ -341,14 +341,14 @@ func TestTxCursorLeafRoot(t *testing.T) { // Ensure that a Tx cursor can iterate in reverse over a single root with a couple elements. func TestTxCursorLeafRootReverse(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") tx.Bucket("widgets").Put([]byte("baz"), []byte{}) tx.Bucket("widgets").Put([]byte("foo"), []byte{0}) tx.Bucket("widgets").Put([]byte("bar"), []byte{1}) return nil }) - tx, _ := db.Tx() + tx, _ := db.Begin(false) c := tx.Bucket("widgets").Cursor() k, v := c.Last() @@ -378,14 +378,14 @@ func TestTxCursorLeafRootReverse(t *testing.T) { // Ensure that a Tx cursor can restart from the beginning. func TestTxCursorRestart(t *testing.T) { withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") tx.Bucket("widgets").Put([]byte("bar"), []byte{}) tx.Bucket("widgets").Put([]byte("foo"), []byte{}) return nil }) - tx, _ := db.Tx() + tx, _ := db.Begin(false) c := tx.Bucket("widgets").Cursor() k, _ := c.First() @@ -413,7 +413,7 @@ func TestTxCursorIterate(t *testing.T) { f := func(items testdata) bool { withOpenDB(func(db *DB, path string) { // Bulk insert all values. - tx, _ := db.RWTx() + tx, _ := db.Begin(true) tx.CreateBucket("widgets") b := tx.Bucket("widgets") for _, item := range items { @@ -426,7 +426,7 @@ func TestTxCursorIterate(t *testing.T) { // Iterate over all items and check consistency. var index = 0 - tx, _ = db.Tx() + tx, _ = db.Begin(false) c := tx.Bucket("widgets").Cursor() for k, v := c.First(); k != nil && index < len(items); k, v = c.Next() { assert.Equal(t, k, items[index].Key) @@ -454,7 +454,7 @@ func TestTxCursorIterateReverse(t *testing.T) { f := func(items testdata) bool { withOpenDB(func(db *DB, path string) { // Bulk insert all values. - tx, _ := db.RWTx() + tx, _ := db.Begin(true) tx.CreateBucket("widgets") b := tx.Bucket("widgets") for _, item := range items { @@ -467,7 +467,7 @@ func TestTxCursorIterateReverse(t *testing.T) { // Iterate over all items and check consistency. var index = 0 - tx, _ = db.Tx() + tx, _ = db.Begin(false) c := tx.Bucket("widgets").Cursor() for k, v := c.Last(); k != nil && index < len(items); k, v = c.Prev() { assert.Equal(t, k, items[index].Key) @@ -493,7 +493,7 @@ func BenchmarkTxCursor(b *testing.B) { withOpenDB(func(db *DB, path string) { // Write data to bucket. - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { tx.CreateBucket("widgets") bucket := tx.Bucket("widgets") for i := 0; i < b.N; i++ { @@ -504,7 +504,7 @@ func BenchmarkTxCursor(b *testing.B) { b.ResetTimer() // Iterate over bucket using cursor. - db.With(func(tx *Tx) error { + db.View(func(tx *Tx) error { count := 0 c := tx.Bucket("widgets").Cursor() for k, _ := c.First(); k != nil; k, _ = c.Next() { @@ -523,7 +523,7 @@ func BenchmarkTxPutRandom(b *testing.B) { indexes := rand.Perm(b.N) value := []byte(strings.Repeat("0", 64)) withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { return tx.CreateBucket("widgets") }) var tx *Tx @@ -533,7 +533,7 @@ func BenchmarkTxPutRandom(b *testing.B) { if tx != nil { tx.Commit() } - tx, _ = db.RWTx() + tx, _ = db.Begin(true) bucket = tx.Bucket("widgets") } bucket.Put([]byte(strconv.Itoa(indexes[i])), value) @@ -546,10 +546,10 @@ func BenchmarkTxPutRandom(b *testing.B) { func BenchmarkTxPutSequential(b *testing.B) { value := []byte(strings.Repeat("0", 64)) withOpenDB(func(db *DB, path string) { - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { return tx.CreateBucket("widgets") }) - db.Do(func(tx *Tx) error { + db.Update(func(tx *Tx) error { bucket := tx.Bucket("widgets") for i := 0; i < b.N; i++ { bucket.Put([]byte(strconv.Itoa(i)), value) |