-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinsights_test.go
More file actions
352 lines (316 loc) · 11.8 KB
/
insights_test.go
File metadata and controls
352 lines (316 loc) · 11.8 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package threads
import (
"context"
"testing"
"time"
)
func TestGetPostInsights_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{
"data": [
{"name": "views", "period": "lifetime", "values": [{"value": 100}]},
{"name": "likes", "period": "lifetime", "values": [{"value": 25}]}
]
}`))
resp, err := client.GetPostInsights(context.Background(), ConvertToPostID("post_1"), []string{"views", "likes"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 2 {
t.Errorf("expected 2 metrics, got %d", len(resp.Data))
}
if resp.Data[0].Name != "views" {
t.Errorf("expected 'views', got %s", resp.Data[0].Name)
}
}
func TestGetPostInsights_InvalidPostID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetPostInsights(context.Background(), PostID(""), []string{"views"})
if err == nil {
t.Fatal("expected error for empty post ID")
}
}
func TestGetPostInsights_InvalidMetric(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetPostInsights(context.Background(), ConvertToPostID("post_1"), []string{"invalid_metric"})
if err == nil {
t.Fatal("expected error for invalid metric")
}
}
func TestGetPostInsights_DefaultMetrics(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[]}`))
_, err := client.GetPostInsights(context.Background(), ConvertToPostID("post_1"), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetPostInsightsWithOptions_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[{"name":"views","period":"day","values":[{"value":50}]}]}`))
since := time.Now().Add(-24 * time.Hour)
until := time.Now()
resp, err := client.GetPostInsightsWithOptions(context.Background(), ConvertToPostID("post_1"), &PostInsightsOptions{
Metrics: []PostInsightMetric{PostInsightViews},
Period: InsightPeriodDay,
Since: &since,
Until: &until,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 1 {
t.Errorf("expected 1 metric, got %d", len(resp.Data))
}
}
func TestGetPostInsightsWithOptions_InvalidPostID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetPostInsightsWithOptions(context.Background(), PostID(""), nil)
if err == nil {
t.Fatal("expected error for empty post ID")
}
}
func TestGetPostInsightsWithOptions_NilOpts(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[]}`))
_, err := client.GetPostInsightsWithOptions(context.Background(), ConvertToPostID("post_1"), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetPostInsightsWithOptions_InvalidMetric(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetPostInsightsWithOptions(context.Background(), ConvertToPostID("post_1"), &PostInsightsOptions{
Metrics: []PostInsightMetric{"invalid"},
})
if err == nil {
t.Fatal("expected error for invalid metric")
}
}
func TestGetPostInsightsWithOptions_InvalidPeriod(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetPostInsightsWithOptions(context.Background(), ConvertToPostID("post_1"), &PostInsightsOptions{
Period: "weekly",
})
if err == nil {
t.Fatal("expected error for invalid period")
}
}
func TestGetPostInsightsWithOptions_InvalidDateRange(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
since := time.Now()
until := time.Now().Add(-24 * time.Hour) // until before since
_, err := client.GetPostInsightsWithOptions(context.Background(), ConvertToPostID("post_1"), &PostInsightsOptions{
Since: &since,
Until: &until,
})
if err == nil {
t.Fatal("expected error for invalid date range")
}
}
func TestGetAccountInsights_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{
"data": [{"name": "followers_count", "period": "day", "values": [{"value": 500}]}]
}`))
resp, err := client.GetAccountInsights(context.Background(), ConvertToUserID("12345"), []string{"followers_count"}, "day")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 1 {
t.Errorf("expected 1 metric, got %d", len(resp.Data))
}
}
func TestGetAccountInsights_InvalidUserID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetAccountInsights(context.Background(), UserID(""), nil, "")
if err == nil {
t.Fatal("expected error for empty user ID")
}
}
func TestGetAccountInsights_DefaultMetrics(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[]}`))
_, err := client.GetAccountInsights(context.Background(), ConvertToUserID("12345"), nil, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetAccountInsights_InvalidMetric(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetAccountInsights(context.Background(), ConvertToUserID("12345"), []string{"bad_metric"}, "day")
if err == nil {
t.Fatal("expected error for invalid metric")
}
}
func TestGetAccountInsights_InvalidPeriod(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetAccountInsights(context.Background(), ConvertToUserID("12345"), []string{"views"}, "weekly")
if err == nil {
t.Fatal("expected error for invalid period")
}
}
func TestGetAccountInsightsWithOptions_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[{"name":"views","period":"day","values":[{"value":100}]}]}`))
since := time.Date(2024, 6, 1, 0, 0, 0, 0, time.UTC)
until := time.Date(2024, 6, 30, 0, 0, 0, 0, time.UTC)
resp, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightViews},
Period: InsightPeriodDay,
Since: &since,
Until: &until,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 1 {
t.Errorf("expected 1 metric, got %d", len(resp.Data))
}
}
func TestGetAccountInsightsWithOptions_InvalidUserID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetAccountInsightsWithOptions(context.Background(), UserID(""), nil)
if err == nil {
t.Fatal("expected error for empty user ID")
}
}
func TestGetAccountInsightsWithOptions_NilOpts(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[]}`))
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetAccountInsightsWithOptions_InvalidMetric(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{"invalid"},
})
if err == nil {
t.Fatal("expected error for invalid metric")
}
}
func TestGetAccountInsightsWithOptions_InvalidPeriod(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Period: "weekly",
})
if err == nil {
t.Fatal("expected error for invalid period")
}
}
func TestGetAccountInsightsWithOptions_FollowerDemographicsWithSince(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
since := time.Now()
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightFollowerDemographics},
Since: &since,
})
if err == nil {
t.Fatal("expected error for follower_demographics with since")
}
}
func TestGetAccountInsightsWithOptions_FollowerDemographicsWithBreakdown(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[]}`))
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightFollowerDemographics},
Breakdown: "country",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetAccountInsightsWithOptions_FollowerDemographicsInvalidBreakdown(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightFollowerDemographics},
Breakdown: "invalid",
})
if err == nil {
t.Fatal("expected error for invalid breakdown")
}
}
func TestGetAccountInsightsWithOptions_FollowersCountWithSince(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
since := time.Now()
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightFollowersCount},
Since: &since,
})
if err == nil {
t.Fatal("expected error for followers_count with since")
}
}
func TestGetAccountInsightsWithOptions_MinSinceTimestamp(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
// Timestamp before MinInsightTimestamp
early := time.Unix(100, 0)
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightViews},
Since: &early,
})
if err == nil {
t.Fatal("expected error for since before MinInsightTimestamp")
}
}
func TestGetAccountInsightsWithOptions_MinUntilTimestamp(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
early := time.Unix(100, 0)
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightViews},
Until: &early,
})
if err == nil {
t.Fatal("expected error for until before MinInsightTimestamp")
}
}
func TestGetAccountInsightsWithOptions_InvalidDateRange(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
since := time.Date(2024, 7, 1, 0, 0, 0, 0, time.UTC)
until := time.Date(2024, 6, 1, 0, 0, 0, 0, time.UTC)
_, err := client.GetAccountInsightsWithOptions(context.Background(), ConvertToUserID("12345"), &AccountInsightsOptions{
Metrics: []AccountInsightMetric{AccountInsightViews},
Since: &since,
Until: &until,
})
if err == nil {
t.Fatal("expected error for invalid date range")
}
}
func TestValidateFollowerDemographicsBreakdown_ValidValues(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
for _, b := range []string{"country", "city", "age", "gender"} {
if err := client.validateFollowerDemographicsBreakdown(b); err != nil {
t.Errorf("expected no error for breakdown %s, got %v", b, err)
}
}
}
func TestValidateFollowerDemographicsBreakdown_Invalid(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
err := client.validateFollowerDemographicsBreakdown("invalid")
if err == nil {
t.Fatal("expected error for invalid breakdown")
}
}
func TestGetAvailablePostInsightMetrics(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
metrics := client.GetAvailablePostInsightMetrics()
if len(metrics) != 6 {
t.Errorf("expected 6 metrics, got %d", len(metrics))
}
}
func TestGetAvailableAccountInsightMetrics(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
metrics := client.GetAvailableAccountInsightMetrics()
if len(metrics) != 8 {
t.Errorf("expected 8 metrics, got %d", len(metrics))
}
}
func TestGetAvailableInsightPeriods(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
periods := client.GetAvailableInsightPeriods()
if len(periods) != 2 {
t.Errorf("expected 2 periods, got %d", len(periods))
}
}
func TestGetAvailableFollowerDemographicsBreakdowns(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
breakdowns := client.GetAvailableFollowerDemographicsBreakdowns()
if len(breakdowns) != 4 {
t.Errorf("expected 4 breakdowns, got %d", len(breakdowns))
}
}