github.com/wit-ai/wit-go/v2@v2.0.2/utterance_test.go (about) 1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 3 package witai 4 5 import ( 6 "net/http" 7 "net/http/httptest" 8 "reflect" 9 "testing" 10 ) 11 12 func TestGetUtterances(t *testing.T) { 13 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 14 w.Write([]byte(`[ 15 { 16 "text": "text 1", 17 "intent": { 18 "id": "i1", 19 "name": "intent1" 20 }, 21 "entities": [ 22 { 23 "id": "e1", 24 "name": "entity1", 25 "role": "role1", 26 "start": 0, 27 "end": 10, 28 "body": "entity1", 29 "entities": [ 30 { 31 "id": "sub-e1", 32 "name": "sub-entity1", 33 "role": "sub-role1", 34 "start": 5, 35 "end": 7, 36 "body": "sub-entity1", 37 "entities": null 38 } 39 ] 40 }, 41 { 42 "id": "e2", 43 "name": "entity2", 44 "role": "role2", 45 "start": 10, 46 "end": 20, 47 "body": "entity2", 48 "entities": [ 49 { 50 "id": "sub-e2", 51 "name": "sub-entity2", 52 "role": "sub-role2", 53 "start": 15, 54 "end": 17, 55 "body": "sub-entity2", 56 "entities": null 57 } 58 ] 59 } 60 ], 61 "traits": [ 62 { 63 "id": "t1", 64 "name": "trait1", 65 "value": "value1" 66 }, 67 { 68 "id": "t2", 69 "name": "trait2", 70 "value": "value2" 71 } 72 ] 73 }, 74 { 75 "text": "text 2", 76 "intent": { 77 "id": "i2", 78 "name": "intent2" 79 }, 80 "entities": [ 81 { 82 "id": "e1", 83 "name": "entity1", 84 "role": "role1", 85 "start": 0, 86 "end": 10, 87 "body": "entity1", 88 "entities": [ 89 { 90 "id": "sub-e1", 91 "name": "sub-entity1", 92 "role": "sub-role1", 93 "start": 5, 94 "end": 7, 95 "body": "sub-entity1", 96 "entities": null 97 } 98 ] 99 }, 100 { 101 "id": "e2", 102 "name": "entity2", 103 "role": "role2", 104 "start": 10, 105 "end": 20, 106 "body": "entity2", 107 "entities": [ 108 { 109 "id": "sub-e2", 110 "name": "sub-entity2", 111 "role": "sub-role2", 112 "start": 15, 113 "end": 17, 114 "body": "sub-entity2", 115 "entities": null 116 } 117 ] 118 } 119 ], 120 "traits": [ 121 { 122 "id": "t1", 123 "name": "trait1", 124 "value": "value1" 125 }, 126 { 127 "id": "t2", 128 "name": "trait2", 129 "value": "value2" 130 } 131 ] 132 } 133 ]`)) 134 })) 135 defer testServer.Close() 136 137 c := NewClient(unitTestToken) 138 c.APIBase = testServer.URL 139 utterances, _ := c.GetUtterances(5, 0) 140 141 wantUtterances := []Utterance{ 142 { 143 Text: "text 1", 144 Intent: UtteranceIntent{ID: "i1", Name: "intent1"}, 145 Entities: []UtteranceEntity{ 146 { 147 ID: "e1", 148 Name: "entity1", 149 Role: "role1", 150 Start: 0, 151 End: 10, 152 Body: "entity1", 153 Entities: []UtteranceEntity{ 154 { 155 ID: "sub-e1", 156 Name: "sub-entity1", 157 Role: "sub-role1", 158 Start: 5, 159 End: 7, 160 Body: "sub-entity1", 161 }, 162 }, 163 }, 164 { 165 ID: "e2", 166 Name: "entity2", 167 Role: "role2", 168 Start: 10, 169 End: 20, 170 Body: "entity2", 171 Entities: []UtteranceEntity{ 172 { 173 ID: "sub-e2", 174 Name: "sub-entity2", 175 Role: "sub-role2", 176 Start: 15, 177 End: 17, 178 Body: "sub-entity2", 179 }, 180 }, 181 }, 182 }, 183 Traits: []UtteranceTrait{ 184 {ID: "t1", Name: "trait1", Value: "value1"}, 185 {ID: "t2", Name: "trait2", Value: "value2"}, 186 }, 187 }, 188 { 189 Text: "text 2", 190 Intent: UtteranceIntent{ID: "i2", Name: "intent2"}, 191 Entities: []UtteranceEntity{ 192 { 193 ID: "e1", 194 Name: "entity1", 195 Role: "role1", 196 Start: 0, 197 End: 10, 198 Body: "entity1", 199 Entities: []UtteranceEntity{ 200 { 201 ID: "sub-e1", 202 Name: "sub-entity1", 203 Role: "sub-role1", 204 Start: 5, 205 End: 7, 206 Body: "sub-entity1", 207 }, 208 }, 209 }, 210 { 211 ID: "e2", 212 Name: "entity2", 213 Role: "role2", 214 Start: 10, 215 End: 20, 216 Body: "entity2", 217 Entities: []UtteranceEntity{ 218 { 219 ID: "sub-e2", 220 Name: "sub-entity2", 221 Role: "sub-role2", 222 Start: 15, 223 End: 17, 224 Body: "sub-entity2", 225 }, 226 }, 227 }, 228 }, 229 Traits: []UtteranceTrait{ 230 {ID: "t1", Name: "trait1", Value: "value1"}, 231 {ID: "t2", Name: "trait2", Value: "value2"}, 232 }, 233 }, 234 } 235 236 if !reflect.DeepEqual(utterances, wantUtterances) { 237 t.Fatalf("expected \n\tmsg %+v \n\tgot %+v", wantUtterances, utterances) 238 } 239 } 240 241 func TestDeleteUtterances(t *testing.T) { 242 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 243 w.Write([]byte(`{"sent": true, "n": 2}`)) 244 })) 245 defer testServer.Close() 246 247 c := NewClient(unitTestToken) 248 c.APIBase = testServer.URL 249 resp, _ := c.DeleteUtterances([]string{"text1", "text2"}) 250 251 wantResp := &TrainingResponse{ 252 Sent: true, 253 N: 2, 254 } 255 256 if !reflect.DeepEqual(resp, wantResp) { 257 t.Fatalf("expected \n\tresp %+v \n\tgot %+v", wantResp, resp) 258 } 259 } 260 261 func TestTrainUtterances(t *testing.T) { 262 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 263 w.Write([]byte(`{"sent": true, "n": 2}`)) 264 })) 265 defer testServer.Close() 266 267 c := NewClient(unitTestToken) 268 c.APIBase = testServer.URL 269 resp, _ := c.TrainUtterances([]Training{ 270 {Text: "text1"}, 271 }) 272 273 wantResp := &TrainingResponse{ 274 Sent: true, 275 N: 2, 276 } 277 278 if !reflect.DeepEqual(resp, wantResp) { 279 t.Fatalf("expected \n\tresp %+v \n\tgot %+v", wantResp, resp) 280 } 281 }