github.com/portworx/kvdb@v0.0.0-20241107215734-a185a966f535/bolt/kv_bolt_Test.go (about)

     1  package bolt
     2  
     3  // go test -v .
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/portworx/kvdb"
    11  	"github.com/portworx/kvdb/test"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestAll(t *testing.T) {
    16  	// test.Run(New, t, Start, Stop)
    17  	// test.RunWatch(New, t, Start, Stop)
    18  	test.RunWatch(New, t, Start, Stop)
    19  }
    20  
    21  func testNoCopy(kv kvdb.Kvdb, t *testing.T) {
    22  	fmt.Println("testNoCopy")
    23  	type Test struct {
    24  		A int
    25  		B string
    26  	}
    27  	val := Test{1, "abc"}
    28  	_, err := kv.Put("key1", &val, 0)
    29  	assert.NoError(t, err, "Expected no error on put")
    30  	val.A = 2
    31  	val.B = "def"
    32  	var newVal Test
    33  	_, err = kv.GetVal("key1", &newVal)
    34  	assert.NoError(t, err, "Expected no error on get")
    35  	assert.Equal(t, newVal.A, val.A, "Expected equal values")
    36  	assert.Equal(t, newVal.B, val.B, "Expected equal values")
    37  }
    38  
    39  func testGetCopy(kv kvdb.Kvdb, t *testing.T) {
    40  	fmt.Println("testGetCopy")
    41  	type Test struct {
    42  		A int
    43  		B string
    44  	}
    45  	val := Test{1, "abc"}
    46  	_, err := kv.Put("key-copy", &val, 0)
    47  	assert.NoError(t, err, "Expected no error on put")
    48  	copySelect := func(val interface{}) interface{} {
    49  		cpy := val.(*Test)
    50  		return &Test{cpy.A, cpy.B}
    51  	}
    52  	_, err = kv.GetWithCopy("key-copy", nil)
    53  	assert.Error(t, err, "Expected an error")
    54  	valIntf, err := kv.GetWithCopy("key-copy", copySelect)
    55  	assert.NoError(t, err, "Expected no error on GetWithCopy")
    56  	newVal, ok := valIntf.(*Test)
    57  	assert.True(t, ok, "Expected correct interface to be returned")
    58  	// Modify the actual value
    59  	val.A = 2
    60  	val.B = "def"
    61  
    62  	assert.NoError(t, err, "Expected no error on get")
    63  	assert.NotEqual(t, newVal.A, val.A, "Expected unequal values")
    64  	assert.NotEqual(t, newVal.B, val.B, "Expected unequal values")
    65  }
    66  
    67  func testEnumerateWithSelect(kv kvdb.Kvdb, t *testing.T) {
    68  	fmt.Println("enumerateWithSelect")
    69  	type EWS struct {
    70  		A int
    71  		B string
    72  	}
    73  	a := &EWS{1, "abc"}
    74  	b := &EWS{2, "def"}
    75  	c := &EWS{3, "abcdef"}
    76  	prefix := "ews"
    77  	kv.Put(prefix+"/"+"key1", a, 0)
    78  	kv.Put(prefix+"/"+"key2", b, 0)
    79  	kv.Put(prefix+"/"+"key3", c, 0)
    80  	enumerateSelect := func(val interface{}) bool {
    81  		v, ok := val.(*EWS)
    82  		if !ok {
    83  			return false
    84  		}
    85  		if len(v.B) != 3 {
    86  			return false
    87  		}
    88  		return true
    89  	}
    90  	copySelect := func(val interface{}) interface{} {
    91  		v, ok := val.(*EWS)
    92  		if !ok {
    93  			return nil
    94  		}
    95  		cpy := *v
    96  		return &cpy
    97  	}
    98  	_, err := kv.EnumerateWithSelect(prefix, nil, nil)
    99  	assert.Error(t, err, "Expected error on EnumerateWithSelect")
   100  	output, err := kv.EnumerateWithSelect(prefix, enumerateSelect, copySelect)
   101  	assert.NoError(t, err, "Unexpected error on EnumerateWithSelect")
   102  	assert.Equal(t, 2, len(output), "Unexpected no. of values returned")
   103  	// Check if copy is returned
   104  	for _, out := range output {
   105  		ews, ok := out.(*EWS)
   106  		assert.Equal(t, ok, true, "Unexpected type of object returned")
   107  		if ews.A == a.A {
   108  			a.A = 999
   109  			assert.NotEqual(t, a.A, ews.A, "Copy was not returned")
   110  		} else if ews.A == b.A {
   111  			b.A = 888
   112  			assert.NotEqual(t, b.A, ews.A, "Copy was not returned")
   113  		} else {
   114  			assert.Fail(t, "Unexpected objects returned")
   115  		}
   116  	}
   117  	// Invalid copy function
   118  	invalidCopySelect := func(val interface{}) interface{} {
   119  		return nil
   120  	}
   121  	output, err = kv.EnumerateWithSelect(prefix, enumerateSelect, invalidCopySelect)
   122  	assert.EqualError(t, err, ErrIllegalSelect.Error(), "Unexpected error")
   123  	assert.Equal(t, 0, len(output), "Unexpected output")
   124  }
   125  
   126  func Start(removeDir bool) error {
   127  	if removeDir {
   128  		os.RemoveAll("px.db")
   129  	}
   130  	return nil
   131  }
   132  
   133  func Stop() error {
   134  	return nil
   135  }