-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_quota_usage_middleware_test.go
58 lines (44 loc) · 1.56 KB
/
update_quota_usage_middleware_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
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 TestUpdateQuotaUsageMiddleware(t *testing.T) {
ctx := context.TODO()
mockCtrl := gomock.NewController(t)
mockPrev := mocks.NewMockUpdateQuotaUsage(mockCtrl)
mockNext := mocks.NewMockUpdateQuotaUsage(mockCtrl)
updateQuotaUsage := andromeda.NewUpdateQuotaUsageMiddleware(mockPrev, mockNext)
t.Run("ExitWhenPrevHasError", func(t *testing.T) {
req := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
mockErr := errors.New("unexpected")
mockPrev.EXPECT().Do(ctx, req).Return(nil, mockErr)
res, err := updateQuotaUsage.Do(ctx, req)
assert.Nil(t, res)
assert.EqualError(t, err, mockErr.Error())
})
t.Run("NextHasError", func(t *testing.T) {
req := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
mockRes := "result"
mockErr := errors.New("unexpected")
mockPrev.EXPECT().Do(ctx, req).Return(mockRes, nil)
mockNext.EXPECT().Do(ctx, req).Return(nil, mockErr)
res, err := updateQuotaUsage.Do(ctx, req)
assert.Nil(t, res)
assert.EqualError(t, err, mockErr.Error())
})
t.Run("NoError", func(t *testing.T) {
req := &andromeda.QuotaUsageRequest{QuotaID: "123", Usage: int64(1000)}
mockRes := "result"
mockPrev.EXPECT().Do(ctx, req).Return(mockRes, nil)
mockNext.EXPECT().Do(ctx, req).Return(mockRes, nil)
res, err := updateQuotaUsage.Do(ctx, req)
assert.Equal(t, mockRes, res)
assert.Nil(t, err)
})
}