-
Notifications
You must be signed in to change notification settings - Fork 31
/
intent_test.go
120 lines (104 loc) · 2.72 KB
/
intent_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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestGetIntents(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`[
{
"id": "2690212494559269",
"name": "buy_car"
},
{
"id": "254954985556896",
"name": "get_weather"
},
{
"id": "233273197778131",
"name": "make_call"
}
]`))
}))
defer testServer.Close()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
intents, _ := c.GetIntents()
wantIntents := []Intent{
{ID: "2690212494559269", Name: "buy_car"},
{ID: "254954985556896", Name: "get_weather"},
{ID: "233273197778131", Name: "make_call"},
}
if !reflect.DeepEqual(intents, wantIntents) {
t.Fatalf("expected\n\tintents: %v\n\tgot: %v", wantIntents, intents)
}
}
func TestCreateIntent(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{
"id": "13989798788",
"name": "buy_flowers"
}`))
}))
defer testServer.Close()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
intent, err := c.CreateIntent("buy_flowers")
wantIntent := &Intent{
ID: "13989798788",
Name: "buy_flowers",
}
if err != nil {
t.Fatalf("nil error expected, got %v", err)
}
if !reflect.DeepEqual(wantIntent, intent) {
t.Fatalf("expected\n\tentity: %v\n\tgot: %v", wantIntent, intent)
}
}
func TestGetIntent(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{
"id": "13989798788",
"name": "buy_flowers",
"entities": [{
"id": "9078938883",
"name": "flower:flower"
},{
"id": "11223229984",
"name": "wit$contact:contact"
}]
}`))
}))
defer testServer.Close()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
intent, err := c.GetIntent("buy_flowers")
wantIntent := &Intent{
ID: "13989798788",
Name: "buy_flowers",
Entities: []Entity{
{ID: "9078938883", Name: "flower:flower"},
{ID: "11223229984", Name: "wit$contact:contact"},
},
}
if err != nil {
t.Fatalf("nil error expected, got %v", err)
}
if !reflect.DeepEqual(wantIntent, intent) {
t.Fatalf("expected\n\tentity: %v\n\tgot: %v", wantIntent, intent)
}
}
func TestDeleteIntent(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"deleted": "buy_flowers"}`))
}))
defer testServer.Close()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
if err := c.DeleteIntent("buy_flowers"); err != nil {
t.Fatalf("nil error expected, got %v", err)
}
}