forked from jsgoecke/go-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages_test.go
128 lines (117 loc) · 2.9 KB
/
messages_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
// Copyright (c) 2014 Jason Goecke
// messages_test.go
package wit
import (
"os"
"testing"
"time"
)
var msg_id string
func TestWitMessageParsing(t *testing.T) {
data := `
{
"msg_id":"1234",
"msg_body":"how many people between Tuesday and Friday?",
"outcome":{
"intent":"query_metrics",
"entities":{
"metric":{
"value":"metric_visitors",
"body":"people"
},
"datetime":[
{
"value":{
"from":"2013-10-21T00:00:00.000Z",
"to":"2013-10-22T00:00:00.000Z"
},
"body":"Tuesday"
},
{
"value":{
"from":"2013-10-24T00:00:00.000Z",
"to":"2013-10-25T00:00:00.000Z"
},
"body":"Friday"
}
]
},
"confidence":0.979
}
}`
message, err := parseMessage([]byte(data))
if err != nil {
t.Error(err.Error())
}
if message.MsgId != "1234" ||
message.Outcome.Intent != "query_metrics" ||
message.Outcome.Entities.Datetime[0].Body != "Tuesday" {
t.Error("Message JSON did not parse properly.")
}
}
func TestWitMessageRequest(t *testing.T) {
client := NewClient(os.Getenv("WIT_ACCESS_TOKEN"))
request := &MessageRequest{}
request.Query = "Hello world"
result, err := client.Message(request)
if err != nil {
t.Error(err)
}
if result.MsgBody != "Hello world" {
t.Error("Did not process properly")
}
msg_id = result.MsgId
}
func TestWitPostAudioMessage(t *testing.T) {
client := NewClient(os.Getenv("WIT_ACCESS_TOKEN"))
request := &MessageRequest{}
request.File = "./audio_sample/helloWorld.wav"
request.ContentType = "audio/wav"
message, err := client.AudioMessage(request)
if err != nil {
t.Error(err)
} else {
if message.MsgBody != "hello world" {
t.Error("Audio POST did not work properly")
}
}
}
func TestWitPostAudioContentsMessage(t *testing.T) {
client := NewClient(os.Getenv("WIT_ACCESS_TOKEN"))
file, err := os.Open("./audio_sample/helloWorld.wav")
if err != nil {
t.Error(err)
}
defer file.Close()
stats, statsErr := file.Stat()
if statsErr != nil {
t.Error(err)
}
var size int64 = stats.Size()
data := make([]byte, size)
file.Read(data)
request := &MessageRequest{}
request.FileContents = data
request.ContentType = "audio/wav"
message, err := client.AudioMessage(request)
if err != nil {
t.Error(err)
} else {
if message.MsgBody != "hello world" {
t.Error("Audio POST did not work properly")
}
}
}
func TestWitMessages(t *testing.T) {
client := NewClient(os.Getenv("WIT_ACCESS_TOKEN"))
//Wait for the message to be indexed
time.Sleep(300 * time.Millisecond)
message, err := client.Messages(msg_id)
if err != nil {
t.Error("Message JSON did not parse properly.")
} else {
if message.MsgId != msg_id {
t.Error("Message JSON did not parse properly.")
}
}
}