-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
79 lines (64 loc) · 1.52 KB
/
store_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
package ratelimit
import (
"context"
"github.com/go-redis/redismock/v9"
"strconv"
"testing"
"time"
)
func TestRedisStore_Set(t *testing.T) {
db, mock := redismock.NewClientMock()
key := "key"
record := slidingWindowRecord{
Start: time.Now().UnixNano(),
PrevCount: 1,
CurrentCount: 2,
}
mock.ExpectHSet(key, record).SetVal(0)
s := NewRedisStore[slidingWindowRecord](db)
err := s.Set(context.Background(), key, &record)
if err != nil {
t.Error(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}
func TestRedisStore_Increment(t *testing.T) {
db, mock := redismock.NewClientMock()
key := "key"
mock.ExpectHIncrBy(key, currentCountFieldTag, 1).SetVal(1)
s := NewRedisStore[slidingWindowRecord](db)
err := s.Increment(context.Background(), key)
if err != nil {
t.Error(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}
func TestRedisStore_Get(t *testing.T) {
db, mock := redismock.NewClientMock()
key := "key"
record := slidingWindowRecord{
Start: time.Now().UnixNano(),
PrevCount: 1,
CurrentCount: 2,
}
mock.ExpectHGetAll(key).SetVal(map[string]string{
"start": strconv.FormatInt(record.Start, 10),
"prev": "1",
"current": "2",
})
s := NewRedisStore[slidingWindowRecord](db)
result, err := s.Get(context.Background(), key)
if err != nil {
t.Error(err)
}
if *result != record {
t.Errorf("expected: %+v, got: %+v", record, *result)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
}