AsyncAwaitIntegrationTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 FirebaseFirestoreSwift
  18. import Foundation
  19. let emptyBundle = """
  20. {\
  21. "metadata":{\
  22. "id":"test",\
  23. "createTime":{\
  24. "seconds":0,\
  25. "nanos":0\
  26. },\
  27. "version":1,\
  28. "totalDocuments":0,\
  29. "totalBytes":0\
  30. }\
  31. }
  32. """
  33. #if swift(>=5.5.2)
  34. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  35. class AsyncAwaitIntegrationTests: FSTIntegrationTestCase {
  36. func testAddData() async throws {
  37. let collection = collectionRef()
  38. let document = try await collection.addDocument(data: [:])
  39. let snapshot = try await document.getDocument()
  40. XCTAssertTrue(snapshot.exists)
  41. }
  42. func testLoadBundleFromData() async throws {
  43. let bundle = "\(emptyBundle.count)\(emptyBundle)"
  44. let bundleProgress = try await db.loadBundle(Data(bundle.utf8))
  45. XCTAssertEqual(LoadBundleTaskState.success, bundleProgress.state)
  46. }
  47. func testLoadBundleFromEmptyDataFails() async throws {
  48. do {
  49. _ = try await db.loadBundle(Data())
  50. XCTFail("Bundle loading should have failed")
  51. } catch {
  52. XCTAssertEqual((error as NSError).domain, FirestoreErrorDomain)
  53. XCTAssertEqual((error as NSError).code, FirestoreErrorCode.unknown.rawValue)
  54. }
  55. }
  56. func testLoadBundleFromStream() async throws {
  57. let bundle = "\(emptyBundle.count)\(emptyBundle)"
  58. let bundleProgress = try await db
  59. .loadBundle(InputStream(data: bundle.data(using: String.Encoding.utf8)!))
  60. XCTAssertEqual(LoadBundleTaskState.success, bundleProgress.state)
  61. }
  62. func testRunTransactionDoesNotCrashOnNilSuccess() async throws {
  63. let value = try await db.runTransaction { transact, error in
  64. nil // should not crash
  65. }
  66. XCTAssertNil(value, "value should be nil on success")
  67. }
  68. func testQuery() async throws {
  69. let collRef = collectionRef(
  70. withDocuments: ["doc1": ["a": 1, "b": 0],
  71. "doc2": ["a": 2, "b": 1],
  72. "doc3": ["a": 3, "b": 2],
  73. "doc4": ["a": 1, "b": 3],
  74. "doc5": ["a": 1, "b": 1]]
  75. )
  76. // Two equalities: a==1 || b==1.
  77. let filter = Filter.orFilter(
  78. [Filter.whereField("a", isEqualTo: 1),
  79. Filter.whereField("b", isEqualTo: 1)]
  80. )
  81. let query = collRef.whereFilter(filter)
  82. let snapshot = try await query.getDocuments(source: FirestoreSource.server)
  83. XCTAssertEqual(FIRQuerySnapshotGetIDs(snapshot),
  84. ["doc1", "doc2", "doc4", "doc5"])
  85. }
  86. func testAutoIndexCreationAfterFailsTermination() async throws {
  87. try await db.terminate()
  88. let enableIndexAutoCreation = {
  89. try FSTExceptionCatcher.catchException {
  90. self.db.persistentCacheIndexManager?.enableIndexAutoCreation()
  91. }
  92. }
  93. XCTAssertThrowsError(try enableIndexAutoCreation(), "The client has already been terminated.")
  94. let disableIndexAutoCreation = {
  95. try FSTExceptionCatcher.catchException {
  96. self.db.persistentCacheIndexManager?.disableIndexAutoCreation()
  97. }
  98. }
  99. XCTAssertThrowsError(
  100. try disableIndexAutoCreation(),
  101. "The client has already been terminated."
  102. )
  103. let deleteAllIndexes = {
  104. try FSTExceptionCatcher.catchException {
  105. self.db.persistentCacheIndexManager?.deleteAllIndexes()
  106. }
  107. }
  108. XCTAssertThrowsError(try deleteAllIndexes(), "The client has already been terminated.")
  109. }
  110. }
  111. #endif