PropertyWrapperDefaultConfigsTests.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2022 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import FirebaseCore
  17. import FirebaseRemoteConfig
  18. import FirebaseRemoteConfigSwift
  19. import XCTest
  20. let ConfigKeyForThisTestOnly = "PropertyWrapperDefaultConfigsTestsKey"
  21. #if compiler(>=5.5.2) && canImport(_Concurrency)
  22. @available(iOS 14.0, macOS 11.0, macCatalyst 14.0, tvOS 14.0, watchOS 7.0, *)
  23. class PropertyWrapperDefaultConfigsTests: XCTestCase {
  24. struct Recipe: Decodable, Encodable {
  25. var recipeName: String
  26. var ingredients: [String]
  27. var cookTime: Int
  28. }
  29. static let defaultRecipe = Recipe(
  30. recipeName: "muffin", ingredients: ["flour", "sugar"], cookTime: 45
  31. )
  32. // MARK: - Test Remote Config default values with property wrapper
  33. struct DefaultsValuesTester {
  34. @RemoteConfigProperty(
  35. key: ConfigKeyForThisTestOnly,
  36. fallback: Recipe(recipeName: "test", ingredients: [], cookTime: 0)
  37. )
  38. var dictValue: Recipe
  39. }
  40. override class func setUp() {
  41. if FirebaseApp.app() == nil {
  42. let options = FirebaseOptions(googleAppID: "1:123:ios:123abc",
  43. gcmSenderID: "correct_gcm_sender_id")
  44. options.apiKey = "A23456789012345678901234567890123456789"
  45. options.projectID = "Fake Project"
  46. FirebaseApp.configure(options: options)
  47. }
  48. }
  49. func testDefaultValues() async throws {
  50. try? RemoteConfig.remoteConfig().setDefaults(
  51. from: [ConfigKeyForThisTestOnly: PropertyWrapperDefaultConfigsTests.defaultRecipe]
  52. )
  53. let tester = await DefaultsValuesTester()
  54. let dictValue = await tester.dictValue
  55. XCTAssertEqual(dictValue.recipeName, "muffin")
  56. XCTAssertEqual(dictValue.cookTime, 45)
  57. XCTAssertEqual(dictValue.ingredients, ["flour", "sugar"])
  58. }
  59. }
  60. #endif