-
Notifications
You must be signed in to change notification settings - Fork 3
/
segment_test.go
194 lines (153 loc) · 4.73 KB
/
segment_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package lytics
import (
"fmt"
"testing"
"github.com/bmizerany/assert"
"github.com/jarcoal/httpmock"
"github.com/lytics/go-lytics/mock"
)
func TestGetSegments(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
// user segment
client := NewLytics(mock.MockApiKey, nil)
segs, err := client.GetSegments("user")
assert.Equal(t, err, nil)
assert.T(t, len(segs) > 1)
assert.Equal(t, segs[0].Table, "user")
// content segment
segs, err = client.GetSegments("content")
assert.Equal(t, err, nil)
assert.T(t, len(segs) > 1)
assert.Equal(t, segs[0].Table, "content")
}
func TestGetSegment(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
client := NewLytics(mock.MockApiKey, nil)
seg, err := client.GetSegment(mock.MockSegmentID1)
assert.Equal(t, err, nil)
assert.T(t, seg.Id == mock.MockSegmentID1)
}
func TestGetSegmentSize(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
client := NewLytics(mock.MockApiKey, nil)
seg, err := client.GetSegmentSize(mock.MockSegmentID1)
assert.Equal(t, err, nil)
assert.T(t, seg.Id == mock.MockSegmentID1)
}
func TestGetSegmentSizes(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
var segments []string
client := NewLytics(mock.MockApiKey, nil)
segments = []string{
mock.MockSegmentID1,
}
seg, err := client.GetSegmentSizes(segments)
assert.Equal(t, err, nil)
assert.T(t, seg[0].Id == segments[0])
segments = []string{
mock.MockSegmentID1,
mock.MockSegmentID2,
}
// params
seg, err = client.GetSegmentSizes(segments)
assert.Equal(t, err, nil)
assert.T(t, len(seg) == 2)
}
func TestGetSegmentAttribution(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
segments := []string{
mock.MockSegmentID1,
mock.MockSegmentID2,
}
client := NewLytics(mock.MockApiKey, nil)
attr, err := client.GetSegmentAttribution(segments)
assert.Equal(t, err, nil)
assert.T(t, len(attr[0].Metrics) == 5)
assert.T(t, len(attr[1].Metrics) == 5)
}
func TestGetSegmentCollection(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
client := NewLytics(mock.MockApiKey, nil)
collection, err := client.GetSegmentCollection(mock.MockSegmentCollection)
assert.Equal(t, err, nil)
assert.Equal(t, collection.Slug, "goal_funnel")
assert.T(t, len(collection.Collection) == 2)
assert.Equal(t, collection.Collection[0].Id, mock.MockSegmentID1)
assert.Equal(t, collection.Collection[1].Id, mock.MockSegmentID2)
}
func TestGetSegmentCollectionList(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
client := NewLytics(mock.MockApiKey, nil)
collections, err := client.GetSegmentCollectionList()
assert.Equal(t, err, nil)
assert.T(t, len(collections) == 2)
assert.Equal(t, collections[0].Slug, "goal_funnel")
assert.T(t, len(collections[0].Collection) == 2)
assert.Equal(t, collections[1].Slug, "mobile")
assert.T(t, len(collections[1].Collection) == 3)
}
func TestSegmentPager(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
var (
completed bool
countEntities int
)
client := NewLytics(mock.MockApiKey, nil)
// start the paging routine
scan := client.PageSegmentId(mock.MockSegmentID1)
assert.Equal(t, nil, scan.Err())
// handle processing the entities
for {
e := scan.Next()
if e == nil {
completed = true
break
}
countEntities++
assert.Equal(t, e.Fields["email"], fmt.Sprintf("email%[email protected]", countEntities))
}
assert.Equal(t, countEntities, 13)
assert.Equal(t, completed, true)
t.Logf("*** COMPLETED SCAN: %d total entities", scan.Total)
}
func TestCreateSegment(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
client := NewLytics(mock.MockApiKey, nil)
ql := "FILTER AND (created >= \"now-30d\", aspects = \"articles\") FROM content"
seg, err := client.CreateSegment("Recent Articles", ql, "recent_articles")
assert.Equal(t, err, nil)
assert.T(t, seg.Id != "")
assert.Equal(t, seg.FilterQL, ql)
seg, err = client.CreateSegment("Invalid Segment", "", "invalid")
assert.NotEqual(t, err, nil)
}
func TestValidateSegment(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mock.RegisterSegmentMocks()
client := NewLytics(mock.MockApiKey, nil)
valid, err := client.ValidateSegment("FILTER AND (created >= \"now-30d\", aspects = \"articles\") FROM content")
assert.Equal(t, err, nil)
assert.T(t, valid)
valid, err = client.ValidateSegment("")
assert.NotEqual(t, err, nil)
assert.T(t, !valid)
}