Skip to content

Commit

Permalink
add test for srcBucket or dstBucket are RootBucket
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Elbehery <[email protected]>
  • Loading branch information
Elbehery committed Dec 13, 2023
1 parent b268840 commit ec490c8
Showing 1 changed file with 118 additions and 48 deletions.
166 changes: 118 additions & 48 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,34 @@ func TestTx_MoveBucket(t *testing.T) {
false,
nil,
},
{
"dstBucket is RootBucket",
"srcBucket",
"subBucket",
"this is subBucket key",
"this is subBucket value",
"dstBucket",
true,
false,
false,
false,
true,
nil,
},
{
"srcBucket is RootBucket, and dstBucket is RootBucket, corner case",
"srcBucket",
"subBucket",
"this is subBucket key",
"this is subBucket value",
"dstBucket",
true,
true,
false,
true,
true,
nil,
},
}

for _, tc := range testCases {
Expand All @@ -526,49 +554,66 @@ func TestTx_MoveBucket(t *testing.T) {

// arrange
if err := db.Update(func(tx *bolt.Tx) error {
// create src bucket
srcBucket, bErr := tx.CreateBucketIfNotExists([]byte(tc.srcBucketName))
if bErr != nil {
t.Fatalf("error creating src bucket %s: %v", tc.srcBucketName, bErr)
}
// insert K/V pair into src bucket
if pErr := srcBucket.Put([]byte("foo"), []byte("0000")); pErr != nil {
t.Fatal(pErr)
var srcBucket *bolt.Bucket = nil
var bErr error = nil
var subBucket *bolt.Bucket = nil
var sErr error = nil

if !tc.srcBucketIsRootBucket {
// create src bucket
srcBucket, bErr = tx.CreateBucketIfNotExists([]byte(tc.srcBucketName))
if bErr != nil {
t.Fatalf("error creating src bucket %s: %v", tc.srcBucketName, bErr)
}
// insert K/V pair into src bucket
if pErr := srcBucket.Put([]byte("foo"), []byte("0000")); pErr != nil {
t.Fatal(pErr)
}
}

if tc.subBucketExistSrcBucket {
// create subBucket within srcBucket
subBucket, sErr := srcBucket.CreateBucket([]byte(tc.subBucketName))
if sErr != nil {
t.Fatalf("error creating subBucket %s within root bucket: %v", tc.subBucketName, sErr)
if tc.srcBucketIsRootBucket {
// create subBucket within root bucket
subBucket, sErr = tx.CreateBucket([]byte(tc.subBucketName))
if sErr != nil {
t.Fatalf("error creating subBucket %s within root bucket: %v", tc.subBucketName, sErr)
}
} else {
// create subBucket within srcBucket
subBucket, sErr = srcBucket.CreateBucket([]byte(tc.subBucketName))
if sErr != nil {
t.Fatalf("error creating subBucket %s within srcBucket %s: %v", tc.subBucketName, tc.srcBucketName, sErr)
}
}
// insert K/V pair into subBucket
// insert K/V pair into subBucket
if pErr := subBucket.Put([]byte(tc.subBucketKey), []byte(tc.subBucketValue)); pErr != nil {
t.Fatal(pErr)
}
}

// create dst bucket
dstBucket, bErr := tx.CreateBucketIfNotExists([]byte(tc.dstBucketName))
if bErr != nil {
t.Fatalf("error creating dst bucket %s: %v", tc.dstBucketName, bErr)
}
// insert K/V pair into dst bucket
if pErr := dstBucket.Put([]byte("bar"), []byte("0000")); pErr != nil {
t.Fatal(pErr)
}
if !tc.dstBucketIsRootBucket {
dstBucket, bErr := tx.CreateBucketIfNotExists([]byte(tc.dstBucketName))
if bErr != nil {
t.Fatalf("error creating dst bucket %s: %v", tc.dstBucketName, bErr)
}
// insert K/V pair into dst bucket
if pErr := dstBucket.Put([]byte("bar"), []byte("0000")); pErr != nil {
t.Fatal(pErr)
}

if tc.subBucketExistDstBucket {
// create subBucket within dstBucket
_, sErr := dstBucket.CreateBucket([]byte(tc.subBucketName))
if sErr != nil {
t.Fatal(sErr)
if tc.subBucketExistDstBucket {
// create subBucket within dstBucket
_, sErr := dstBucket.CreateBucket([]byte(tc.subBucketName))
if sErr != nil {
t.Fatal(sErr)
}
}
}

if tc.keyNoSubBucketDstBucket {
if pErr := dstBucket.Put([]byte(tc.subBucketName), []byte(tc.subBucketValue)); pErr != nil {
t.Fatal(pErr)
if tc.keyNoSubBucketDstBucket {
if pErr := dstBucket.Put([]byte(tc.subBucketName), []byte(tc.subBucketValue)); pErr != nil {
t.Fatal(pErr)
}
}
}

Expand All @@ -581,12 +626,12 @@ func TestTx_MoveBucket(t *testing.T) {
// act: move the subBucket from srcBucket to dstBucket
if err := db.Update(func(tx *bolt.Tx) error {
srcBucket := tx.Bucket([]byte(tc.srcBucketName))
if srcBucket == nil {
if srcBucket == nil && !tc.srcBucketIsRootBucket {
t.Fatalf("src bucket %s does not exist: %v", tc.srcBucketName, berrors.ErrBucketNotFound)
}

dstBucket := tx.Bucket([]byte(tc.dstBucketName))
if dstBucket == nil {
if dstBucket == nil && !tc.dstBucketIsRootBucket {
t.Fatalf("dst bucket %s does not exist: %v", tc.dstBucketName, berrors.ErrBucketNotFound)
}

Expand All @@ -611,24 +656,49 @@ func TestTx_MoveBucket(t *testing.T) {

// assert: check subBucket has been deleted from srcBucket, and exists in dstBucket
if err := db.View(func(tx *bolt.Tx) error {
rootCur := tx.Cursor()
k, _ := rootCur.Seek([]byte(tc.subBucketName))
if bytes.Equal([]byte(tc.subBucketName), k) {
t.Fatalf("key %q still exists in the root bucket: %v", tc.subBucketName, berrors.ErrIncompatibleValue)
}
if !tc.srcBucketIsRootBucket {
srcBucket := tx.Bucket([]byte(tc.srcBucketName))
if srcBucket == nil {
t.Fatalf("src bucket %s does not exist: %v", tc.srcBucketName, berrors.ErrBucketNotFound)
}

dstBucket := tx.Bucket([]byte(tc.dstBucketName))
if dstBucket == nil {
t.Fatalf("dst bucket %s does not exist: %v", tc.dstBucketName, berrors.ErrBucketNotFound)
}
v := dstBucket.Get([]byte(tc.subBucketName))
if v != nil {
t.Fatalf("expected nil value, as the key is nested bucket, got %v instead", v)
srcCur := srcBucket.Cursor()
k, _ := srcCur.Seek([]byte(tc.subBucketName))
if bytes.Equal([]byte(tc.subBucketName), k) {
t.Fatalf("key %q still exists in the srcBucket %s : %v", tc.subBucketName, tc.srcBucketName, berrors.ErrIncompatibleValue)
}
} else {
subBucket := tx.Bucket([]byte(tc.subBucketName))
if subBucket != nil {
t.Fatalf("expected subBucket '%s' to be moved, but still exist in rootBucket", tc.subBucketName)
}
}
subBucket := dstBucket.Bucket([]byte(tc.subBucketName))
v = subBucket.Get([]byte(tc.subBucketKey))
if v == nil {
t.Fatalf("expected value %v, but got %v instead", tc.subBucketValue, v)

if !tc.dstBucketIsRootBucket {
dstBucket := tx.Bucket([]byte(tc.dstBucketName))
if dstBucket == nil {
t.Fatalf("dst bucket %s does not exist: %v", tc.dstBucketName, berrors.ErrBucketNotFound)
}

v := dstBucket.Get([]byte(tc.subBucketName))
if v != nil {
t.Fatalf("expected nil value, as the key is nested bucket, got %v instead", v)
}
subBucket := dstBucket.Bucket([]byte(tc.subBucketName))
v = subBucket.Get([]byte(tc.subBucketKey))
if v == nil {
t.Fatalf("expected value %v, but got %v instead", tc.subBucketValue, v)
}
} else {
subBucket := tx.Bucket([]byte(tc.subBucketName))
if subBucket == nil {
t.Fatalf("dst bucket %s does not exist in rootBucket: %v", tc.subBucketName, berrors.ErrBucketNotFound)
}

v := subBucket.Get([]byte(tc.subBucketKey))
if v == nil {
t.Fatalf("expected value %v, but got %v instead", tc.subBucketValue, v)
}
}

return nil
Expand Down

0 comments on commit ec490c8

Please sign in to comment.