-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_test.go
More file actions
622 lines (530 loc) · 14.3 KB
/
store_test.go
File metadata and controls
622 lines (530 loc) · 14.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package gofeat_test
import (
"context"
"errors"
"testing"
"time"
"github.com/w0rng/gofeat"
)
func TestNew_Validation(t *testing.T) {
tests := []struct {
name string
config gofeat.Config
wantErr bool
}{
{
name: "valid config",
config: gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
},
wantErr: false,
},
{
name: "no features",
config: gofeat.Config{},
wantErr: true,
},
{
name: "empty feature name",
config: gofeat.Config{
Features: []gofeat.Feature{
{Name: "", Aggregate: gofeat.Count},
},
},
wantErr: true,
},
{
name: "nil aggregate",
config: gofeat.Config{
Features: []gofeat.Feature{
{Name: "count"},
},
},
wantErr: true,
},
{
name: "nil window defaults to Lifetime",
config: gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count, Window: nil},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := gofeat.New(tt.config)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestStore_PushGet(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
{Name: "sum", Aggregate: gofeat.Sum("amount")},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Now().UTC()
err = store.Push(ctx, "user1",
gofeat.Event{Timestamp: now.Add(-1 * time.Second), Data: map[string]any{"amount": 200.0}},
gofeat.Event{Timestamp: now, Data: map[string]any{"amount": 100.0}},
)
if err != nil {
t.Fatalf("Push failed: %v", err)
}
result, err := store.Get(ctx, "user1")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
count, err := result.Int("count")
if err != nil {
t.Fatalf("Int failed: %v", err)
}
if count != 2 {
t.Errorf("count: got %d, want 2", count)
}
sum, err := result.Float("sum")
if err != nil {
t.Fatalf("Float failed: %v", err)
}
if sum != 300.0 {
t.Errorf("sum: got %f, want 300.0", sum)
}
}
func TestStore_GetAt_PointInTime(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count, Window: gofeat.Sliding(1 * time.Hour)},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
// Push events at different times
err = store.Push(ctx, "user1",
gofeat.Event{Timestamp: now.Add(-2 * time.Hour), Data: nil}, // too old
gofeat.Event{Timestamp: now.Add(-30 * time.Minute), Data: nil},
gofeat.Event{Timestamp: now, Data: nil},
gofeat.Event{Timestamp: now.Add(1 * time.Hour), Data: nil}, // in the future
)
if err != nil {
t.Fatalf("Push failed: %v", err)
}
// Query at "now" should exclude future events
result, err := store.GetAt(ctx, "user1", now)
if err != nil {
t.Fatalf("GetAt failed: %v", err)
}
count := result.IntOr("count", 0)
if count != 2 {
t.Errorf("count at now: got %d, want 2 (excludes future event)", count)
}
// Query at past time
pastTime := now.Add(-1 * time.Hour)
result, err = store.GetAt(ctx, "user1", pastTime)
if err != nil {
t.Fatalf("GetAt failed: %v", err)
}
count = result.IntOr("count", 0)
if count != 1 {
t.Errorf("count at past: got %d, want 1", count)
}
}
func TestStore_TTL(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
TTL: 1 * time.Hour,
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
err = store.Push(ctx, "user1",
gofeat.Event{Timestamp: now.Add(-2 * time.Hour), Data: nil},
gofeat.Event{Timestamp: now.Add(-30 * time.Minute), Data: nil},
gofeat.Event{Timestamp: now, Data: nil},
)
if err != nil {
t.Fatalf("Push failed: %v", err)
}
// TTL should filter out events older than 1 hour
result, err := store.GetAt(ctx, "user1", now)
if err != nil {
t.Fatalf("GetAt failed: %v", err)
}
count := result.IntOr("count", 0)
if count != 2 {
t.Errorf("count with TTL: got %d, want 2 (2h old event excluded)", count)
}
}
func TestStore_Evict(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
TTL: 1 * time.Hour,
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Now().UTC()
// Push old events
err = store.Push(ctx, "user1",
gofeat.Event{Timestamp: now.Add(-2 * time.Hour), Data: nil},
gofeat.Event{Timestamp: now, Data: nil},
)
if err != nil {
t.Fatalf("Push failed: %v", err)
}
// Evict old events
err = store.Evict(ctx)
if err != nil {
t.Fatalf("Evict failed: %v", err)
}
// Verify old events are gone
stats, err := store.Stats(ctx)
if err != nil {
t.Fatalf("Stats failed: %v", err)
}
if stats.TotalEvents != 1 {
t.Errorf("after evict: got %d events, want 1", stats.TotalEvents)
}
}
func TestStore_Evict_NoTTL(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
err = store.Evict(ctx)
if err != nil {
t.Errorf("Evict with no TTL should not error: %v", err)
}
}
func TestStore_BatchGet(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Now().UTC()
// Push events for multiple users
for i := 1; i <= 3; i++ {
entityID := "user" + string(rune('0'+i))
events := make([]gofeat.Event, i)
for j := 0; j < i; j++ {
events[j] = gofeat.Event{Timestamp: now, Data: nil}
}
err = store.Push(ctx, entityID, events...)
if err != nil {
t.Fatalf("Push failed for %s: %v", entityID, err)
}
}
// Batch get
results, err := store.BatchGet(ctx, "user1", "user2", "user3")
if err != nil {
t.Fatalf("BatchGet failed: %v", err)
}
if len(results) != 3 {
t.Fatalf("BatchGet: got %d results, want 3", len(results))
}
for i := 1; i <= 3; i++ {
entityID := "user" + string(rune('0'+i))
result, ok := results[entityID]
if !ok {
t.Errorf("BatchGet: missing result for %s", entityID)
continue
}
count := result.IntOr("count", -1)
if count != i {
t.Errorf("%s count: got %d, want %d", entityID, count, i)
}
}
}
func TestStore_BatchGetAt(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
// Push events before and after "now"
err = store.Push(ctx, "user1",
gofeat.Event{Timestamp: now.Add(-1 * time.Hour), Data: nil},
gofeat.Event{Timestamp: now.Add(1 * time.Hour), Data: nil},
)
if err != nil {
t.Fatalf("Push failed: %v", err)
}
results, err := store.BatchGetAt(ctx, now, "user1")
if err != nil {
t.Fatalf("BatchGetAt failed: %v", err)
}
count := results["user1"].IntOr("count", -1)
if count != 1 {
t.Errorf("count: got %d, want 1 (future event excluded)", count)
}
}
func TestStore_ValidateUTC(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
// Non-UTC timestamp should be rejected
local := time.FixedZone("Local", 3600)
event := gofeat.Event{
Timestamp: time.Now().In(local),
Data: nil,
}
err = store.Push(ctx, "user1", event)
if err == nil {
t.Error("Push should reject non-UTC timestamp")
}
}
// TestStore_ContextCancellation removed - we simplified the code by removing upfront ctx.Err() checks
// Context cancellation is still respected by the underlying storage operations if they check context
func TestStore_Stats(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Now().UTC()
stats, err := store.Stats(ctx)
if err != nil {
t.Fatalf("Stats failed: %v", err)
}
if stats.Entities != 0 || stats.TotalEvents != 0 {
t.Errorf("empty store: got %+v, want zeros", stats)
}
store.Push(ctx, "user1", gofeat.Event{Timestamp: now}, gofeat.Event{Timestamp: now})
store.Push(ctx, "user2", gofeat.Event{Timestamp: now})
stats, err = store.Stats(ctx)
if err != nil {
t.Fatalf("Stats failed: %v", err)
}
if stats.Entities != 2 {
t.Errorf("entities: got %d, want 2", stats.Entities)
}
if stats.TotalEvents != 3 {
t.Errorf("total events: got %d, want 3", stats.TotalEvents)
}
}
func TestStore_CustomStorage(t *testing.T) {
mockStorage := &mockStorage{events: make(map[string][]gofeat.Event)}
store, err := gofeat.New(gofeat.Config{
Storage: mockStorage,
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Now().UTC()
err = store.Push(ctx, "user1", gofeat.Event{Timestamp: now})
if err != nil {
t.Fatalf("Push failed: %v", err)
}
if !mockStorage.pushCalled {
t.Error("custom storage Push was not called")
}
_, err = store.Get(ctx, "user1")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if !mockStorage.getCalled {
t.Error("custom storage Get was not called")
}
}
func TestStore_MultipleFeatures(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
{Name: "sum", Aggregate: gofeat.Sum("amount")},
{Name: "min", Aggregate: gofeat.Min("amount")},
{Name: "max", Aggregate: gofeat.Max("amount")},
{Name: "last_country", Aggregate: gofeat.Last("country")},
{Name: "distinct_countries", Aggregate: gofeat.DistinctCount("country")},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
defer store.Close()
ctx := context.Background()
now := time.Now().UTC()
err = store.Push(ctx, "user1",
gofeat.Event{Timestamp: now.Add(-3 * time.Second), Data: map[string]any{"amount": 100.0, "country": "US"}},
gofeat.Event{Timestamp: now.Add(-2 * time.Second), Data: map[string]any{"amount": 50.0, "country": "CA"}},
gofeat.Event{Timestamp: now.Add(-1 * time.Second), Data: map[string]any{"amount": 200.0, "country": "US"}},
)
if err != nil {
t.Fatalf("Push failed: %v", err)
}
result, err := store.Get(ctx, "user1")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if count := result.IntOr("count", 0); count != 3 {
t.Errorf("count: got %d, want 3", count)
}
if sum := result.FloatOr("sum", 0); sum != 350.0 {
t.Errorf("sum: got %f, want 350.0", sum)
}
if minF := result.FloatOr("min", 0); minF != 50.0 {
t.Errorf("min: got %f, want 50.0", minF)
}
if maxF := result.FloatOr("max", 0); maxF != 200.0 {
t.Errorf("max: got %f, want 200.0", maxF)
}
if last := result.StringOr("last_country", ""); last != "US" {
t.Errorf("last_country: got %s, want US", last)
}
if distinct := result.IntOr("distinct_countries", 0); distinct != 2 {
t.Errorf("distinct_countries: got %d, want 2", distinct)
}
}
// Mock storage for testing custom storage backends.
type mockStorage struct {
events map[string][]gofeat.Event
pushCalled bool
getCalled bool
evictCalled bool
}
func (m *mockStorage) Push(_ context.Context, entityID string, events ...gofeat.Event) error {
m.pushCalled = true
m.events[entityID] = append(m.events[entityID], events...)
return nil
}
func (m *mockStorage) Get(_ context.Context, entityID string, at time.Time) ([]gofeat.Event, error) {
m.getCalled = true
// Simple mock: return all events up to 'at'
var result []gofeat.Event
for _, e := range m.events[entityID] {
if !e.Timestamp.After(at) {
result = append(result, e)
}
}
return result, nil
}
func (m *mockStorage) Evict(_ context.Context) error {
m.evictCalled = true
return nil
}
func (m *mockStorage) Stats(_ context.Context) (gofeat.StorageStats, error) {
total := int64(0)
for _, events := range m.events {
total += int64(len(events))
}
return gofeat.StorageStats{
Entities: len(m.events),
TotalEvents: total,
}, nil
}
func (m *mockStorage) Close() error {
return nil
}
// Error storage for testing error handling.
type errorStorage struct{}
func (e *errorStorage) Push(_ context.Context, _ string, _ ...gofeat.Event) error {
return errors.New("storage error")
}
func (e *errorStorage) Get(_ context.Context, _ string, _ time.Time) ([]gofeat.Event, error) {
return nil, errors.New("storage error")
}
func (e *errorStorage) Evict(_ context.Context) error {
return errors.New("storage error")
}
func (e *errorStorage) Stats(_ context.Context) (gofeat.StorageStats, error) {
return gofeat.StorageStats{}, errors.New("storage error")
}
func (e *errorStorage) Close() error {
return errors.New("storage error")
}
func TestStore_StorageErrors(t *testing.T) {
store, err := gofeat.New(gofeat.Config{
TTL: 1 * time.Hour, // Need TTL for Evict to call storage
Storage: &errorStorage{},
Features: []gofeat.Feature{
{Name: "count", Aggregate: gofeat.Count},
},
})
if err != nil {
t.Fatalf("New failed: %v", err)
}
ctx := context.Background()
now := time.Now().UTC()
err = store.Push(ctx, "user1", gofeat.Event{Timestamp: now})
if err == nil {
t.Error("Push should propagate storage error")
}
_, err = store.Get(ctx, "user1")
if err == nil {
t.Error("Get should propagate storage error")
}
_, err = store.GetAt(ctx, "user1", now)
if err == nil {
t.Error("GetAt should propagate storage error")
}
err = store.Evict(ctx)
if err == nil {
t.Error("Evict should propagate storage error")
}
_, err = store.Stats(ctx)
if err == nil {
t.Error("Stats should propagate storage error")
}
err = store.Close()
if err == nil {
t.Error("Close should propagate storage error")
}
}