-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmessages_test.go
157 lines (146 loc) · 3.81 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
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
// Copyright (c) 2014 Jason Goecke
// messages_test.go
package wit
import (
"os"
"testing"
"time"
)
var msgID string
func TestWitMessageParsing(t *testing.T) {
data := `
{
"msg_id" : "2f41839e-2b54-4de2-aa59-fc016c3e58d1",
"_text" : "how many people between Tuesday and Friday?",
"outcomes" : [ {
"_text" : "how many people between Tuesday and Friday?",
"confidence" : 0.522,
"intent" : "query_metrics",
"entities" : {
"datetime" : [ {
"type" : "interval",
"from" : {
"value" : "2015-12-01T00:00:00.000-08:00",
"grain" : "day"
},
"to" : {
"value" : "2015-12-05T00:00:00.000-08:00",
"grain" : "day"
},
"values" : [ {
"type" : "interval",
"from" : {
"value" : "2015-12-01T00:00:00.000-08:00",
"grain" : "day"
},
"to" : {
"value" : "2015-12-05T00:00:00.000-08:00",
"grain" : "day"
}
}, {
"type" : "interval",
"from" : {
"value" : "2015-12-08T00:00:00.000-08:00",
"grain" : "day"
},
"to" : {
"value" : "2015-12-12T00:00:00.000-08:00",
"grain" : "day"
}
}, {
"type" : "interval",
"from" : {
"value" : "2015-12-15T00:00:00.000-08:00",
"grain" : "day"
},
"to" : {
"value" : "2015-12-19T00:00:00.000-08:00",
"grain" : "day"
}
} ]
} ]
}
} ]
}`
message, err := parseMessage([]byte(data))
if err != nil {
t.Error(err.Error())
}
if message.MsgID != "2f41839e-2b54-4de2-aa59-fc016c3e58d1" {
t.Errorf("not equal %s != %s", "2f41839e-2b54-4de2-aa59-fc016c3e58d1", message.MsgID)
}
if message.Outcomes[0].Intent != "query_metrics" {
t.Errorf("not equal %s != %s", "query_metrics", message.Outcomes[0].Intent)
}
if message.Outcomes[0].Entities["datetime"][0].From.Grain != "day" {
t.Errorf("not equal %s != %s", "day", message.Outcomes[0].Entities["datetime"][0].From.Grain)
}
}
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)
return
}
if result.Text != "Hello world" {
t.Error("Did not process properly")
}
msgID = 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.Text != "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)
return
}
defer file.Close()
stats, statsErr := file.Stat()
if statsErr != nil {
t.Error(err)
}
size := 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.Text != "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(msgID)
if err != nil {
t.Error(err)
} else {
if message.MsgID != msgID {
t.Error("Message JSON did not parse properly.")
}
}
}