Skip to content

Commit

Permalink
Merge pull request #18 from roanutil/accidental-transaction-author-pa…
Browse files Browse the repository at this point in the history
…rameter-on-batch-read

Accidental transaction author parameter on batch read
  • Loading branch information
roanutil authored Jun 12, 2023
2 parents 9fa4ae4 + d201954 commit d4dc88e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/CoreDataRepository/CoreDataRepository+Batch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,20 @@ extension CoreDataRepository {
/// - urls: [URL]
/// - Returns
/// - (success: [Model, failed: [Model])
@available(*, deprecated, message: "This method has an unused parameter for transactionAuthor.")
public func read<Model: UnmanagedModel>(
urls: [URL],
transactionAuthor _: String? = nil
) async -> (success: [Model], failed: [URL]) {
await read(urls: urls)
}

/// Batch update objects in CoreData
/// - Parameters
/// - urls: [URL]
/// - Returns
/// - (success: [Model, failed: [Model])
public func read<Model: UnmanagedModel>(urls: [URL]) async -> (success: [Model], failed: [URL]) {
var successes = [Model]()
var failures = [URL]()
await withTaskGroup(of: _Result<Model, URL>.self, body: { [weak self] group in
Expand Down
22 changes: 22 additions & 0 deletions Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ final class BatchRepositoryTests: CoreDataXCTestCase {
try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp)
}

func testDeprecatedReadSuccess() async throws {
let fetchRequest = NSFetchRequest<RepoMovie>(entityName: "RepoMovie")
var movies = [Movie]()
try await repositoryContext().perform {
let count = try self.repositoryContext().count(for: fetchRequest)
XCTAssertEqual(count, 0, "Count of objects in CoreData should be zero at the start of each test.")

let repoMovies = try self.movies
.map(self.mapDictToRepoMovie(_:))
try self.repositoryContext().save()
movies = repoMovies.map(\.asUnmanaged)
}

let result: (success: [Movie], failed: [URL]) = try await repository()
.read(urls: movies.compactMap(\.url), transactionAuthor: "Unused")

XCTAssertEqual(result.success.count, movies.count)
XCTAssertEqual(result.failed.count, 0)

XCTAssertEqual(Set(movies), Set(result.success))
}

func testReadSuccess() async throws {
let fetchRequest = NSFetchRequest<RepoMovie>(entityName: "RepoMovie")
var movies = [Movie]()
Expand Down

0 comments on commit d4dc88e

Please sign in to comment.