Files
todos/UnitTests/Tests/Extensions/TodoFetchDescriptor+InitsTests.swift
T

190 lines
5.3 KiB
Swift
Raw Normal View History

2026-02-10 15:31:52 +00:00
import Foundation
import SwiftData
import Testing
@testable import Todos
2026-02-11 15:28:22 +00:00
@Suite("TodoFetchDescriptor+Inits extension tests")
struct TodoFetchDescriptor_InitsTests {
2026-02-10 15:31:52 +00:00
// MARK: Initialization tests
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
@Test(
2026-02-11 15:28:22 +00:00
"initialization when empty tasks",
2026-02-10 15:31:52 +00:00
arguments: TodoGroup.allCases
)
@MainActor
func initializationWhenEmptyTasks(for group: TodoGroup) throws {
// GIVEN
let container = try modelContainer
let context = container.mainContext
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
// WHEN
2026-02-11 13:00:46 +00:00
let descriptor = FetchDescriptor<Todo>(
group: .today,
showCompleted: .random()
)
2026-02-10 15:31:52 +00:00
let results = try context.fetch(descriptor)
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
// THEN
#expect(results.isEmpty)
}
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
@Test(
2026-02-11 15:28:22 +00:00
"initialization with show completed disabled",
2026-02-10 15:31:52 +00:00
arguments: TodoGroup.allCases
)
@MainActor
2026-02-11 15:28:22 +00:00
func initializationWithNoShowCompleted(for group: TodoGroup) throws {
2026-02-10 15:31:52 +00:00
// GIVEN
let container = try modelContainer
let context = container.mainContext
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
insert(into: context)
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
// WHEN
2026-02-11 13:00:46 +00:00
let descriptor = FetchDescriptor<Todo>(
group: group,
showCompleted: false
)
2026-02-10 15:31:52 +00:00
let results = try context.fetch(descriptor)
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
// THEN
#expect(results.count == 2)
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
switch group {
2026-02-11 15:28:22 +00:00
case .completed:
#expect(results[0].title == "Completed task #2")
#expect(results[1].title == "Completed task #1")
case .overdue:
#expect(results[0].title == "Overdue task #2")
#expect(results[1].title == "Overdue task #1")
case .today:
#expect(results[0].title == "Today task #1")
#expect(results[1].title == "Today task #2")
case .upcoming:
#expect(results[0].title == "Upcoming task #1")
#expect(results[1].title == "Upcoming task #2")
2026-02-10 15:31:52 +00:00
}
}
2026-02-11 15:28:22 +00:00
2026-02-11 13:00:46 +00:00
@Test(
2026-02-11 15:28:22 +00:00
"initialization with show completed enabled",
2026-02-11 13:00:46 +00:00
arguments: TodoGroup.allCases
)
@MainActor
func initializationWithShowCompleted(for group: TodoGroup) throws {
// GIVEN
let container = try modelContainer
let context = container.mainContext
2026-02-11 15:28:22 +00:00
2026-02-11 13:00:46 +00:00
insert(into: context)
2026-02-11 15:28:22 +00:00
2026-02-11 13:00:46 +00:00
// WHEN
let descriptor = FetchDescriptor<Todo>(
group: group,
showCompleted: true
)
let results = try context.fetch(descriptor)
2026-02-11 15:28:22 +00:00
2026-02-11 13:00:46 +00:00
// THEN
if group == .overdue || group == .upcoming {
#expect(results.count == 3)
2026-02-11 15:28:22 +00:00
}
else {
2026-02-11 13:00:46 +00:00
#expect(results.count == 2)
}
2026-02-11 15:28:22 +00:00
2026-02-11 13:00:46 +00:00
switch group {
2026-02-11 15:28:22 +00:00
case .completed:
#expect(results[0].title == "Completed task #2")
#expect(results[1].title == "Completed task #1")
case .overdue:
#expect(results[0].title == "Overdue task #2")
#expect(results[1].title == "Completed task #1")
#expect(results[2].title == "Overdue task #1")
case .today:
#expect(results[0].title == "Today task #1")
#expect(results[1].title == "Today task #2")
case .upcoming:
#expect(results[0].title == "Completed task #2")
#expect(results[1].title == "Upcoming task #1")
#expect(results[2].title == "Upcoming task #2")
2026-02-11 13:00:46 +00:00
}
}
2026-02-10 15:31:52 +00:00
}
// MARK: - Helpers
2026-02-11 15:28:22 +00:00
private extension TodoFetchDescriptor_InitsTests {
2026-02-10 15:31:52 +00:00
// MARK: Computed
var modelContainer: ModelContainer {
get throws {
try ModelContainer(
for: Todo.self,
configurations: .init(isStoredInMemoryOnly: true)
)
}
}
// MARK: Functions
func insert(
into context: ModelContext,
calendar: Calendar = .current
) {
let tasks: [Todo] = [
.init(
title: "Today task #1",
2026-02-11 15:28:22 +00:00
due: .now,
order: 0
2026-02-10 15:31:52 +00:00
),
.init(
title: "Today task #2",
2026-02-11 15:28:22 +00:00
due: .now,
order: 1
2026-02-10 15:31:52 +00:00
),
.init(
title: "Upcoming task #1",
2026-02-11 15:28:22 +00:00
due: calendar.date(byAdding: .day, value: 2, to: .now) ?? .now,
order: 0
2026-02-10 15:31:52 +00:00
),
.init(
title: "Upcoming task #2",
2026-02-11 15:28:22 +00:00
due: calendar.date(byAdding: .day, value: 3, to: .now) ?? .now,
order: 1
2026-02-10 15:31:52 +00:00
),
.init(
title: "Overdue task #1",
2026-02-11 15:28:22 +00:00
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
order: 1
2026-02-10 15:31:52 +00:00
),
.init(
title: "Overdue task #2",
2026-02-11 15:28:22 +00:00
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
order: 0
2026-02-10 15:31:52 +00:00
),
.init(
title: "Completed task #1",
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
2026-02-11 15:28:22 +00:00
completed: .now,
order: 1
2026-02-10 15:31:52 +00:00
),
.init(
title: "Completed task #2",
due: calendar.date(byAdding: .day, value: 1, to: .now) ?? .now,
2026-02-11 15:28:22 +00:00
completed: .now,
order: 0
),
2026-02-10 15:31:52 +00:00
]
2026-02-11 15:28:22 +00:00
2026-02-10 15:31:52 +00:00
for task in tasks {
context.insert(task)
}
}
}