AsyncAwaitIntegrationTests.swift 4.0 KB

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