This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
event_summarizer_test.go
106 lines (91 loc) · 3.49 KB
/
event_summarizer_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
package ldclient
import (
"testing"
"github.com/stretchr/testify/assert"
)
var user = NewUser("key")
func TestSummarizeEventDoesNothingForIdentifyEvent(t *testing.T) {
es := newEventSummarizer()
snapshot := es.snapshot()
event := NewIdentifyEvent(user)
es.summarizeEvent(event)
assert.Equal(t, snapshot, es.snapshot())
}
func TestSummarizeEventDoesNothingForCustomEvent(t *testing.T) {
es := newEventSummarizer()
snapshot := es.snapshot()
event := NewCustomEvent("whatever", user, nil)
es.summarizeEvent(event)
assert.Equal(t, snapshot, es.snapshot())
}
func TestSummarizeEventSetsStartAndEndDates(t *testing.T) {
es := newEventSummarizer()
flag := FeatureFlag{
Key: "key",
}
event1 := NewFeatureRequestEvent(flag.Key, &flag, user, nil, nil, nil, nil)
event2 := NewFeatureRequestEvent(flag.Key, &flag, user, nil, nil, nil, nil)
event3 := NewFeatureRequestEvent(flag.Key, &flag, user, nil, nil, nil, nil)
event1.BaseEvent.CreationDate = 2000
event2.BaseEvent.CreationDate = 1000
event3.BaseEvent.CreationDate = 1500
es.summarizeEvent(event1)
es.summarizeEvent(event2)
data := es.snapshot()
assert.Equal(t, uint64(1000), data.startDate)
assert.Equal(t, uint64(2000), data.endDate)
}
func TestSummarizeEventIncrementsCounters(t *testing.T) {
es := newEventSummarizer()
flag1 := FeatureFlag{
Key: "key1",
Version: 11,
}
flag2 := FeatureFlag{
Key: "key2",
Version: 22,
}
unknownFlagKey := "badkey"
variation1 := 1
variation2 := 2
event1 := NewFeatureRequestEvent(flag1.Key, &flag1, user, &variation1, "value1", "default1", nil)
event2 := NewFeatureRequestEvent(flag1.Key, &flag1, user, &variation2, "value2", "default1", nil)
event3 := NewFeatureRequestEvent(flag2.Key, &flag2, user, &variation1, "value99", "default2", nil)
event4 := NewFeatureRequestEvent(flag1.Key, &flag1, user, &variation1, "value1", "default1", nil)
event5 := NewFeatureRequestEvent(unknownFlagKey, nil, user, nil, "default3", "default3", nil)
es.summarizeEvent(event1)
es.summarizeEvent(event2)
es.summarizeEvent(event3)
es.summarizeEvent(event4)
es.summarizeEvent(event5)
data := es.snapshot()
expectedCounters := map[counterKey]*counterValue{
counterKey{flag1.Key, variation1, flag1.Version}: &counterValue{2, "value1", "default1"},
counterKey{flag1.Key, variation2, flag1.Version}: &counterValue{1, "value2", "default1"},
counterKey{flag2.Key, variation1, flag2.Version}: &counterValue{1, "value99", "default2"},
counterKey{unknownFlagKey, -1, 0}: &counterValue{1, "default3", "default3"},
}
assert.Equal(t, expectedCounters, data.counters)
}
func TestCounterForNilVariationIsDistinctFromOthers(t *testing.T) {
es := newEventSummarizer()
flag := FeatureFlag{
Key: "key1",
Version: 11,
}
variation1 := 1
variation2 := 2
event1 := NewFeatureRequestEvent(flag.Key, &flag, user, &variation1, "value1", "default1", nil)
event2 := NewFeatureRequestEvent(flag.Key, &flag, user, &variation2, "value2", "default1", nil)
event3 := NewFeatureRequestEvent(flag.Key, &flag, user, nil, "default1", "default1", nil)
es.summarizeEvent(event1)
es.summarizeEvent(event2)
es.summarizeEvent(event3)
data := es.snapshot()
expectedCounters := map[counterKey]*counterValue{
counterKey{flag.Key, variation1, flag.Version}: &counterValue{1, "value1", "default1"},
counterKey{flag.Key, variation2, flag.Version}: &counterValue{1, "value2", "default1"},
counterKey{flag.Key, -1, flag.Version}: &counterValue{1, "default1", "default1"},
}
assert.Equal(t, expectedCounters, data.counters)
}