-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_cached_quota_test.go
95 lines (68 loc) · 2.37 KB
/
get_cached_quota_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package andromeda_test
import (
"context"
"errors"
"github.com/golang/mock/gomock"
"github.com/ramadani/andromeda"
"github.com/ramadani/andromeda/mocks"
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetCachedQuota(t *testing.T) {
ctx := context.TODO()
mockCtrl := gomock.NewController(t)
mockCache := mocks.NewMockCache(mockCtrl)
mockGetQuotaKey := mocks.NewMockGetQuotaKey(mockCtrl)
getCachedQuota := andromeda.NewGetCachedQuota(mockCache, mockGetQuotaKey)
t.Run("ErrorGetQuotaKey", func(t *testing.T) {
defer mockCtrl.Finish()
req := &andromeda.QuotaRequest{QuotaID: "123"}
mockErr := errors.New("unexpected")
mockGetQuotaKey.EXPECT().Do(ctx, req).Return("", mockErr)
res, err := getCachedQuota.Do(ctx, req)
assert.Equal(t, int64(0), res)
assert.EqualError(t, err, mockErr.Error())
})
t.Run("ErrorGetCache", func(t *testing.T) {
defer mockCtrl.Finish()
req := &andromeda.QuotaRequest{QuotaID: "123"}
key := "123-key"
mockErr := errors.New("unexpected")
mockGetQuotaKey.EXPECT().Do(ctx, req).Return(key, nil)
mockCache.EXPECT().Get(ctx, key).Return("", mockErr)
res, err := getCachedQuota.Do(ctx, req)
assert.Equal(t, int64(0), res)
assert.EqualError(t, err, mockErr.Error())
})
t.Run("ErrorQuotaNotFound", func(t *testing.T) {
defer mockCtrl.Finish()
req := &andromeda.QuotaRequest{QuotaID: "123"}
key := "123-key"
mockGetQuotaKey.EXPECT().Do(ctx, req).Return(key, nil)
mockCache.EXPECT().Get(ctx, key).Return("", andromeda.ErrCacheNotFound)
res, err := getCachedQuota.Do(ctx, req)
assert.Equal(t, int64(0), res)
assert.Error(t, err)
assert.True(t, errors.Is(err, andromeda.ErrQuotaNotFound))
})
t.Run("SucceedGetQuota", func(t *testing.T) {
defer mockCtrl.Finish()
req := &andromeda.QuotaRequest{QuotaID: "123"}
key := "123-key"
mockGetQuotaKey.EXPECT().Do(ctx, req).Return(key, nil)
mockCache.EXPECT().Get(ctx, key).Return("1000", nil)
res, err := getCachedQuota.Do(ctx, req)
assert.Equal(t, int64(1000), res)
assert.Nil(t, err)
})
t.Run("ErrorConvertValue", func(t *testing.T) {
defer mockCtrl.Finish()
req := &andromeda.QuotaRequest{QuotaID: "123"}
key := "123-key"
mockGetQuotaKey.EXPECT().Do(ctx, req).Return(key, nil)
mockCache.EXPECT().Get(ctx, key).Return("lorem", nil)
res, err := getCachedQuota.Do(ctx, req)
assert.Equal(t, int64(0), res)
assert.NotNil(t, err)
})
}