From 5e5debee2e0e591d55670b3b732ba114646db251 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 29 Oct 2024 07:58:14 -0500 Subject: [PATCH] Add exists property to DocumentSnapshot in Firestore --- .../SkipFirebaseFirestore.swift | 4 ++ .../SkipFirebaseFirestoreTests.swift | 50 +++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/Sources/SkipFirebaseFirestore/SkipFirebaseFirestore.swift b/Sources/SkipFirebaseFirestore/SkipFirebaseFirestore.swift index 8d815f8..726df46 100644 --- a/Sources/SkipFirebaseFirestore/SkipFirebaseFirestore.swift +++ b/Sources/SkipFirebaseFirestore/SkipFirebaseFirestore.swift @@ -679,6 +679,10 @@ public class DocumentSnapshot: KotlinConverting [String: Any] { if let data = doc.getData() { diff --git a/Tests/SkipFirebaseFirestoreTests/SkipFirebaseFirestoreTests.swift b/Tests/SkipFirebaseFirestoreTests/SkipFirebaseFirestoreTests.swift index 1b2ee99..d4c4671 100644 --- a/Tests/SkipFirebaseFirestoreTests/SkipFirebaseFirestoreTests.swift +++ b/Tests/SkipFirebaseFirestoreTests/SkipFirebaseFirestoreTests.swift @@ -196,15 +196,7 @@ final class SkipFirebaseFirestoreTests: XCTestCase { let bos = citiesRef.document("BOS") - try await bos.setData([ - "name": "Boston", - "state": "MA", - "country": "USA", - "capital": false, - "population": 555000, - "regions": ["east_coast", "new_england"], - "time": Timestamp(date: Date(timeIntervalSince1970: 1234)) - ]) + try await bos.setData(Self.bostonData) let bdoc = try await bos.getDocument() XCTAssertEqual("Boston", bdoc.get("name") as? String) @@ -343,6 +335,46 @@ final class SkipFirebaseFirestoreTests: XCTestCase { // SKIP NOWARN await cacheApp.delete() } + + func test_exists_falseForNonExistentDocument() async throws { + XCTAssertEqual(appName, self.app.name) + let dbname = "(default)" + let db: Firestore = Firestore.firestore(app: self.app, database: dbname) + let citiesRef = db.collection("cities") + let bos = citiesRef.document("BOS") + try await bos.setData(Self.bostonData) + let ref = citiesRef.document("NON_EXISTENT_DOCUMENT") + do { + let snapshot = try await ref.getDocument() + XCTAssertFalse(snapshot.exists) + } + } + + func test_exists_trueForExistentDocument() async throws { + XCTAssertEqual(appName, self.app.name) + let dbname = "(default)" + let db: Firestore = Firestore.firestore(app: self.app, database: dbname) + let citiesRef = db.collection("cities") + let bos = citiesRef.document("BOS") + try await bos.setData(Self.bostonData) + let ref = citiesRef.document("BOS") + do { + let snapshot = try await ref.getDocument() + XCTAssertTrue(snapshot.exists) + } + } +} + +extension SkipFirebaseFirestoreTests { + private static let bostonData: [String : Any] = [ + "name": "Boston", + "state": "MA", + "country": "USA", + "capital": false, + "population": 555000, + "regions": ["east_coast", "new_england"], + "time": Timestamp(date: Date(timeIntervalSince1970: 1234)) + ] } struct TestData : Codable, Hashable {