Skip to content

Commit 4592179

Browse files
authored
test: add more unit test cases (aws-amplify#4)
1 parent 40bc1f6 commit 4592179

File tree

6 files changed

+458
-42
lines changed

6 files changed

+458
-42
lines changed

Sources/AWSAppSyncApolloExtensions/Utilities/AppSyncApolloLogger.swift

+12-38
Original file line numberDiff line numberDiff line change
@@ -41,69 +41,43 @@ public enum AppSyncApolloLogger {
4141
}
4242
}
4343

44-
// iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *
45-
static func error(_ log: String) {
46-
// Always logged, no conditional check needed
47-
if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
48-
os_log("%@", type: .error, log)
49-
} else {
50-
NSLog("%@", log)
51-
}
44+
static func error(_ log: @autoclosure () -> String) {
45+
os_log("%@", type: .error, log())
5246
}
5347

54-
static func error(_ error: Error) {
55-
if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
56-
os_log("%@", type: .error, error.localizedDescription)
57-
} else {
58-
NSLog("%@", error.localizedDescription)
59-
}
48+
static func error(_ error: @autoclosure () -> Error) {
49+
os_log("%@", type: .error, error().localizedDescription)
6050
}
6151

62-
static func warn(_ log: String) {
52+
static func warn(_ log: @autoclosure () -> String) {
6353
guard logLevel.rawValue >= LogLevel.warn.rawValue else {
6454
return
6555
}
6656

67-
if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
68-
os_log("%@", type: .info, log)
69-
} else {
70-
NSLog("%@", log)
71-
}
57+
os_log("%@", type: .info, log())
7258
}
7359

74-
static func info(_ log: String) {
60+
static func info(_ log: @autoclosure () -> String) {
7561
guard logLevel.rawValue >= LogLevel.info.rawValue else {
7662
return
7763
}
7864

79-
if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
80-
os_log("%@", type: .info, log)
81-
} else {
82-
NSLog("%@", log)
83-
}
65+
os_log("%@", type: .info, log())
8466
}
8567

86-
static func debug(_ log: String) {
68+
static func debug(_ log: @autoclosure () -> String) {
8769
guard logLevel.rawValue >= LogLevel.debug.rawValue else {
8870
return
8971
}
9072

91-
if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
92-
os_log("%@", type: .debug, log)
93-
} else {
94-
NSLog("%@", log)
95-
}
73+
os_log("%@", type: .debug, log())
9674
}
9775

98-
static func verbose(_ log: String) {
76+
static func verbose(_ log: @autoclosure () -> String) {
9977
guard logLevel.rawValue >= LogLevel.verbose.rawValue else {
10078
return
10179
}
10280

103-
if #available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *) {
104-
os_log("%@", type: .debug, log)
105-
} else {
106-
NSLog("%@", log)
107-
}
81+
os_log("%@", type: .debug, log())
10882
}
10983
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 import AWSAppSyncApolloExtensions
11+
12+
class AppSyncApolloLoggerTests: XCTestCase {
13+
14+
override func tearDown() async throws {
15+
// reset to default
16+
AppSyncApolloLogger.logLevel = AppSyncApolloLogger.LogLevel.error
17+
}
18+
19+
func testLogger_withDefaulLogLevel_thenOnlyRecordsErrorLogs() async {
20+
let expectations = LogExpectations()
21+
AppSyncApolloLogger.error("error \(expectations.error.fulfill())")
22+
23+
AppSyncApolloLogger.warn("warn \(expectations.warn.fulfill())")
24+
expectations.warn.isInverted = true
25+
26+
AppSyncApolloLogger.info("info \(expectations.info.fulfill())")
27+
expectations.info.isInverted = true
28+
29+
AppSyncApolloLogger.debug("debug \(expectations.debug.fulfill())")
30+
expectations.debug.isInverted = true
31+
32+
AppSyncApolloLogger.verbose("verbose \(expectations.verbose.fulfill())")
33+
expectations.verbose.isInverted = true
34+
35+
await fulfillment(of: expectations.all, timeout: 1)
36+
}
37+
38+
func testLogger_withErrorLogLevel_thenOnlyRecordsErrorLogs() async {
39+
AppSyncApolloLogger.logLevel = .error
40+
let expectations = LogExpectations()
41+
AppSyncApolloLogger.error("error \(expectations.error.fulfill())")
42+
43+
AppSyncApolloLogger.warn("warn \(expectations.warn.fulfill())")
44+
expectations.warn.isInverted = true
45+
46+
AppSyncApolloLogger.info("info \(expectations.info.fulfill())")
47+
expectations.info.isInverted = true
48+
49+
AppSyncApolloLogger.debug("debug \(expectations.debug.fulfill())")
50+
expectations.debug.isInverted = true
51+
52+
AppSyncApolloLogger.verbose("verbose \(expectations.verbose.fulfill())")
53+
expectations.verbose.isInverted = true
54+
55+
await fulfillment(of: expectations.all, timeout: 1)
56+
}
57+
58+
func testLogger_withWarnLogLevel_thenOnlyRecordsErrorWarnLogs() async {
59+
AppSyncApolloLogger.logLevel = .warn
60+
let expectations = LogExpectations()
61+
AppSyncApolloLogger.error("error \(expectations.error.fulfill())")
62+
63+
AppSyncApolloLogger.warn("warn \(expectations.warn.fulfill())")
64+
65+
AppSyncApolloLogger.info("info \(expectations.info.fulfill())")
66+
expectations.info.isInverted = true
67+
68+
AppSyncApolloLogger.debug("debug \(expectations.debug.fulfill())")
69+
expectations.debug.isInverted = true
70+
71+
AppSyncApolloLogger.verbose("verbose \(expectations.verbose.fulfill())")
72+
expectations.verbose.isInverted = true
73+
74+
await fulfillment(of: expectations.all, timeout: 1)
75+
}
76+
77+
func testLogger_withInfoLogLevel_thenOnlyRecordsErrorWarnInfoLogs() async {
78+
AppSyncApolloLogger.logLevel = .info
79+
let expectations = LogExpectations()
80+
AppSyncApolloLogger.error("error \(expectations.error.fulfill())")
81+
82+
AppSyncApolloLogger.warn("warn \(expectations.warn.fulfill())")
83+
84+
AppSyncApolloLogger.info("info \(expectations.info.fulfill())")
85+
86+
AppSyncApolloLogger.debug("debug \(expectations.debug.fulfill())")
87+
expectations.debug.isInverted = true
88+
89+
AppSyncApolloLogger.verbose("verbose \(expectations.verbose.fulfill())")
90+
expectations.verbose.isInverted = true
91+
92+
await fulfillment(of: expectations.all, timeout: 1)
93+
}
94+
95+
func testLogger_withDebugLogLevel_thenOnlyRecordsErrorWarnInfoDebugLogs() async {
96+
AppSyncApolloLogger.logLevel = .debug
97+
let expectations = LogExpectations()
98+
AppSyncApolloLogger.error("error \(expectations.error.fulfill())")
99+
100+
AppSyncApolloLogger.warn("warn \(expectations.warn.fulfill())")
101+
102+
AppSyncApolloLogger.info("info \(expectations.info.fulfill())")
103+
104+
AppSyncApolloLogger.debug("debug \(expectations.debug.fulfill())")
105+
106+
AppSyncApolloLogger.verbose("verbose \(expectations.verbose.fulfill())")
107+
expectations.verbose.isInverted = true
108+
109+
await fulfillment(of: expectations.all, timeout: 1)
110+
}
111+
112+
func testLogger_withVerboseLogLevel_thenOnlyRecordsErrorWarnInfoDebugVerboseLogs() async {
113+
AppSyncApolloLogger.logLevel = .verbose
114+
let expectations = LogExpectations()
115+
AppSyncApolloLogger.error("error \(expectations.error.fulfill())")
116+
117+
AppSyncApolloLogger.warn("warn \(expectations.warn.fulfill())")
118+
119+
AppSyncApolloLogger.info("info \(expectations.info.fulfill())")
120+
121+
AppSyncApolloLogger.debug("debug \(expectations.debug.fulfill())")
122+
123+
AppSyncApolloLogger.verbose("verbose \(expectations.verbose.fulfill())")
124+
125+
await fulfillment(of: expectations.all, timeout: 1)
126+
}
127+
128+
}
129+
130+
131+
fileprivate struct LogExpectations {
132+
let error = XCTestExpectation(description: "error message was evaluated")
133+
let warn = XCTestExpectation(description: "warn message was evaluated")
134+
let info = XCTestExpectation(description: "info message was evaluated")
135+
let debug = XCTestExpectation(description: "debug message was evaluated")
136+
let verbose = XCTestExpectation(description: "verbose message was evaluated")
137+
138+
var all: [XCTestExpectation] {
139+
[error, warn, info, debug, verbose]
140+
}
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 import AWSAppSyncApolloExtensions
11+
12+
class Base64EncoderTests: XCTestCase {
13+
14+
func testBase64EncodedString_generateCorrectResult() throws {
15+
XCTAssertEqual(
16+
try "aws-appsync-apollo-extensions-swift".base64EncodedString(),
17+
"YXdzLWFwcHN5bmMtYXBvbGxvLWV4dGVuc2lvbnMtc3dpZnQ="
18+
)
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 import AWSAppSyncApolloExtensions
11+
12+
final class TaskQueueTests: XCTestCase {
13+
14+
/// Test basic TaskQueue.sync behavior
15+
///
16+
/// - Given: A task queue
17+
/// - When: I add tasks to the queue using the `sync` method
18+
/// - Then: The tasks execute in the order added
19+
func testSync() async throws {
20+
for _ in 1 ... 1_000 {
21+
try await doSyncTest()
22+
}
23+
}
24+
25+
func doSyncTest() async throws {
26+
let expectation1 = expectation(description: "expectation1")
27+
let expectation2 = expectation(description: "expectation2")
28+
let expectation3 = expectation(description: "expectation3")
29+
30+
let taskQueue = TaskQueue<Void>()
31+
try await taskQueue.sync {
32+
try await Task.sleep(nanoseconds: 1)
33+
expectation1.fulfill()
34+
}
35+
36+
try await taskQueue.sync {
37+
try await Task.sleep(nanoseconds: 1)
38+
expectation2.fulfill()
39+
}
40+
41+
try await taskQueue.sync {
42+
try await Task.sleep(nanoseconds: 1)
43+
expectation3.fulfill()
44+
}
45+
46+
await fulfillment(of: [expectation1, expectation2, expectation3], enforceOrder: true)
47+
}
48+
49+
func testAsync() async throws {
50+
let taskCount = 1_000
51+
let expectations: [XCTestExpectation] = (0..<taskCount).map {
52+
expectation(description: "Expected execution of a task number \($0)")
53+
}
54+
55+
let taskQueue = TaskQueue<Void>()
56+
57+
for i in 0..<taskCount {
58+
taskQueue.async {
59+
expectations[i].fulfill()
60+
}
61+
}
62+
63+
await fulfillment(of: expectations, enforceOrder: true)
64+
}
65+
}

0 commit comments

Comments
 (0)