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:
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import Testing
|
||||
|
||||
@testable import Todos
|
||||
|
||||
@MainActor
|
||||
@Suite("TodoGroup enumeration tests")
|
||||
struct TodoGroupTests {
|
||||
|
||||
@@ -12,7 +12,7 @@ struct TodoGroupTests {
|
||||
@Test(
|
||||
"canShowCompleted returns correct values",
|
||||
arguments: zip(
|
||||
TodoGroup.allCases,
|
||||
[TodoGroup.today, .upcoming, .overdue, .completed],
|
||||
[true, true, true, false]
|
||||
)
|
||||
)
|
||||
@@ -31,7 +31,7 @@ struct TodoGroupTests {
|
||||
@Test(
|
||||
"canReorder returns correct values",
|
||||
arguments: zip(
|
||||
TodoGroup.allCases,
|
||||
[TodoGroup.today, .upcoming, .overdue, .completed],
|
||||
[true, true, true, false]
|
||||
)
|
||||
)
|
||||
@@ -50,7 +50,7 @@ struct TodoGroupTests {
|
||||
@Test(
|
||||
"group color match expected values",
|
||||
arguments: zip(
|
||||
TodoGroup.allCases,
|
||||
[TodoGroup.today, .upcoming, .overdue, .completed],
|
||||
[Color.blue, .green, .red, .gray]
|
||||
)
|
||||
)
|
||||
@@ -69,7 +69,7 @@ struct TodoGroupTests {
|
||||
@Test(
|
||||
"group icon names match expected values",
|
||||
arguments: zip(
|
||||
TodoGroup.allCases,
|
||||
[TodoGroup.today, .upcoming, .overdue, .completed],
|
||||
["calendar.badge.clock", "calendar", "calendar.badge.exclamationmark", "checkmark.circle.fill"]
|
||||
)
|
||||
)
|
||||
@@ -88,7 +88,7 @@ struct TodoGroupTests {
|
||||
@Test(
|
||||
"group titles match expected values",
|
||||
arguments: zip(
|
||||
TodoGroup.allCases,
|
||||
[TodoGroup.today, .upcoming, .overdue, .completed],
|
||||
["today", "upcoming", "overdue", "completed"]
|
||||
)
|
||||
)
|
||||
@@ -124,7 +124,10 @@ struct TodoGroupTests {
|
||||
|
||||
// MARK: Identifiable Tests
|
||||
|
||||
@Test("id returns an identity of a case", arguments: TodoGroup.allCases)
|
||||
@Test(
|
||||
"id returns an identity of a case",
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
func id(for group: TodoGroup) {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
|
||||
@@ -4,6 +4,7 @@ import Testing
|
||||
|
||||
@testable import Todos
|
||||
|
||||
@MainActor
|
||||
@Suite("TodoFetchDescriptor+Inits extension tests")
|
||||
struct TodoFetchDescriptor_InitsTests {
|
||||
|
||||
@@ -11,9 +12,8 @@ struct TodoFetchDescriptor_InitsTests {
|
||||
|
||||
@Test(
|
||||
"initialization when empty tasks",
|
||||
arguments: TodoGroup.allCases
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
@MainActor
|
||||
func initializationWhenEmptyTasks(for group: TodoGroup) throws {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
@@ -32,9 +32,8 @@ struct TodoFetchDescriptor_InitsTests {
|
||||
|
||||
@Test(
|
||||
"initialization with show completed disabled",
|
||||
arguments: TodoGroup.allCases
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
@MainActor
|
||||
func initializationWithNoShowCompleted(for group: TodoGroup) throws {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
@@ -70,9 +69,8 @@ struct TodoFetchDescriptor_InitsTests {
|
||||
|
||||
@Test(
|
||||
"initialization with show completed enabled",
|
||||
arguments: TodoGroup.allCases
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
@MainActor
|
||||
func initializationWithShowCompleted(for group: TodoGroup) throws {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import Testing
|
||||
|
||||
@testable import Todos
|
||||
|
||||
@MainActor
|
||||
@Suite("ContentView view model tests")
|
||||
struct ContentViewModelTests {
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import Testing
|
||||
|
||||
@testable import Todos
|
||||
|
||||
@MainActor
|
||||
@Suite("NewTaskView view model tests")
|
||||
struct NewTaskViewModelTests {
|
||||
|
||||
@@ -121,11 +121,9 @@ struct NewTaskViewModelTests {
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test("save creates todo with all properties")
|
||||
@MainActor
|
||||
func saveWithAllProperties() throws {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
let context = container.mainContext
|
||||
let repository = MockTodoRepository()
|
||||
let viewModel = NewTaskView.ViewModel()
|
||||
|
||||
// WHEN
|
||||
@@ -133,12 +131,10 @@ struct NewTaskViewModelTests {
|
||||
viewModel.note = "Finish by end of week"
|
||||
viewModel.due = Date(timeIntervalSince1970: 1000000000)
|
||||
|
||||
viewModel.save(to: context)
|
||||
viewModel.save(to: repository)
|
||||
|
||||
// THEN
|
||||
let descriptor = FetchDescriptor<Todo>()
|
||||
let todos = try context.fetch(descriptor)
|
||||
let todo = try #require(todos.first)
|
||||
let todo = try #require(repository.todos.first)
|
||||
|
||||
#expect(todo.title == viewModel.title)
|
||||
#expect(todo.note == viewModel.note)
|
||||
@@ -147,11 +143,9 @@ struct NewTaskViewModelTests {
|
||||
}
|
||||
|
||||
@Test("save creates todo without note when note is empty")
|
||||
@MainActor
|
||||
func saveWithEmptyNote() throws {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
let context = container.mainContext
|
||||
let repository = MockTodoRepository()
|
||||
let viewModel = NewTaskView.ViewModel()
|
||||
|
||||
// WHEN
|
||||
@@ -159,12 +153,10 @@ struct NewTaskViewModelTests {
|
||||
viewModel.note = ""
|
||||
viewModel.due = Date()
|
||||
|
||||
viewModel.save(to: context)
|
||||
viewModel.save(to: repository)
|
||||
|
||||
// THEN
|
||||
let descriptor = FetchDescriptor<Todo>()
|
||||
let todos = try context.fetch(descriptor)
|
||||
let todo = try #require(todos.first)
|
||||
let todo = try #require(repository.todos.first)
|
||||
|
||||
#expect(todo.title == viewModel.title)
|
||||
#expect(todo.note == nil)
|
||||
@@ -173,11 +165,9 @@ struct NewTaskViewModelTests {
|
||||
}
|
||||
|
||||
@Test("save creates todo with note containing only whitespace as nil")
|
||||
@MainActor
|
||||
func saveWithWhitespaceNote() throws {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
let context = container.mainContext
|
||||
let repository = MockTodoRepository()
|
||||
let viewModel = NewTaskView.ViewModel()
|
||||
|
||||
// WHEN
|
||||
@@ -185,12 +175,10 @@ struct NewTaskViewModelTests {
|
||||
viewModel.note = " "
|
||||
viewModel.due = Date()
|
||||
|
||||
viewModel.save(to: context)
|
||||
viewModel.save(to: repository)
|
||||
|
||||
// THEN
|
||||
let descriptor = FetchDescriptor<Todo>()
|
||||
let todos = try context.fetch(descriptor)
|
||||
let todo = try #require(todos.first)
|
||||
let todo = try #require(repository.todos.first)
|
||||
|
||||
#expect(todo.title == viewModel.title)
|
||||
#expect(todo.note == nil)
|
||||
@@ -199,46 +187,24 @@ struct NewTaskViewModelTests {
|
||||
}
|
||||
|
||||
@Test("save inserts multiple todos independently")
|
||||
@MainActor
|
||||
func saveMultipleTodos() throws {
|
||||
func saveMultipleTodos() {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
let context = container.mainContext
|
||||
let repository = MockTodoRepository()
|
||||
let viewModelFirst = NewTaskView.ViewModel()
|
||||
let viewModelSecond = NewTaskView.ViewModel()
|
||||
|
||||
// WHEN
|
||||
viewModelFirst.title = "First task"
|
||||
viewModelFirst.save(to: context)
|
||||
viewModelFirst.save(to: repository)
|
||||
|
||||
viewModelSecond.title = "Second task"
|
||||
viewModelSecond.note = "With a note"
|
||||
viewModelSecond.save(to: context)
|
||||
viewModelSecond.save(to: repository)
|
||||
|
||||
// THEN
|
||||
let descriptor = FetchDescriptor<Todo>()
|
||||
let todos = try context.fetch(descriptor)
|
||||
|
||||
#expect(todos.count == 2)
|
||||
#expect(todos.contains { $0.title == "First task" })
|
||||
#expect(todos.contains { $0.title == "Second task" })
|
||||
#expect(repository.todos.count == 2)
|
||||
#expect(repository.todos.contains { $0.title == "First task" })
|
||||
#expect(repository.todos.contains { $0.title == "Second task" })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension NewTaskViewModelTests {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
var modelContainer: ModelContainer {
|
||||
get throws {
|
||||
try ModelContainer(
|
||||
for: Todo.self,
|
||||
configurations: .init(isStoredInMemoryOnly: true)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import Testing
|
||||
|
||||
@testable import Todos
|
||||
|
||||
@MainActor
|
||||
@Suite("TaskGroupListView view model tests")
|
||||
struct TaskGroupListViewModelTests {
|
||||
|
||||
@@ -33,7 +33,7 @@ struct TaskGroupListViewModelTests {
|
||||
|
||||
@Test(
|
||||
"count returns zero when empty data",
|
||||
arguments: TodoGroup.allCases
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
func countWhenEmptyData(for group: TodoGroup) throws {
|
||||
// GIVEN
|
||||
@@ -48,7 +48,7 @@ struct TaskGroupListViewModelTests {
|
||||
|
||||
@Test(
|
||||
"count returns correct count when filled data",
|
||||
arguments: TodoGroup.allCases
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
func countWhenFilledData(for group: TodoGroup) throws {
|
||||
// GIVEN
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import SwiftUI
|
||||
import Testing
|
||||
|
||||
@testable import Todos
|
||||
|
||||
@MainActor
|
||||
@Suite("TaskList view model tests")
|
||||
struct TaskListViewModelTests {
|
||||
|
||||
@@ -12,7 +11,7 @@ struct TaskListViewModelTests {
|
||||
|
||||
@Test(
|
||||
"properties are set correctly during initialization",
|
||||
arguments: TodoGroup.allCases
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
func initialization(for group: TodoGroup) {
|
||||
// GIVEN
|
||||
@@ -28,7 +27,7 @@ struct TaskListViewModelTests {
|
||||
|
||||
@Test(
|
||||
"buttonCompleted returns correct values when showCompleted is true",
|
||||
arguments: TodoGroup.allCases, [true, false]
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed], [true, false]
|
||||
)
|
||||
func buttonCompleted(
|
||||
for group: TodoGroup,
|
||||
@@ -56,7 +55,7 @@ struct TaskListViewModelTests {
|
||||
@Test(
|
||||
"canShowCompleted returns correct value for each group",
|
||||
arguments: zip(
|
||||
TodoGroup.allCases,
|
||||
[TodoGroup.today, .upcoming, .overdue, .completed],
|
||||
[true, true, true, false]
|
||||
)
|
||||
)
|
||||
@@ -76,7 +75,7 @@ struct TaskListViewModelTests {
|
||||
|
||||
@Test(
|
||||
"navigationTitle returns correct view title for each group",
|
||||
arguments: TodoGroup.allCases
|
||||
arguments: [TodoGroup.today, .upcoming, .overdue, .completed]
|
||||
)
|
||||
func navigationTitle(for group: TodoGroup) {
|
||||
// GIVEN
|
||||
@@ -91,43 +90,59 @@ struct TaskListViewModelTests {
|
||||
|
||||
// MARK: Method tests
|
||||
|
||||
@Test("deleteTasks removes a single task from model context")
|
||||
func deleteTasksSingle() throws {
|
||||
@Test("delete removes a single todo task from repository")
|
||||
func deleteSingleTodo() {
|
||||
// GIVEN
|
||||
let container = try ModelContainer(
|
||||
for: Todo.self,
|
||||
configurations: .init(isStoredInMemoryOnly: true)
|
||||
)
|
||||
let context = ModelContext(container)
|
||||
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
let todosToInsert: [Todo] = [
|
||||
let todos: [Todo] = [
|
||||
.init(title: "Task 1", due: .now, order: 0),
|
||||
.init(title: "Task 2", due: .now, order: 1),
|
||||
.init(title: "Task 3", due: .now, order: 2),
|
||||
]
|
||||
let countInitial = todosToInsert.count
|
||||
|
||||
for todo in todosToInsert {
|
||||
context.insert(todo)
|
||||
}
|
||||
let repository = MockTodoRepository(todos: todos)
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
|
||||
// WHEN
|
||||
viewModel.deleteTasks(
|
||||
todosToInsert,
|
||||
viewModel.delete(
|
||||
todos,
|
||||
at: IndexSet(integer: 1),
|
||||
from: context
|
||||
from: repository
|
||||
)
|
||||
|
||||
// THEN
|
||||
let descriptor = FetchDescriptor<Todo>()
|
||||
let todos = try context.fetch(descriptor)
|
||||
#expect(repository.todos.count == 2)
|
||||
#expect(repository.todos.contains { $0.title == "Task 1" })
|
||||
#expect(repository.todos.contains { $0.title == "Task 3" })
|
||||
#expect(!repository.todos.contains { $0.title == "Task 2" })
|
||||
}
|
||||
|
||||
#expect(countInitial == 3)
|
||||
#expect(todos.count == 2)
|
||||
#expect(todos.contains(where: { $0.title == "Task 1" }))
|
||||
#expect(todos.contains(where: { $0.title == "Task 3" }))
|
||||
#expect(!todos.contains(where: { $0.title == "Task 2" }))
|
||||
@Test("delete removes multiple todo tasks from repository")
|
||||
func deleteMultipleTodos() {
|
||||
// GIVEN
|
||||
let todos: [Todo] = [
|
||||
.init(title: "Task 1", due: .now, order: 0),
|
||||
.init(title: "Task 2", due: .now, order: 1),
|
||||
.init(title: "Task 3", due: .now, order: 2),
|
||||
.init(title: "Task 4", due: .now, order: 3),
|
||||
.init(title: "Task 5", due: .now, order: 4),
|
||||
]
|
||||
|
||||
let repository = MockTodoRepository(todos: todos)
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
|
||||
// WHEN
|
||||
viewModel.delete(
|
||||
todos,
|
||||
at: IndexSet([1, 3]),
|
||||
from: repository
|
||||
)
|
||||
|
||||
// THEN
|
||||
#expect(repository.todos.count == 3)
|
||||
#expect(repository.todos.contains { $0.title == "Task 1" })
|
||||
#expect(repository.todos.contains { $0.title == "Task 3" })
|
||||
#expect(repository.todos.contains { $0.title == "Task 5" })
|
||||
#expect(!repository.todos.contains { $0.title == "Task 2" })
|
||||
#expect(!repository.todos.contains { $0.title == "Task 4" })
|
||||
}
|
||||
|
||||
@Test("reorderTasks updates order property when moving task forward")
|
||||
@@ -180,7 +195,7 @@ struct TaskListViewModelTests {
|
||||
#expect(tasks[3].order == 1)
|
||||
}
|
||||
|
||||
@Test("toggleCompletedVisbility toggles the showCompleted property")
|
||||
@Test("toggleCompletedVisibility toggles the showCompleted property")
|
||||
func toggleCompletedVisibility() {
|
||||
// GIVEN
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
|
||||
Reference in New Issue
Block a user