Todo repository protocol #8
@@ -1,5 +1,4 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import SwiftData
|
|
||||||
import Testing
|
import Testing
|
||||||
|
|
||||||
@testable import Todos
|
@testable import Todos
|
||||||
@@ -121,11 +120,9 @@ struct NewTaskViewModelTests {
|
|||||||
// MARK: Functions tests
|
// MARK: Functions tests
|
||||||
|
|
||||||
@Test("save creates todo with all properties")
|
@Test("save creates todo with all properties")
|
||||||
@MainActor
|
|
||||||
func saveWithAllProperties() throws {
|
func saveWithAllProperties() throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let container = try modelContainer
|
let repository = MockTodoRepository()
|
||||||
let context = container.mainContext
|
|
||||||
let viewModel = NewTaskView.ViewModel()
|
let viewModel = NewTaskView.ViewModel()
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
@@ -133,12 +130,10 @@ struct NewTaskViewModelTests {
|
|||||||
viewModel.note = "Finish by end of week"
|
viewModel.note = "Finish by end of week"
|
||||||
viewModel.due = Date(timeIntervalSince1970: 1000000000)
|
viewModel.due = Date(timeIntervalSince1970: 1000000000)
|
||||||
|
|
||||||
viewModel.save(to: context)
|
viewModel.save(to: repository)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
let descriptor = FetchDescriptor<Todo>()
|
let todo = try #require(repository.todos.first)
|
||||||
let todos = try context.fetch(descriptor)
|
|
||||||
let todo = try #require(todos.first)
|
|
||||||
|
|
||||||
#expect(todo.title == viewModel.title)
|
#expect(todo.title == viewModel.title)
|
||||||
#expect(todo.note == viewModel.note)
|
#expect(todo.note == viewModel.note)
|
||||||
@@ -147,11 +142,9 @@ struct NewTaskViewModelTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test("save creates todo without note when note is empty")
|
@Test("save creates todo without note when note is empty")
|
||||||
@MainActor
|
|
||||||
func saveWithEmptyNote() throws {
|
func saveWithEmptyNote() throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let container = try modelContainer
|
let repository = MockTodoRepository()
|
||||||
let context = container.mainContext
|
|
||||||
let viewModel = NewTaskView.ViewModel()
|
let viewModel = NewTaskView.ViewModel()
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
@@ -159,12 +152,10 @@ struct NewTaskViewModelTests {
|
|||||||
viewModel.note = ""
|
viewModel.note = ""
|
||||||
viewModel.due = Date()
|
viewModel.due = Date()
|
||||||
|
|
||||||
viewModel.save(to: context)
|
viewModel.save(to: repository)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
let descriptor = FetchDescriptor<Todo>()
|
let todo = try #require(repository.todos.first)
|
||||||
let todos = try context.fetch(descriptor)
|
|
||||||
let todo = try #require(todos.first)
|
|
||||||
|
|
||||||
#expect(todo.title == viewModel.title)
|
#expect(todo.title == viewModel.title)
|
||||||
#expect(todo.note == nil)
|
#expect(todo.note == nil)
|
||||||
@@ -173,11 +164,9 @@ struct NewTaskViewModelTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test("save creates todo with note containing only whitespace as nil")
|
@Test("save creates todo with note containing only whitespace as nil")
|
||||||
@MainActor
|
|
||||||
func saveWithWhitespaceNote() throws {
|
func saveWithWhitespaceNote() throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let container = try modelContainer
|
let repository = MockTodoRepository()
|
||||||
let context = container.mainContext
|
|
||||||
let viewModel = NewTaskView.ViewModel()
|
let viewModel = NewTaskView.ViewModel()
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
@@ -185,12 +174,10 @@ struct NewTaskViewModelTests {
|
|||||||
viewModel.note = " "
|
viewModel.note = " "
|
||||||
viewModel.due = Date()
|
viewModel.due = Date()
|
||||||
|
|
||||||
viewModel.save(to: context)
|
viewModel.save(to: repository)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
let descriptor = FetchDescriptor<Todo>()
|
let todo = try #require(repository.todos.first)
|
||||||
let todos = try context.fetch(descriptor)
|
|
||||||
let todo = try #require(todos.first)
|
|
||||||
|
|
||||||
#expect(todo.title == viewModel.title)
|
#expect(todo.title == viewModel.title)
|
||||||
#expect(todo.note == nil)
|
#expect(todo.note == nil)
|
||||||
@@ -199,46 +186,24 @@ struct NewTaskViewModelTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test("save inserts multiple todos independently")
|
@Test("save inserts multiple todos independently")
|
||||||
@MainActor
|
func saveMultipleTodos() {
|
||||||
func saveMultipleTodos() throws {
|
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let container = try modelContainer
|
let repository = MockTodoRepository()
|
||||||
let context = container.mainContext
|
|
||||||
let viewModelFirst = NewTaskView.ViewModel()
|
let viewModelFirst = NewTaskView.ViewModel()
|
||||||
let viewModelSecond = NewTaskView.ViewModel()
|
let viewModelSecond = NewTaskView.ViewModel()
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
viewModelFirst.title = "First task"
|
viewModelFirst.title = "First task"
|
||||||
viewModelFirst.save(to: context)
|
viewModelFirst.save(to: repository)
|
||||||
|
|
||||||
viewModelSecond.title = "Second task"
|
viewModelSecond.title = "Second task"
|
||||||
viewModelSecond.note = "With a note"
|
viewModelSecond.note = "With a note"
|
||||||
viewModelSecond.save(to: context)
|
viewModelSecond.save(to: repository)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
let descriptor = FetchDescriptor<Todo>()
|
#expect(repository.todos.count == 2)
|
||||||
let todos = try context.fetch(descriptor)
|
#expect(repository.todos.contains { $0.title == "First task" })
|
||||||
|
#expect(repository.todos.contains { $0.title == "Second task" })
|
||||||
#expect(todos.count == 2)
|
|
||||||
#expect(todos.contains { $0.title == "First task" })
|
|
||||||
#expect(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)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user