-
Notifications
You must be signed in to change notification settings - Fork 9
/
query_test.go
110 lines (102 loc) · 2.93 KB
/
query_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
package apiai
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestQuery(t *testing.T) {
c, err := NewClient(&ClientConfig{Token: "fakeToken"})
if err != nil {
t.FailNow()
}
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
tests := []struct {
description string
responder httpmock.Responder
expectedResponse *QueryResponse
expectedError error
}{
{
description: "api ai success, no errors",
responder: httpmock.NewStringResponder(200, `{
"id": "b340a1f7-abee-4e13-9bdd-5e8938a48b7d",
"timestamp": "1992-02-04T00:00:00Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "my name is Marcos and I live in Barcelona",
"action": "greetings",
"actionIncomplete": false,
"parameters": { "name": "Marcos", "height": 1.77 },
"contexts": [],
"metadata": {
"intentId": "a123a123",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "greetings"
},
"fulfillment": {
"speech": "Hi Marcos! Nice to meet you!",
"messages": [
{
"type": "0",
"speech": "Hi Marcos! Nice to meet you!"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "123454321"
}`),
expectedResponse: &QueryResponse{
Id: "b340a1f7-abee-4e13-9bdd-5e8938a48b7d",
Timestamp: time.Date(1992, time.February, 04, 00, 00, 00, 0, time.UTC),
Language: "en",
Result: Result{
Source: "agent",
ResolvedQuery: "my name is Marcos and I live in Barcelona",
Action: "greetings",
Params: map[string]interface{}{"name": "Marcos", "height": 1.77},
Contexts: []Context{},
Fulfillment: Fulfilment{
Speech: "Hi Marcos! Nice to meet you!",
Messages: []Message{
{Type: "0", Speech: "Hi Marcos! Nice to meet you!"},
},
},
Score: 1,
Metadata: Metadata{
IntentId: "a123a123",
WebhookUsed: "false",
WebhookForSlotFillingUsed: "false",
IntentName: "greetings",
},
},
Status: Status{Code: http.StatusOK, ErrorType: "success"},
SessionId: "123454321",
},
expectedError: nil,
}, {
description: "api ai failed with an error 400",
responder: httpmock.NewStringResponder(http.StatusBadRequest, `{}`),
expectedResponse: nil,
expectedError: fmt.Errorf("apiai: wops something happens because status code is 400"),
},
}
for _, tc := range tests {
httpmock.RegisterResponder("POST", c.buildUrl("query", nil), tc.responder)
r, err := c.Query(Query{Query: []string{"my name is Marcos and I live in Barcelona"}, SessionId: "123454321"})
assert.Equal(r, tc.expectedResponse, tc.description)
assert.Equal(err, tc.expectedError, tc.description)
httpmock.Reset()
}
}