-
Notifications
You must be signed in to change notification settings - Fork 18
/
event_summarizer_test.go
107 lines (92 loc) · 3.84 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
107
package ldclient
import (
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/launchdarkly/go-sdk-common.v1/ldvalue"
)
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 := newSuccessfulEvalEvent(&flag, user, nil, ldvalue.Null(), ldvalue.Null(), nil, false, nil)
event2 := newSuccessfulEvalEvent(&flag, user, nil, ldvalue.Null(), ldvalue.Null(), nil, false, nil)
event3 := newSuccessfulEvalEvent(&flag, user, nil, ldvalue.Null(), ldvalue.Null(), nil, false, 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 := newSuccessfulEvalEvent(&flag1, user, &variation1, ldvalue.String("value1"), ldvalue.String("default1"), nil, false, nil)
event2 := newSuccessfulEvalEvent(&flag1, user, &variation2, ldvalue.String("value2"), ldvalue.String("default1"), nil, false, nil)
event3 := newSuccessfulEvalEvent(&flag2, user, &variation1, ldvalue.String("value99"), ldvalue.String("default2"), nil, false, nil)
event4 := newSuccessfulEvalEvent(&flag1, user, &variation1, ldvalue.String("value1"), ldvalue.String("default1"), nil, false, nil)
event5 := newUnknownFlagEvent(unknownFlagKey, user, ldvalue.String("default3"), nil, false)
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 := newSuccessfulEvalEvent(&flag, user, &variation1, ldvalue.String("value1"), ldvalue.String("default1"), nil, false, nil)
event2 := newSuccessfulEvalEvent(&flag, user, &variation2, ldvalue.String("value2"), ldvalue.String("default1"), nil, false, nil)
event3 := newSuccessfulEvalEvent(&flag, user, nil, ldvalue.String("default1"), ldvalue.String("default1"), nil, false, 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)
}