-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_test.go
236 lines (204 loc) · 4.6 KB
/
storage_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"testing"
"time"
"github.com/alicebob/miniredis"
"github.com/go-redis/redis/v7"
"github.com/google/go-cmp/cmp"
)
var (
regionASubRegionA = createPollenReport("region-a", "subregion-aa")
regionASubRegionB = createPollenReport("region-a", "subregion-ab")
regionBSubRegionA = createPollenReport("region-b", "subregion-ba")
regionCNoSubregion = createPollenReport("region-c", "")
)
func newMiniRedisServer() *miniredis.Miniredis {
s, err := miniredis.Run()
if err != nil {
panic(err)
}
return s
}
func newStorage(mr *miniredis.Miniredis) *RedisStorage {
s := &RedisStorage{
client: redis.NewClient(&redis.Options{
Addr: mr.Addr(),
}),
}
rs := []*PollenReport{
regionASubRegionA,
regionASubRegionB,
regionBSubRegionA,
regionCNoSubregion,
}
for _, r := range rs {
if err := s.Save(r); err != nil {
panic(err)
}
}
return s
}
func createPollenReport(region, subregion string) *PollenReport {
return &PollenReport{
Region: region,
SubRegion: subregion,
Pollen: []*pollen{
{
Name: "Roggen",
Today: &pollenDayReport{
Description: "mittlere Belastung",
Severity: "2",
},
},
},
}
}
func TestFetchByRegion(t *testing.T) {
mr := newMiniRedisServer()
defer mr.Close()
s := newStorage(mr)
testCases := []struct {
description string
region string
expectedCount int
want []*PollenReport
}{
{
"multiple subregions",
"region-a",
2,
[]*PollenReport{regionASubRegionA, regionASubRegionB},
},
{
"single subregion",
"region-b",
1,
[]*PollenReport{regionBSubRegionA},
},
{
"region with no subregion is its own subregion",
"region-c",
1,
[]*PollenReport{regionCNoSubregion},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
got, err := s.GetByRegion(tc.region)
if err != nil {
t.Errorf("tried to fetch reports for region, got error instead: %q", err)
}
if len(got) != tc.expectedCount {
t.Errorf("expected exactly %d results for region-a, got %d instead", tc.expectedCount, len(got))
}
if !cmp.Equal(got, tc.want) {
t.Errorf("wanted %+v, got %+v", tc.want, got)
}
})
}
}
func TestFetchBySubregion(t *testing.T) {
mr := newMiniRedisServer()
defer mr.Close()
s := newStorage(mr)
tests := []struct {
description string
subregion string
want *PollenReport
err error
}{
{
"existing region",
"subregion-aa",
regionASubRegionA,
nil,
},
{
"region without subregions can be queried as its own subregion",
"region-c",
regionCNoSubregion,
nil,
},
{
"non existent region",
"subregion-ca",
nil,
ErrNotFound,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
got, err := s.GetBySubregion(tc.subregion)
if err != nil && err != tc.err {
t.Errorf("wanted error %q, got %q", tc.err, err)
} else {
if !cmp.Equal(got, tc.want) {
t.Errorf("wanted %+v, got %+v", tc.want, got)
}
}
})
}
}
func TestGetAllRegions(t *testing.T) {
mr := newMiniRedisServer()
defer mr.Close()
s := newStorage(mr)
// region names get normalized, so the dashes
// should have been replaced by underscores
want := []string{
"region_a",
"region_b",
"region_c",
}
got, err := s.AllRegions()
if err != nil {
t.Errorf("got error %q", err)
}
if !cmp.Equal(got, want) {
t.Errorf("got %q, want %q", got, want)
}
}
func TestGetAllSubRegions(t *testing.T) {
mr := newMiniRedisServer()
defer mr.Close()
s := newStorage(mr)
want := []string{
// regions without subregions get treated as
// their own subregion
"region_c",
// subregion names get normalized, so the dashes
// should have been replaced by underscores
"subregion_aa",
"subregion_ab",
"subregion_ba",
}
got, err := s.AllSubregions()
if err != nil {
t.Errorf("got error %q", err)
}
if !cmp.Equal(got, want) {
t.Errorf("got, %q, want %q", got, want)
}
}
func TestErrorHandling(t *testing.T) {
t.Run("key doesn't exist", func(t *testing.T) {
s := newMiniRedisServer()
defer s.Close()
storage := newStorage(s)
_, err := storage.GetBySubregion("::doesnt-exist::")
if err == nil {
t.Error("expected error, but got no error instead")
}
if err != ErrNotFound {
t.Errorf("expected custom error, got %q instead", err.Error())
}
})
t.Run("cannot connect to redis server", func(t *testing.T) {
_, err := NewRedisStorage("99.99.99.99:6379", "", "", 1*time.Millisecond)
if err == nil {
t.Error("expected error, got nothing")
} else if err != ErrCouldNotConnectToStorage {
t.Errorf("got unexpected error: %q", err)
}
})
}