DatabaseTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2023 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 Foundation
  17. import XCTest
  18. import FirebaseCore
  19. import FirebaseFirestore
  20. #if swift(>=5.5.2)
  21. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  22. class DatabaseTests: FSTIntegrationTestCase {
  23. func testCanStillUseDisablePersistenceSettings() async throws {
  24. let settings = db.settings
  25. settings.isPersistenceEnabled = false
  26. db.settings = settings
  27. try await db.document("coll/doc").setData(["foo": "bar"])
  28. let result = try? await db.document("coll/doc").getDocument(source: .cache)
  29. XCTAssertNil(result)
  30. }
  31. func testCanStillUseEnablePersistenceSettings() async throws {
  32. let settings = db.settings
  33. settings.isPersistenceEnabled = true
  34. db.settings = settings
  35. try await db.document("coll/doc").setData(["foo": "bar"])
  36. let result = try? await db.document("coll/doc").getDocument(source: .cache)
  37. XCTAssertEqual(["foo": "bar"], result?.data() as! [String: String])
  38. }
  39. func testCanUseMemoryCacheSettings() async throws {
  40. let settings = db.settings
  41. settings.cacheSettings = MemoryCacheSettings()
  42. db.settings = settings
  43. try await db.document("coll/doc").setData(["foo": "bar"])
  44. let result = try? await db.document("coll/doc").getDocument(source: .cache)
  45. XCTAssertNil(result)
  46. }
  47. func testCanGetDocumentWithMemoryLruGCEnabled() async throws {
  48. let settings = db.settings
  49. settings
  50. .cacheSettings =
  51. MemoryCacheSettings(
  52. garbageCollectorSettings: MemoryLRUGCSettings(sizeBytes: 2_000_000)
  53. )
  54. db.settings = settings
  55. try await db.document("coll/doc").setData(["foo": "bar"])
  56. let result = try? await db.document("coll/doc").getDocument(source: .cache)
  57. XCTAssertEqual(["foo": "bar"], result?.data() as! [String: String])
  58. }
  59. func testCannotGetDocumentWithMemoryEagerGCEnabled() async throws {
  60. let settings = db.settings
  61. settings
  62. .cacheSettings =
  63. MemoryCacheSettings(garbageCollectorSettings: MemoryEagerGCSetting())
  64. db.settings = settings
  65. try await db.document("coll/doc").setData(["foo": "bar"])
  66. let result = try? await db.document("coll/doc").getDocument(source: .cache)
  67. XCTAssertNil(result)
  68. }
  69. func testCanUsePersistentCacheSettings() async throws {
  70. let settings = db.settings
  71. settings.cacheSettings = PersistentCacheSettings()
  72. db.settings = settings
  73. try await db.document("coll/doc").setData(["foo": "bar"])
  74. let result = try? await db.document("coll/doc").getDocument(source: .cache)
  75. XCTAssertEqual(["foo": "bar"], result?.data() as! [String: String])
  76. }
  77. func testCanSetCacheSettingsMultipleTimes() async throws {
  78. let settings = db.settings
  79. settings.cacheSettings = PersistentCacheSettings()
  80. settings.cacheSettings = MemoryCacheSettings()
  81. db.settings = settings
  82. try await db.document("coll/doc").setData(["foo": "bar"])
  83. let result = try? await db.document("coll/doc").getDocument(source: .cache)
  84. XCTAssertNil(result)
  85. }
  86. }
  87. #endif