Skip to content

Commit

Permalink
Merge pull request #15 from Kevin-French/firestore-documentsnapshot-e…
Browse files Browse the repository at this point in the history
…xists

Add exists property to DocumentSnapshot in Firestore
  • Loading branch information
marcprux authored Oct 29, 2024
2 parents b38dc57 + 5e5debe commit eb45685
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Sources/SkipFirebaseFirestore/SkipFirebaseFirestore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,10 @@ public class DocumentSnapshot: KotlinConverting<com.google.firebase.firestore.Do
public var documentID: String {
doc.getId()
}

public var exists: Bool {
doc.exists()
}

public func data() -> [String: Any] {
if let data = doc.getData() {
Expand Down
50 changes: 41 additions & 9 deletions Tests/SkipFirebaseFirestoreTests/SkipFirebaseFirestoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit eb45685

Please sign in to comment.