-
Notifications
You must be signed in to change notification settings - Fork 31
/
trait_test.go
191 lines (168 loc) · 4.2 KB
/
trait_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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestGetTraits(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`[
{
"id": "2690212494559269",
"name": "wit$sentiment"
},
{
"id": "254954985556896",
"name": "faq"
}
]`))
}))
defer testServer.Close()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
traits, _ := c.GetTraits()
wantTraits := []Trait{
{ID: "2690212494559269", Name: "wit$sentiment"},
{ID: "254954985556896", Name: "faq"},
}
if !reflect.DeepEqual(traits, wantTraits) {
t.Fatalf("expected\n\ttraits: %v\n\tgot: %v", wantTraits, traits)
}
}
func TestCreateTrait(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{
"id": "13989798788",
"name": "politeness",
"values": [
{
"id": "97873388",
"value": "polite"
},
{
"id": "54493392772",
"value": "rude"
}
]
}`))
}))
defer testServer.Close()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
trait, err := c.CreateTrait("politeness", []string{"polite", "rude"})
wantTrait := &Trait{
ID: "13989798788",
Name: "politeness",
Values: []TraitValue{
{ID: "97873388", Value: "polite"},
{ID: "54493392772", Value: "rude"},
},
}
if err != nil {
t.Fatalf("nil error expected, got %v", err)
}
if !reflect.DeepEqual(wantTrait, trait) {
t.Fatalf("expected\n\ttrait: %v\n\tgot: %v", wantTrait, trait)
}
}
func TestGetTrait(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{
"id": "13989798788",
"name": "politeness",
"values": [
{
"id": "97873388",
"value": "polite"
},
{
"id": "54493392772",
"value": "rude"
}
]
}`))
}))
defer testServer.Close()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
trait, err := c.GetTrait("politeness")
wantTrait := &Trait{
ID: "13989798788",
Name: "politeness",
Values: []TraitValue{
{ID: "97873388", Value: "polite"},
{ID: "54493392772", Value: "rude"},
},
}
if err != nil {
t.Fatalf("nil error expected, got %v", err)
}
if !reflect.DeepEqual(wantTrait, trait) {
t.Fatalf("expected\n\ttrait: %v\n\tgot: %v", wantTrait, trait)
}
}
func TestDeleteTrait(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`{"deleted": "politeness"}`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
if err := c.DeleteTrait("politeness"); err != nil {
t.Fatalf("expected nil error, got: %v", err)
}
}
func TestAddTraitValue(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{
"id": "13989798788",
"name": "politeness",
"values": [
{
"id": "97873388",
"value": "polite"
},
{
"id": "54493392772",
"value": "rude"
},
{
"id": "828283932",
"value": "neutral"
}
]
}`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
trait, err := c.AddTraitValue("politeness", "neutral")
wantTrait := &Trait{
ID: "13989798788",
Name: "politeness",
Values: []TraitValue{
{ID: "97873388", Value: "polite"},
{ID: "54493392772", Value: "rude"},
{ID: "828283932", Value: "neutral"},
},
}
if err != nil {
t.Fatalf("nil error expected, got %v", err)
}
if !reflect.DeepEqual(wantTrait, trait) {
t.Fatalf("expected\n\ttrait: %v\n\tgot: %v", wantTrait, trait)
}
}
func TestDeleteTraitValue(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`{"deleted": "neutral"}`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
if err := c.DeleteTraitValue("politeness", "neutral"); err != nil {
t.Fatalf("expected nil error, got: %v", err)
}
}