Skip to content

Commit 1104182

Browse files
committed
add test helpers
Signed-off-by: Mustafa Elbehery <melbeher@redhat.com>
1 parent 4ba996b commit 1104182

File tree

1 file changed

+75
-28
lines changed

1 file changed

+75
-28
lines changed

bucket_test.go

+75-28
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,81 @@ func TestBucket_Delete_NonExisting(t *testing.T) {
464464
}
465465
}
466466

467+
func TestBucket_MoveBucket2(t *testing.T) {
468+
testCases := []struct {
469+
name string
470+
srcBucketPath []string
471+
dstBucketPath []string
472+
isInlined bool
473+
expErr error
474+
}{
475+
{
476+
name: "happy path",
477+
srcBucketPath: []string{"x", "y", "z"},
478+
dstBucketPath: []string{"x", "y"},
479+
isInlined: false,
480+
expErr: nil,
481+
},
482+
}
483+
484+
for _, tc := range testCases {
485+
t.Run(tc.name, func(*testing.T) {
486+
db := btesting.MustCreateDB(t)
487+
if err := db.Update(func(tx *bolt.Tx) error {
488+
489+
bk := createBucketIfNotExist(t, tx, tc.srcBucketPath...)
490+
insertRandKeysValuesBucket(t, bk, rand.Int())
491+
492+
return nil
493+
}); err != nil {
494+
t.Fatal(err)
495+
}
496+
db.MustCheck()
497+
})
498+
}
499+
}
500+
func createBucketIfNotExist(t testing.TB, tx *bolt.Tx, paths ...string) *bolt.Bucket {
501+
t.Helper()
502+
503+
bk, err := tx.CreateBucketIfNotExists([]byte(paths[0]))
504+
if err != nil {
505+
t.Fatalf("error creating bucket %v: %v", paths[0], err)
506+
}
507+
508+
for _, key := range paths[1:] {
509+
bk, err = bk.CreateBucketIfNotExists([]byte(key))
510+
t.Fatalf("error creating bucket %v: %v", key, err)
511+
}
512+
513+
return bk
514+
}
515+
516+
func insertRandKeysValuesBucket(t testing.TB, bk *bolt.Bucket, n int) {
517+
518+
var min, max = 1, 1024
519+
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
520+
521+
for i := 0; i < n; i++ {
522+
// generate rand key/value length
523+
keyLength := rand.Intn(max-min) + min
524+
valLength := rand.Intn(max-min) + min
525+
526+
keyData := make([]rune, keyLength)
527+
valData := make([]rune, valLength)
528+
529+
for i := range keyData {
530+
keyData[i] = letters[rand.Intn(len(letters))]
531+
valData[i] = letters[rand.Intn(len(letters))]
532+
}
533+
534+
pErr := bk.Put([]byte(string(keyData)), []byte(string(valData)))
535+
if pErr != nil {
536+
t.Fatalf("error inserting key %v and value %v in bucket %v: %v", string(keyData), string(valData), bk.String(), pErr)
537+
}
538+
}
539+
540+
}
541+
467542
func generateTestKeysValues(t testing.TB, srcBucketPath []string, db *btesting.DB) {
468543
t.Helper()
469544

@@ -540,34 +615,6 @@ func generateTestKeysValues(t testing.TB, srcBucketPath []string, db *btesting.D
540615
db.MustCheck()
541616
}
542617

543-
func TestBucket_MoveBucket2(t *testing.T) {
544-
testCases := []struct {
545-
name string
546-
srcBucketPath []string
547-
dstBucketPath []string
548-
isInlined bool
549-
expErr error
550-
}{
551-
{
552-
name: "happy path",
553-
srcBucketPath: []string{"x", "y", "z"},
554-
dstBucketPath: []string{"x", "y"},
555-
isInlined: false,
556-
expErr: nil,
557-
},
558-
}
559-
560-
for _, tc := range testCases {
561-
t.Run(tc.name, func(*testing.T) {
562-
563-
db := btesting.MustCreateDB(t)
564-
565-
generateTestKeysValues(t, tc.srcBucketPath, db)
566-
567-
})
568-
}
569-
}
570-
571618
func TestBucket_MoveBucket(t *testing.T) {
572619
testCases := []struct {
573620
name string

0 commit comments

Comments
 (0)