#903
fix(Predictions): Fix of PredicationPlugin unit tests
Showing 7 of 7 files from the diff.
@@ -12,6 +12,8 @@
Loading
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,14 +28,16 @@
Loading
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]) { |
@@ -12,7 +12,7 @@
Loading
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,21 +59,25 @@
Loading
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,15 +94,19 @@
Loading
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,21 +118,25 @@
Loading
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,21 +154,25 @@
Loading
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,15 +189,19 @@
Loading
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 { |
|
179 | 196 | case .completed(let result): |
|
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,21 +213,25 @@
Loading
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,21 +249,25 @@
Loading
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,15 +284,19 @@
Loading
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 { |
|
262 | 291 | case .completed(let result): |
|
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,20 +308,24 @@
Loading
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,16 +59,22 @@
Loading
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,17 +95,23 @@
Loading
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,15 +124,20 @@
Loading
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 { |
|
118 | 132 | case .completed(let result): |
|
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,15 +149,20 @@
Loading
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 { |
|
138 | 157 | case .completed(let result): |
|
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,15 +177,20 @@
Loading
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 { |
|
161 | 185 | case .completed(let result): |
|
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,7 +237,9 @@
Loading
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,10 +251,13 @@
Loading
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,11 +36,13 @@
Loading
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,9 +61,11 @@
Loading
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,17 +78,23 @@
Loading
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,15 +111,19 @@
Loading
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,16 +136,16 @@
Loading
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,21 +163,28 @@
Loading
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,13 +197,19 @@
Loading
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,18 +56,23 @@
Loading
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,22 +84,26 @@
Loading
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,18 +153,23 @@
Loading
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,16 +185,21 @@
Loading
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,16 +215,21 @@
Loading
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,16 +242,22 @@
Loading
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,16 +271,22 @@
Loading
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,18 +302,23 @@
Loading
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 | } |
Click to load this diff.
Loading diff...
Click to load this diff.
Loading diff...
Learn more Showing 17 files with coverage changes found.
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Service/Predictions/AWSPredictionsService+Rekognition.swift
-1
+1
Loading file...
Changes in
→
AmplifyTests/CategoryTests/Hub/DefaultPluginTests/DefaultHubPluginTests.swift
-1
+1
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Utils/PredictionsErrorHelper.swift
-2
+2
Loading file...
Changes in
→
Amplify/Categories/DataStore/Model/Internal/Schema/ModelSchema.swift
-1
+1
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Internal/NativeWebSocketProvider.swift
-2
+2
Loading file...
Changes in
→
AmplifyPlugins/API/AWSAPICategoryPlugin/Operation/AWSGraphQLOperation+APIOperation.swift
-2
+2
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Utils/IdentifyTextResultTransformers+Tables.swift
-4
+4
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Utils/IdentifyTextResultTransformers+KeyValueSet.swift
-4
+4
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPluginTests/Mocks/Service/MockTranscribeStreamingBehavior.swift
-1
+1
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Utils/IdentifyTextResultTransformers.swift
-25
+25
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPluginTests/Service/PredictionsTest/PredictionsServiceTextractTests.swift
-40
+40
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Service/Predictions/AWSPredictionsService+Transcribe.swift
-20
+20
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPluginTests/Mocks/Service/MockTextractBehavior.swift
-7
+7
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Service/Predictions/AWSPredictionsService+Textract.swift
-22
+22
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Constants/AWSTranscribeStreamingErrorMessage.swift
-9
+9
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Utils/ConvertSpeechToTextTransformers.swift
-7
+7
Loading file...
Changes in
→
AmplifyPlugins/Predictions/AWSPredictionsPlugin/Support/Internal/FormatType+AWSExtension.swift
-9
+9
Loading file...
3 Commits
Files | Coverage |
---|---|
Amplify | 0.02% 47.29% |
AmplifyPlugins | +1.23% 68.34% |
AmplifyTestApp | 44.44% |
AmplifyTestCommon | 26.26% |
AmplifyTests | 0.02% 91.41% |
Project Totals (870 files) | 67.22% |
#903
b8aeafc
#903
8672fe8
#903
8ecaa5b
70970d2