Skip to content

Commit

Permalink
move txnkeys test to shared test pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Sep 4, 2024
1 parent def08d9 commit 5d5af9c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
54 changes: 2 additions & 52 deletions storage/kv/kvtxn/kvtxn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"github.com/micromdm/nanolib/storage/kv"
"github.com/micromdm/nanolib/storage/kv/kvmap"
"github.com/micromdm/nanolib/storage/kv/test"
)
Expand All @@ -15,55 +14,6 @@ func TestKVTxn(t *testing.T) {
test.TestBucketSimple(t, ctx, b)
test.TestKeysTraversing(t, ctx, b)
test.TestTxnSimple(t, ctx, b)
}

func slicesEqual[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func TestKVTxnKeys(t *testing.T) {
u := kvmap.New()
b := New(u)
ctx := context.Background()
bt, err := b.BeginKeysPrefixTraversingBucketTxn(ctx)
if err != nil {
t.Fatal(err)
}
err = bt.Set(ctx, "hello", []byte("world"))
if err != nil {
t.Fatal(err)
}
// make sure we have what we set in the txn
keys := kv.AllKeys(ctx, bt)
if want, have := []string{"hello"}, keys; !slicesEqual(want, have) {
t.Errorf("want: %v, have: %v", want, have)
}
// delete the key
err = bt.Delete(ctx, "hello")
if err != nil {
t.Fatal(err)
}
// check that we don't see it
keys = kv.AllKeys(ctx, bt)
if want, have := []string{}, keys; !slicesEqual(want, have) {
t.Errorf("want: %v, have: %v", want, have)
}
// set a value on the non-txn store (auto-commit)
err = u.Set(ctx, "hello", []byte("dlrow"))
if err != nil {
t.Fatal(err)
}
// again check that our txn does not see it
keys = kv.AllKeys(ctx, bt)
if want, have := []string{}, keys; !slicesEqual(want, have) {
t.Errorf("want: %v, have: %v", want, have)
}
b = New(kvmap.New()) // clear test data
t.Run("TestKVTxnKeys", func(t *testing.T) { test.TestKVTxnKeys(t, ctx, b) })
}
52 changes: 52 additions & 0 deletions storage/kv/test/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,55 @@ func TestTxnSimple(t *testing.T, ctx context.Context, b kv.BucketTxnBucket) {
t.Fatal(err)
}
}

func slicesEqual[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

func TestKVTxnKeys(t *testing.T, ctx context.Context, b kv.KeysPrefixTraversingBucketTxnBucket) {
err := b.Set(ctx, "hello", []byte("dlrow"))
if err != nil {
t.Fatal(err)
}
bt, err := b.BeginKeysPrefixTraversingBucketTxn(ctx)
if err != nil {
t.Fatal(err)
}
err = bt.Set(ctx, "hello", []byte("world"))
if err != nil {
t.Fatal(err)
}
// make sure we have what we set in the txn
keys := kv.AllKeys(ctx, bt)
if want, have := []string{"hello"}, keys; !slicesEqual(want, have) {
t.Errorf("want: %v, have: %v", want, have)
}
// delete the key
err = bt.Delete(ctx, "hello")
if err != nil {
t.Fatal(err)
}
// check that we don't see it
keys = kv.AllKeys(ctx, bt)
if want, have := []string{}, keys; !slicesEqual(want, have) {
t.Errorf("want: %v, have: %v", want, have)
}
// roll it back
err = bt.Rollback(ctx)
if err != nil {
t.Fatal(err)
}
// check that we don't see it in the parent store
keys = kv.AllKeys(ctx, b)
if want, have := []string{"hello"}, keys; !slicesEqual(want, have) {
t.Errorf("want: %v, have: %v", want, have)
}
}

0 comments on commit 5d5af9c

Please sign in to comment.