github.com/msales/pkg/v3@v3.24.0/cache/cache_test.go (about) 1 package cache_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/msales/pkg/v3/cache" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/mock" 11 ) 12 13 func TestGet(t *testing.T) { 14 m := new(MockCache) 15 m.On("Get", "test").Return(&cache.Item{}) 16 ctx := cache.WithCache(context.Background(), m) 17 18 cache.Get(ctx, "test") 19 20 m.AssertExpectations(t) 21 } 22 23 func TestGetMulti(t *testing.T) { 24 m := new(MockCache) 25 m.On("GetMulti", []string{"test"}).Return([]*cache.Item{{}}, nil) 26 ctx := cache.WithCache(context.Background(), m) 27 28 cache.GetMulti(ctx, "test") 29 30 m.AssertExpectations(t) 31 } 32 33 func TestSet(t *testing.T) { 34 m := new(MockCache) 35 m.On("Set", "test", 1, 0*time.Second).Return(nil) 36 ctx := cache.WithCache(context.Background(), m) 37 38 cache.Set(ctx, "test", 1, 0) 39 40 m.AssertExpectations(t) 41 } 42 43 func TestAdd(t *testing.T) { 44 m := new(MockCache) 45 m.On("Add", "test", 1, 0*time.Second).Return(nil) 46 ctx := cache.WithCache(context.Background(), m) 47 48 cache.Add(ctx, "test", 1, 0) 49 50 m.AssertExpectations(t) 51 } 52 53 func TestReplace(t *testing.T) { 54 m := new(MockCache) 55 m.On("Replace", "test", 1, 0*time.Second).Return(nil) 56 ctx := cache.WithCache(context.Background(), m) 57 58 cache.Replace(ctx, "test", 1, 0) 59 60 m.AssertExpectations(t) 61 } 62 63 func TestDelete(t *testing.T) { 64 m := new(MockCache) 65 m.On("Delete", "test").Return(nil) 66 ctx := cache.WithCache(context.Background(), m) 67 68 cache.Delete(ctx, "test") 69 70 m.AssertExpectations(t) 71 } 72 73 func TestInc(t *testing.T) { 74 m := new(MockCache) 75 m.On("Inc", "test", uint64(1)).Return(int64(1), nil) 76 ctx := cache.WithCache(context.Background(), m) 77 78 cache.Inc(ctx, "test", 1) 79 80 m.AssertExpectations(t) 81 } 82 83 func TestDec(t *testing.T) { 84 m := new(MockCache) 85 m.On("Dec", "test", uint64(1)).Return(int64(1), nil) 86 ctx := cache.WithCache(context.Background(), m) 87 88 cache.Dec(ctx, "test", 1) 89 90 m.AssertExpectations(t) 91 } 92 93 func TestNullCache_Get(t *testing.T) { 94 i := cache.Null.Get("test") 95 v, err := i.Bytes() 96 97 assert.NoError(t, err) 98 assert.Equal(t, []byte{}, v) 99 } 100 101 func TestNullCache_GetBool(t *testing.T) { 102 i := cache.Null.Get("test") 103 b, err := i.Bool() 104 105 assert.NoError(t, err) 106 assert.Equal(t, false, b) 107 } 108 109 func TestNullCache_GetInt64(t *testing.T) { 110 i := cache.Null.Get("test") 111 b, err := i.Int64() 112 113 assert.NoError(t, err) 114 assert.Equal(t, int64(0), b) 115 } 116 117 func TestNullCache_GetUint64(t *testing.T) { 118 i := cache.Null.Get("test") 119 b, err := i.Uint64() 120 121 assert.NoError(t, err) 122 assert.Equal(t, uint64(0), b) 123 } 124 125 func TestNullCache_GetFloat64(t *testing.T) { 126 i := cache.Null.Get("test") 127 v, err := i.Float64() 128 129 assert.NoError(t, err) 130 assert.Equal(t, float64(0), v) 131 } 132 133 func TestNullCache_GetMulti(t *testing.T) { 134 v, err := cache.Null.GetMulti("test") 135 136 assert.NoError(t, err) 137 assert.Len(t, v, 0) 138 } 139 140 func TestNullCache_Set(t *testing.T) { 141 assert.NoError(t, cache.Null.Set("test", 1, 0)) 142 } 143 144 func TestNullCache_Add(t *testing.T) { 145 assert.NoError(t, cache.Null.Add("test", 1, 0)) 146 } 147 148 func TestNullCache_Replace(t *testing.T) { 149 assert.NoError(t, cache.Null.Replace("test", 1, 0)) 150 } 151 152 func TestNullCache_Delete(t *testing.T) { 153 assert.NoError(t, cache.Null.Delete("test")) 154 } 155 156 func TestNullCache_Inc(t *testing.T) { 157 v, err := cache.Null.Inc("test", 1) 158 159 assert.NoError(t, err) 160 assert.Equal(t, int64(0), v) 161 } 162 163 func TestNullCache_Dec(t *testing.T) { 164 v, err := cache.Null.Dec("test", 1) 165 166 assert.NoError(t, err) 167 assert.Equal(t, int64(0), v) 168 } 169 170 type MockCache struct { 171 mock.Mock 172 } 173 174 func (c *MockCache) Get(key string) *cache.Item { 175 args := c.Called(key) 176 return args.Get(0).(*cache.Item) 177 } 178 179 func (c *MockCache) GetMulti(keys ...string) ([]*cache.Item, error) { 180 args := c.Called(keys) 181 return args.Get(0).([]*cache.Item), args.Error(1) 182 } 183 184 func (c *MockCache) Set(key string, value interface{}, expire time.Duration) error { 185 args := c.Called(key, value, expire) 186 return args.Error(0) 187 } 188 189 func (c *MockCache) Add(key string, value interface{}, expire time.Duration) error { 190 args := c.Called(key, value, expire) 191 return args.Error(0) 192 } 193 194 func (c *MockCache) Replace(key string, value interface{}, expire time.Duration) error { 195 args := c.Called(key, value, expire) 196 return args.Error(0) 197 } 198 199 func (c *MockCache) Delete(key string) error { 200 args := c.Called(key) 201 return args.Error(0) 202 } 203 204 func (c *MockCache) Inc(key string, value uint64) (int64, error) { 205 args := c.Called(key, value) 206 return args.Get(0).(int64), args.Error(1) 207 } 208 209 func (c *MockCache) Dec(key string, value uint64) (int64, error) { 210 args := c.Called(key, value) 211 return args.Get(0).(int64), args.Error(1) 212 } 213 214 func runCacheTests(t *testing.T, c cache.Cache) { 215 // Set 216 err := c.Set("test", "foobar", 0) 217 assert.NoError(t, err) 218 219 // Get 220 str, err := c.Get("test").String() 221 assert.NoError(t, err) 222 assert.Equal(t, "foobar", str) 223 _, err = c.Get("_").String() 224 assert.EqualError(t, err, cache.ErrCacheMiss.Error()) 225 226 // Add 227 err = c.Add("test1", "foobar", 0) 228 assert.NoError(t, err) 229 err = c.Add("test1", "foobar", 0) 230 assert.EqualError(t, err, cache.ErrNotStored.Error()) 231 232 // Replace 233 err = c.Replace("test1", "foobar", 0) 234 assert.NoError(t, err) 235 err = c.Replace("_", "foobar", 0) 236 assert.EqualError(t, err, cache.ErrNotStored.Error()) 237 238 // GetMulti 239 v, err := c.GetMulti("test", "test1", "_") 240 assert.NoError(t, err) 241 assert.Len(t, v, 3) 242 assert.EqualError(t, v[2].Err(), "cache: miss") 243 244 // Delete 245 err = c.Delete("test1") 246 assert.NoError(t, err) 247 _, err = c.Get("test1").String() 248 assert.Error(t, err) 249 250 // Inc 251 err = c.Set("test2", 1, 0) 252 assert.NoError(t, err) 253 i, err := c.Inc("test2", 1) 254 assert.NoError(t, err) 255 assert.Equal(t, int64(2), i) 256 257 // Dec 258 err = c.Set("test2", 1, 0) 259 assert.NoError(t, err) 260 i, err = c.Dec("test2", 1) 261 assert.NoError(t, err) 262 assert.Equal(t, int64(0), i) 263 }