-
Notifications
You must be signed in to change notification settings - Fork 5
/
rubbish_cache_test.go
142 lines (137 loc) · 3.27 KB
/
rubbish_cache_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package aklapi
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
tomorrow = &CollectionDayDetailResult{
Collections: []RubbishCollection{
{Date: time.Now().Add(24 * time.Hour), Rubbish: true},
{Date: time.Now().Add(7 * 24 * time.Hour), Rubbish: true},
},
Address: testAddr,
}
today = &CollectionDayDetailResult{
Collections: []RubbishCollection{
{Date: time.Now().Add(1 * time.Minute), Rubbish: true},
{Date: time.Now().Add(7 * 24 * time.Hour), Rubbish: true},
},
Address: testAddr,
}
yesterday = &CollectionDayDetailResult{
Collections: []RubbishCollection{
{Date: time.Now().Add(-24 * time.Hour), Rubbish: true},
{Date: time.Now().Add(5 * 24 * time.Hour), Rubbish: true},
},
Address: testAddr,
}
secondYesterday = &CollectionDayDetailResult{
Collections: []RubbishCollection{
{Date: time.Now().Add(24 * time.Hour), Rubbish: true},
{Date: time.Now().Add(-24 * time.Hour), Rubbish: true},
},
Address: testAddr,
}
)
func Test_rubbishResultCache_Lookup(t *testing.T) {
defer func() {
NoCache = false
}()
type args struct {
searchText string
}
tests := []struct {
name string
NoCache bool // if true, set NoCache to true before running test
c rubbishResultCache
args args
wantResult *CollectionDayDetailResult
wantOk bool
}{
{"not in cache (empty)", false, rubbishResultCache{}, args{"blah"}, nil, false},
{"not in cache",
false,
rubbishResultCache{
testAddr.Address: tomorrow,
},
args{"blah"},
nil, false},
{"in cache (future)",
false,
rubbishResultCache{
testAddr.Address: tomorrow,
},
args{testAddr.Address},
tomorrow, true},
{"in cache (today)",
false,
rubbishResultCache{
testAddr.Address: today,
},
args{testAddr.Address},
today, true},
{"in cache, expired",
false,
rubbishResultCache{
testAddr.Address: yesterday,
},
args{testAddr.Address},
nil, false},
{"in cache, second entry expired",
false,
rubbishResultCache{
testAddr.Address: secondYesterday,
},
args{testAddr.Address},
nil, false},
{"no cache",
true,
rubbishResultCache{
testAddr.Address: today,
},
args{testAddr.Address},
nil, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NoCache = tt.NoCache
gotResult, gotOk := tt.c.Lookup(tt.args.searchText)
if !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("rubbishResultCache.Lookup() gotResult = %v, want %v", gotResult, tt.wantResult)
}
if gotOk != tt.wantOk {
t.Errorf("rubbishResultCache.Lookup() gotOk = %v, want %v", gotOk, tt.wantOk)
}
})
}
}
func Test_rubbishResultCache_Add(t *testing.T) {
type args struct {
searchText string
ar *CollectionDayDetailResult
}
tests := []struct {
name string
c rubbishResultCache
args args
wantrubbishResultCache rubbishResultCache
}{
{"add",
rubbishResultCache{
testAddr.Address: tomorrow,
},
args{testAddr.Address, tomorrow},
rubbishResultCache{
testAddr.Address: tomorrow,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.c.Add(tt.args.searchText, tt.args.ar)
assert.Equal(t, tt.wantrubbishResultCache, tt.c)
})
}
}