github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/cache/memory_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/binbinly/pkg/codec"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_NewMemoryCache(t *testing.T) {
    12  	asserts := assert.New(t)
    13  
    14  	client := NewMemoryCache(WithCodec(codec.JSONEncoding{}), WithPrefix("demo"))
    15  	asserts.NotNil(client)
    16  }
    17  
    18  func TestMemoStore_Set(t *testing.T) {
    19  	asserts := assert.New(t)
    20  
    21  	store := NewMemoryCache(WithCodec(codec.JSONEncoding{}), WithPrefix("demo"))
    22  	err := store.Set(context.Background(), "test-key", "test-val", -1)
    23  	asserts.NotNil(err)
    24  }
    25  
    26  func TestMemoStore_Get(t *testing.T) {
    27  	asserts := assert.New(t)
    28  	store := NewMemoryCache(WithCodec(codec.JSONEncoding{}), WithPrefix("demo"))
    29  	ctx := context.Background()
    30  
    31  	type testStruct struct {
    32  		Name string
    33  		Age  int
    34  	}
    35  
    36  	// normal
    37  	{
    38  		testKey := "test-key2"
    39  		testVal := testStruct{
    40  			Name: "test-name",
    41  			Age:  18,
    42  		}
    43  		err := store.Set(ctx, testKey, &testVal, 60)
    44  		asserts.Nil(err)
    45  
    46  		var gotVal testStruct
    47  		err = store.Get(ctx, testKey, &gotVal)
    48  		asserts.Nil(err)
    49  		asserts.NotNil(gotVal)
    50  	}
    51  }