forked from wit-ai/wit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
238 lines (201 loc) · 5.3 KB
/
integration_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
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
//go:build integration
// +build integration
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"net/http"
"os"
"strings"
"testing"
"time"
)
var (
integrationEntity = Entity{
ID: "integration_entity_id",
Doc: "integration_entity_doc",
}
integrationApp = App{
Name: "integration_app_id",
Private: false,
Description: "integration_app_desc",
Lang: "en",
}
integrationEntityUpdateFields = Entity{
ID: "integration_entity_id",
Lookups: []string{"keywords"},
Doc: "integration_entity_doc_updated",
}
)
func TestIntegrationInvalidToken(t *testing.T) {
c := NewClient("invalid_token")
_, err := c.GetEntity(integrationEntity.ID)
if err == nil {
t.Fatalf("expected error, got: nil")
}
}
func TestIntegrationGetUnknownEntity(t *testing.T) {
c := getIntegrationClient()
_, err := c.GetEntity("unknown_id")
if err == nil {
t.Fatalf("expected error, got: nil")
}
}
func TestIntegrationDeleteUnknownEntity(t *testing.T) {
c := getIntegrationClient()
err := c.DeleteEntity("unknown_id")
if err == nil {
t.Fatalf("expected error, got: nil")
}
}
func TestIntegrationUnknownEntity(t *testing.T) {
c := getIntegrationClient()
_, err := c.GetEntity("unknown_id")
if err == nil {
t.Fatalf("expected error, got: nil")
}
}
func TestIntegrationCreateEntity(t *testing.T) {
c := getIntegrationClient()
// just to make sure we don't create duplicates
c.DeleteEntity(integrationEntity.ID)
// create entity
entity, err := c.CreateEntity(integrationEntity)
if err != nil {
t.Fatalf("expected nil error got: %v", err)
}
if entity == nil {
t.Fatalf("expected non nil entity")
}
if entity.Lang != "en" {
t.Fatalf("expected lang=en, got: %s", entity.Lang)
}
// create may take some time
time.Sleep(2 * time.Second)
}
func TestIntegrationUpdateEntity(t *testing.T) {
c := getIntegrationClient()
// update entity
err := c.UpdateEntity(integrationEntity.ID, integrationEntityUpdateFields)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
time.Sleep(time.Second)
}
func TestIntegrationAddEntityValue(t *testing.T) {
c := getIntegrationClient()
var err error
// add entity value 1
if _, err = c.AddEntityValue(integrationEntity.ID, EntityValue{
Value: "London",
Expressions: []string{"London"},
}); err != nil {
t.Fatalf("expected nil error, got %v", err)
}
// add entity value 2
if _, err = c.AddEntityValue(integrationEntity.ID, EntityValue{
Value: "Ho Chi Minh City",
Expressions: []string{"Ho Chi Minh City"},
}); err != nil {
t.Fatalf("expected nil error, got %v", err)
}
// add entity value expression
if _, err = c.AddEntityValueExpression(integrationEntity.ID, "Ho Chi Minh City", "HoChiMinh"); err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if _, err = c.AddEntityValueExpression(integrationEntity.ID, "Ho Chi Minh City", "hochiminhcity"); err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if err = c.DeleteEntityValueExpression(integrationEntity.ID, "Ho Chi Minh City", "HoChiMinh"); err != nil {
t.Fatalf("expected nil error, got %v", err)
}
// delete entity value 1
if err = c.DeleteEntityValue(integrationEntity.ID, "Ho Chi Minh City"); err != nil {
t.Fatalf("expected nil error, got %v", err)
}
}
func TestIntegrationGetEntity(t *testing.T) {
c := getIntegrationClient()
// check entity
e, err := c.GetEntity(integrationEntity.ID)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if len(e.Values) != 1 {
t.Fatalf("expected 1 value, got %v", e.Values)
}
if e.Doc != integrationEntityUpdateFields.Doc {
t.Fatalf("expected doc=%s, got %s", integrationEntityUpdateFields.Doc, e.Doc)
}
entities, err := c.GetEntities()
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if len(entities) == 0 {
t.Fatalf("expected >0 entities, got %v", entities)
}
err = c.DeleteEntity(integrationEntity.ID)
if err != nil {
t.Fatalf("expected nil error got err=%v", err)
}
}
func TestIntegrationSamples(t *testing.T) {
c := getIntegrationClient()
// cleanup
c.DeleteSamples([]Sample{
{
Text: "I want to fly to SFO",
},
})
// Deletion takes time
time.Sleep(time.Second * 5)
// samples test
_, validateErr := c.ValidateSamples([]Sample{
{
Text: "I want to fly to SFO",
Entities: []SampleEntity{
{
Entity: "wit$location",
Value: "SFO",
Start: 17,
End: 20,
},
},
},
})
if validateErr != nil {
t.Fatalf("expected nil error, got %v", validateErr)
}
// Training takes time
time.Sleep(time.Second * 20)
// get samples
samples, samplesErr := c.GetSamples(1, 0)
if samplesErr != nil {
t.Fatalf("expected nil error, got %v", samplesErr)
}
if len(samples) != 1 {
t.Fatalf("expected 1 sample, got %v", samples)
}
// delete samples
_, delSamplesErr := c.DeleteSamples([]Sample{
{
Text: "I want to fly to SFO",
},
})
if delSamplesErr != nil {
t.Fatalf("expected nil error, got %v", delSamplesErr)
}
}
func TestIntegrationExport(t *testing.T) {
c := getIntegrationClient()
uri, _ := c.Export()
if !strings.Contains(uri, "fbcdn.net") {
t.Fatalf("uri should contain fbcdn.net, got: %s", uri)
}
}
func getIntegrationClient() *Client {
c := NewClient(os.Getenv("WITAI_INTEGRATION_TOKEN"))
c.SetHTTPClient(&http.Client{
Timeout: time.Second * 20,
})
return c
}