Skip to content

Commit

Permalink
fix: return if collection is empty (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
reddavis authored Nov 10, 2023
1 parent b9b3da5 commit 7be0a60
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/Papyrus/PapyrusStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public struct PapyrusStore: Sendable {
/// Saves all objects to the store.
/// - Parameter objects: An array of objects to add to the store.
public func save<T: Papyrus>(objects: [T]) async throws where T: Sendable {
guard !objects.isEmpty else { return }

try await withThrowingTaskGroup(of: Void.self) { group in
let timestamp = Date.now

Expand Down Expand Up @@ -227,6 +229,8 @@ public struct PapyrusStore: Sendable {
/// Deletes an array of objects.
/// - Parameter objects: An array of objects to delete.
public func delete<T: Papyrus>(objects: [T]) async throws {
guard !objects.isEmpty else { return }

try await withThrowingTaskGroup(of: Void.self) { group in
for object in objects {
group.addTask {
Expand Down
33 changes: 33 additions & 0 deletions Tests/Unit/Helpers/Assertions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import XCTest

func XCTAsyncAssertThrow<T>(
_ closure: () async throws -> T,
errorHandler: ((Error) -> Void)? = nil,
file: StaticString = #filePath,
line: UInt = #line
) async {
do {
_ = try await closure()
XCTFail(
"Failed to throw error",
file: file,
line: line
)
} catch {}
}

func XCTAsyncAssertNoThrow<T>(
_ closure: () async throws -> T,
file: StaticString = #filePath,
line: UInt = #line
) async {
do {
_ = try await closure()
} catch {
XCTFail(
"Unexpected error thrown \(error)",
file: file,
line: line
)
}
}
14 changes: 14 additions & 0 deletions Tests/Unit/PapyrusStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ final class PapyrusStoreTests: XCTestCase {
XCTAssertNotNil(self.store.object(id: idB, of: ExampleB.self))
}

func test_savingEmptyObjectsArray() async throws {
let objects: [ExampleA] = []
await XCTAsyncAssertNoThrow {
try await self.store.save(objects: objects)
}
}

func test_updatesReceivedOnSaving() async throws {
let expectation = self.expectation(description: "Received values")
expectation.expectedFulfillmentCount = 1
Expand Down Expand Up @@ -133,6 +140,13 @@ final class PapyrusStoreTests: XCTestCase {
XCTAssertNil(fetchedObjectB1)
}

func test_deletingEmptyObjectsArray() async throws {
let objects: [ExampleA] = []
await XCTAsyncAssertNoThrow {
try await self.store.delete(objects: objects)
}
}

func test_deleteAll() async throws {
try self.store.save(ExampleB(id: "1"))
try self.store.save(ExampleB(id: "2"))
Expand Down

0 comments on commit 7be0a60

Please sign in to comment.