diff --git a/go.mod b/go.mod index ad0b436..de385f0 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,6 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/hashicorp/golang-lru/v2 v2.0.6 github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sync v0.1.0 // indirect + golang.org/x/sync v0.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6300369..1d120f6 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU= go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mock_cache_test.go b/mock_cache_test.go new file mode 100644 index 0000000..a9c12c4 --- /dev/null +++ b/mock_cache_test.go @@ -0,0 +1,253 @@ +// Copyright 2023 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by MockGen. DO NOT EDIT. +// Source: types.go + +// Package ecache is a generated GoMock package. +package ecache + +import ( + context "context" + reflect "reflect" + time "time" + + gomock "go.uber.org/mock/gomock" +) + +// MockCache is a mock of Cache interface. +type MockCache struct { + ctrl *gomock.Controller + recorder *MockCacheMockRecorder +} + +// MockCacheMockRecorder is the mock recorder for MockCache. +type MockCacheMockRecorder struct { + mock *MockCache +} + +// NewMockCache creates a new mock instance. +func NewMockCache(ctrl *gomock.Controller) *MockCache { + mock := &MockCache{ctrl: ctrl} + mock.recorder = &MockCacheMockRecorder{mock} + return mock +} + +func NewMockNamespaceCache(cache *MockCache, namespace string) *NamespaceCache { + return &NamespaceCache{ + C: cache, + Namespace: namespace, + } +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockCache) EXPECT() *MockCacheMockRecorder { + return m.recorder +} + +// DecrBy mocks base method. +func (m *MockCache) DecrBy(ctx context.Context, key string, value int64) (int64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DecrBy", ctx, key, value) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DecrBy indicates an expected call of DecrBy. +func (mr *MockCacheMockRecorder) DecrBy(ctx, key, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DecrBy", reflect.TypeOf((*MockCache)(nil).DecrBy), ctx, key, value) +} + +// Delete mocks base method. +func (m *MockCache) Delete(ctx context.Context, key ...string) (int64, error) { + m.ctrl.T.Helper() + varargs := []interface{}{ctx} + for _, a := range key { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "Delete", varargs...) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Delete indicates an expected call of Delete. +func (mr *MockCacheMockRecorder) Delete(ctx interface{}, key ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx}, key...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockCache)(nil).Delete), varargs...) +} + +// Get mocks base method. +func (m *MockCache) Get(ctx context.Context, key string) Value { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Get", ctx, key) + ret0, _ := ret[0].(Value) + return ret0 +} + +// Get indicates an expected call of Get. +func (mr *MockCacheMockRecorder) Get(ctx, key interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockCache)(nil).Get), ctx, key) +} + +// GetSet mocks base method. +func (m *MockCache) GetSet(ctx context.Context, key, val string) Value { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSet", ctx, key, val) + ret0, _ := ret[0].(Value) + return ret0 +} + +// GetSet indicates an expected call of GetSet. +func (mr *MockCacheMockRecorder) GetSet(ctx, key, val interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSet", reflect.TypeOf((*MockCache)(nil).GetSet), ctx, key, val) +} + +// IncrBy mocks base method. +func (m *MockCache) IncrBy(ctx context.Context, key string, value int64) (int64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IncrBy", ctx, key, value) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// IncrBy indicates an expected call of IncrBy. +func (mr *MockCacheMockRecorder) IncrBy(ctx, key, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IncrBy", reflect.TypeOf((*MockCache)(nil).IncrBy), ctx, key, value) +} + +// IncrByFloat mocks base method. +func (m *MockCache) IncrByFloat(ctx context.Context, key string, value float64) (float64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IncrByFloat", ctx, key, value) + ret0, _ := ret[0].(float64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// IncrByFloat indicates an expected call of IncrByFloat. +func (mr *MockCacheMockRecorder) IncrByFloat(ctx, key, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IncrByFloat", reflect.TypeOf((*MockCache)(nil).IncrByFloat), ctx, key, value) +} + +// LPop mocks base method. +func (m *MockCache) LPop(ctx context.Context, key string) Value { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LPop", ctx, key) + ret0, _ := ret[0].(Value) + return ret0 +} + +// LPop indicates an expected call of LPop. +func (mr *MockCacheMockRecorder) LPop(ctx, key interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LPop", reflect.TypeOf((*MockCache)(nil).LPop), ctx, key) +} + +// LPush mocks base method. +func (m *MockCache) LPush(ctx context.Context, key string, val ...any) (int64, error) { + m.ctrl.T.Helper() + varargs := []interface{}{ctx, key} + for _, a := range val { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "LPush", varargs...) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// LPush indicates an expected call of LPush. +func (mr *MockCacheMockRecorder) LPush(ctx, key interface{}, val ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx, key}, val...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LPush", reflect.TypeOf((*MockCache)(nil).LPush), varargs...) +} + +// SAdd mocks base method. +func (m *MockCache) SAdd(ctx context.Context, key string, members ...any) (int64, error) { + m.ctrl.T.Helper() + varargs := []interface{}{ctx, key} + for _, a := range members { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "SAdd", varargs...) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SAdd indicates an expected call of SAdd. +func (mr *MockCacheMockRecorder) SAdd(ctx, key interface{}, members ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx, key}, members...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SAdd", reflect.TypeOf((*MockCache)(nil).SAdd), varargs...) +} + +// SRem mocks base method. +func (m *MockCache) SRem(ctx context.Context, key string, members ...any) (int64, error) { + m.ctrl.T.Helper() + varargs := []interface{}{ctx, key} + for _, a := range members { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "SRem", varargs...) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SRem indicates an expected call of SRem. +func (mr *MockCacheMockRecorder) SRem(ctx, key interface{}, members ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx, key}, members...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SRem", reflect.TypeOf((*MockCache)(nil).SRem), varargs...) +} + +// Set mocks base method. +func (m *MockCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Set", ctx, key, val, expiration) + ret0, _ := ret[0].(error) + return ret0 +} + +// Set indicates an expected call of Set. +func (mr *MockCacheMockRecorder) Set(ctx, key, val, expiration interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Set", reflect.TypeOf((*MockCache)(nil).Set), ctx, key, val, expiration) +} + +// SetNX mocks base method. +func (m *MockCache) SetNX(ctx context.Context, key string, val any, expiration time.Duration) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetNX", ctx, key, val, expiration) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SetNX indicates an expected call of SetNX. +func (mr *MockCacheMockRecorder) SetNX(ctx, key, val, expiration interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNX", reflect.TypeOf((*MockCache)(nil).SetNX), ctx, key, val, expiration) +} diff --git a/namespace_cache.go b/namespace_cache.go new file mode 100644 index 0000000..7fc0cff --- /dev/null +++ b/namespace_cache.go @@ -0,0 +1,80 @@ +// Copyright 2023 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ecache + +import ( + "context" + "time" +) + +type NamespaceCache struct { + C Cache + Namespace string +} + +func (c *NamespaceCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error { + return c.C.Set(ctx, c.Namespace+key, val, expiration) +} + +func (c *NamespaceCache) SetNX(ctx context.Context, key string, val any, expiration time.Duration) (bool, error) { + return c.C.SetNX(ctx, c.Namespace+key, val, expiration) +} + +func (c *NamespaceCache) GetSet(ctx context.Context, key string, val string) Value { + return c.C.GetSet(ctx, c.Namespace+key, val) +} + +func (c *NamespaceCache) Delete(ctx context.Context, key ...string) (int64, error) { + if len(key) == 1 { + return c.C.Delete(ctx, c.Namespace+key[0]) + } + newkey := make([]string, len(key)) + for i, v := range key { + newkey[i] = c.Namespace + v + } + return c.C.Delete(ctx, newkey...) +} + +func (c *NamespaceCache) LPush(ctx context.Context, key string, val ...any) (int64, error) { + return c.C.LPush(ctx, c.Namespace+key, val...) +} + +func (c *NamespaceCache) LPop(ctx context.Context, key string) Value { + return c.C.LPop(ctx, c.Namespace+key) +} + +func (c *NamespaceCache) SAdd(ctx context.Context, key string, members ...any) (int64, error) { + return c.C.SAdd(ctx, c.Namespace+key, members...) +} + +func (c *NamespaceCache) SRem(ctx context.Context, key string, members ...any) (int64, error) { + return c.C.SRem(ctx, c.Namespace+key, members...) +} + +func (c *NamespaceCache) IncrBy(ctx context.Context, key string, value int64) (int64, error) { + return c.C.IncrBy(ctx, c.Namespace+key, value) +} + +func (c *NamespaceCache) DecrBy(ctx context.Context, key string, value int64) (int64, error) { + return c.C.DecrBy(ctx, c.Namespace+key, value) +} + +func (c *NamespaceCache) IncrByFloat(ctx context.Context, key string, value float64) (float64, error) { + return c.C.IncrByFloat(ctx, c.Namespace+key, value) +} + +func (c *NamespaceCache) Get(ctx context.Context, key string) Value { + return c.C.Get(ctx, c.Namespace+key) +} diff --git a/namespace_cache_test.go b/namespace_cache_test.go new file mode 100644 index 0000000..f71418d --- /dev/null +++ b/namespace_cache_test.go @@ -0,0 +1,656 @@ +// Copyright 2023 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ecache + +import ( + "context" + "reflect" + "testing" + "time" + + "github.com/ecodeclub/ecache/internal/errs" + "github.com/ecodeclub/ekit" + "go.uber.org/mock/gomock" +) + +// 所有测试的目的在于检验namespacecache有没有正确处理key +func TestNamespaceCache_DecrBy(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + value int64 + } + tests := []struct { + name string + fields fields + args args + want int64 + wantErr bool + }{ + { + name: "test_decrby", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + value: 1, + }, + want: 1, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().DecrBy(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.value).Return(tt.want, nil) + got, err := c.DecrBy(tt.args.ctx, tt.args.key, tt.args.value) + if (err != nil) != tt.wantErr { + t.Errorf("DecrBy() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("DecrBy() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_Delete(t *testing.T) { + tests := []struct { + name string + keys []string + mock func(ctrl *gomock.Controller) Cache + wantCnt int64 + wantError bool + }{ + { + name: "test_delete", + keys: []string{"key1", "key2"}, + wantCnt: 2, + wantError: false, + mock: func(ctrl *gomock.Controller) Cache { + c := NewMockCache(ctrl) + c.EXPECT().Delete(gomock.Any(), gomock.Any()).Return(int64(2), nil) + return c + }, + }, + { + name: "test_delete_1", + keys: []string{"key1"}, + wantCnt: 1, + wantError: false, + mock: func(ctrl *gomock.Controller) Cache { + c := NewMockCache(ctrl) + c.EXPECT().Delete(gomock.Any(), gomock.Any()).Return(int64(1), nil) + return c + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := NamespaceCache{ + C: tt.mock(gomock.NewController(t)), + Namespace: "app1:", + } + got, err := c.Delete(context.Background(), tt.keys...) + if (err != nil) != tt.wantError { + t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantError) + return + } + if got != tt.wantCnt { + t.Errorf("Delete() got = %v, want %v", got, tt.wantCnt) + } + }) + } +} + +func TestNamespaceCache_GetSet(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + val string + } + tests := []struct { + name string + fields fields + args args + want Value + }{ + { + name: "test_getset", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + val: "val", + }, + want: Value{ + AnyValue: ekit.AnyValue{ + Val: "val", + Err: nil, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().GetSet(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.val).Return(tt.want) + if got := c.GetSet(tt.args.ctx, tt.args.key, tt.args.val); !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetSet() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_IncrBy(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + value int64 + } + tests := []struct { + name string + fields fields + args args + want int64 + wantErr bool + }{ + { + name: "test_incrby", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + value: 1, + }, + want: 1, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().IncrBy(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.value).Return(tt.want, nil) + got, err := c.IncrBy(tt.args.ctx, tt.args.key, tt.args.value) + if (err != nil) != tt.wantErr { + t.Errorf("IncrBy() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("IncrBy() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_IncrByFloat(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + value float64 + } + tests := []struct { + name string + fields fields + args args + want float64 + wantErr bool + }{ + { + name: "test_incrbyfloat", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + value: 1.0, + }, + want: 1.0, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().IncrByFloat(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.value).Return(tt.want, nil) + got, err := c.IncrByFloat(tt.args.ctx, tt.args.key, tt.args.value) + if (err != nil) != tt.wantErr { + t.Errorf("IncrByFloat() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("IncrByFloat() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_LPop(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + } + tests := []struct { + name string + fields fields + args args + want Value + }{ + { + name: "test_lpop", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + }, + want: Value{ + AnyValue: ekit.AnyValue{ + Val: "val", + Err: nil, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().LPop(tt.args.ctx, tt.fields.Namespace+tt.args.key).Return(tt.want) + if got := c.LPop(tt.args.ctx, tt.args.key); !reflect.DeepEqual(got, tt.want) { + t.Errorf("LPop() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_LPush(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + val []any + } + tests := []struct { + name string + fields fields + args args + want int64 + wantErr bool + }{ + { + name: "test_lpush", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + val: []any{"val1", "val2"}, + }, + want: 1, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().LPush(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.val...).Return(tt.want, nil) + got, err := c.LPush(tt.args.ctx, tt.args.key, tt.args.val...) + if (err != nil) != tt.wantErr { + t.Errorf("LPush() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("LPush() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_SAdd(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + members []any + } + tests := []struct { + name string + fields fields + args args + want int64 + wantErr bool + }{ + { + name: "test_sadd", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + members: []any{"member1", "member2"}, + }, + want: 1, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().SAdd(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.members...).Return(tt.want, nil) + got, err := c.SAdd(tt.args.ctx, tt.args.key, tt.args.members...) + if (err != nil) != tt.wantErr { + t.Errorf("SAdd() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("SAdd() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_SRem(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + members []any + } + tests := []struct { + name string + fields fields + args args + want int64 + wantErr bool + }{ + { + name: "test_srem", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + members: []any{"member1", "member2"}, + }, + want: 1, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().SRem(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.members...).Return(tt.want, nil) + got, err := c.SRem(tt.args.ctx, tt.args.key, tt.args.members...) + if (err != nil) != tt.wantErr { + t.Errorf("SRem() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("SRem() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_Set(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + val any + expiration time.Duration + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "test_set", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + val: "val", + expiration: time.Second, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().Set(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.val, tt.args.expiration).Return(nil) + if err := c.Set(tt.args.ctx, tt.args.key, tt.args.val, tt.args.expiration); (err != nil) != tt.wantErr { + t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestNamespaceCache_SetNX(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + val any + expiration time.Duration + } + tests := []struct { + name string + fields fields + args args + want bool + wantErr bool + }{ + { + name: "test_setnx", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + val: "val", + expiration: time.Second, + }, + want: true, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + tt.fields.C.EXPECT().SetNX(tt.args.ctx, tt.fields.Namespace+tt.args.key, tt.args.val, tt.args.expiration).Return(tt.want, nil) + got, err := c.SetNX(tt.args.ctx, tt.args.key, tt.args.val, tt.args.expiration) + if (err != nil) != tt.wantErr { + t.Errorf("SetNX() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("SetNX() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestValue_KeyNotFound(t *testing.T) { + type fields struct { + AnyValue ekit.AnyValue + } + tests := []struct { + name string + fields fields + want bool + }{ + { + name: "test_key_not_found", + fields: fields{ + AnyValue: ekit.AnyValue{ + Val: nil, + Err: errs.ErrKeyNotExist, + }, + }, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Value{ + AnyValue: tt.fields.AnyValue, + } + if got := v.KeyNotFound(); got != tt.want { + t.Errorf("KeyNotFound() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNamespaceCache_Get(t *testing.T) { + type fields struct { + C *MockCache + Namespace string + } + type args struct { + ctx context.Context + key string + } + tests := []struct { + name string + fields fields + args args + want Value + }{ + { + name: "test_get", + fields: fields{ + C: NewMockCache(gomock.NewController(t)), + Namespace: "app1:", + }, + args: args{ + ctx: context.Background(), + key: "key", + }, + want: Value{ + AnyValue: ekit.AnyValue{ + Val: nil, + Err: nil, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.fields.C.EXPECT().Get(tt.args.ctx, tt.fields.Namespace+tt.args.key).Return(tt.want) + c := &NamespaceCache{ + C: tt.fields.C, + Namespace: tt.fields.Namespace, + } + if got := c.Get(tt.args.ctx, tt.args.key); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Get() = %v, want %v", got, tt.want) + } + }) + } +}