-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
mixpanel_test.go
158 lines (125 loc) · 3.88 KB
/
mixpanel_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
157
158
package mixpanel
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
)
type Request struct {
*http.Request
DecodedBody string
}
var (
ts *httptest.Server
client Mixpanel
LastRequest *Request
)
func setup() {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("1\n"))
body, _ := io.ReadAll(r.Body)
LastRequest = &Request{
r,
decodeData(string(body)),
}
}))
client = NewWithSecret("e3bc4100330c35722740fb8c6f5abddc", "mysecret", ts.URL)
}
func teardown() {
ts.Close()
}
func decodeData(url string) string {
data := strings.Split(url, "data=")[1]
decoded, _ := base64.StdEncoding.DecodeString(data)
return string(decoded[:])
}
func assert(t *testing.T, desc string, current interface{}, wanted interface{}) {
if !reflect.DeepEqual(current, wanted) {
t.Errorf("%s returned %+v, want %+v", desc,
current, wanted)
}
}
func TestTrack(t *testing.T) {
setup()
defer teardown()
client.Track("13793", "Signed Up", &Event{
Properties: map[string]interface{}{
"Referred By": "Friend",
},
})
wantedBody := "{\"event\":\"Signed Up\",\"properties\":{\"Referred By\":\"Friend\",\"distinct_id\":\"13793\",\"token\":\"e3bc4100330c35722740fb8c6f5abddc\"}}"
assert(t, "body", LastRequest.DecodedBody, wantedBody)
assert(t, "path", LastRequest.URL.Path, "/track")
}
func TestImport(t *testing.T) {
setup()
defer teardown()
importTime := time.Now().Add(-5 * 24 * time.Hour)
client.Import("13793", "Signed Up", &Event{
Properties: map[string]interface{}{
"Referred By": "Friend",
},
Timestamp: &importTime,
})
wantedBody := fmt.Sprintf("{\"event\":\"Signed Up\",\"properties\":{\"Referred By\":\"Friend\",\"distinct_id\":\"13793\",\"time\":%d,\"token\":\"e3bc4100330c35722740fb8c6f5abddc\"}}", importTime.Unix())
assert(t, "body", LastRequest.DecodedBody, wantedBody)
assert(t, "path", LastRequest.URL.Path, "/import")
}
func TestGroupOperations(t *testing.T) {
setup()
defer teardown()
client.UpdateGroup("company_id", "11", &Update{
Operation: "$set",
Properties: map[string]interface{}{
"Address": "1313 Mockingbird Lane",
"Birthday": "1948-01-01",
},
})
wantedBody := "{\"$group_id\":\"11\",\"$group_key\":\"company_id\",\"$set\":{\"Address\":\"1313 Mockingbird Lane\",\"Birthday\":\"1948-01-01\"},\"$token\":\"e3bc4100330c35722740fb8c6f5abddc\"}"
assert(t, "body", LastRequest.DecodedBody, wantedBody)
assert(t, "path", LastRequest.URL.Path, "/groups")
}
func TestUpdateUser(t *testing.T) {
setup()
defer teardown()
client.UpdateUser("13793", &Update{
Operation: "$set",
Properties: map[string]interface{}{
"Address": "1313 Mockingbird Lane",
"Birthday": "1948-01-01",
},
})
wantedBody := "{\"$distinct_id\":\"13793\",\"$set\":{\"Address\":\"1313 Mockingbird Lane\",\"Birthday\":\"1948-01-01\"},\"$token\":\"e3bc4100330c35722740fb8c6f5abddc\"}"
assert(t, "body", LastRequest.DecodedBody, wantedBody)
assert(t, "path", LastRequest.URL.Path, "/engage")
}
func TestError(t *testing.T) {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(`{"error": "some error", "status": 0}`))
LastRequest = &Request{r, ""}
}))
assertErrTrackFailed := func(err error) {
merr, ok := err.(*MixpanelError)
if !ok {
t.Errorf("Error should be wrapped in a MixpanelError: %v", err)
return
}
terr, ok := merr.Err.(*ErrTrackFailed)
if !ok {
t.Errorf("Error should be a *ErrTrackFailed: %v", err)
return
}
assert(t, "error", "error=some error; status=0; httpCode=200", terr.Message)
}
client = New("e3bc4100330c35722740fb8c6f5abddc", ts.URL)
assertErrTrackFailed(client.UpdateUser("1", &Update{}))
assertErrTrackFailed(client.Track("1", "name", &Event{}))
assertErrTrackFailed(client.Import("1", "name", &Event{}))
}