forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
96 lines (84 loc) · 2.83 KB
/
logger_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
package statsig
import (
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"testing"
"time"
)
func TestLog(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {}))
defer testServer.Close()
opt := &Options{
API: testServer.URL,
}
transport := newTransport("secret", opt)
errorBoundary := newErrorBoundary("secret", opt, nil)
logger := newLogger(transport, opt, nil, errorBoundary)
user := User{
UserID: "123",
Email: "123@gmail.com",
PrivateAttributes: map[string]interface{}{"private": "shh"},
}
privateUser := User{
UserID: "123",
Email: "123@gmail.com",
}
nowSecond := time.Now().Unix()
// Test custom logs
customEvent := Event{
EventName: "test_event",
User: user, Value: "3"}
logger.logCustom(customEvent)
evt1, ok := logger.events[0].(Event)
if !ok {
t.Errorf("Custom event type incorrect.")
}
customEventNoPrivate := Event{
EventName: "test_event",
User: privateUser, Value: "3",
Time: evt1.Time,
}
if !reflect.DeepEqual(evt1, customEventNoPrivate) {
t.Errorf("Custom event not logged correctly.")
}
if evt1.Time/1000 < nowSecond-2 || evt1.Time/1000 > nowSecond+2 {
t.Errorf("Custom event time not set correctly.")
}
// Test gate exposures
exposures := []SecondaryExposure{{Gate: "another_gate", GateValue: "true", RuleID: "default"}}
logger.logGateExposure(user, "test_gate", true, "rule_id", exposures, nil, nil)
evt2, ok := logger.events[1].(ExposureEvent)
if !ok {
t.Errorf("Gate exposure event type incorrect.")
}
gateExposureEvent := ExposureEvent{EventName: GateExposureEventName, User: privateUser, Metadata: map[string]string{
"gate": "test_gate",
"gateValue": strconv.FormatBool(true),
"ruleID": "rule_id",
}, SecondaryExposures: exposures, Time: evt2.Time}
if !reflect.DeepEqual(evt2, gateExposureEvent) {
t.Errorf("Gate exposure not logged correctly.")
}
if evt2.Time/1000 < nowSecond-2 || evt2.Time/1000 > nowSecond+2 {
t.Errorf("Gate exposure event time not set correctly.")
}
// Test config exposures
exposures = append(exposures, SecondaryExposure{Gate: "yet_another_gate", GateValue: "false", RuleID: ""})
logger.logConfigExposure(user, "test_config", "rule_id_config", exposures, nil, nil)
evt3, ok := logger.events[2].(ExposureEvent)
if !ok {
t.Errorf("Config exposure event type incorrect.")
}
configExposureEvent := ExposureEvent{EventName: ConfigExposureEventName, User: privateUser, Metadata: map[string]string{
"config": "test_config",
"ruleID": "rule_id_config",
}, SecondaryExposures: exposures, Time: evt3.Time}
if !reflect.DeepEqual(evt3, configExposureEvent) {
t.Errorf("Config exposure not logged correctly.")
}
if evt3.Time/1000 < nowSecond-2 || evt3.Time/1000 > nowSecond+2 {
t.Errorf("Config exposure event time not set correctly.")
}
}