-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
mock.go
143 lines (121 loc) · 2.83 KB
/
mock.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
package mixpanel
import (
"errors"
"fmt"
"sync"
"time"
)
// Mock Mixpanel client which can be used in unit tests.
type Mock struct {
// All people identified, mapped by distinctId
people sync.Map
}
func NewMock() *Mock {
return &Mock{
people: sync.Map{},
}
}
func (m *Mock) String() string {
str := ""
m.people.Range(func(id, p interface{}) bool {
str += id.(string) + ":\n" + p.(*MockPeople).String()
return true
})
return str
}
func (m *Mock) Reset() {
m.people = sync.Map{}
}
// People identifies a user. The user will be added to the people map.
func (m *Mock) People(distinctId string) *MockPeople {
p, _ := m.people.LoadOrStore(distinctId, &MockPeople{
Properties: map[string]interface{}{},
})
return p.(*MockPeople)
}
func (m *Mock) Track(distinctId, eventName string, e *Event) error {
p := m.People(distinctId)
p.Events = append(p.Events, MockEvent{
Event: *e,
Name: eventName,
})
return nil
}
func (m *Mock) Import(distinctId, eventName string, e *Event) error {
p := m.People(distinctId)
p.Events = append(p.Events, MockEvent{
Event: *e,
Name: eventName,
})
return nil
}
type MockPeople struct {
Properties map[string]interface{}
Time *time.Time
IP string
Events []MockEvent
}
func (mp *MockPeople) String() string {
timeStr := ""
if mp.Time != nil {
timeStr = mp.Time.Format(time.RFC3339)
}
str := fmt.Sprintf(" ip: %s\n time: %s\n", mp.IP, timeStr)
str += " properties:\n"
for key, val := range mp.Properties {
str += fmt.Sprintf(" %s: %v\n", key, val)
}
str += " events:\n"
for _, event := range mp.Events {
str += " " + event.Name + ":\n"
str += fmt.Sprintf(" IP: %s\n", event.IP)
if event.Timestamp != nil {
str += fmt.Sprintf(
" Timestamp: %s\n", event.Timestamp.Format(time.RFC3339),
)
} else {
str += " Timestamp:\n"
}
for key, val := range event.Properties {
str += fmt.Sprintf(" %s: %v\n", key, val)
}
}
return str
}
func (m *Mock) Update(distinctId string, u *Update) error {
return m.UpdateUser(distinctId, u)
}
func (m *Mock) UpdateUser(distinctId string, u *Update) error {
p := m.People(distinctId)
if u.IP != "" {
p.IP = u.IP
}
if u.Timestamp != nil && u.Timestamp != IgnoreTime {
p.Time = u.Timestamp
}
switch u.Operation {
case "$set", "$set_once":
for key, val := range u.Properties {
p.Properties[key] = val
}
default:
return errors.New("mixpanel.Mock only supports the $set and $set_once operations")
}
return nil
}
func (m *Mock) UnionUser(userID string, u *Update) error {
return nil
}
func (m *Mock) UpdateGroup(groupKey, groupUser string, u *Update) error {
return nil
}
func (m *Mock) UnionGroup(groupKey, groupUser string, u *Update) error {
return nil
}
func (m *Mock) Alias(distinctId, newId string) error {
return nil
}
type MockEvent struct {
Event
Name string
}