-
Notifications
You must be signed in to change notification settings - Fork 10
/
layer_exposure_test.go
156 lines (123 loc) · 3.73 KB
/
layer_exposure_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
package statsig
import (
"testing"
)
func TestLayerExposure(t *testing.T) {
events := []Event{}
testServer := getTestServer(testServerOptions{
onLogEvent: func(newEvents []map[string]interface{}) {
for _, newEvent := range newEvents {
eventTyped := convertToExposureEvent(newEvent)
events = append(events, eventTyped)
}
},
isLayerExposure: true,
})
opt := &Options{
API: testServer.URL,
Environment: Environment{Tier: "test"},
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
user := User{UserID: "some_user_id"}
start := func() {
events = []Event{}
InitializeWithOptions("secret-key", opt)
}
//
t.Run("does not log on getLayer", func(t *testing.T) {
start()
GetLayer(user, "unallocated_layer")
ShutdownAndDangerouslyClearInstance()
if len(events) != 0 {
t.Errorf("Should receive exactly one log_event")
}
})
//
t.Run("does not log on non existent keys", func(t *testing.T) {
start()
layer := GetLayer(user, "unallocated_layer")
layer.GetString("a_string", "err")
ShutdownAndDangerouslyClearInstance()
if len(events) != 0 {
t.Errorf("Should receive exactly one log_event")
}
})
//
t.Run("does not log on invalid types", func(t *testing.T) {
start()
layer := GetLayer(user, "unallocated_layer")
layer.GetString("an_int", "err")
layer.GetBool("an_int", false)
layer.GetSlice("an_int", make([]interface{}, 0))
ShutdownAndDangerouslyClearInstance()
if len(events) != 0 {
t.Errorf("Should receive exactly one log_event")
}
})
//
t.Run("unallocated layer logging", func(t *testing.T) {
start()
layer := GetLayer(user, "unallocated_layer")
layer.GetNumber("an_int", 0)
ShutdownAndDangerouslyClearInstance()
if len(events) != 1 {
t.Errorf("Should receive exactly one log_event")
}
compareMetadata(t, events[0].Metadata, map[string]string{
"config": "unallocated_layer",
"ruleID": "default",
"allocatedExperiment": "",
"parameterName": "an_int",
"isExplicitParameter": "false",
"reason": "Network",
}, 1631638014811)
})
//
t.Run("explicit vs implicit parameter logging", func(t *testing.T) {
start()
layer := GetLayer(user, "explicit_vs_implicit_parameter_layer")
layer.GetNumber("an_int", 0)
layer.GetString("a_string", "err")
ShutdownAndDangerouslyClearInstance()
if len(events) != 2 {
t.Errorf("Should receive exactly two log_events")
}
compareMetadata(t, events[0].Metadata, map[string]string{
"config": "explicit_vs_implicit_parameter_layer",
"ruleID": "alwaysPass",
"allocatedExperiment": "experiment",
"parameterName": "an_int",
"isExplicitParameter": "true",
"reason": "Network",
}, 1631638014811)
compareMetadata(t, events[1].Metadata, map[string]string{
"config": "explicit_vs_implicit_parameter_layer",
"ruleID": "alwaysPass",
"allocatedExperiment": "",
"parameterName": "a_string",
"isExplicitParameter": "false",
"reason": "Network",
}, 1631638014811)
})
//
t.Run("logs user and event name", func(t *testing.T) {
start()
layer := GetLayer(User{UserID: "dloomb", Email: "[email protected]"}, "unallocated_layer")
layer.GetNumber("an_int", 0)
ShutdownAndDangerouslyClearInstance()
if len(events) != 1 {
t.Errorf("Should receive exactly one log_event")
}
if events[0].EventName != "statsig::layer_exposure" {
t.Errorf("Incorrect exposure name")
}
if events[0].User.UserID != "dloomb" {
t.Errorf("Invalid user ID in log")
}
if events[0].User.Email != "[email protected]" {
t.Errorf("Invalid email in log")
}
})
defer testServer.Close()
}