|
| 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 | +} |
0 commit comments