diff --git a/Sources/Papyrus/PapyrusStore.swift b/Sources/Papyrus/PapyrusStore.swift index 4d78e25..7c880f0 100644 --- a/Sources/Papyrus/PapyrusStore.swift +++ b/Sources/Papyrus/PapyrusStore.swift @@ -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(objects: [T]) async throws where T: Sendable { + guard !objects.isEmpty else { return } + try await withThrowingTaskGroup(of: Void.self) { group in let timestamp = Date.now @@ -227,6 +229,8 @@ public struct PapyrusStore: Sendable { /// Deletes an array of objects. /// - Parameter objects: An array of objects to delete. public func delete(objects: [T]) async throws { + guard !objects.isEmpty else { return } + try await withThrowingTaskGroup(of: Void.self) { group in for object in objects { group.addTask { diff --git a/Tests/Unit/Helpers/Assertions.swift b/Tests/Unit/Helpers/Assertions.swift new file mode 100644 index 0000000..a429d42 --- /dev/null +++ b/Tests/Unit/Helpers/Assertions.swift @@ -0,0 +1,33 @@ +import XCTest + +func XCTAsyncAssertThrow( + _ 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( + _ 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 + ) + } +} diff --git a/Tests/Unit/PapyrusStoreTests.swift b/Tests/Unit/PapyrusStoreTests.swift index 940d3b8..d12be99 100644 --- a/Tests/Unit/PapyrusStoreTests.swift +++ b/Tests/Unit/PapyrusStoreTests.swift @@ -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 @@ -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"))