AsyncAwaitIntegrationTests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2021 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 FirebaseFirestore
  17. import Foundation
  18. let emptyBundle = """
  19. {\
  20. "metadata":{\
  21. "id":"test",\
  22. "createTime":{\
  23. "seconds":0,\
  24. "nanos":0\
  25. },\
  26. "version":1,\
  27. "totalDocuments":0,\
  28. "totalBytes":0\
  29. }\
  30. }
  31. """
  32. #if swift(>=5.5.2)
  33. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  34. class AsyncAwaitIntegrationTests: FSTIntegrationTestCase {
  35. func assertQuerySnapshotDataEquals(_ snapshot: Any,
  36. _ expectedData: [[String: Any]]) throws {
  37. let extractedData = FIRQuerySnapshotGetData(snapshot as! QuerySnapshot)
  38. guard extractedData.count == expectedData.count else {
  39. XCTFail(
  40. "Result count mismatch: Expected \(expectedData.count), got \(extractedData.count)"
  41. )
  42. return
  43. }
  44. for index in 0 ..< extractedData.count {
  45. XCTAssertTrue(areDictionariesEqual(extractedData[index], expectedData[index]))
  46. }
  47. }
  48. // TODO(swift testing): update the function to be able to check other value types as well.
  49. func areDictionariesEqual(_ dict1: [String: Any], _ dict2: [String: Any]) -> Bool {
  50. guard dict1.count == dict2.count
  51. else { return false } // Check if the number of elements matches
  52. for (key, value1) in dict1 {
  53. guard let value2 = dict2[key] else { return false }
  54. // Value Checks (Assuming consistent types after the type check)
  55. if let str1 = value1 as? String, let str2 = value2 as? String {
  56. if str1 != str2 { return false }
  57. } else if let int1 = value1 as? Int, let int2 = value2 as? Int {
  58. if int1 != int2 { return false }
  59. } else {
  60. // Handle other potential types or return false for mismatch
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. func testAddData() async throws {
  67. let collection = collectionRef()
  68. let document = try await collection.addDocument(data: [:])
  69. let snapshot = try await document.getDocument()
  70. XCTAssertTrue(snapshot.exists)
  71. }
  72. func testLoadBundleFromData() async throws {
  73. let bundle = "\(emptyBundle.count)\(emptyBundle)"
  74. let bundleProgress = try await db.loadBundle(Data(bundle.utf8))
  75. XCTAssertEqual(LoadBundleTaskState.success, bundleProgress.state)
  76. }
  77. func testLoadBundleFromEmptyDataFails() async throws {
  78. do {
  79. _ = try await db.loadBundle(Data())
  80. XCTFail("Bundle loading should have failed")
  81. } catch {
  82. XCTAssertEqual((error as NSError).domain, FirestoreErrorDomain)
  83. XCTAssertEqual((error as NSError).code, FirestoreErrorCode.unknown.rawValue)
  84. }
  85. }
  86. func testLoadBundleFromStream() async throws {
  87. let bundle = "\(emptyBundle.count)\(emptyBundle)"
  88. let bundleProgress = try await db
  89. .loadBundle(InputStream(data: bundle.data(using: String.Encoding.utf8)!))
  90. XCTAssertEqual(LoadBundleTaskState.success, bundleProgress.state)
  91. }
  92. func testRunTransactionDoesNotCrashOnNilSuccess() async throws {
  93. let value = try await db.runTransaction { transact, error in
  94. nil // should not crash
  95. }
  96. XCTAssertNil(value, "value should be nil on success")
  97. }
  98. func testQuery() async throws {
  99. let collRef = collectionRef(
  100. withDocuments: ["doc1": ["a": 1, "b": 0],
  101. "doc2": ["a": 2, "b": 1],
  102. "doc3": ["a": 3, "b": 2],
  103. "doc4": ["a": 1, "b": 3],
  104. "doc5": ["a": 1, "b": 1]]
  105. )
  106. // Two equalities: a==1 || b==1.
  107. let filter = Filter.orFilter(
  108. [Filter.whereField("a", isEqualTo: 1),
  109. Filter.whereField("b", isEqualTo: 1)]
  110. )
  111. let query = collRef.whereFilter(filter)
  112. let snapshot = try await query.getDocuments(source: FirestoreSource.server)
  113. XCTAssertEqual(FIRQuerySnapshotGetIDs(snapshot),
  114. ["doc1", "doc2", "doc4", "doc5"])
  115. }
  116. func testAutoIndexCreationAfterFailsTermination() async throws {
  117. try await db.terminate()
  118. let enableIndexAutoCreation = {
  119. try FSTExceptionCatcher.catchException {
  120. self.db.persistentCacheIndexManager?.enableIndexAutoCreation()
  121. }
  122. }
  123. XCTAssertThrowsError(try enableIndexAutoCreation(), "The client has already been terminated.")
  124. let disableIndexAutoCreation = {
  125. try FSTExceptionCatcher.catchException {
  126. self.db.persistentCacheIndexManager?.disableIndexAutoCreation()
  127. }
  128. }
  129. XCTAssertThrowsError(
  130. try disableIndexAutoCreation(),
  131. "The client has already been terminated."
  132. )
  133. let deleteAllIndexes = {
  134. try FSTExceptionCatcher.catchException {
  135. self.db.persistentCacheIndexManager?.deleteAllIndexes()
  136. }
  137. }
  138. XCTAssertThrowsError(try deleteAllIndexes(), "The client has already been terminated.")
  139. }
  140. }
  141. #endif