kvdb: make etcd tests use testify require instead of assert

This commit is contained in:
Andras Banki-Horvath 2020-06-29 14:53:38 +02:00
parent 77549f1fb8
commit 70a69ce990
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8
6 changed files with 317 additions and 317 deletions

@ -8,7 +8,7 @@ import (
"testing"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCopy(t *testing.T) {
@ -18,30 +18,30 @@ func TestCopy(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
// "apple"
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.NoError(t, err)
assert.NotNil(t, apple)
require.NoError(t, err)
require.NotNil(t, apple)
assert.NoError(t, apple.Put([]byte("key"), []byte("val")))
require.NoError(t, apple.Put([]byte("key"), []byte("val")))
return nil
})
// Expect non-zero copy.
var buf bytes.Buffer
assert.NoError(t, db.Copy(&buf))
assert.Greater(t, buf.Len(), 0)
assert.Nil(t, err)
require.NoError(t, db.Copy(&buf))
require.Greater(t, buf.Len(), 0)
require.Nil(t, err)
expected := map[string]string{
bkey("apple"): bval("apple"),
vkey("key", "apple"): "val",
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}
func TestAbortContext(t *testing.T) {
@ -57,19 +57,19 @@ func TestAbortContext(t *testing.T) {
// Pass abort context and abort right away.
db, err := newEtcdBackend(config)
assert.NoError(t, err)
require.NoError(t, err)
cancel()
// Expect that the update will fail.
err = db.Update(func(tx walletdb.ReadWriteTx) error {
_, err := tx.CreateTopLevelBucket([]byte("bucket"))
assert.NoError(t, err)
require.NoError(t, err)
return nil
})
assert.Error(t, err, "context canceled")
require.Error(t, err, "context canceled")
// No changes in the DB.
assert.Equal(t, map[string]string{}, f.Dump())
require.Equal(t, map[string]string{}, f.Dump())
}

@ -6,25 +6,25 @@ import (
"testing"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOpenCreateFailure(t *testing.T) {
t.Parallel()
db, err := walletdb.Open(dbType)
assert.Error(t, err)
assert.Nil(t, db)
require.Error(t, err)
require.Nil(t, db)
db, err = walletdb.Open(dbType, "wrong")
assert.Error(t, err)
assert.Nil(t, db)
require.Error(t, err)
require.Nil(t, db)
db, err = walletdb.Create(dbType)
assert.Error(t, err)
assert.Nil(t, db)
require.Error(t, err)
require.Nil(t, db)
db, err = walletdb.Create(dbType, "wrong")
assert.Error(t, err)
assert.Nil(t, db)
require.Error(t, err)
require.Nil(t, db)
}

@ -8,7 +8,7 @@ import (
"testing"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBucketCreation(t *testing.T) {
@ -18,70 +18,70 @@ func TestBucketCreation(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
// empty bucket name
b, err := tx.CreateTopLevelBucket(nil)
assert.Error(t, walletdb.ErrBucketNameRequired, err)
assert.Nil(t, b)
require.Error(t, walletdb.ErrBucketNameRequired, err)
require.Nil(t, b)
// empty bucket name
b, err = tx.CreateTopLevelBucket([]byte(""))
assert.Error(t, walletdb.ErrBucketNameRequired, err)
assert.Nil(t, b)
require.Error(t, walletdb.ErrBucketNameRequired, err)
require.Nil(t, b)
// "apple"
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.NoError(t, err)
assert.NotNil(t, apple)
require.NoError(t, err)
require.NotNil(t, apple)
// Check bucket tx.
assert.Equal(t, tx, apple.Tx())
require.Equal(t, tx, apple.Tx())
// "apple" already created
b, err = tx.CreateTopLevelBucket([]byte("apple"))
assert.NoError(t, err)
assert.NotNil(t, b)
require.NoError(t, err)
require.NotNil(t, b)
// "apple/banana"
banana, err := apple.CreateBucket([]byte("banana"))
assert.NoError(t, err)
assert.NotNil(t, banana)
require.NoError(t, err)
require.NotNil(t, banana)
banana, err = apple.CreateBucketIfNotExists([]byte("banana"))
assert.NoError(t, err)
assert.NotNil(t, banana)
require.NoError(t, err)
require.NotNil(t, banana)
// Try creating "apple/banana" again
b, err = apple.CreateBucket([]byte("banana"))
assert.Error(t, walletdb.ErrBucketExists, err)
assert.Nil(t, b)
require.Error(t, walletdb.ErrBucketExists, err)
require.Nil(t, b)
// "apple/mango"
mango, err := apple.CreateBucket([]byte("mango"))
assert.Nil(t, err)
assert.NotNil(t, mango)
require.Nil(t, err)
require.NotNil(t, mango)
// "apple/banana/pear"
pear, err := banana.CreateBucket([]byte("pear"))
assert.Nil(t, err)
assert.NotNil(t, pear)
require.Nil(t, err)
require.NotNil(t, pear)
// empty bucket
assert.Nil(t, apple.NestedReadWriteBucket(nil))
assert.Nil(t, apple.NestedReadWriteBucket([]byte("")))
require.Nil(t, apple.NestedReadWriteBucket(nil))
require.Nil(t, apple.NestedReadWriteBucket([]byte("")))
// "apple/pear" doesn't exist
assert.Nil(t, apple.NestedReadWriteBucket([]byte("pear")))
require.Nil(t, apple.NestedReadWriteBucket([]byte("pear")))
// "apple/banana" exits
assert.NotNil(t, apple.NestedReadWriteBucket([]byte("banana")))
assert.NotNil(t, apple.NestedReadBucket([]byte("banana")))
require.NotNil(t, apple.NestedReadWriteBucket([]byte("banana")))
require.NotNil(t, apple.NestedReadBucket([]byte("banana")))
return nil
})
assert.Nil(t, err)
require.Nil(t, err)
expected := map[string]string{
bkey("apple"): bval("apple"),
@ -89,7 +89,7 @@ func TestBucketCreation(t *testing.T) {
bkey("apple", "mango"): bval("apple", "mango"),
bkey("apple", "banana", "pear"): bval("apple", "banana", "pear"),
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}
func TestBucketDeletion(t *testing.T) {
@ -99,99 +99,99 @@ func TestBucketDeletion(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
// "apple"
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.Nil(t, err)
assert.NotNil(t, apple)
require.Nil(t, err)
require.NotNil(t, apple)
// "apple/banana"
banana, err := apple.CreateBucket([]byte("banana"))
assert.Nil(t, err)
assert.NotNil(t, banana)
require.Nil(t, err)
require.NotNil(t, banana)
kvs := []KV{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
for _, kv := range kvs {
assert.NoError(t, banana.Put([]byte(kv.key), []byte(kv.val)))
assert.Equal(t, []byte(kv.val), banana.Get([]byte(kv.key)))
require.NoError(t, banana.Put([]byte(kv.key), []byte(kv.val)))
require.Equal(t, []byte(kv.val), banana.Get([]byte(kv.key)))
}
// Delete a k/v from "apple/banana"
assert.NoError(t, banana.Delete([]byte("key2")))
require.NoError(t, banana.Delete([]byte("key2")))
// Try getting/putting/deleting invalid k/v's.
assert.Nil(t, banana.Get(nil))
assert.Error(t, walletdb.ErrKeyRequired, banana.Put(nil, []byte("val")))
assert.Error(t, walletdb.ErrKeyRequired, banana.Delete(nil))
require.Nil(t, banana.Get(nil))
require.Error(t, walletdb.ErrKeyRequired, banana.Put(nil, []byte("val")))
require.Error(t, walletdb.ErrKeyRequired, banana.Delete(nil))
// Try deleting a k/v that doesn't exist.
assert.NoError(t, banana.Delete([]byte("nokey")))
require.NoError(t, banana.Delete([]byte("nokey")))
// "apple/pear"
pear, err := apple.CreateBucket([]byte("pear"))
assert.Nil(t, err)
assert.NotNil(t, pear)
require.Nil(t, err)
require.NotNil(t, pear)
// Put some values into "apple/pear"
for _, kv := range kvs {
assert.Nil(t, pear.Put([]byte(kv.key), []byte(kv.val)))
assert.Equal(t, []byte(kv.val), pear.Get([]byte(kv.key)))
require.Nil(t, pear.Put([]byte(kv.key), []byte(kv.val)))
require.Equal(t, []byte(kv.val), pear.Get([]byte(kv.key)))
}
// Create nested bucket "apple/pear/cherry"
cherry, err := pear.CreateBucket([]byte("cherry"))
assert.Nil(t, err)
assert.NotNil(t, cherry)
require.Nil(t, err)
require.NotNil(t, cherry)
// Put some values into "apple/pear/cherry"
for _, kv := range kvs {
assert.NoError(t, cherry.Put([]byte(kv.key), []byte(kv.val)))
require.NoError(t, cherry.Put([]byte(kv.key), []byte(kv.val)))
}
// Read back values in "apple/pear/cherry" trough a read bucket.
cherryReadBucket := pear.NestedReadBucket([]byte("cherry"))
for _, kv := range kvs {
assert.Equal(
require.Equal(
t, []byte(kv.val),
cherryReadBucket.Get([]byte(kv.key)),
)
}
// Try deleting some invalid buckets.
assert.Error(t,
require.Error(t,
walletdb.ErrBucketNameRequired, apple.DeleteNestedBucket(nil),
)
// Try deleting a non existing bucket.
assert.Error(
require.Error(
t,
walletdb.ErrBucketNotFound,
apple.DeleteNestedBucket([]byte("missing")),
)
// Delete "apple/pear"
assert.Nil(t, apple.DeleteNestedBucket([]byte("pear")))
require.Nil(t, apple.DeleteNestedBucket([]byte("pear")))
// "apple/pear" deleted
assert.Nil(t, apple.NestedReadWriteBucket([]byte("pear")))
require.Nil(t, apple.NestedReadWriteBucket([]byte("pear")))
// "apple/pear/cherry" deleted
assert.Nil(t, pear.NestedReadWriteBucket([]byte("cherry")))
require.Nil(t, pear.NestedReadWriteBucket([]byte("cherry")))
// Values deleted too.
for _, kv := range kvs {
assert.Nil(t, pear.Get([]byte(kv.key)))
assert.Nil(t, cherry.Get([]byte(kv.key)))
require.Nil(t, pear.Get([]byte(kv.key)))
require.Nil(t, cherry.Get([]byte(kv.key)))
}
// "aple/banana" exists
assert.NotNil(t, apple.NestedReadWriteBucket([]byte("banana")))
require.NotNil(t, apple.NestedReadWriteBucket([]byte("banana")))
return nil
})
assert.Nil(t, err)
require.Nil(t, err)
expected := map[string]string{
bkey("apple"): bval("apple"),
@ -199,7 +199,7 @@ func TestBucketDeletion(t *testing.T) {
vkey("key1", "apple", "banana"): "val1",
vkey("key3", "apple", "banana"): "val3",
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}
func TestBucketForEach(t *testing.T) {
@ -209,28 +209,28 @@ func TestBucketForEach(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
// "apple"
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.Nil(t, err)
assert.NotNil(t, apple)
require.Nil(t, err)
require.NotNil(t, apple)
// "apple/banana"
banana, err := apple.CreateBucket([]byte("banana"))
assert.Nil(t, err)
assert.NotNil(t, banana)
require.Nil(t, err)
require.NotNil(t, banana)
kvs := []KV{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
// put some values into "apple" and "apple/banana" too
for _, kv := range kvs {
assert.Nil(t, apple.Put([]byte(kv.key), []byte(kv.val)))
assert.Equal(t, []byte(kv.val), apple.Get([]byte(kv.key)))
require.Nil(t, apple.Put([]byte(kv.key), []byte(kv.val)))
require.Equal(t, []byte(kv.val), apple.Get([]byte(kv.key)))
assert.Nil(t, banana.Put([]byte(kv.key), []byte(kv.val)))
assert.Equal(t, []byte(kv.val), banana.Get([]byte(kv.key)))
require.Nil(t, banana.Put([]byte(kv.key), []byte(kv.val)))
require.Equal(t, []byte(kv.val), banana.Get([]byte(kv.key)))
}
got := make(map[string]string)
@ -246,8 +246,8 @@ func TestBucketForEach(t *testing.T) {
"banana": "",
}
assert.NoError(t, err)
assert.Equal(t, expected, got)
require.NoError(t, err)
require.Equal(t, expected, got)
got = make(map[string]string)
err = banana.ForEach(func(key, val []byte) error {
@ -255,15 +255,15 @@ func TestBucketForEach(t *testing.T) {
return nil
})
assert.NoError(t, err)
require.NoError(t, err)
// remove the sub-bucket key
delete(expected, "banana")
assert.Equal(t, expected, got)
require.Equal(t, expected, got)
return nil
})
assert.Nil(t, err)
require.Nil(t, err)
expected := map[string]string{
bkey("apple"): bval("apple"),
@ -275,7 +275,7 @@ func TestBucketForEach(t *testing.T) {
vkey("key2", "apple", "banana"): "val2",
vkey("key3", "apple", "banana"): "val3",
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}
func TestBucketForEachWithError(t *testing.T) {
@ -285,30 +285,30 @@ func TestBucketForEachWithError(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
// "apple"
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.Nil(t, err)
assert.NotNil(t, apple)
require.Nil(t, err)
require.NotNil(t, apple)
// "apple/banana"
banana, err := apple.CreateBucket([]byte("banana"))
assert.Nil(t, err)
assert.NotNil(t, banana)
require.Nil(t, err)
require.NotNil(t, banana)
// "apple/pear"
pear, err := apple.CreateBucket([]byte("pear"))
assert.Nil(t, err)
assert.NotNil(t, pear)
require.Nil(t, err)
require.NotNil(t, pear)
kvs := []KV{{"key1", "val1"}, {"key2", "val2"}}
// Put some values into "apple" and "apple/banana" too.
for _, kv := range kvs {
assert.Nil(t, apple.Put([]byte(kv.key), []byte(kv.val)))
assert.Equal(t, []byte(kv.val), apple.Get([]byte(kv.key)))
require.Nil(t, apple.Put([]byte(kv.key), []byte(kv.val)))
require.Equal(t, []byte(kv.val), apple.Get([]byte(kv.key)))
}
got := make(map[string]string)
@ -328,8 +328,8 @@ func TestBucketForEachWithError(t *testing.T) {
"key1": "val1",
}
assert.Equal(t, expected, got)
assert.Error(t, err)
require.Equal(t, expected, got)
require.Error(t, err)
got = make(map[string]string)
i = 0
@ -350,12 +350,12 @@ func TestBucketForEachWithError(t *testing.T) {
"banana": "",
}
assert.Equal(t, expected, got)
assert.Error(t, err)
require.Equal(t, expected, got)
require.Error(t, err)
return nil
})
assert.Nil(t, err)
require.Nil(t, err)
expected := map[string]string{
bkey("apple"): bval("apple"),
@ -364,7 +364,7 @@ func TestBucketForEachWithError(t *testing.T) {
vkey("key1", "apple"): "val1",
vkey("key2", "apple"): "val2",
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}
func TestBucketSequence(t *testing.T) {
@ -374,31 +374,31 @@ func TestBucketSequence(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.Nil(t, err)
assert.NotNil(t, apple)
require.Nil(t, err)
require.NotNil(t, apple)
banana, err := apple.CreateBucket([]byte("banana"))
assert.Nil(t, err)
assert.NotNil(t, banana)
require.Nil(t, err)
require.NotNil(t, banana)
assert.Equal(t, uint64(0), apple.Sequence())
assert.Equal(t, uint64(0), banana.Sequence())
require.Equal(t, uint64(0), apple.Sequence())
require.Equal(t, uint64(0), banana.Sequence())
assert.Nil(t, apple.SetSequence(math.MaxUint64))
assert.Equal(t, uint64(math.MaxUint64), apple.Sequence())
require.Nil(t, apple.SetSequence(math.MaxUint64))
require.Equal(t, uint64(math.MaxUint64), apple.Sequence())
for i := uint64(0); i < uint64(5); i++ {
s, err := apple.NextSequence()
assert.Nil(t, err)
assert.Equal(t, i, s)
require.Nil(t, err)
require.Equal(t, i, s)
}
return nil
})
assert.Nil(t, err)
require.Nil(t, err)
}

@ -6,7 +6,7 @@ import (
"testing"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReadCursorEmptyInterval(t *testing.T) {
@ -16,41 +16,41 @@ func TestReadCursorEmptyInterval(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
err = db.Update(func(tx walletdb.ReadWriteTx) error {
b, err := tx.CreateTopLevelBucket([]byte("alma"))
assert.NoError(t, err)
assert.NotNil(t, b)
require.NoError(t, err)
require.NotNil(t, b)
return nil
})
assert.NoError(t, err)
require.NoError(t, err)
err = db.View(func(tx walletdb.ReadTx) error {
b := tx.ReadBucket([]byte("alma"))
assert.NotNil(t, b)
require.NotNil(t, b)
cursor := b.ReadCursor()
k, v := cursor.First()
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
k, v = cursor.Next()
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
k, v = cursor.Last()
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
k, v = cursor.Prev()
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
return nil
})
assert.NoError(t, err)
require.NoError(t, err)
}
func TestReadCursorNonEmptyInterval(t *testing.T) {
@ -60,7 +60,7 @@ func TestReadCursorNonEmptyInterval(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
testKeyValues := []KV{
{"b", "1"},
@ -71,20 +71,20 @@ func TestReadCursorNonEmptyInterval(t *testing.T) {
err = db.Update(func(tx walletdb.ReadWriteTx) error {
b, err := tx.CreateTopLevelBucket([]byte("alma"))
assert.NoError(t, err)
assert.NotNil(t, b)
require.NoError(t, err)
require.NotNil(t, b)
for _, kv := range testKeyValues {
assert.NoError(t, b.Put([]byte(kv.key), []byte(kv.val)))
require.NoError(t, b.Put([]byte(kv.key), []byte(kv.val)))
}
return nil
})
assert.NoError(t, err)
require.NoError(t, err)
err = db.View(func(tx walletdb.ReadTx) error {
b := tx.ReadBucket([]byte("alma"))
assert.NotNil(t, b)
require.NotNil(t, b)
// Iterate from the front.
var kvs []KV
@ -95,7 +95,7 @@ func TestReadCursorNonEmptyInterval(t *testing.T) {
kvs = append(kvs, KV{string(k), string(v)})
k, v = cursor.Next()
}
assert.Equal(t, testKeyValues, kvs)
require.Equal(t, testKeyValues, kvs)
// Iterate from the back.
kvs = []KV{}
@ -105,29 +105,29 @@ func TestReadCursorNonEmptyInterval(t *testing.T) {
kvs = append(kvs, KV{string(k), string(v)})
k, v = cursor.Prev()
}
assert.Equal(t, reverseKVs(testKeyValues), kvs)
require.Equal(t, reverseKVs(testKeyValues), kvs)
// Random access
perm := []int{3, 0, 2, 1}
for _, i := range perm {
k, v := cursor.Seek([]byte(testKeyValues[i].key))
assert.Equal(t, []byte(testKeyValues[i].key), k)
assert.Equal(t, []byte(testKeyValues[i].val), v)
require.Equal(t, []byte(testKeyValues[i].key), k)
require.Equal(t, []byte(testKeyValues[i].val), v)
}
// Seek to nonexisting key.
k, v = cursor.Seek(nil)
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
k, v = cursor.Seek([]byte("x"))
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
return nil
})
assert.NoError(t, err)
require.NoError(t, err)
}
func TestReadWriteCursor(t *testing.T) {
@ -137,7 +137,7 @@ func TestReadWriteCursor(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
testKeyValues := []KV{
{"b", "1"},
@ -149,24 +149,24 @@ func TestReadWriteCursor(t *testing.T) {
count := len(testKeyValues)
// Pre-store the first half of the interval.
assert.NoError(t, db.Update(func(tx walletdb.ReadWriteTx) error {
require.NoError(t, db.Update(func(tx walletdb.ReadWriteTx) error {
b, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.NoError(t, err)
assert.NotNil(t, b)
require.NoError(t, err)
require.NotNil(t, b)
for i := 0; i < count/2; i++ {
err = b.Put(
[]byte(testKeyValues[i].key),
[]byte(testKeyValues[i].val),
)
assert.NoError(t, err)
require.NoError(t, err)
}
return nil
}))
err = db.Update(func(tx walletdb.ReadWriteTx) error {
b := tx.ReadWriteBucket([]byte("apple"))
assert.NotNil(t, b)
require.NotNil(t, b)
// Store the second half of the interval.
for i := count / 2; i < count; i++ {
@ -174,77 +174,77 @@ func TestReadWriteCursor(t *testing.T) {
[]byte(testKeyValues[i].key),
[]byte(testKeyValues[i].val),
)
assert.NoError(t, err)
require.NoError(t, err)
}
cursor := b.ReadWriteCursor()
// First on valid interval.
fk, fv := cursor.First()
assert.Equal(t, []byte("b"), fk)
assert.Equal(t, []byte("1"), fv)
require.Equal(t, []byte("b"), fk)
require.Equal(t, []byte("1"), fv)
// Prev(First()) = nil
k, v := cursor.Prev()
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
// Last on valid interval.
lk, lv := cursor.Last()
assert.Equal(t, []byte("e"), lk)
assert.Equal(t, []byte("4"), lv)
require.Equal(t, []byte("e"), lk)
require.Equal(t, []byte("4"), lv)
// Next(Last()) = nil
k, v = cursor.Next()
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
// Delete first item, then add an item before the
// deleted one. Check that First/Next will "jump"
// over the deleted item and return the new first.
_, _ = cursor.First()
assert.NoError(t, cursor.Delete())
assert.NoError(t, b.Put([]byte("a"), []byte("0")))
require.NoError(t, cursor.Delete())
require.NoError(t, b.Put([]byte("a"), []byte("0")))
fk, fv = cursor.First()
assert.Equal(t, []byte("a"), fk)
assert.Equal(t, []byte("0"), fv)
require.Equal(t, []byte("a"), fk)
require.Equal(t, []byte("0"), fv)
k, v = cursor.Next()
assert.Equal(t, []byte("c"), k)
assert.Equal(t, []byte("2"), v)
require.Equal(t, []byte("c"), k)
require.Equal(t, []byte("2"), v)
// Similarly test that a new end is returned if
// the old end is deleted first.
_, _ = cursor.Last()
assert.NoError(t, cursor.Delete())
assert.NoError(t, b.Put([]byte("f"), []byte("5")))
require.NoError(t, cursor.Delete())
require.NoError(t, b.Put([]byte("f"), []byte("5")))
lk, lv = cursor.Last()
assert.Equal(t, []byte("f"), lk)
assert.Equal(t, []byte("5"), lv)
require.Equal(t, []byte("f"), lk)
require.Equal(t, []byte("5"), lv)
k, v = cursor.Prev()
assert.Equal(t, []byte("da"), k)
assert.Equal(t, []byte("3"), v)
require.Equal(t, []byte("da"), k)
require.Equal(t, []byte("3"), v)
// Overwrite k/v in the middle of the interval.
assert.NoError(t, b.Put([]byte("c"), []byte("3")))
require.NoError(t, b.Put([]byte("c"), []byte("3")))
k, v = cursor.Prev()
assert.Equal(t, []byte("c"), k)
assert.Equal(t, []byte("3"), v)
require.Equal(t, []byte("c"), k)
require.Equal(t, []byte("3"), v)
// Insert new key/values.
assert.NoError(t, b.Put([]byte("cx"), []byte("x")))
assert.NoError(t, b.Put([]byte("cy"), []byte("y")))
require.NoError(t, b.Put([]byte("cx"), []byte("x")))
require.NoError(t, b.Put([]byte("cy"), []byte("y")))
k, v = cursor.Next()
assert.Equal(t, []byte("cx"), k)
assert.Equal(t, []byte("x"), v)
require.Equal(t, []byte("cx"), k)
require.Equal(t, []byte("x"), v)
k, v = cursor.Next()
assert.Equal(t, []byte("cy"), k)
assert.Equal(t, []byte("y"), v)
require.Equal(t, []byte("cy"), k)
require.Equal(t, []byte("y"), v)
expected := []KV{
{"a", "0"},
@ -263,7 +263,7 @@ func TestReadWriteCursor(t *testing.T) {
kvs = append(kvs, KV{string(k), string(v)})
k, v = cursor.Next()
}
assert.Equal(t, expected, kvs)
require.Equal(t, expected, kvs)
// Iterate from the back.
kvs = []KV{}
@ -273,12 +273,12 @@ func TestReadWriteCursor(t *testing.T) {
kvs = append(kvs, KV{string(k), string(v)})
k, v = cursor.Prev()
}
assert.Equal(t, reverseKVs(expected), kvs)
require.Equal(t, reverseKVs(expected), kvs)
return nil
})
assert.NoError(t, err)
require.NoError(t, err)
expected := map[string]string{
bkey("apple"): bval("apple"),
@ -289,5 +289,5 @@ func TestReadWriteCursor(t *testing.T) {
vkey("da", "apple"): "3",
vkey("f", "apple"): "5",
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}

@ -6,7 +6,7 @@ import (
"testing"
"github.com/btcsuite/btcwallet/walletdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTxManualCommit(t *testing.T) {
@ -16,11 +16,11 @@ func TestTxManualCommit(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
tx, err := db.BeginReadWriteTx()
assert.NoError(t, err)
assert.NotNil(t, tx)
require.NoError(t, err)
require.NotNil(t, tx)
committed := false
@ -29,24 +29,24 @@ func TestTxManualCommit(t *testing.T) {
})
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.NoError(t, err)
assert.NotNil(t, apple)
assert.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
require.NoError(t, err)
require.NotNil(t, apple)
require.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
banana, err := tx.CreateTopLevelBucket([]byte("banana"))
assert.NoError(t, err)
assert.NotNil(t, banana)
assert.NoError(t, banana.Put([]byte("testKey"), []byte("testVal")))
assert.NoError(t, tx.DeleteTopLevelBucket([]byte("banana")))
require.NoError(t, err)
require.NotNil(t, banana)
require.NoError(t, banana.Put([]byte("testKey"), []byte("testVal")))
require.NoError(t, tx.DeleteTopLevelBucket([]byte("banana")))
assert.NoError(t, tx.Commit())
assert.True(t, committed)
require.NoError(t, tx.Commit())
require.True(t, committed)
expected := map[string]string{
bkey("apple"): bval("apple"),
vkey("testKey", "apple"): "testVal",
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}
func TestTxRollback(t *testing.T) {
@ -56,21 +56,21 @@ func TestTxRollback(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
tx, err := db.BeginReadWriteTx()
assert.Nil(t, err)
assert.NotNil(t, tx)
require.Nil(t, err)
require.NotNil(t, tx)
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.Nil(t, err)
assert.NotNil(t, apple)
require.Nil(t, err)
require.NotNil(t, apple)
assert.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
require.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
assert.NoError(t, tx.Rollback())
assert.Error(t, walletdb.ErrTxClosed, tx.Commit())
assert.Equal(t, map[string]string{}, f.Dump())
require.NoError(t, tx.Rollback())
require.Error(t, walletdb.ErrTxClosed, tx.Commit())
require.Equal(t, map[string]string{}, f.Dump())
}
func TestChangeDuringManualTx(t *testing.T) {
@ -80,24 +80,24 @@ func TestChangeDuringManualTx(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
tx, err := db.BeginReadWriteTx()
assert.Nil(t, err)
assert.NotNil(t, tx)
require.Nil(t, err)
require.NotNil(t, tx)
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.Nil(t, err)
assert.NotNil(t, apple)
require.Nil(t, err)
require.NotNil(t, apple)
assert.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
require.NoError(t, apple.Put([]byte("testKey"), []byte("testVal")))
// Try overwriting the bucket key.
f.Put(bkey("apple"), "banana")
// TODO: translate error
assert.NotNil(t, tx.Commit())
assert.Equal(t, map[string]string{
require.NotNil(t, tx.Commit())
require.Equal(t, map[string]string{
bkey("apple"): "banana",
}, f.Dump())
}
@ -109,16 +109,16 @@ func TestChangeDuringUpdate(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
count := 0
err = db.Update(func(tx walletdb.ReadWriteTx) error {
apple, err := tx.CreateTopLevelBucket([]byte("apple"))
assert.NoError(t, err)
assert.NotNil(t, apple)
require.NoError(t, err)
require.NotNil(t, apple)
assert.NoError(t, apple.Put([]byte("key"), []byte("value")))
require.NoError(t, apple.Put([]byte("key"), []byte("value")))
if count == 0 {
f.Put(vkey("key", "apple"), "new_value")
@ -127,30 +127,30 @@ func TestChangeDuringUpdate(t *testing.T) {
cursor := apple.ReadCursor()
k, v := cursor.First()
assert.Equal(t, []byte("key"), k)
assert.Equal(t, []byte("value"), v)
assert.Equal(t, v, apple.Get([]byte("key")))
require.Equal(t, []byte("key"), k)
require.Equal(t, []byte("value"), v)
require.Equal(t, v, apple.Get([]byte("key")))
k, v = cursor.Next()
if count == 0 {
assert.Nil(t, k)
assert.Nil(t, v)
require.Nil(t, k)
require.Nil(t, v)
} else {
assert.Equal(t, []byte("key2"), k)
assert.Equal(t, []byte("value2"), v)
require.Equal(t, []byte("key2"), k)
require.Equal(t, []byte("value2"), v)
}
count++
return nil
})
assert.Nil(t, err)
assert.Equal(t, count, 2)
require.Nil(t, err)
require.Equal(t, count, 2)
expected := map[string]string{
bkey("apple"): bval("apple"),
vkey("key", "apple"): "value",
vkey("key2", "apple"): "value2",
}
assert.Equal(t, expected, f.Dump())
require.Equal(t, expected, f.Dump())
}

@ -6,7 +6,7 @@ import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func reverseKVs(a []KV) []KV {
@ -24,7 +24,7 @@ func TestPutToEmpty(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
apply := func(stm STM) error {
stm.Put("123", "abc")
@ -32,9 +32,9 @@ func TestPutToEmpty(t *testing.T) {
}
err = RunSTM(db.cli, apply)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "abc", f.Get("123"))
require.Equal(t, "abc", f.Get("123"))
}
func TestGetPutDel(t *testing.T) {
@ -56,64 +56,64 @@ func TestGetPutDel(t *testing.T) {
}
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
apply := func(stm STM) error {
// Get some non existing keys.
v, err := stm.Get("")
assert.NoError(t, err)
assert.Nil(t, v)
require.NoError(t, err)
require.Nil(t, v)
v, err = stm.Get("x")
assert.NoError(t, err)
assert.Nil(t, v)
require.NoError(t, err)
require.Nil(t, v)
// Get all existing keys.
for _, kv := range testKeyValues {
v, err = stm.Get(kv.key)
assert.NoError(t, err)
assert.Equal(t, []byte(kv.val), v)
require.NoError(t, err)
require.Equal(t, []byte(kv.val), v)
}
// Overwrite, then delete an existing key.
stm.Put("c", "6")
v, err = stm.Get("c")
assert.NoError(t, err)
assert.Equal(t, []byte("6"), v)
require.NoError(t, err)
require.Equal(t, []byte("6"), v)
stm.Del("c")
v, err = stm.Get("c")
assert.NoError(t, err)
assert.Nil(t, v)
require.NoError(t, err)
require.Nil(t, v)
// Re-add the deleted key.
stm.Put("c", "7")
v, err = stm.Get("c")
assert.NoError(t, err)
assert.Equal(t, []byte("7"), v)
require.NoError(t, err)
require.Equal(t, []byte("7"), v)
// Add a new key.
stm.Put("x", "x")
v, err = stm.Get("x")
assert.NoError(t, err)
assert.Equal(t, []byte("x"), v)
require.NoError(t, err)
require.Equal(t, []byte("x"), v)
return nil
}
err = RunSTM(db.cli, apply)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "1", f.Get("a"))
assert.Equal(t, "2", f.Get("b"))
assert.Equal(t, "7", f.Get("c"))
assert.Equal(t, "4", f.Get("d"))
assert.Equal(t, "5", f.Get("e"))
assert.Equal(t, "x", f.Get("x"))
require.Equal(t, "1", f.Get("a"))
require.Equal(t, "2", f.Get("b"))
require.Equal(t, "7", f.Get("c"))
require.Equal(t, "4", f.Get("d"))
require.Equal(t, "5", f.Get("e"))
require.Equal(t, "x", f.Get("x"))
}
func TestFirstLastNextPrev(t *testing.T) {
@ -134,44 +134,44 @@ func TestFirstLastNextPrev(t *testing.T) {
}
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
apply := func(stm STM) error {
// First/Last on valid multi item interval.
kv, err := stm.First("k")
assert.NoError(t, err)
assert.Equal(t, &KV{"kb", "1"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"kb", "1"}, kv)
kv, err = stm.Last("k")
assert.NoError(t, err)
assert.Equal(t, &KV{"ke", "4"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"ke", "4"}, kv)
// First/Last on single item interval.
kv, err = stm.First("w")
assert.NoError(t, err)
assert.Equal(t, &KV{"w", "w"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"w", "w"}, kv)
kv, err = stm.Last("w")
assert.NoError(t, err)
assert.Equal(t, &KV{"w", "w"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"w", "w"}, kv)
// Next/Prev on start/end.
kv, err = stm.Next("k", "ke")
assert.NoError(t, err)
assert.Nil(t, kv)
require.NoError(t, err)
require.Nil(t, kv)
kv, err = stm.Prev("k", "kb")
assert.NoError(t, err)
assert.Nil(t, kv)
require.NoError(t, err)
require.Nil(t, kv)
// Next/Prev in the middle.
kv, err = stm.Next("k", "kc")
assert.NoError(t, err)
assert.Equal(t, &KV{"kda", "3"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"kda", "3"}, kv)
kv, err = stm.Prev("k", "ke")
assert.NoError(t, err)
assert.Equal(t, &KV{"kda", "3"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"kda", "3"}, kv)
// Delete first item, then add an item before the
// deleted one. Check that First/Next will "jump"
@ -180,12 +180,12 @@ func TestFirstLastNextPrev(t *testing.T) {
stm.Put("ka", "0")
kv, err = stm.First("k")
assert.NoError(t, err)
assert.Equal(t, &KV{"ka", "0"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"ka", "0"}, kv)
kv, err = stm.Prev("k", "kc")
assert.NoError(t, err)
assert.Equal(t, &KV{"ka", "0"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"ka", "0"}, kv)
// Similarly test that a new end is returned if
// the old end is deleted first.
@ -193,19 +193,19 @@ func TestFirstLastNextPrev(t *testing.T) {
stm.Put("kf", "5")
kv, err = stm.Last("k")
assert.NoError(t, err)
assert.Equal(t, &KV{"kf", "5"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"kf", "5"}, kv)
kv, err = stm.Next("k", "kda")
assert.NoError(t, err)
assert.Equal(t, &KV{"kf", "5"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"kf", "5"}, kv)
// Overwrite one in the middle.
stm.Put("kda", "6")
kv, err = stm.Next("k", "kc")
assert.NoError(t, err)
assert.Equal(t, &KV{"kda", "6"}, kv)
require.NoError(t, err)
require.Equal(t, &KV{"kda", "6"}, kv)
// Add three in the middle, then delete one.
stm.Put("kdb", "7")
@ -218,12 +218,12 @@ func TestFirstLastNextPrev(t *testing.T) {
var kvs []KV
curr, err := stm.First("k")
assert.NoError(t, err)
require.NoError(t, err)
for curr != nil {
kvs = append(kvs, *curr)
curr, err = stm.Next("k", curr.key)
assert.NoError(t, err)
require.NoError(t, err)
}
expected := []KV{
@ -234,37 +234,37 @@ func TestFirstLastNextPrev(t *testing.T) {
{"kdd", "9"},
{"kf", "5"},
}
assert.Equal(t, expected, kvs)
require.Equal(t, expected, kvs)
// Similarly check that stepping from last to first
// returns the expected sequence.
kvs = []KV{}
curr, err = stm.Last("k")
assert.NoError(t, err)
require.NoError(t, err)
for curr != nil {
kvs = append(kvs, *curr)
curr, err = stm.Prev("k", curr.key)
assert.NoError(t, err)
require.NoError(t, err)
}
expected = reverseKVs(expected)
assert.Equal(t, expected, kvs)
require.Equal(t, expected, kvs)
return nil
}
err = RunSTM(db.cli, apply)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "0", f.Get("ka"))
assert.Equal(t, "2", f.Get("kc"))
assert.Equal(t, "6", f.Get("kda"))
assert.Equal(t, "7", f.Get("kdb"))
assert.Equal(t, "9", f.Get("kdd"))
assert.Equal(t, "5", f.Get("kf"))
assert.Equal(t, "w", f.Get("w"))
require.Equal(t, "0", f.Get("ka"))
require.Equal(t, "2", f.Get("kc"))
require.Equal(t, "6", f.Get("kda"))
require.Equal(t, "7", f.Get("kdb"))
require.Equal(t, "9", f.Get("kdd"))
require.Equal(t, "5", f.Get("kf"))
require.Equal(t, "w", f.Get("w"))
}
func TestCommitError(t *testing.T) {
@ -274,7 +274,7 @@ func TestCommitError(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
// Preset DB state.
f.Put("123", "xyz")
@ -285,10 +285,10 @@ func TestCommitError(t *testing.T) {
apply := func(stm STM) error {
// STM must have the key/value.
val, err := stm.Get("123")
assert.NoError(t, err)
require.NoError(t, err)
if cnt == 0 {
assert.Equal(t, []byte("xyz"), val)
require.Equal(t, []byte("xyz"), val)
// Put a conflicting key/value during the first apply.
f.Put("123", "def")
@ -302,10 +302,10 @@ func TestCommitError(t *testing.T) {
}
err = RunSTM(db.cli, apply)
assert.NoError(t, err)
assert.Equal(t, 2, cnt)
require.NoError(t, err)
require.Equal(t, 2, cnt)
assert.Equal(t, "abc", f.Get("123"))
require.Equal(t, "abc", f.Get("123"))
}
func TestManualTxError(t *testing.T) {
@ -315,7 +315,7 @@ func TestManualTxError(t *testing.T) {
defer f.Cleanup()
db, err := newEtcdBackend(f.BackendConfig())
assert.NoError(t, err)
require.NoError(t, err)
// Preset DB state.
f.Put("123", "xyz")
@ -323,22 +323,22 @@ func TestManualTxError(t *testing.T) {
stm := NewSTM(db.cli)
val, err := stm.Get("123")
assert.NoError(t, err)
assert.Equal(t, []byte("xyz"), val)
require.NoError(t, err)
require.Equal(t, []byte("xyz"), val)
// Put a conflicting key/value.
f.Put("123", "def")
// Should still get the original version.
val, err = stm.Get("123")
assert.NoError(t, err)
assert.Equal(t, []byte("xyz"), val)
require.NoError(t, err)
require.Equal(t, []byte("xyz"), val)
// Commit will fail with CommitError.
err = stm.Commit()
var e CommitError
assert.True(t, errors.As(err, &e))
require.True(t, errors.As(err, &e))
// We expect that the transacton indeed did not commit.
assert.Equal(t, "def", f.Get("123"))
require.Equal(t, "def", f.Get("123"))
}