Skip to content

Commit ac17377

Browse files
committed
add unit test cases
1 parent b27dca7 commit ac17377

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

AmplifyPlugins/API/Sources/AWSAPIPlugin/Operation/AWSGraphQLSubscriptionTaskRunner.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ fileprivate func toAPIError<R: Decodable>(_ errors: [Error], type: R.Type) -> AP
379379
}
380380

381381
#if swift(<5.8)
382-
if let errors = castArray(errors, withElementType: AppSyncRealTimeRequest.Error.self) {
382+
if let errors = errors.cast(to: AppSyncRealTimeRequest.Error.self) {
383383
let hasAuthorizationError = errors.contains(where: { $0 == .unauthorized})
384384
return APIError.operationError(
385385
errorDescription(hasAuthorizationError),
386386
"",
387387
errors.first
388388
)
389-
} else if let errors = castArray(errors, withElementType: GraphQLError.self) {
389+
} else if let errors = errors.cast(to: GraphQLError.self) {
390390
let hasAuthorizationError = errors.map(\.extensions)
391391
.compactMap { $0.flatMap { $0["errorType"]?.stringValue } }
392392
.contains(where: { AppSyncErrorType($0) == .unauthorized })
@@ -429,14 +429,3 @@ fileprivate func toAPIError<R: Decodable>(_ errors: [Error], type: R.Type) -> AP
429429
}
430430
#endif
431431
}
432-
433-
#if swift(<5.8)
434-
fileprivate func castArray<T>(_ arr: Array<Any>, withElementType: T.Type) -> [T]? {
435-
arr.reduce([]) { result, ele in
436-
if let result, let ele = ele as? T {
437-
return result + [ele]
438-
}
439-
return nil
440-
}
441-
}
442-
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
9+
import Foundation
10+
11+
@_spi(AmplifyAPI)
12+
extension Array where Element == Error {
13+
func cast<T>(to type: T.Type) -> [T]? {
14+
self.reduce([]) { partialResult, ele in
15+
if let partialResult, let ele = ele as? T {
16+
return partialResult + [ele]
17+
}
18+
return nil
19+
}
20+
}
21+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
9+
import XCTest
10+
@testable @_spi(AmplifyAPI) import AWSAPIPlugin
11+
12+
class ArrayWithErrorElementExtensionTests: XCTestCase {
13+
14+
15+
16+
func testCast_toCorrectErrorType_returnCastedErrorType() {
17+
let errors: [Error] = [
18+
Error1(), Error1(), Error1()
19+
]
20+
21+
let error1s = errors.cast(to: Error1.self)
22+
XCTAssertNotNil(error1s)
23+
XCTAssertTrue(!error1s!.isEmpty)
24+
XCTAssertEqual(errors.count, error1s!.count)
25+
}
26+
27+
func testCast_toWrongErrorType_returnNil() {
28+
let errors: [Error] = [
29+
Error1(), Error1(), Error1()
30+
]
31+
32+
let error2s = errors.cast(to: Error2.self)
33+
XCTAssertNil(error2s)
34+
}
35+
36+
func testCast_partiallyToWrongErrorType_returnNil() {
37+
let errors: [Error] = [
38+
Error2(), Error2(), Error1()
39+
]
40+
41+
let error2s = errors.cast(to: Error2.self)
42+
XCTAssertNil(error2s)
43+
}
44+
45+
struct Error1: Error { }
46+
47+
struct Error2: Error { }
48+
}

0 commit comments

Comments
 (0)