|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import FirebaseRemoteConfig |
| 16 | +import FirebaseRemoteConfigSwift |
| 17 | + |
| 18 | +import XCTest |
| 19 | + |
| 20 | +/// String constants used for testing. |
| 21 | +private enum Constants { |
| 22 | + static let jsonKey = "Recipe" |
| 23 | + static let recipe = ["recipeName": "PB&J", |
| 24 | + "ingredients": ["bread", "peanut butter", "jelly"], |
| 25 | + "cookTime": 7] as [String: AnyHashable] |
| 26 | + static let nonJsonKey = "notJSON" |
| 27 | + static let nonJsonValue = "notJSON" |
| 28 | +} |
| 29 | + |
| 30 | +#if compiler(>=5.5) && canImport(_Concurrency) |
| 31 | + @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *) |
| 32 | + class CodableTests: APITestBase { |
| 33 | + var console: RemoteConfigConsole! |
| 34 | + |
| 35 | + override func setUp() { |
| 36 | + super.setUp() |
| 37 | + do { |
| 38 | + let jsonData = try JSONSerialization.data( |
| 39 | + withJSONObject: Constants.recipe, |
| 40 | + options: .prettyPrinted |
| 41 | + ) |
| 42 | + guard let jsonValue = String(data: jsonData, encoding: .ascii) else { |
| 43 | + fatalError("Failed to make json Value from jsonData") |
| 44 | + } |
| 45 | + if APITests.useFakeConfig { |
| 46 | + fakeConsole.config = [Constants.jsonKey: jsonValue, |
| 47 | + Constants.nonJsonKey: Constants.nonJsonValue] |
| 48 | + } else { |
| 49 | + console = RemoteConfigConsole() |
| 50 | + console.updateRemoteConfigValue(jsonValue, forKey: Constants.jsonKey) |
| 51 | + console.updateRemoteConfigValue(Constants.nonJsonKey, forKey: Constants.nonJsonValue) |
| 52 | + } |
| 53 | + } catch { |
| 54 | + print("Failed to initialize json data in fake Console \(error.localizedDescription)") |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + override func tearDown() { |
| 59 | + super.tearDown() |
| 60 | + |
| 61 | + // If using RemoteConfigConsole, reset remote config values. |
| 62 | + if !APITests.useFakeConfig { |
| 63 | + console.removeRemoteConfigValue(forKey: Constants.jsonKey) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + func testFetchAndActivateWithoutCodable() async throws { |
| 68 | + let status = try await config.fetchAndActivate() |
| 69 | + XCTAssertEqual(status, .successFetchedFromRemote) |
| 70 | + guard let dict = config[Constants.jsonKey].jsonValue as? [String: AnyHashable] else { |
| 71 | + XCTFail("Failed to extract json") |
| 72 | + return |
| 73 | + } |
| 74 | + XCTAssertEqual(dict["recipeName"], "PB&J") |
| 75 | + XCTAssertEqual(dict["ingredients"], ["bread", "peanut butter", "jelly"]) |
| 76 | + XCTAssertEqual(dict["cookTime"], 7) |
| 77 | + XCTAssertEqual( |
| 78 | + config[Constants.jsonKey].jsonValue as! [String: AnyHashable], |
| 79 | + Constants.recipe |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + struct Recipe: Decodable { |
| 84 | + var recipeName: String |
| 85 | + var ingredients: [String] |
| 86 | + var cookTime: Int |
| 87 | + } |
| 88 | + |
| 89 | + func testFetchAndActivateWithCodable() async throws { |
| 90 | + let status = try await config.fetchAndActivate() |
| 91 | + XCTAssertEqual(status, .successFetchedFromRemote) |
| 92 | + guard let value = try config[Constants.jsonKey].decode(asType: Recipe?.self) else { |
| 93 | + XCTFail("Failed to extract json") |
| 94 | + return |
| 95 | + } |
| 96 | + XCTAssertEqual(value.recipeName, "PB&J") |
| 97 | + XCTAssertEqual(value.ingredients, ["bread", "peanut butter", "jelly"]) |
| 98 | + XCTAssertEqual(value.cookTime, 7) |
| 99 | + } |
| 100 | + |
| 101 | + func testFetchAndActivateWithCodableBadJson() async throws { |
| 102 | + let status = try await config.fetchAndActivate() |
| 103 | + XCTAssertEqual(status, .successFetchedFromRemote) |
| 104 | + do { |
| 105 | + _ = try config[Constants.nonJsonKey].decode(asType: String?.self) |
| 106 | + } catch RemoteConfigCodableError.jsonValueError { |
| 107 | + return |
| 108 | + } |
| 109 | + XCTFail("Failed to catch trying to decode non-JSON key as JSON") |
| 110 | + } |
| 111 | + |
| 112 | + struct DataTestDefaults: Encodable { |
| 113 | + var bool: Bool |
| 114 | + var int: Int32 |
| 115 | + var long: Int64 |
| 116 | + var string: String |
| 117 | + } |
| 118 | + |
| 119 | + func testSetEncodeableDefaults() throws { |
| 120 | + let data = DataTestDefaults( |
| 121 | + bool: true, |
| 122 | + int: 2, |
| 123 | + long: 9_876_543_210, |
| 124 | + string: "four" |
| 125 | + ) |
| 126 | + try config.setDefaults(from: data) |
| 127 | + let boolValue = try XCTUnwrap(config.defaultValue(forKey: "bool")).numberValue.boolValue |
| 128 | + XCTAssertTrue(boolValue) |
| 129 | + let intValue = try XCTUnwrap(config.defaultValue(forKey: "int")).numberValue.intValue |
| 130 | + XCTAssertEqual(intValue, 2) |
| 131 | + let longValue = try XCTUnwrap(config.defaultValue(forKey: "long")).numberValue.int64Value |
| 132 | + XCTAssertEqual(longValue, 9_876_543_210) |
| 133 | + let stringValue = try XCTUnwrap(config.defaultValue(forKey: "string")).stringValue |
| 134 | + XCTAssertEqual(stringValue, "four") |
| 135 | + } |
| 136 | + } |
| 137 | +#endif |
0 commit comments