Skip to content

Commit

Permalink
generate synthetic test data
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 ec490c8 commit 4ba996b
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,110 @@ func TestBucket_Delete_NonExisting(t *testing.T) {
}
}

func generateTestKeysValues(t testing.TB, srcBucketPath []string, db *btesting.DB) {
t.Helper()

var min, max = 1, 1024
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// init matrix of bucket keys
keys := make([][]string, 0, len(srcBucketPath))
values := make([][]string, 0, len(srcBucketPath))

for idx := range srcBucketPath {
// rand number of keys/values in each bucket
n := rand.Intn(max-min) + min
keys[idx] = make([]string, n)
values[idx] = make([]string, n)

for i := 0; i < n; i++ {
// generate rand key/value length
keyLength := rand.Intn(max-min) + min
valLength := rand.Intn(max-min) + min

keyData := make([]rune, keyLength)
valData := make([]rune, valLength)

for i := range keyData {
keyData[i] = letters[rand.Intn(len(letters))]
}
keys[idx][i] = string(keyData)

for i := range valData {
valData[i] = letters[rand.Intn(len(letters))]
}
values[idx][i] = string(valData)
}
}

// dump the data in the testDB
if err := db.Update(func(tx *bolt.Tx) error {
lastBucket := ""

for idx, bucket := range srcBucketPath {
var b *bolt.Bucket
var bErr error

if lastBucket == "" {
b, bErr = tx.CreateBucketIfNotExists([]byte(bucket))
if bErr != nil {
t.Fatalf("error creating bucket %v: %v", bucket, bErr)
}
} else {
b = b.Bucket([]byte(lastBucket))
if b == nil {
t.Fatalf("error retrieving last bucket %v", lastBucket)
}
}

lastBucket = bucket

// insert all key/values for this bucket
ks := keys[idx]
vs := values[idx]

for i := 0; i < len(ks); i++ {
pErr := b.Put([]byte(ks[i]), []byte(vs[i]))
if pErr != nil {
t.Fatalf("error inserting key %v and value %v in bucket %v: %v", ks[i], vs[i], bucket, pErr)
}
}
}

return nil
}); err != nil {
t.Fatal(err)
}
db.MustCheck()
}

func TestBucket_MoveBucket2(t *testing.T) {
testCases := []struct {
name string
srcBucketPath []string
dstBucketPath []string
isInlined bool
expErr error
}{
{
name: "happy path",
srcBucketPath: []string{"x", "y", "z"},
dstBucketPath: []string{"x", "y"},
isInlined: false,
expErr: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(*testing.T) {

db := btesting.MustCreateDB(t)

generateTestKeysValues(t, tc.srcBucketPath, db)

})
}
}

func TestBucket_MoveBucket(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit 4ba996b

Please sign in to comment.