AsyncAwaitIntegrationTests.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }
  69. #endif