-
Notifications
You must be signed in to change notification settings - Fork 31
/
integration_test.go
148 lines (124 loc) · 3.26 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
//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{
Name: "integration_entity_name",
Roles: []string{"favorite_city"},
}
integrationApp = App{
Name: "integration_app_id",
Private: false,
}
integrationEntityUpdateFields = Entity{
Name: "integration_entity_name",
Roles: []string{"favorite_city"},
Lookups: []string{"keywords"},
}
)
func TestIntegrationInvalidToken(t *testing.T) {
c := NewClient("invalid_token")
_, err := c.GetEntity(integrationEntity.Name)
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.Name)
// delete may take some time
time.Sleep(2 * time.Second)
// 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.Name != integrationEntity.Name {
t.Fatalf("expected entity name %s, got %s", integrationEntity.Name, entity.Name)
}
// create may take some time
time.Sleep(2 * time.Second)
}
func TestIntegrationUpdateEntity(t *testing.T) {
c := getIntegrationClient()
// update entity
entity, err := c.UpdateEntity(integrationEntity.Name, integrationEntityUpdateFields)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if entity == nil {
t.Fatalf("expected non nil entity")
}
if entity.Name != integrationEntity.Name {
t.Fatalf("expected entity name %s, got %s", integrationEntity.Name, entity.Name)
}
time.Sleep(time.Second)
}
func TestIntegrationGetEntity(t *testing.T) {
c := getIntegrationClient()
e, err := c.GetEntity(integrationEntity.Name)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if e.Name != integrationEntity.Name {
t.Fatalf("expected entity name %s, got %s", integrationEntity.Name, e.Name)
}
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.Name)
if err != nil {
t.Fatalf("expected nil error got err=%v", err)
}
}
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
}