-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore_test.go
335 lines (302 loc) · 7.3 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"time"
)
func openTempStore(t *testing.T) *Store {
dir, err := ioutil.TempDir("", "apec-")
if err != nil {
t.Fatalf("could not create store temporary directory: %s", err)
}
path := filepath.Join(dir, "store")
store, err := OpenStore(path)
if err != nil {
t.Fatalf("could not open store on %s: %s", path, err)
}
return store
}
func closeAndDeleteStore(t *testing.T, store *Store) {
err := store.Close()
if err != nil {
t.Fatalf("could not close store on %s: %s", store.Path(), err)
}
}
func TestOfferDeletion(t *testing.T) {
store := openTempStore(t)
defer closeAndDeleteStore(t, store)
now := time.Now()
data := []byte("dummy")
id := "id1"
// Delete missing offer
_, err := store.Delete(id, now)
if err != nil {
t.Fatalf("error while deleted missing entry: %s", err)
}
// Add something, deleted it and check the records
err = store.Put(id, data)
if err != nil {
t.Fatalf("could not write entry: %s", err)
}
deletedId, err := store.Delete(id, now)
if err != nil {
t.Fatalf("could not deleted created entry: %s", err)
}
data2, err := store.Get(id)
if err != nil {
t.Fatalf("error while getting missing entry: %s", err)
}
if data2 != nil {
t.Fatalf("deleted data is still available: %v", data2)
}
deletedIds, err := store.ListDeletedIds()
if err != nil {
t.Fatalf("could not list deleted ids: %s", err)
}
if len(deletedIds) == 0 || deletedIds[0] != id {
t.Fatalf("unexpected deleted ids: %v", deletedIds)
}
deletedOffers, err := store.ListDeletedOffers(id)
if err != nil {
t.Fatalf("could not list deleted offers for %s: %s", id, err)
}
if len(deletedOffers) == 0 {
t.Fatalf("deleted data was not recorded")
}
if deletedOffers[0].Id != deletedId {
t.Fatalf("expected deleted id %d, got %d", deletedId, deletedOffers[0].Id)
}
deletedData, err := store.GetDeleted(deletedOffers[0].Id)
if err != nil {
t.Fatalf("could not get deleted data: %s", err)
}
if !bytes.Equal(deletedData, data) {
t.Fatalf("deleted data does not match data: %x != %x", deletedData, data)
}
// List missing deleted offers
deletedOffers, err = store.ListDeletedOffers("missing")
if err != nil {
t.Fatal(err)
}
if len(deletedOffers) > 0 {
t.Fatalf("missing deleted offers returned non-empty list")
}
}
func TestOfferSize(t *testing.T) {
store := openTempStore(t)
defer closeAndDeleteStore(t, store)
now := time.Now()
data := []byte("dummy")
id := "id1"
size := store.Size()
if size != 0 {
t.Fatalf("empty store has %d items", size)
}
err := store.Put(id, data)
if err != nil {
t.Fatalf("could not write entry: %s", err)
}
size = store.Size()
if size != 1 {
t.Fatalf("store should have 1 element, got %d", size)
}
_, err = store.Delete(id, now)
if err != nil {
t.Fatalf("could not delete %s: %s", id, err)
}
size = store.Size()
if size != 0 {
t.Fatalf("empty store has %d items", size)
}
}
func TestOfferLocation(t *testing.T) {
store := openTempStore(t)
defer closeAndDeleteStore(t, store)
id := "1"
now := time.Now()
data := []byte("dummy")
loc := &Location{
City: "Paris",
}
err := store.PutLocation(id, loc, now)
if err == nil {
t.Fatalf("adding location to missing offers should have failed")
}
// Test non-nil location
err = store.Put(id, data)
if err != nil {
t.Fatal(err)
}
err = store.PutLocation(id, loc, now)
if err != nil {
t.Fatal(err)
}
loc2, date, err := store.GetLocation(id)
if err != nil {
t.Fatal(err)
}
if loc2 == nil || date.IsZero() {
t.Fatalf("unexpected nil location")
}
// Resetting the data should invalidate the location
err = store.Put(id, data)
if err != nil {
t.Fatal(err)
}
loc2, date, err = store.GetLocation(id)
if err != nil {
t.Fatal(err)
}
if loc2 != nil || !date.IsZero() {
t.Fatal("location should have been reset by Put()")
}
// Test empty location
err = store.PutLocation(id, nil, now)
if err != nil {
t.Fatal(err)
}
loc2, date, err = store.GetLocation(id)
if err != nil {
t.Fatal(err)
}
if loc2 != nil || date.IsZero() {
t.Fatal("could not retrieve empty location")
}
// Deleting an offer remove its location
_, err = store.Delete(id, now)
if err != nil {
t.Fatal(err)
}
loc2, date, err = store.GetLocation(id)
if err != nil {
t.Fatal(err)
}
if loc2 != nil || !date.IsZero() {
t.Fatal("location should have been removed by Delete()")
}
}
func TestOfferAge(t *testing.T) {
store := openTempStore(t)
defer closeAndDeleteStore(t, store)
baseDate, err := time.Parse("2006-01-02T15:04:05", "2016-01-01T13:00:00")
if err != nil {
t.Fatalf("could not parse base date: %s", err)
}
day := time.Hour * 24
// Missing date
d, err := store.GetInitialDate("o1")
if err != nil {
t.Fatalf("error fetching missing initial date: %s", err)
}
if !d.IsZero() {
t.Fatalf("missing date must be zero: %v", d)
}
putDate := func(hash string, date OfferAge, expected time.Time) error {
err = store.PutOfferDate(hash, date)
if err != nil {
return err
}
d, err = store.GetInitialDate(date.Id)
if err != nil {
return err
}
if d != expected {
return fmt.Errorf("got %v, expected %v", d, expected)
}
return nil
}
// Add one live date
a1_1 := OfferAge{
Id: "o1",
PublicationDate: baseDate,
}
err = putDate("h1", a1_1, baseDate)
if err != nil {
t.Fatal(err)
}
// Update it in the future
a1_1 = OfferAge{
Id: "o1",
PublicationDate: baseDate.Add(30 * day),
}
err = putDate("h1", a1_1, baseDate.Add(30*day))
if err != nil {
t.Fatal(err)
}
// Move live date in the past
a1_1 = OfferAge{
Id: "o1",
PublicationDate: baseDate.Add(29 * day),
}
err = putDate("h1", a1_1, baseDate.Add(29*day))
if err != nil {
t.Fatal(err)
}
// Add deleted date in tolerance range
a1_2 := OfferAge{
Id: "o1",
DeletedId: 1,
PublicationDate: baseDate.Add(24 * day),
DeletionDate: baseDate.Add(25 * day),
}
err = putDate("h1", a1_2, baseDate.Add(24*day))
if err != nil {
t.Fatal(err)
}
// Add it again with a different date, check deduplication
a1_2 = OfferAge{
Id: "o1",
DeletedId: 1,
PublicationDate: baseDate.Add(25 * day),
DeletionDate: baseDate.Add(26 * day),
}
err = putDate("h1", a1_2, baseDate.Add(25*day))
if err != nil {
t.Fatal(err)
}
// Add a deleted value out of range
a1_3 := OfferAge{
Id: "o1",
DeletedId: 3,
PublicationDate: baseDate.Add(3 * day),
DeletionDate: baseDate.Add(15 * day),
}
err = putDate("h1", a1_3, baseDate.Add(25*day))
if err != nil {
t.Fatal(err)
}
// Insert another one to link both ranges
a1_4 := OfferAge{
Id: "o1",
DeletedId: 4,
PublicationDate: baseDate.Add(16 * day),
DeletionDate: baseDate.Add(21 * day),
}
err = putDate("h1", a1_4, baseDate.Add(3*day))
if err != nil {
t.Fatal(err)
}
// Add live offer in the future, past tolerance
a1_5 := OfferAge{
Id: "o1",
PublicationDate: baseDate.Add(60 * day),
}
err = putDate("h1", a1_5, baseDate.Add(60*day))
if err != nil {
t.Fatal(err)
}
// Another deleted offer does not link with the previous ones
a1_6 := OfferAge{
Id: "o1",
DeletedId: 6,
PublicationDate: baseDate.Add(58 * day),
DeletionDate: baseDate.Add(59 * day),
}
err = putDate("h1", a1_6, baseDate.Add(58*day))
if err != nil {
t.Fatal(err)
}
}