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

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