initialized predication unit tests fix PR
removed the import of foundation
addressed a minor comment
Co-authored-by: Guo <48600426+DongQuanRui@users.noreply.github.com>
12 | 12 | ||
13 | 13 | class MockTranscribeBehavior: AWSTranscribeStreamingBehavior { |
|
14 | 14 | ||
15 | + | var delegate: AWSTranscribeStreamingClientDelegate? |
|
16 | + | var callbackQueue: DispatchQueue? |
|
15 | 17 | var transcriptionResult: AWSTranscribeStreamingTranscriptResultStream? |
|
16 | 18 | var error: Error? |
|
17 | 19 |
26 | 28 | ||
27 | 29 | public func setResult(result: AWSTranscribeStreamingTranscriptResultStream?) { |
|
28 | 30 | transcriptionResult = result |
|
31 | + | error = nil |
|
29 | 32 | } |
|
30 | 33 | ||
31 | 34 | func startTranscriptionWSS(request: AWSTranscribeStreamingStartStreamTranscriptionRequest) { |
|
32 | - | ||
35 | + | delegate?.didReceiveEvent(transcriptionResult, decodingError: error) |
|
33 | 36 | } |
|
34 | 37 | ||
35 | 38 | func setDelegate(delegate: AWSTranscribeStreamingClientDelegate, callbackQueue: DispatchQueue) { |
|
36 | - | ||
39 | + | self.delegate = delegate |
|
40 | + | self.callbackQueue = callbackQueue |
|
37 | 41 | } |
|
38 | 42 | ||
39 | 43 | func send(data: Data, headers: [String: String]) { |
78 | 78 | /// |
|
79 | 79 | func testIdentifyLabelsService() { |
|
80 | 80 | setUpAmplify() |
|
81 | + | ||
82 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
81 | 83 | let mockResponse: AWSRekognitionDetectLabelsResponse = AWSRekognitionDetectLabelsResponse() |
|
82 | 84 | mockResponse.labels = [AWSRekognitionLabel]() |
|
83 | 85 |
94 | 96 | let labelResult = result as? IdentifyLabelsResult |
|
95 | 97 | let labels = IdentifyLabelsResultTransformers.processLabels(mockResponse.labels!) |
|
96 | 98 | XCTAssertEqual(labelResult?.labels, labels, "Labels should be the same") |
|
99 | + | resultReceived.fulfill() |
|
97 | 100 | case .failed(let error): |
|
98 | 101 | XCTFail("Should not produce error: \(error)") |
|
99 | 102 | } |
|
100 | 103 | } |
|
104 | + | ||
105 | + | waitForExpectations(timeout: 1) |
|
101 | 106 | } |
|
102 | 107 | ||
103 | 108 | /// Test whether error is correctly propogated |
110 | 115 | /// |
|
111 | 116 | func testIdentifyLabelsServiceWithError() { |
|
112 | 117 | setUpAmplify() |
|
118 | + | ||
119 | + | let errorReceived = expectation(description: "Error should be returned") |
|
113 | 120 | let mockError = NSError(domain: AWSRekognitionErrorDomain, |
|
114 | 121 | code: AWSRekognitionErrorType.invalidImageFormat.rawValue, |
|
115 | 122 | userInfo: [:]) |
122 | 129 | XCTFail("Should not produce result: \(result)") |
|
123 | 130 | case .failed(let error): |
|
124 | 131 | XCTAssertNotNil(error, "Should produce an error") |
|
132 | + | errorReceived.fulfill() |
|
125 | 133 | } |
|
126 | 134 | } |
|
135 | + | ||
136 | + | waitForExpectations(timeout: 1) |
|
127 | 137 | } |
|
128 | 138 | ||
129 | 139 | /// Test whether error is correctly propogated |
136 | 146 | /// |
|
137 | 147 | func testIdentifyLabelsServiceWithNilResponse() { |
|
138 | 148 | setUpAmplify() |
|
139 | - | ||
140 | 149 | mockRekognition.setLabelsResponse(result: nil) |
|
150 | + | ||
151 | + | let errorReceived = expectation(description: "Error should be returned") |
|
141 | 152 | let testBundle = Bundle(for: type(of: self)) |
|
142 | 153 | guard let url = testBundle.url(forResource: "testImageLabels", withExtension: "jpg") else { |
|
143 | 154 | XCTFail("Unable to find image") |
150 | 161 | XCTFail("Should not produce result: \(result)") |
|
151 | 162 | case .failed(let error): |
|
152 | 163 | XCTAssertNotNil(error, "Should produce an error") |
|
164 | + | errorReceived.fulfill() |
|
153 | 165 | } |
|
154 | 166 | } |
|
167 | + | ||
168 | + | waitForExpectations(timeout: 1) |
|
155 | 169 | } |
|
156 | 170 | ||
157 | 171 | /// Test whether we can make a successful rekognition call to identify moderation labels |
164 | 178 | /// |
|
165 | 179 | func testIdentifyModerationLabelsService() { |
|
166 | 180 | setUpAmplify() |
|
181 | + | ||
182 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
167 | 183 | let mockResponse: AWSRekognitionDetectModerationLabelsResponse = AWSRekognitionDetectModerationLabelsResponse() |
|
168 | 184 | mockResponse.moderationLabels = [AWSRekognitionModerationLabel]() |
|
169 | 185 |
182 | 198 | XCTAssertEqual(labelResult?.labels, labels, "Labels should be the same") |
|
183 | 199 | XCTAssertNotNil(labelResult?.unsafeContent, |
|
184 | 200 | "unsafe content should have a boolean in it since we called moderation labels") |
|
201 | + | resultReceived.fulfill() |
|
185 | 202 | case .failed(let error): |
|
186 | 203 | XCTFail("Should not produce error: \(error)") |
|
187 | 204 | } |
|
188 | 205 | } |
|
206 | + | ||
207 | + | waitForExpectations(timeout: 1) |
|
189 | 208 | } |
|
190 | 209 | ||
191 | 210 | /// Test whether error is prograted correctly when making a rekognition call to identify moderation labels |
198 | 217 | /// |
|
199 | 218 | func testIdentifyModerationLabelsServiceWithError() { |
|
200 | 219 | setUpAmplify() |
|
220 | + | ||
221 | + | let errorReceived = expectation(description: "Error should be returned") |
|
201 | 222 | let mockError = NSError(domain: AWSRekognitionErrorDomain, |
|
202 | 223 | code: AWSRekognitionErrorType.invalidImageFormat.rawValue, |
|
203 | 224 | userInfo: [:]) |
207 | 228 | predictionsService.detectLabels(image: url, type: .moderation) { event in |
|
208 | 229 | switch event { |
|
209 | 230 | case .completed(let result): |
|
210 | - | XCTFail("Should not produce result: \(result)") |
|
231 | + | XCTFail("Should not produce result: \(result)") |
|
211 | 232 | case .failed(let error): |
|
212 | 233 | XCTAssertNotNil(error, "Should produce an error") |
|
234 | + | errorReceived.fulfill() |
|
213 | 235 | } |
|
214 | 236 | } |
|
237 | + | ||
238 | + | waitForExpectations(timeout: 1) |
|
215 | 239 | } |
|
216 | 240 | ||
217 | 241 | /// Test whether we can make a successful rekognition call to identify moderation labels but receive a nil response |
224 | 248 | /// |
|
225 | 249 | func testIdentifyModerationLabelsServiceWithNilResponse() { |
|
226 | 250 | setUpAmplify() |
|
227 | - | ||
228 | 251 | mockRekognition.setModerationLabelsResponse(result: nil) |
|
252 | + | ||
253 | + | let errorReceived = expectation(description: "Error should be returned") |
|
229 | 254 | let testBundle = Bundle(for: type(of: self)) |
|
230 | 255 | guard let url = testBundle.url(forResource: "testImageLabels", withExtension: "jpg") else { |
|
231 | 256 | XCTFail("Unable to find image") |
235 | 260 | predictionsService.detectLabels(image: url, type: .moderation) { event in |
|
236 | 261 | switch event { |
|
237 | 262 | case .completed(let result): |
|
238 | - | XCTFail("Should not produce result: \(result)") |
|
263 | + | XCTFail("Should not produce result: \(result)") |
|
239 | 264 | case .failed(let error): |
|
240 | 265 | XCTAssertNotNil(error, "Should produce an error") |
|
266 | + | errorReceived.fulfill() |
|
241 | 267 | } |
|
242 | 268 | } |
|
269 | + | ||
270 | + | waitForExpectations(timeout: 1) |
|
243 | 271 | } |
|
244 | 272 | ||
245 | 273 | /// Test whether we can make a successful rekognition call to identify all labels |
252 | 280 | /// |
|
253 | 281 | func testIdentifyAllLabelsService() { |
|
254 | 282 | setUpAmplify() |
|
283 | + | ||
284 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
255 | 285 | let mockLabelsResponse: AWSRekognitionDetectLabelsResponse = AWSRekognitionDetectLabelsResponse() |
|
256 | 286 | mockLabelsResponse.labels = [AWSRekognitionLabel]() |
|
257 | 287 |
274 | 304 | XCTAssertEqual(labelResult?.labels, labels, "Labels should be the same") |
|
275 | 305 | XCTAssertNotNil(labelResult?.unsafeContent, |
|
276 | 306 | "unsafe content should have a boolean in it since we called all labels") |
|
307 | + | resultReceived.fulfill() |
|
277 | 308 | case .failed(let error): |
|
278 | 309 | XCTFail("Should not produce error: \(error)") |
|
279 | 310 | } |
|
280 | 311 | } |
|
312 | + | ||
313 | + | waitForExpectations(timeout: 1) |
|
281 | 314 | } |
|
282 | 315 | ||
283 | 316 | /// Test whether error is prograted correctly when making a rekognition call to identify all labels |
291 | 324 | func testIdentifyAllLabelsServiceWithNilResponse() { |
|
292 | 325 | setUpAmplify() |
|
293 | 326 | mockRekognition.setAllLabelsResponse(labelsResult: nil, moderationResult: nil) |
|
327 | + | ||
294 | 328 | let testBundle = Bundle(for: type(of: self)) |
|
295 | 329 | guard let url = testBundle.url(forResource: "testImageLabels", withExtension: "jpg") else { |
|
296 | 330 | XCTFail("Unable to find image") |
|
297 | 331 | return |
|
298 | 332 | } |
|
299 | 333 | ||
334 | + | let errorReceived = expectation(description: "Error should be returned") |
|
335 | + | errorReceived.expectedFulfillmentCount = 2 |
|
336 | + | ||
300 | 337 | predictionsService.detectLabels(image: url, type: .all) { event in |
|
301 | 338 | switch event { |
|
302 | 339 | case .completed(let result): |
|
303 | - | XCTFail("Should not produce result: \(result)") |
|
340 | + | XCTFail("Should not produce result: \(result)") |
|
304 | 341 | case .failed(let error): |
|
305 | 342 | XCTAssertNotNil(error, "Should produce an error") |
|
343 | + | errorReceived.fulfill() |
|
306 | 344 | } |
|
307 | 345 | } |
|
346 | + | ||
347 | + | waitForExpectations(timeout: 1) |
|
308 | 348 | } |
|
309 | 349 | ||
310 | 350 | /// Test whether error is prograted correctly when making a rekognition call to identify all labels |
317 | 357 | /// |
|
318 | 358 | func testIdentifyAllLabelsServiceWithError() { |
|
319 | 359 | setUpAmplify() |
|
360 | + | ||
320 | 361 | let mockError = NSError(domain: AWSRekognitionErrorDomain, |
|
321 | 362 | code: AWSRekognitionErrorType.invalidImageFormat.rawValue, |
|
322 | 363 | userInfo: [:]) |
|
323 | 364 | mockRekognition.setError(error: mockError) |
|
324 | 365 | let url = URL(fileURLWithPath: "") |
|
366 | + | let errorReceived = expectation(description: "Error should be returned") |
|
325 | 367 | ||
326 | 368 | predictionsService.detectLabels(image: url, type: .all) { event in |
|
327 | 369 | switch event { |
|
328 | 370 | case .completed(let result): |
|
329 | - | XCTFail("Should not produce result: \(result)") |
|
371 | + | XCTFail("Should not produce result: \(result)") |
|
330 | 372 | case .failed(let error): |
|
331 | 373 | XCTAssertNotNil(error, "Should produce an error") |
|
374 | + | errorReceived.fulfill() |
|
332 | 375 | } |
|
333 | 376 | } |
|
377 | + | ||
378 | + | waitForExpectations(timeout: 1) |
|
334 | 379 | } |
|
335 | 380 | ||
336 | 381 | /// Test whether we can make a successfull rekognition call to identify entities |
343 | 388 | /// |
|
344 | 389 | func testIdentifyEntitiesService() { |
|
345 | 390 | setUpAmplify() |
|
391 | + | ||
346 | 392 | let mockResponse: AWSRekognitionDetectFacesResponse = AWSRekognitionDetectFacesResponse() |
|
347 | 393 | mockResponse.faceDetails = [AWSRekognitionFaceDetail]() |
|
348 | 394 |
353 | 399 | return |
|
354 | 400 | } |
|
355 | 401 | ||
402 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
403 | + | ||
356 | 404 | predictionsService.detectEntities(image: url) { event in |
|
357 | 405 | switch event { |
|
358 | 406 | case .completed(let result): |
|
359 | 407 | let entitiesResult = result as? IdentifyEntitiesResult |
|
360 | 408 | let newFaces = IdentifyEntitiesResultTransformers.processFaces(mockResponse.faceDetails!) |
|
361 | 409 | XCTAssertEqual(entitiesResult?.entities.count, newFaces.count, "Faces count number should be the same") |
|
410 | + | resultReceived.fulfill() |
|
362 | 411 | case .failed(let error): |
|
363 | 412 | XCTFail("Should not produce error: \(error)") |
|
364 | 413 | } |
|
365 | 414 | } |
|
415 | + | ||
416 | + | waitForExpectations(timeout: 1) |
|
366 | 417 | } |
|
367 | 418 | ||
368 | 419 | /// Test whether error is correctly propogated for detecting entities |
375 | 426 | /// |
|
376 | 427 | func testIdentifyEntitiesServiceWithError() { |
|
377 | 428 | setUpAmplify() |
|
429 | + | ||
378 | 430 | let mockError = NSError(domain: AWSRekognitionErrorDomain, |
|
379 | 431 | code: AWSRekognitionErrorType.invalidImageFormat.rawValue, |
|
380 | 432 | userInfo: [:]) |
|
381 | 433 | mockRekognition.setError(error: mockError) |
|
382 | 434 | let url = URL(fileURLWithPath: "") |
|
435 | + | let errorReceived = expectation(description: "Error should be returned") |
|
383 | 436 | ||
384 | 437 | predictionsService.detectEntities(image: url) { event in |
|
385 | 438 | switch event { |
387 | 440 | XCTFail("Should not produce result: \(result)") |
|
388 | 441 | case .failed(let error): |
|
389 | 442 | XCTAssertNotNil(error, "Should produce an error") |
|
443 | + | errorReceived.fulfill() |
|
390 | 444 | } |
|
391 | 445 | } |
|
446 | + | ||
447 | + | waitForExpectations(timeout: 1) |
|
392 | 448 | } |
|
393 | 449 | ||
394 | 450 | /// Test whether error is correctly propogated for detecting entities when a nil response is received |
402 | 458 | func testIdentifyEntitiesServiceWithNilResponse() { |
|
403 | 459 | setUpAmplify() |
|
404 | 460 | mockRekognition.setFacesResponse(result: nil) |
|
461 | + | ||
405 | 462 | let testBundle = Bundle(for: type(of: self)) |
|
406 | 463 | guard let url = testBundle.url(forResource: "testImageEntities", withExtension: "jpg") else { |
|
407 | 464 | XCTFail("Unable to find image") |
|
408 | 465 | return |
|
409 | 466 | } |
|
467 | + | let errorReceived = expectation(description: "Error should be returned") |
|
410 | 468 | ||
411 | 469 | predictionsService.detectEntities(image: url) { event in |
|
412 | 470 | switch event { |
414 | 472 | XCTFail("Should not produce result: \(result)") |
|
415 | 473 | case .failed(let error): |
|
416 | 474 | XCTAssertNotNil(error, "Should produce an error") |
|
475 | + | errorReceived.fulfill() |
|
417 | 476 | } |
|
418 | 477 | } |
|
478 | + | ||
479 | + | waitForExpectations(timeout: 1) |
|
419 | 480 | } |
|
420 | 481 | ||
421 | 482 | /// Test whether we can make a successfull rekognition call to identify entities from a collection |
428 | 489 | /// |
|
429 | 490 | func testIdentifyEntityMatchesService() { |
|
430 | 491 | setUpAmplify(withCollection: true) |
|
492 | + | ||
431 | 493 | let mockResponse: AWSRekognitionSearchFacesByImageResponse = AWSRekognitionSearchFacesByImageResponse() |
|
432 | 494 | mockResponse.faceMatches = [AWSRekognitionFaceMatch]() |
|
433 | 495 |
437 | 499 | XCTFail("Unable to find image") |
|
438 | 500 | return |
|
439 | 501 | } |
|
502 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
440 | 503 | ||
441 | 504 | predictionsService.detectEntities(image: url) { event in |
|
442 | 505 | switch event { |
444 | 507 | let entitiesResult = result as? IdentifyEntityMatchesResult |
|
445 | 508 | let newFaces = IdentifyEntitiesResultTransformers.processCollectionFaces(mockResponse.faceMatches!) |
|
446 | 509 | XCTAssertEqual(entitiesResult?.entities.count, newFaces.count, "Faces count number should be the same") |
|
510 | + | resultReceived.fulfill() |
|
447 | 511 | case .failed(let error): |
|
448 | 512 | XCTFail("Should not produce error: \(error)") |
|
449 | 513 | } |
|
450 | 514 | } |
|
515 | + | ||
516 | + | waitForExpectations(timeout: 1) |
|
451 | 517 | } |
|
452 | 518 | ||
453 | 519 | /// Test whether error is correctly propogated for entity matches |
460 | 526 | /// |
|
461 | 527 | func testIdentifyEntityMatchesServiceWithError() { |
|
462 | 528 | setUpAmplify(withCollection: true) |
|
529 | + | ||
463 | 530 | let mockError = NSError(domain: AWSRekognitionErrorDomain, |
|
464 | 531 | code: AWSRekognitionErrorType.invalidImageFormat.rawValue, |
|
465 | 532 | userInfo: [:]) |
|
466 | 533 | mockRekognition.setError(error: mockError) |
|
467 | 534 | let url = URL(fileURLWithPath: "") |
|
535 | + | let errorReceived = expectation(description: "Error should be returned") |
|
468 | 536 | ||
469 | 537 | predictionsService.detectEntities(image: url) { event in |
|
470 | 538 | switch event { |
472 | 540 | XCTFail("Should not produce result: \(result)") |
|
473 | 541 | case .failed(let error): |
|
474 | 542 | XCTAssertNotNil(error, "Should produce an error") |
|
543 | + | errorReceived.fulfill() |
|
475 | 544 | } |
|
476 | 545 | } |
|
546 | + | ||
547 | + | waitForExpectations(timeout: 1) |
|
477 | 548 | } |
|
478 | 549 | ||
479 | 550 | /// Test whether error is correctly propogated for entity matches when request is nil |
487 | 558 | func testIdentifyEntityMatchesServiceWithNilResponse() { |
|
488 | 559 | setUpAmplify(withCollection: true) |
|
489 | 560 | mockRekognition.setFacesFromCollection(result: nil) |
|
561 | + | ||
490 | 562 | let testBundle = Bundle(for: type(of: self)) |
|
491 | 563 | guard let url = testBundle.url(forResource: "testImageEntities", withExtension: "jpg") else { |
|
492 | 564 | XCTFail("Unable to find image") |
|
493 | 565 | return |
|
494 | 566 | } |
|
567 | + | let errorReceived = expectation(description: "Error should be returned") |
|
495 | 568 | ||
496 | 569 | predictionsService.detectEntities(image: url) { event in |
|
497 | 570 | switch event { |
499 | 572 | XCTFail("Should not produce result: \(result)") |
|
500 | 573 | case .failed(let error): |
|
501 | 574 | XCTAssertNotNil(error, "Should produce an error") |
|
575 | + | errorReceived.fulfill() |
|
502 | 576 | } |
|
503 | 577 | } |
|
578 | + | ||
579 | + | waitForExpectations(timeout: 1) |
|
504 | 580 | } |
|
505 | 581 | ||
506 | 582 | /// Test whether we can make a successfull rekognition call to identify plain text |
513 | 589 | /// |
|
514 | 590 | func testIdentifyPlainTextService() { |
|
515 | 591 | setUpAmplify() |
|
592 | + | ||
516 | 593 | let mockResponse: AWSRekognitionDetectTextResponse = AWSRekognitionDetectTextResponse() |
|
517 | 594 | mockResponse.textDetections = [AWSRekognitionTextDetection]() |
|
518 | 595 |
522 | 599 | XCTFail("Unable to find image") |
|
523 | 600 | return |
|
524 | 601 | } |
|
602 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
525 | 603 | ||
526 | 604 | predictionsService.detectText(image: url, format: .plain) { event in |
|
527 | 605 | switch event { |
530 | 608 | let newText = IdentifyTextResultTransformers.processText(mockResponse.textDetections!) |
|
531 | 609 | XCTAssertEqual(textResult?.identifiedLines?.count, |
|
532 | 610 | newText.identifiedLines?.count, "Text line count number should be the same") |
|
611 | + | resultReceived.fulfill() |
|
533 | 612 | case .failed(let error): |
|
534 | 613 | XCTFail("Should not produce error: \(error)") |
|
535 | 614 | } |
|
536 | 615 | } |
|
616 | + | ||
617 | + | waitForExpectations(timeout: 1) |
|
537 | 618 | } |
|
538 | 619 | ||
539 | 620 | /// Test whether error is correctly propogated for text matches |
546 | 627 | /// |
|
547 | 628 | func testIdentifyPlainTextServiceWithError() { |
|
548 | 629 | setUpAmplify() |
|
630 | + | ||
549 | 631 | let mockError = NSError(domain: AWSRekognitionErrorDomain, |
|
550 | 632 | code: AWSRekognitionErrorType.invalidImageFormat.rawValue, |
|
551 | 633 | userInfo: [:]) |
|
552 | 634 | mockRekognition.setError(error: mockError) |
|
553 | 635 | let url = URL(fileURLWithPath: "") |
|
636 | + | let errorReceived = expectation(description: "Error should be returned") |
|
554 | 637 | ||
555 | 638 | predictionsService.detectText(image: url, format: .plain) { event in |
|
556 | 639 | switch event { |
558 | 641 | XCTFail("Should not produce result: \(result)") |
|
559 | 642 | case .failed(let error): |
|
560 | 643 | XCTAssertNotNil(error, "Should produce an error") |
|
644 | + | errorReceived.fulfill() |
|
561 | 645 | } |
|
562 | 646 | } |
|
647 | + | ||
648 | + | waitForExpectations(timeout: 1) |
|
563 | 649 | } |
|
564 | 650 | ||
565 | 651 | /// Test whether error is correctly propogated for text matches and receive a nil response |
579 | 665 | XCTFail("Unable to find image") |
|
580 | 666 | return |
|
581 | 667 | } |
|
668 | + | let errorReceived = expectation(description: "Error should be returned") |
|
582 | 669 | ||
583 | 670 | predictionsService.detectText(image: url, format: .plain) { event in |
|
584 | 671 | switch event { |
586 | 673 | XCTFail("Should not produce result: \(result)") |
|
587 | 674 | case .failed(let error): |
|
588 | 675 | XCTAssertNotNil(error, "Should produce an error") |
|
676 | + | errorReceived.fulfill() |
|
589 | 677 | } |
|
590 | 678 | } |
|
679 | + | ||
680 | + | waitForExpectations(timeout: 1) |
|
591 | 681 | } |
|
592 | 682 | } |
17 | 17 | var error: Error? |
|
18 | 18 | ||
19 | 19 | func analyzeDocument(request: AWSTextractAnalyzeDocumentRequest) -> AWSTask<AWSTextractAnalyzeDocumentResponse> { |
|
20 | - | if let finalResult = analyzeDocument { |
|
21 | - | return AWSTask(result: finalResult) |
|
20 | + | guard let finalError = error else { |
|
21 | + | return AWSTask(result: analyzeDocument) |
|
22 | 22 | } |
|
23 | - | return AWSTask(error: error!) |
|
23 | + | return AWSTask(error: finalError) |
|
24 | 24 | } |
|
25 | 25 | ||
26 | 26 | func detectDocumentText(request: AWSTextractDetectDocumentTextRequest) |
|
27 | - | -> AWSTask<AWSTextractDetectDocumentTextResponse> { |
|
28 | - | if let finalResult = detectDocumentText { |
|
29 | - | return AWSTask(result: finalResult) |
|
30 | - | } |
|
31 | - | return AWSTask(error: error!) |
|
27 | + | -> AWSTask<AWSTextractDetectDocumentTextResponse> { |
|
28 | + | guard let finalError = error else { |
|
29 | + | return AWSTask(result: detectDocumentText) |
|
30 | + | } |
|
31 | + | return AWSTask(error: finalError) |
|
32 | 32 | } |
|
33 | 33 | ||
34 | 34 | func getTextract() -> AWSTextract { |
12 | 12 | import Foundation |
|
13 | 13 | @testable import AWSPredictionsPlugin |
|
14 | 14 | ||
15 | - | class PredictionsServiceTextractTests: XCTest { |
|
15 | + | class PredictionsServiceTextractTests: XCTestCase { |
|
16 | 16 | var predictionsService: AWSPredictionsService! |
|
17 | 17 | let mockTextract = MockTextractBehavior() |
|
18 | 18 |
59 | 59 | mockTextract.setAnalyzeDocument(result: mockResponse) |
|
60 | 60 | let testBundle = Bundle(for: type(of: self)) |
|
61 | 61 | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
62 | - | XCTFail("Unable to find image") |
|
63 | - | return |
|
62 | + | XCTFail("Unable to find image") |
|
63 | + | return |
|
64 | 64 | } |
|
65 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
65 | 66 | ||
66 | 67 | predictionsService.detectText(image: url, format: .table) { event in |
|
67 | 68 | switch event { |
|
68 | 69 | case .completed(let result): |
|
69 | - | let textResult = result as? IdentifyTextResult |
|
70 | + | let textResult = result as? IdentifyDocumentTextResult |
|
70 | 71 | let text = IdentifyTextResultTransformers.processText(mockResponse.blocks!) |
|
71 | - | XCTAssertEqual(textResult?.identifiedLines?.count, |
|
72 | + | XCTAssertEqual(textResult?.identifiedLines.count, |
|
72 | 73 | text.identifiedLines.count, "Line count should be the same") |
|
74 | + | resultReceived.fulfill() |
|
73 | 75 | case .failed(let error): |
|
74 | 76 | XCTFail("Should not produce error: \(error)") |
|
75 | 77 | } |
|
76 | 78 | } |
|
79 | + | ||
80 | + | waitForExpectations(timeout: 1) |
|
77 | 81 | } |
|
78 | 82 | ||
79 | 83 | /// Test whether error is correctly propogated for text matches |
90 | 94 | userInfo: [:]) |
|
91 | 95 | mockTextract.setError(error: mockError) |
|
92 | 96 | let url = URL(fileURLWithPath: "") |
|
97 | + | let errorReceived = expectation(description: "Error should be returned") |
|
93 | 98 | ||
94 | - | predictionsService.detectText(image: url, format: .table) { event in |
|
99 | + | predictionsService.detectText(image: url, format: .table) { event in |
|
95 | 100 | switch event { |
|
96 | 101 | case .completed(let result): |
|
97 | 102 | XCTFail("Should not produce result: \(result)") |
|
98 | 103 | case .failed(let error): |
|
99 | 104 | XCTAssertNotNil(error, "Should produce an error") |
|
105 | + | errorReceived.fulfill() |
|
100 | 106 | } |
|
101 | 107 | } |
|
108 | + | ||
109 | + | waitForExpectations(timeout: 1) |
|
102 | 110 | } |
|
103 | 111 | ||
104 | 112 | /// Test whether error is correctly propogated for text matches with nil response |
110 | 118 | /// - I should get back a service error |
|
111 | 119 | /// |
|
112 | 120 | func testIdentifyTablesServiceWithNilResponse() { |
|
113 | - | ||
114 | 121 | mockTextract.setAnalyzeDocument(result: nil) |
|
115 | - | let testBundle = Bundle(for: type(of: self)) |
|
116 | - | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
117 | - | XCTFail("Unable to find image") |
|
118 | - | return |
|
119 | - | } |
|
120 | - | predictionsService.detectText(image: url, format: .table) { event in |
|
122 | + | let testBundle = Bundle(for: type(of: self)) |
|
123 | + | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
124 | + | XCTFail("Unable to find image") |
|
125 | + | return |
|
126 | + | } |
|
127 | + | let errorReceived = expectation(description: "Error should be returned") |
|
128 | + | ||
129 | + | predictionsService.detectText(image: url, format: .table) { event in |
|
121 | 130 | switch event { |
|
122 | 131 | case .completed(let result): |
|
123 | 132 | XCTFail("Should not produce result: \(result)") |
|
124 | 133 | case .failed(let error): |
|
125 | 134 | XCTAssertNotNil(error, "Should produce an error") |
|
135 | + | errorReceived.fulfill() |
|
126 | 136 | } |
|
127 | 137 | } |
|
138 | + | ||
139 | + | waitForExpectations(timeout: 1) |
|
128 | 140 | } |
|
129 | 141 | ||
130 | 142 | /// Test whether we can make a successfull textract call to identify forms |
142 | 154 | mockTextract.setAnalyzeDocument(result: mockResponse) |
|
143 | 155 | let testBundle = Bundle(for: type(of: self)) |
|
144 | 156 | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
145 | - | XCTFail("Unable to find image") |
|
146 | - | return |
|
157 | + | XCTFail("Unable to find image") |
|
158 | + | return |
|
147 | 159 | } |
|
160 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
148 | 161 | ||
149 | 162 | predictionsService.detectText(image: url, format: .form) { event in |
|
150 | 163 | switch event { |
|
151 | 164 | case .completed(let result): |
|
152 | - | let textResult = result as? IdentifyTextResult |
|
165 | + | let textResult = result as? IdentifyDocumentTextResult |
|
153 | 166 | let text = IdentifyTextResultTransformers.processText(mockResponse.blocks!) |
|
154 | - | XCTAssertEqual(textResult?.identifiedLines?.count, |
|
167 | + | XCTAssertEqual(textResult?.identifiedLines.count, |
|
155 | 168 | text.identifiedLines.count, "Line count should be the same") |
|
169 | + | resultReceived.fulfill() |
|
156 | 170 | case .failed(let error): |
|
157 | 171 | XCTFail("Should not produce error: \(error)") |
|
158 | 172 | } |
|
159 | 173 | } |
|
174 | + | ||
175 | + | waitForExpectations(timeout: 1) |
|
160 | 176 | } |
|
161 | 177 | ||
162 | 178 | /// Test whether error is correctly propogated for document text matches |
173 | 189 | userInfo: [:]) |
|
174 | 190 | mockTextract.setError(error: mockError) |
|
175 | 191 | let url = URL(fileURLWithPath: "") |
|
192 | + | let errorReceived = expectation(description: "Error should be returned") |
|
176 | 193 | ||
177 | 194 | predictionsService.detectText(image: url, format: .form) { event in |
|
178 | 195 | switch event { |
180 | 197 | XCTFail("Should not produce result: \(result)") |
|
181 | 198 | case .failed(let error): |
|
182 | 199 | XCTAssertNotNil(error, "Should produce an error") |
|
200 | + | errorReceived.fulfill() |
|
183 | 201 | } |
|
184 | 202 | } |
|
203 | + | ||
204 | + | waitForExpectations(timeout: 1) |
|
185 | 205 | } |
|
186 | 206 | ||
187 | 207 | /// Test whether error is correctly propogated for text matches with nil response |
193 | 213 | /// - I should get back a service error because response is nil |
|
194 | 214 | /// |
|
195 | 215 | func testIdentifyFormsServiceWithNilResponse() { |
|
196 | - | ||
197 | 216 | mockTextract.setAnalyzeDocument(result: nil) |
|
198 | - | let testBundle = Bundle(for: type(of: self)) |
|
199 | - | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
200 | - | XCTFail("Unable to find image") |
|
201 | - | return |
|
202 | - | } |
|
217 | + | let testBundle = Bundle(for: type(of: self)) |
|
218 | + | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
219 | + | XCTFail("Unable to find image") |
|
220 | + | return |
|
221 | + | } |
|
222 | + | let errorReceived = expectation(description: "Error should be returned") |
|
223 | + | ||
203 | 224 | predictionsService.detectText(image: url, format: .form) { event in |
|
204 | 225 | switch event { |
|
205 | 226 | case .completed(let result): |
|
206 | 227 | XCTFail("Should not produce result: \(result)") |
|
207 | 228 | case .failed(let error): |
|
208 | 229 | XCTAssertNotNil(error, "Should produce an error") |
|
230 | + | errorReceived.fulfill() |
|
209 | 231 | } |
|
210 | 232 | } |
|
233 | + | ||
234 | + | waitForExpectations(timeout: 1) |
|
211 | 235 | } |
|
212 | 236 | ||
213 | 237 | /// Test whether we can make a successfull textract call to identify forms and tables |
225 | 249 | mockTextract.setAnalyzeDocument(result: mockResponse) |
|
226 | 250 | let testBundle = Bundle(for: type(of: self)) |
|
227 | 251 | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
228 | - | XCTFail("Unable to find image") |
|
229 | - | return |
|
252 | + | XCTFail("Unable to find image") |
|
253 | + | return |
|
230 | 254 | } |
|
255 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
231 | 256 | ||
232 | 257 | predictionsService.detectText(image: url, format: .all) { event in |
|
233 | 258 | switch event { |
|
234 | 259 | case .completed(let result): |
|
235 | - | let textResult = result as? IdentifyTextResult |
|
260 | + | let textResult = result as? IdentifyDocumentTextResult |
|
236 | 261 | let text = IdentifyTextResultTransformers.processText(mockResponse.blocks!) |
|
237 | - | XCTAssertEqual(textResult?.identifiedLines?.count, |
|
262 | + | XCTAssertEqual(textResult?.identifiedLines.count, |
|
238 | 263 | text.identifiedLines.count, "Line count should be the same") |
|
264 | + | resultReceived.fulfill() |
|
239 | 265 | case .failed(let error): |
|
240 | 266 | XCTFail("Should not produce error: \(error)") |
|
241 | 267 | } |
|
242 | 268 | } |
|
269 | + | ||
270 | + | waitForExpectations(timeout: 1) |
|
243 | 271 | } |
|
244 | 272 | ||
245 | 273 | /// Test whether error is correctly propogated for .all document text matches |
256 | 284 | userInfo: [:]) |
|
257 | 285 | mockTextract.setError(error: mockError) |
|
258 | 286 | let url = URL(fileURLWithPath: "") |
|
287 | + | let errorReceived = expectation(description: "Error should be returned") |
|
259 | 288 | ||
260 | 289 | predictionsService.detectText(image: url, format: .all) { event in |
|
261 | 290 | switch event { |
263 | 292 | XCTFail("Should not produce result: \(result)") |
|
264 | 293 | case .failed(let error): |
|
265 | 294 | XCTAssertNotNil(error, "Should produce an error") |
|
295 | + | errorReceived.fulfill() |
|
266 | 296 | } |
|
267 | 297 | } |
|
298 | + | ||
299 | + | waitForExpectations(timeout: 1) |
|
268 | 300 | } |
|
269 | 301 | ||
270 | 302 | /// Test whether error is correctly propogated for text matches with nil response |
276 | 308 | /// - I should get back a service error because response is nil |
|
277 | 309 | /// |
|
278 | 310 | func testIdentifyAllTextServiceWithNilResponse() { |
|
279 | - | ||
280 | 311 | mockTextract.setAnalyzeDocument(result: nil) |
|
281 | - | let testBundle = Bundle(for: type(of: self)) |
|
282 | - | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
283 | - | XCTFail("Unable to find image") |
|
284 | - | return |
|
285 | - | } |
|
312 | + | let testBundle = Bundle(for: type(of: self)) |
|
313 | + | guard let url = testBundle.url(forResource: "testImageText", withExtension: "jpg") else { |
|
314 | + | XCTFail("Unable to find image") |
|
315 | + | return |
|
316 | + | } |
|
317 | + | let errorReceived = expectation(description: "Error should be returned") |
|
318 | + | ||
286 | 319 | predictionsService.detectText(image: url, format: .all) { event in |
|
287 | 320 | switch event { |
|
288 | 321 | case .completed(let result): |
|
289 | 322 | XCTFail("Should not produce result: \(result)") |
|
290 | 323 | case .failed(let error): |
|
291 | 324 | XCTAssertNotNil(error, "Should produce an error") |
|
325 | + | errorReceived.fulfill() |
|
292 | 326 | } |
|
293 | 327 | } |
|
328 | + | ||
329 | + | waitForExpectations(timeout: 1) |
|
294 | 330 | } |
|
295 | 331 | } |
59 | 59 | english.languageCode = "en" |
|
60 | 60 | english.score = 0.5 |
|
61 | 61 | let mockDominantLanguage = mockDominantLanguageResult([english]) |
|
62 | + | ||
63 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
64 | + | ||
62 | 65 | mockComprehend.setResult(languageResponse: mockDominantLanguage) |
|
63 | 66 | predictionsService.comprehend(text: inputForTest) { event in |
|
64 | 67 | switch event { |
|
65 | 68 | case .completed(let result): |
|
66 | 69 | XCTAssertNotNil(result, "Result should be non-nil") |
|
67 | 70 | XCTAssertEqual(result.language?.languageCode, .english, "Dominant language should match") |
|
71 | + | resultReceived.fulfill() |
|
68 | 72 | case .failed(let error): |
|
69 | 73 | XCTFail("Should not produce error: \(error)") |
|
70 | 74 | } |
|
71 | 75 | } |
|
76 | + | ||
77 | + | waitForExpectations(timeout: 1) |
|
72 | 78 | } |
|
73 | 79 | ||
74 | 80 | /// Test whether we get correct dominant language for multiple languages |
89 | 95 | let italian = AWSComprehendDominantLanguage()! |
|
90 | 96 | italian.languageCode = "it" |
|
91 | 97 | italian.score = 0.6 |
|
98 | + | ||
92 | 99 | let mockDominantLanguage = mockDominantLanguageResult([english, spanish, italian]) |
|
100 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
101 | + | ||
93 | 102 | mockComprehend.setResult(languageResponse: mockDominantLanguage) |
|
94 | 103 | predictionsService.comprehend(text: inputForTest) { event in |
|
95 | 104 | switch event { |
|
96 | 105 | case .completed(let result): |
|
97 | 106 | XCTAssertNotNil(result, "Result should be non-nil") |
|
98 | 107 | XCTAssertEqual(result.language?.languageCode, .italian, "Dominant language should match") |
|
108 | + | resultReceived.fulfill() |
|
99 | 109 | case .failed(let error): |
|
100 | 110 | XCTFail("Should not produce error: \(error)") |
|
101 | 111 | } |
|
102 | 112 | } |
|
113 | + | ||
114 | + | waitForExpectations(timeout: 1) |
|
103 | 115 | } |
|
104 | 116 | ||
105 | 117 | /// Test whether empty result from service gives us error |
112 | 124 | /// |
|
113 | 125 | func testWithEmptyLanguageResult() { |
|
114 | 126 | let mockDominantLanguage = mockDominantLanguageResult() |
|
127 | + | let errorReceived = expectation(description: "Error should be returned") |
|
128 | + | ||
115 | 129 | mockComprehend.setResult(languageResponse: mockDominantLanguage) |
|
116 | 130 | predictionsService.comprehend(text: inputForTest) { event in |
|
117 | 131 | switch event { |
119 | 133 | XCTFail("Should not produce result if the service cannot find the language. \(result)") |
|
120 | 134 | case .failed(let error): |
|
121 | 135 | XCTAssertNotNil(error, "Should return an error if language is nil. \(error)") |
|
136 | + | errorReceived.fulfill() |
|
122 | 137 | } |
|
123 | 138 | } |
|
139 | + | ||
140 | + | waitForExpectations(timeout: 1) |
|
124 | 141 | } |
|
125 | 142 | ||
126 | 143 | /// Test whether returninng all nil from service gives us an error |
132 | 149 | /// - I should get an error |
|
133 | 150 | /// |
|
134 | 151 | func testAllNilResult() { |
|
152 | + | let errorReceived = expectation(description: "Error should be returned") |
|
153 | + | ||
135 | 154 | mockComprehend.setResult() |
|
136 | 155 | predictionsService.comprehend(text: inputForTest) { event in |
|
137 | 156 | switch event { |
139 | 158 | XCTFail("Should not produce result if the service cannot find the language. \(result)") |
|
140 | 159 | case .failed(let error): |
|
141 | 160 | XCTAssertNotNil(error, "Should return an error if language is nil. \(error)") |
|
161 | + | errorReceived.fulfill() |
|
142 | 162 | } |
|
143 | 163 | } |
|
164 | + | ||
165 | + | waitForExpectations(timeout: 1) |
|
144 | 166 | } |
|
145 | 167 | ||
146 | 168 | /// Test whether we get an error if service return error for language detection |
155 | 177 | let mockError = NSError(domain: AWSComprehendErrorDomain, |
|
156 | 178 | code: AWSComprehendErrorType.internalServer.rawValue, |
|
157 | 179 | userInfo: [:]) |
|
180 | + | let errorReceived = expectation(description: "Error should be returned") |
|
181 | + | ||
158 | 182 | mockComprehend.setError(error: mockError) |
|
159 | 183 | predictionsService.comprehend(text: inputForTest) { event in |
|
160 | 184 | switch event { |
162 | 186 | XCTFail("Should not produce result if the service cannot find the language. \(result)") |
|
163 | 187 | case .failed(let error): |
|
164 | 188 | XCTAssertNotNil(error, "Should return an error if server returned an error. \(error)") |
|
189 | + | errorReceived.fulfill() |
|
165 | 190 | } |
|
166 | 191 | } |
|
192 | + | ||
193 | + | waitForExpectations(timeout: 1) |
|
167 | 194 | } |
|
168 | 195 | ||
169 | 196 | /// Test a complete response |
210 | 237 | keyPhrase.endOffset = 3 |
|
211 | 238 | keyPhrase.score = 0.8 |
|
212 | 239 | keyPhrase.text = "some text" |
|
240 | + | ||
213 | 241 | let mockKeyPhrases = mockKeyPhrasesResult([keyPhrase]) |
|
242 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
214 | 243 | ||
215 | 244 | mockComprehend.setResult(sentimentResponse: mockSentiment, |
|
216 | 245 | entitiesResponse: mockEntities, |
222 | 251 | case .completed(let result): |
|
223 | 252 | XCTAssertNotNil(result, "Result should be non-nil") |
|
224 | 253 | XCTAssertEqual(result.language?.languageCode, .spanish, "Dominant language should match") |
|
254 | + | resultReceived.fulfill() |
|
225 | 255 | case .failed(let error): |
|
226 | 256 | XCTFail("Should not produce error: \(error)") |
|
227 | 257 | } |
|
228 | 258 | } |
|
259 | + | ||
260 | + | waitForExpectations(timeout: 1) |
|
229 | 261 | } |
|
230 | 262 | ||
231 | 263 | // MARK: - Helper methods |
36 | 36 | awsTextract: MockTextractBehavior(), |
|
37 | 37 | awsComprehend: MockComprehendBehavior(), |
|
38 | 38 | awsPolly: MockPollyBehavior(), |
|
39 | - | awsTranscribeStreaming: MockTranscribeBehavior(), |
|
39 | + | awsTranscribeStreaming: mockTranscribe, |
|
40 | 40 | nativeWebSocketProvider: nativeWebSocketProvider, |
|
41 | 41 | transcribeClientDelegate: clientDelegate, |
|
42 | 42 | configuration: mockConfiguration) |
|
43 | 43 | ||
44 | + | mockTranscribe.setDelegate(delegate: clientDelegate, callbackQueue: dispatchQueue) |
|
45 | + | ||
44 | 46 | let testBundle = Bundle(for: type(of: self)) |
|
45 | 47 | guard let url = testBundle.url(forResource: "audio", withExtension: "wav") else { |
|
46 | 48 | return |
59 | 61 | let resultStream = AWSTranscribeStreamingResult()! |
|
60 | 62 | let alternative = AWSTranscribeStreamingAlternative()! |
|
61 | 63 | alternative.transcript = str |
|
64 | + | resultStream.isPartial = false |
|
62 | 65 | resultStream.alternatives = [alternative] |
|
63 | 66 | results.results = [resultStream] |
|
64 | 67 | transcriptEvent.transcript = results |
|
68 | + | mockResponse.transcriptEvent = transcriptEvent |
|
65 | 69 | return mockResponse |
|
66 | 70 | } |
|
67 | 71 |
74 | 78 | /// - I should get back a result |
|
75 | 79 | /// |
|
76 | 80 | func testTranscribeService() { |
|
77 | - | let transcription = "This is a test" |
|
81 | + | let mockResponse = createMockTranscribeResponse() |
|
82 | + | mockTranscribe.setResult(result: mockResponse) |
|
83 | + | let expectedTranscription = "This is a test" |
|
84 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
85 | + | ||
78 | 86 | predictionsService.transcribe(speechToText: audioFile, language: .usEnglish) { event in |
|
79 | 87 | switch event { |
|
80 | 88 | case .completed(let result): |
|
81 | - | XCTAssertEqual(result.transcription, transcription, "transcribed text should be the same") |
|
89 | + | XCTAssertEqual(result.transcription, expectedTranscription, "transcribed text should be the same") |
|
90 | + | resultReceived.fulfill() |
|
82 | 91 | case .failed(let error): |
|
83 | 92 | XCTFail("Should not produce error: \(error)") |
|
84 | 93 | } |
|
85 | - | ||
86 | 94 | } |
|
87 | 95 | ||
96 | + | waitForExpectations(timeout: 1) |
|
97 | + | ||
88 | 98 | } |
|
89 | 99 | ||
90 | 100 | /// Test whether error is correctly propogated |
101 | 111 | userInfo: [:]) |
|
102 | 112 | mockTranscribe.setError(error: mockError) |
|
103 | 113 | ||
114 | + | let errorReceived = expectation(description: "Error should be returned") |
|
115 | + | ||
104 | 116 | predictionsService.transcribe(speechToText: audioFile, language: .usEnglish) { event in |
|
105 | 117 | switch event { |
|
106 | 118 | case .completed(let result): |
|
107 | 119 | XCTFail("Should not produce result: \(result)") |
|
108 | 120 | case .failed(let error): |
|
109 | 121 | XCTAssertNotNil(error, "Should produce an error") |
|
122 | + | errorReceived.fulfill() |
|
110 | 123 | } |
|
111 | - | ||
112 | 124 | } |
|
125 | + | ||
126 | + | waitForExpectations(timeout: 1) |
|
113 | 127 | } |
|
114 | 128 | ||
115 | 129 | /// Test if language from configuration is picked up |
122 | 136 | /// |
|
123 | 137 | func testLanguageFromConfiguration() { |
|
124 | 138 | let mockConfigurationJSON = """ |
|
125 | - | { |
|
126 | - | "defaultRegion": "us-east-1", |
|
127 | - | "convert": { |
|
128 | - | "transcription": { |
|
129 | - | "region": "us-east-1", |
|
130 | - | "language": "en-US" |
|
139 | + | { |
|
140 | + | "defaultRegion": "us-east-1", |
|
141 | + | "convert": { |
|
142 | + | "transcription": { |
|
143 | + | "region": "us-east-1", |
|
144 | + | "language": "en-US" |
|
145 | + | } |
|
131 | 146 | } |
|
132 | 147 | } |
|
133 | - | } |
|
134 | - | """.data(using: .utf8)! |
|
148 | + | """.data(using: .utf8)! |
|
135 | 149 | do { |
|
136 | 150 | let clientDelegate = NativeWSTranscribeStreamingClientDelegate() |
|
137 | 151 | let dispatchQueue = DispatchQueue(label: "TranscribeStreamingTests") |
149 | 163 | nativeWebSocketProvider: nativeWebSocketProvider, |
|
150 | 164 | transcribeClientDelegate: clientDelegate, |
|
151 | 165 | configuration: mockConfiguration) |
|
166 | + | ||
167 | + | mockTranscribe.setDelegate(delegate: clientDelegate, callbackQueue: dispatchQueue) |
|
152 | 168 | } catch { |
|
153 | 169 | XCTFail("Initialization of the service failed. \(error)") |
|
154 | 170 | } |
|
155 | 171 | ||
156 | 172 | let mockResponse = createMockTranscribeResponse() |
|
157 | 173 | mockTranscribe.setResult(result: mockResponse) |
|
174 | + | let expectedTranscription = "This is a test" |
|
175 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
158 | 176 | ||
159 | 177 | predictionsService.transcribe(speechToText: audioFile, language: nil) {event in |
|
160 | 178 | switch event { |
|
161 | 179 | case .completed(let result): |
|
162 | - | XCTAssertEqual(result.transcription, "This is a test", "Transcribed text should be the same") |
|
180 | + | XCTAssertEqual(result.transcription, expectedTranscription, "Transcribed text should be the same") |
|
181 | + | resultReceived.fulfill() |
|
163 | 182 | case .failed(let error): |
|
164 | 183 | XCTFail("Should not produce error: \(error)") |
|
165 | 184 | } |
|
166 | 185 | } |
|
186 | + | ||
187 | + | waitForExpectations(timeout: 1) |
|
167 | 188 | } |
|
168 | 189 | ||
169 | 190 | /// Test if the service returns nil, we get an error back |
176 | 197 | /// |
|
177 | 198 | func testNilResult() { |
|
178 | 199 | mockTranscribe.setResult(result: nil) |
|
200 | + | ||
201 | + | let errorReceived = expectation(description: "Error should be returned") |
|
202 | + | ||
179 | 203 | predictionsService.transcribe(speechToText: audioFile, language: nil) {event in |
|
180 | 204 | switch event { |
|
181 | 205 | case .completed(let result): |
|
182 | - | XCTFail("Should not produce result: \(result)") |
|
206 | + | XCTFail("Should not produce result: \(result)") |
|
183 | 207 | case .failed(let error): |
|
184 | - | XCTAssertNotNil(error, "Should produce an error") |
|
185 | - | } |
|
208 | + | XCTAssertNotNil(error, "Should produce an error") |
|
209 | + | errorReceived.fulfill() |
|
210 | + | } |
|
186 | 211 | } |
|
212 | + | ||
213 | + | waitForExpectations(timeout: 1) |
|
187 | 214 | } |
|
188 | 215 | } |
56 | 56 | mockResponse.translatedText = "translated text here" |
|
57 | 57 | mockTranslate.setResult(result: mockResponse) |
|
58 | 58 | ||
59 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
60 | + | ||
59 | 61 | predictionsService.translateText(text: "Hello there", |
|
60 | 62 | language: .english, |
|
61 | 63 | targetLanguage: .italian) { event in |
|
62 | - | switch event { |
|
63 | - | case .completed(let result): |
|
64 | - | XCTAssertEqual(result.text, |
|
65 | - | mockResponse.translatedText, |
|
66 | - | "Translated text should be same") |
|
67 | - | case .failed(let error): |
|
68 | - | XCTFail("Should not produce error: \(error)") |
|
69 | - | } |
|
64 | + | switch event { |
|
65 | + | case .completed(let result): |
|
66 | + | XCTAssertEqual(result.text, |
|
67 | + | mockResponse.translatedText, |
|
68 | + | "Translated text should be same") |
|
69 | + | resultReceived.fulfill() |
|
70 | + | case .failed(let error): |
|
71 | + | XCTFail("Should not produce error: \(error)") |
|
72 | + | } |
|
70 | 73 | } |
|
74 | + | ||
75 | + | waitForExpectations(timeout: 1) |
|
71 | 76 | } |
|
72 | 77 | ||
73 | 78 | /// Test whether error is correctly propogated |
79 | 84 | /// - I should get back a service error |
|
80 | 85 | /// |
|
81 | 86 | func testTranslateServiceWithError() { |
|
82 | - | ||
83 | 87 | let mockError = NSError(domain: AWSTranslateErrorDomain, |
|
84 | 88 | code: AWSTranslateErrorType.invalidRequest.rawValue, |
|
85 | 89 | userInfo: [:]) |
|
86 | 90 | mockTranslate.setError(error: mockError) |
|
87 | 91 | ||
92 | + | let errorReceived = expectation(description: "Error should be returned") |
|
93 | + | ||
88 | 94 | predictionsService.translateText(text: "", |
|
89 | 95 | language: .english, |
|
90 | 96 | targetLanguage: .italian) { event in |
|
91 | - | switch event { |
|
92 | - | case .completed(let result): |
|
93 | - | XCTFail("Should not produce result: \(result)") |
|
94 | - | case .failed(let error): |
|
95 | - | XCTAssertNotNil(error, "Should produce an error") |
|
96 | - | } |
|
97 | + | switch event { |
|
98 | + | case .completed(let result): |
|
99 | + | XCTFail("Should not produce result: \(result)") |
|
100 | + | case .failed(let error): |
|
101 | + | XCTAssertNotNil(error, "Should produce an error") |
|
102 | + | errorReceived.fulfill() |
|
103 | + | } |
|
97 | 104 | } |
|
105 | + | ||
106 | + | waitForExpectations(timeout: 1) |
|
98 | 107 | } |
|
99 | 108 | ||
100 | 109 | /// Test if language from configuration is picked up |
144 | 153 | mockResponse.translatedText = "translated text here" |
|
145 | 154 | mockTranslate.setResult(result: mockResponse) |
|
146 | 155 | ||
156 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
157 | + | ||
147 | 158 | predictionsService.translateText(text: "Hello there", |
|
148 | - | language: nil, |
|
149 | - | targetLanguage: nil) { event in |
|
150 | - | switch event { |
|
151 | - | case .completed(let result): |
|
152 | - | XCTAssertEqual(result.text, |
|
153 | - | mockResponse.translatedText, |
|
154 | - | "Translated text should be same") |
|
155 | - | case .failed(let error): |
|
156 | - | XCTFail("Should not produce error: \(error)") |
|
157 | - | } |
|
159 | + | language: nil, |
|
160 | + | targetLanguage: nil) { event in |
|
161 | + | switch event { |
|
162 | + | case .completed(let result): |
|
163 | + | XCTAssertEqual(result.text, |
|
164 | + | mockResponse.translatedText, |
|
165 | + | "Translated text should be same") |
|
166 | + | resultReceived.fulfill() |
|
167 | + | case .failed(let error): |
|
168 | + | XCTFail("Should not produce error: \(error)") |
|
169 | + | } |
|
158 | 170 | } |
|
171 | + | ||
172 | + | waitForExpectations(timeout: 1) |
|
159 | 173 | } |
|
160 | 174 | ||
161 | 175 | /// Test if the source language is nil error is thrown |
171 | 185 | mockResponse.translatedText = "translated text here" |
|
172 | 186 | mockTranslate.setResult(result: mockResponse) |
|
173 | 187 | ||
188 | + | let errorReceived = expectation(description: "Error should be returned") |
|
189 | + | ||
174 | 190 | predictionsService.translateText(text: "", |
|
175 | 191 | language: nil, |
|
176 | 192 | targetLanguage: .italian) { event in |
|
177 | - | switch event { |
|
178 | - | case .completed(let result): |
|
179 | - | XCTFail("Should not produce result: \(result)") |
|
180 | - | case .failed(let error): |
|
181 | - | XCTAssertNotNil(error, "Should produce an error") |
|
182 | - | } |
|
193 | + | switch event { |
|
194 | + | case .completed(let result): |
|
195 | + | XCTFail("Should not produce result: \(result)") |
|
196 | + | case .failed(let error): |
|
197 | + | XCTAssertNotNil(error, "Should produce an error") |
|
198 | + | errorReceived.fulfill() |
|
199 | + | } |
|
183 | 200 | } |
|
201 | + | ||
202 | + | waitForExpectations(timeout: 1) |
|
184 | 203 | } |
|
185 | 204 | ||
186 | 205 | /// Test if the target is nil and configuration is not set |
196 | 215 | mockResponse.translatedText = "translated text here" |
|
197 | 216 | mockTranslate.setResult(result: mockResponse) |
|
198 | 217 | ||
218 | + | let errorReceived = expectation(description: "Error should be returned") |
|
219 | + | ||
199 | 220 | predictionsService.translateText(text: "", |
|
200 | 221 | language: .english, |
|
201 | 222 | targetLanguage: nil) { event in |
|
202 | - | switch event { |
|
203 | - | case .completed(let result): |
|
204 | - | XCTFail("Should not produce result: \(result)") |
|
205 | - | case .failed(let error): |
|
206 | - | XCTAssertNotNil(error, "Should produce an error") |
|
207 | - | } |
|
223 | + | switch event { |
|
224 | + | case .completed(let result): |
|
225 | + | XCTFail("Should not produce result: \(result)") |
|
226 | + | case .failed(let error): |
|
227 | + | XCTAssertNotNil(error, "Should produce an error") |
|
228 | + | errorReceived.fulfill() |
|
229 | + | } |
|
208 | 230 | } |
|
231 | + | ||
232 | + | waitForExpectations(timeout: 1) |
|
209 | 233 | } |
|
210 | 234 | ||
211 | 235 | /// Test if the service returns nil, we get an error back |
218 | 242 | /// |
|
219 | 243 | func testNilResult() { |
|
220 | 244 | mockTranslate.setResult(result: nil) |
|
245 | + | ||
246 | + | let errorReceived = expectation(description: "Error should be returned") |
|
247 | + | ||
221 | 248 | predictionsService.translateText(text: "", |
|
222 | 249 | language: .english, |
|
223 | 250 | targetLanguage: .spanish) { event in |
|
224 | - | switch event { |
|
225 | - | case .completed(let result): |
|
226 | - | XCTFail("Should not produce result: \(result)") |
|
227 | - | case .failed(let error): |
|
228 | - | XCTAssertNotNil(error, "Should produce an error") |
|
229 | - | } |
|
251 | + | switch event { |
|
252 | + | case .completed(let result): |
|
253 | + | XCTFail("Should not produce result: \(result)") |
|
254 | + | case .failed(let error): |
|
255 | + | XCTAssertNotNil(error, "Should produce an error") |
|
256 | + | errorReceived.fulfill() |
|
257 | + | } |
|
230 | 258 | } |
|
259 | + | ||
260 | + | waitForExpectations(timeout: 1) |
|
231 | 261 | } |
|
232 | 262 | ||
233 | 263 | /// Test if the service returns nil for translated text, we get an error back |
241 | 271 | func testNilTranslatedTextResult() { |
|
242 | 272 | let mockResponse = AWSTranslateTranslateTextResponse()! |
|
243 | 273 | mockTranslate.setResult(result: mockResponse) |
|
274 | + | ||
275 | + | let errorReceived = expectation(description: "Error should be returned") |
|
276 | + | ||
244 | 277 | predictionsService.translateText(text: "", |
|
245 | 278 | language: .english, |
|
246 | 279 | targetLanguage: .spanish) { event in |
|
247 | - | switch event { |
|
248 | - | case .completed(let result): |
|
249 | - | XCTFail("Should not produce result: \(result)") |
|
250 | - | case .failed(let error): |
|
251 | - | XCTAssertNotNil(error, "Should produce an error") |
|
252 | - | } |
|
280 | + | switch event { |
|
281 | + | case .completed(let result): |
|
282 | + | XCTFail("Should not produce result: \(result)") |
|
283 | + | case .failed(let error): |
|
284 | + | XCTAssertNotNil(error, "Should produce an error") |
|
285 | + | errorReceived.fulfill() |
|
286 | + | } |
|
253 | 287 | } |
|
288 | + | ||
289 | + | waitForExpectations(timeout: 1) |
|
254 | 290 | } |
|
255 | 291 | ||
256 | 292 | /// Test if the target language is set |
266 | 302 | mockResponse.translatedText = "translated text here" |
|
267 | 303 | mockTranslate.setResult(result: mockResponse) |
|
268 | 304 | ||
305 | + | let resultReceived = expectation(description: "Transcription result should be returned") |
|
306 | + | ||
269 | 307 | predictionsService.translateText(text: "Hello there", |
|
270 | 308 | language: .english, |
|
271 | 309 | targetLanguage: .malayalam) { event in |
|
272 | - | switch event { |
|
273 | - | case .completed(let result): |
|
274 | - | XCTAssertEqual(result.text, |
|
275 | - | mockResponse.translatedText, |
|
276 | - | "Translated text should be same") |
|
277 | - | XCTAssertEqual(result.targetLanguage, .malayalam) |
|
278 | - | case .failed(let error): |
|
279 | - | XCTFail("Should not produce error: \(error)") |
|
280 | - | } |
|
310 | + | switch event { |
|
311 | + | case .completed(let result): |
|
312 | + | XCTAssertEqual(result.text, |
|
313 | + | mockResponse.translatedText, |
|
314 | + | "Translated text should be same") |
|
315 | + | XCTAssertEqual(result.targetLanguage, .malayalam) |
|
316 | + | resultReceived.fulfill() |
|
317 | + | case .failed(let error): |
|
318 | + | XCTFail("Should not produce error: \(error)") |
|
319 | + | } |
|
281 | 320 | } |
|
321 | + | ||
322 | + | waitForExpectations(timeout: 1) |
|
282 | 323 | } |
|
283 | 324 | } |
Files | Coverage |
---|---|
Amplify | 45.75% |
AmplifyPlugins | 68.33% |
AmplifyTestApp | 44.44% |
AmplifyTestCommon | 25.61% |
AmplifyTests | 91.38% |
Project Totals (874 files) | 66.88% |