-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_test.go
More file actions
118 lines (110 loc) · 2.93 KB
/
segment_test.go
File metadata and controls
118 lines (110 loc) · 2.93 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
package plunk
import (
"context"
"encoding/json"
"io"
"net/http"
"testing"
)
func TestCreateSegment(t *testing.T) {
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %s, want POST", r.Method)
}
if r.URL.Path != "/segments" {
t.Errorf("path = %s, want /segments", r.URL.Path)
}
body, _ := io.ReadAll(r.Body)
var req map[string]any
json.Unmarshal(body, &req)
if req["name"] != "Premium Users" {
t.Errorf("name = %v, want Premium Users", req["name"])
}
if req["trackMembership"] != true {
t.Errorf("trackMembership = %v, want true", req["trackMembership"])
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]any{
"id": "seg1",
"name": "Premium Users",
"filters": map[string]any{
"operator": "AND",
"conditions": []any{
map[string]any{
"field": "data.plan",
"operator": "equals",
"value": "premium",
},
},
},
"trackMembership": true,
"memberCount": 42,
})
})
resp, err := c.CreateSegment(context.Background(), &CreateSegmentRequest{
Name: "Premium Users",
Filters: SegmentFilters{
Operator: "AND",
Conditions: []SegmentCondition{
{Field: "data.plan", Operator: "equals", Value: "premium"},
},
},
TrackMembership: true,
})
if err != nil {
t.Fatal(err)
}
if resp.ID != "seg1" {
t.Errorf("id = %s, want seg1", resp.ID)
}
if resp.MemberCount != 42 {
t.Errorf("memberCount = %d, want 42", resp.MemberCount)
}
if len(resp.Filters.Conditions) != 1 {
t.Fatalf("len(conditions) = %d, want 1", len(resp.Filters.Conditions))
}
if resp.Filters.Conditions[0].Field != "data.plan" {
t.Errorf("conditions[0].field = %s, want data.plan", resp.Filters.Conditions[0].Field)
}
}
func TestListSegments(t *testing.T) {
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %s, want GET", r.Method)
}
if r.URL.Path != "/segments" {
t.Errorf("path = %s, want /segments", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode([]map[string]any{
{
"id": "seg1",
"name": "Premium Users",
"filters": map[string]any{},
"trackMembership": true,
"memberCount": 42,
},
{
"id": "seg2",
"name": "Free Users",
"filters": map[string]any{},
"trackMembership": false,
"memberCount": 100,
},
})
})
segments, err := c.ListSegments(context.Background())
if err != nil {
t.Fatal(err)
}
if len(segments) != 2 {
t.Fatalf("len(segments) = %d, want 2", len(segments))
}
if segments[0].ID != "seg1" {
t.Errorf("segments[0].id = %s, want seg1", segments[0].ID)
}
if segments[1].MemberCount != 100 {
t.Errorf("segments[1].memberCount = %d, want 100", segments[1].MemberCount)
}
}