From ec490c825c749c26fc4ba6bfdd1655601246f308 Mon Sep 17 00:00:00 2001 From: Mustafa Elbehery Date: Tue, 12 Dec 2023 14:05:00 +0100 Subject: [PATCH] add test for srcBucket or dstBucket are RootBucket Signed-off-by: Mustafa Elbehery --- tx_test.go | 166 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 118 insertions(+), 48 deletions(-) diff --git a/tx_test.go b/tx_test.go index 48f0b6727..52cbb02f5 100644 --- a/tx_test.go +++ b/tx_test.go @@ -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 { @@ -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) + } } } @@ -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) } @@ -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