Skip to content

Commit

Permalink
removing old test
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 dd2df9e commit b1b529d
Showing 1 changed file with 9 additions and 297 deletions.
306 changes: 9 additions & 297 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,15 @@ func TestBucket_MoveBucket2(t *testing.T) {
db.MustCheck()

// act
var tmpFile string
//var tmpFile string
if err := db.Update(func(tx *bolt.Tx) error {
srcBucket := retrieveParentBucket(t, tx, tc.srcBucketPath...)
dstBucket := retrieveChildBucket(t, tx, tc.dstBucketPath...)
bucketToMove := tc.srcBucketPath[len(tc.srcBucketPath)-1]

// dump bucketToMove to bbolt file for assertion
bk := srcBucket.Bucket([]byte(bucketToMove))
tmpFile = dumpBucketToFile(t, bk)
//// dump bucketToMove to bbolt file for assertion
//bk := srcBucket.Bucket([]byte(bucketToMove))
//tmpFile = dumpBucketToFile(t, bk)

mErr := srcBucket.MoveBucket([]byte(bucketToMove), dstBucket)
require.Equal(t, tc.expErr, mErr)
Expand Down Expand Up @@ -536,12 +536,11 @@ func TestBucket_MoveBucket2(t *testing.T) {
t.Fatalf("expected subBucket %v to exist within dstBucket %v", bucketToMove, dstBucket)
}

bucketOnDisk, err := os.ReadFile(tmpFile)
if err != nil {
t.Fatalf("error reading tmp file %v", tmpFile)
}

require.Equal(t, bucketOnDisk, childBucket.Write())
//bucketOnDisk, err := os.ReadFile(tmpFile)
//if err != nil {
// t.Fatalf("error reading tmp file %v", tmpFile)
//}
//require.Equal(t, bucketOnDisk, childBucket.Write())

return nil
}); err != nil {
Expand Down Expand Up @@ -637,293 +636,6 @@ func insertRandKeysValuesBucket(t testing.TB, bk *bolt.Bucket, n int) {

}

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_MoveBucket(t *testing.T) {
testCases := []struct {
name string
srcBucketName string
subBucketName string
subBucketKey string
subBucketValue string
dstBucketName string
subBucketExistSrcBucket bool
subBucketExistDstBucket bool
keyNoSubBucketSrcBucket bool
keyNoSubBucketDstBucket bool
expErr error
}{
{
"happy path",
"srcBucket",
"subBucket",
"this is subBucket key",
"this is subBucket value",
"dstBucket",
true,
false,
false,
false,
nil,
},
{
"subBucket not exist in srcBucket",
"srcBucket",
"subBucket",
"this is subBucket key",
"this is subBucket value",
"dstBucket",
false,
false,
false,
false,
berrors.ErrBucketNotFound,
},
{
"suBucket exist in dstBucket",
"srcBucket",
"subBucket",
"this is subBucket key",
"this is subBucket value",
"dstBucket",
true,
true,
false,
false,
berrors.ErrBucketExists,
},
{
"subBucket key exist in srcBucket, but no subBucket value",
"srcBucket",
"subBucket",
"this is subBucket key",
"this is subBucket value",
"dstBucket",
false,
false,
true,
false,
berrors.ErrIncompatibleValue,
},
{
"subBucket key exist in dstBucket, but no subBucket value",
"srcBucket",
"subBucket",
"this is subBucket key",
"this is subBucket value",
"dstBucket",
true,
false,
false,
true,
berrors.ErrIncompatibleValue,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
db := btesting.MustCreateDB(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)
}

if tc.subBucketExistSrcBucket {
// create subBucket within srcBucket
subBucket, sErr := srcBucket.CreateBucket([]byte(tc.subBucketName))
if sErr != nil {
t.Fatal(sErr)
}
// insert K/V pair into subBucket
if pErr := subBucket.Put([]byte(tc.subBucketKey), []byte(tc.subBucketValue)); pErr != nil {
t.Fatal(pErr)
}
}

if tc.keyNoSubBucketSrcBucket {
if pErr := srcBucket.Put([]byte(tc.subBucketName), []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.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)
}
}

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

// 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 {
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)
}

mvErr := tx.MoveBucket([]byte(tc.subBucketName), srcBucket, dstBucket)
if !tc.subBucketExistSrcBucket || tc.keyNoSubBucketSrcBucket || tc.subBucketExistDstBucket || tc.keyNoSubBucketDstBucket {
require.ErrorIs(t, mvErr, tc.expErr)
} else if mvErr != nil {
t.Fatalf("failed to move subBucket '%v' from srcBucket '%v' to dstBucket '%v': %v", tc.subBucketName, tc.srcBucketName, tc.dstBucketName, mvErr)
}

return nil

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

// skip assertion in these cases
if !tc.subBucketExistSrcBucket || tc.keyNoSubBucketSrcBucket || tc.subBucketExistDstBucket || tc.keyNoSubBucketDstBucket {
return
}

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

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)
}

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

// Ensure that accessing and updating nested buckets is ok across transactions.
func TestBucket_Nested(t *testing.T) {
db := btesting.MustCreateDB(t)
Expand Down

0 comments on commit b1b529d

Please sign in to comment.