Todo repository protocol (#8)

This PR contains the work done to decouple the direct use of the `ModelContext` from `SwiftData` from the view model that uses it. In addition, the project transitioned to use Swift 6 and strict concurrency which brought some issues with the unit tests, so they were amended as well.

Reviewed-on: rock-n-code/todos-app#8
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #8.
This commit is contained in:
2026-02-11 18:22:26 +00:00
committed by javier
parent b8e6e515dc
commit bbd7ec264b
13 changed files with 187 additions and 115 deletions
+49
View File
@@ -0,0 +1,49 @@
@testable import class Todos.Todo
@testable import protocol Todos.TodoRepository
/// A mock implementation of the TodoRepository protocol for unit testing.
///
/// This mock repository provides a simple in-memory storage mechanism for testing
/// without requiring a full SwiftData ModelContext setup. It tracks inserted and
/// deleted todos, allowing tests to verify repository operations.
final class MockTodoRepository {
// MARK: Properties
/// Array of todo tasks managed by this mock repository.
///
/// This property tracks all todos in the repository, maintaining state across
/// insert and delete operations. It serves as the in-memory storage for the mock,
/// allowing tests to verify repository behavior without requiring SwiftData.
private(set) var todos: [Todo]
// MARK: Initializers
/// Creates a new mock repository with an optional initial set of todo tasks.
///
/// This initializer allows tests to set up a repository with pre-existing todos
/// or start with an empty repository. The default parameter value provides
/// convenient initialization for most test cases.
///
/// - Parameter todos: An array of todo tasks to initialize the repository with. Defaults to an empty array.
init(todos: [Todo] = []) {
self.todos = todos
}
}
// MARK: - TodoRepository
extension MockTodoRepository: TodoRepository {
// MARK: Functions
func delete(_ todo: Todo) {
todos.removeAll { $0.id == todo.id }
}
func insert(_ todo: Todo) {
todos.append(todo)
}
}