Added the order to the sort descriptors inside the initializer for the TodoFetchDescriptor+Inits extensions in the app target.
This commit is contained in:
+66
-57
@@ -4,13 +4,13 @@ import Testing
|
||||
|
||||
@testable import Todos
|
||||
|
||||
@Suite("FetchDescriptor<Todo> extension tests")
|
||||
struct TodoFetchDescriptorTests {
|
||||
@Suite("TodoFetchDescriptor+Inits extension tests")
|
||||
struct TodoFetchDescriptor_InitsTests {
|
||||
|
||||
// MARK: Initialization tests
|
||||
|
||||
|
||||
@Test(
|
||||
"initializes with group when empty tasks",
|
||||
"initialization when empty tasks",
|
||||
arguments: TodoGroup.allCases
|
||||
)
|
||||
@MainActor
|
||||
@@ -18,58 +18,58 @@ struct TodoFetchDescriptorTests {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
let context = container.mainContext
|
||||
|
||||
|
||||
// WHEN
|
||||
let descriptor = FetchDescriptor<Todo>(
|
||||
group: .today,
|
||||
showCompleted: .random()
|
||||
)
|
||||
let results = try context.fetch(descriptor)
|
||||
|
||||
|
||||
// THEN
|
||||
#expect(results.isEmpty)
|
||||
}
|
||||
|
||||
|
||||
@Test(
|
||||
"initializes with group and show completed disabled when filled tasks",
|
||||
"initialization with show completed disabled",
|
||||
arguments: TodoGroup.allCases
|
||||
)
|
||||
@MainActor
|
||||
func initializationWhenFilledTasks(for group: TodoGroup) throws {
|
||||
func initializationWithNoShowCompleted(for group: TodoGroup) throws {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
let context = container.mainContext
|
||||
|
||||
|
||||
insert(into: context)
|
||||
|
||||
|
||||
// WHEN
|
||||
let descriptor = FetchDescriptor<Todo>(
|
||||
group: group,
|
||||
showCompleted: false
|
||||
)
|
||||
let results = try context.fetch(descriptor)
|
||||
|
||||
|
||||
// THEN
|
||||
#expect(results.count == 2)
|
||||
|
||||
|
||||
switch group {
|
||||
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")
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test(
|
||||
"initializes with group and show completed enabled when filled tasks",
|
||||
"initialization with show completed enabled",
|
||||
arguments: TodoGroup.allCases
|
||||
)
|
||||
@MainActor
|
||||
@@ -77,38 +77,39 @@ struct TodoFetchDescriptorTests {
|
||||
// GIVEN
|
||||
let container = try modelContainer
|
||||
let context = container.mainContext
|
||||
|
||||
|
||||
insert(into: context)
|
||||
|
||||
|
||||
// WHEN
|
||||
let descriptor = FetchDescriptor<Todo>(
|
||||
group: group,
|
||||
showCompleted: true
|
||||
)
|
||||
let results = try context.fetch(descriptor)
|
||||
|
||||
|
||||
// THEN
|
||||
if group == .overdue || group == .upcoming {
|
||||
#expect(results.count == 3)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
#expect(results.count == 2)
|
||||
}
|
||||
|
||||
|
||||
switch group {
|
||||
case .completed:
|
||||
#expect(results[0].title == "Completed task #2")
|
||||
#expect(results[1].title == "Completed task #1")
|
||||
case .overdue:
|
||||
#expect(results[0].title == "Completed task #1")
|
||||
#expect(results[1].title == "Overdue task #2")
|
||||
#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 == "Upcoming task #1")
|
||||
#expect(results[1].title == "Upcoming task #2")
|
||||
#expect(results[2].title == "Completed task #2")
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +117,7 @@ struct TodoFetchDescriptorTests {
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension TodoFetchDescriptorTests {
|
||||
private extension TodoFetchDescriptor_InitsTests {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
@@ -138,40 +139,48 @@ private extension TodoFetchDescriptorTests {
|
||||
let tasks: [Todo] = [
|
||||
.init(
|
||||
title: "Today task #1",
|
||||
due: .now
|
||||
due: .now,
|
||||
order: 0
|
||||
),
|
||||
.init(
|
||||
title: "Today task #2",
|
||||
due: .now
|
||||
due: .now,
|
||||
order: 1
|
||||
),
|
||||
.init(
|
||||
title: "Upcoming task #1",
|
||||
due: calendar.date(byAdding: .day, value: 1, to: .now) ?? .now
|
||||
due: calendar.date(byAdding: .day, value: 2, to: .now) ?? .now,
|
||||
order: 0
|
||||
),
|
||||
.init(
|
||||
title: "Upcoming task #2",
|
||||
due: calendar.date(byAdding: .day, value: 1, to: .now) ?? .now
|
||||
due: calendar.date(byAdding: .day, value: 3, to: .now) ?? .now,
|
||||
order: 1
|
||||
),
|
||||
.init(
|
||||
title: "Overdue task #1",
|
||||
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now
|
||||
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
|
||||
order: 1
|
||||
),
|
||||
.init(
|
||||
title: "Overdue task #2",
|
||||
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now
|
||||
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
|
||||
order: 0
|
||||
),
|
||||
.init(
|
||||
title: "Completed task #1",
|
||||
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
|
||||
completed: .now
|
||||
completed: .now,
|
||||
order: 1
|
||||
),
|
||||
.init(
|
||||
title: "Completed task #2",
|
||||
due: calendar.date(byAdding: .day, value: 1, to: .now) ?? .now,
|
||||
completed: .now
|
||||
)
|
||||
completed: .now,
|
||||
order: 0
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
for task in tasks {
|
||||
context.insert(task)
|
||||
}
|
||||
Reference in New Issue
Block a user