Added the order to the sort descriptors inside the initializer for the TodoFetchDescriptor+Inits extensions in the app target.
This commit is contained in:
@@ -13,9 +13,12 @@ extension FetchDescriptor where T == Todo {
|
|||||||
/// relevance and completion status. Each group applies appropriate predicates and sort orders:
|
/// relevance and completion status. Each group applies appropriate predicates and sort orders:
|
||||||
///
|
///
|
||||||
/// - **Completed**: All tasks marked as done, sorted by completion date (most recent first)
|
/// - **Completed**: All tasks marked as done, sorted by completion date (most recent first)
|
||||||
/// - **Overdue**: Incomplete tasks with due dates before today, sorted by due date (most recent first)
|
/// - **Overdue**: Incomplete tasks with due dates before today, sorted by user-defined order, then by due date (most recent first)
|
||||||
/// - **Today**: Incomplete tasks due today, sorted by due date (earliest first)
|
/// - **Today**: Incomplete tasks due today, sorted by user-defined order, then by due date (earliest first)
|
||||||
/// - **Upcoming**: Incomplete tasks due after today, sorted by due date (earliest first)
|
/// - **Upcoming**: Incomplete tasks due after today, sorted by user-defined order, then by due date (earliest first)
|
||||||
|
///
|
||||||
|
/// The user-defined order allows tasks to be manually reordered within each group through drag and drop,
|
||||||
|
/// while maintaining a secondary sort by date for tasks with the same order value.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - group: The todo group category to filter by.
|
/// - group: The todo group category to filter by.
|
||||||
@@ -52,11 +55,11 @@ extension FetchDescriptor where T == Todo {
|
|||||||
case .completed:
|
case .completed:
|
||||||
[.init(\Todo.completed, order: .reverse)]
|
[.init(\Todo.completed, order: .reverse)]
|
||||||
case .overdue:
|
case .overdue:
|
||||||
[.init(\Todo.due, order: .reverse)]
|
[.init(\Todo.order, order: .forward), .init(\Todo.due, order: .reverse)]
|
||||||
case .today:
|
case .today:
|
||||||
[.init(\Todo.due, order: .forward)]
|
[.init(\Todo.order, order: .forward), .init(\Todo.due, order: .forward)]
|
||||||
case .upcoming:
|
case .upcoming:
|
||||||
[.init(\Todo.due, order: .forward)]
|
[.init(\Todo.order, order: .forward), .init(\Todo.due, order: .forward)]
|
||||||
}
|
}
|
||||||
|
|
||||||
self.init(
|
self.init(
|
||||||
|
|||||||
+66
-57
@@ -4,13 +4,13 @@ import Testing
|
|||||||
|
|
||||||
@testable import Todos
|
@testable import Todos
|
||||||
|
|
||||||
@Suite("FetchDescriptor<Todo> extension tests")
|
@Suite("TodoFetchDescriptor+Inits extension tests")
|
||||||
struct TodoFetchDescriptorTests {
|
struct TodoFetchDescriptor_InitsTests {
|
||||||
|
|
||||||
// MARK: Initialization tests
|
// MARK: Initialization tests
|
||||||
|
|
||||||
@Test(
|
@Test(
|
||||||
"initializes with group when empty tasks",
|
"initialization when empty tasks",
|
||||||
arguments: TodoGroup.allCases
|
arguments: TodoGroup.allCases
|
||||||
)
|
)
|
||||||
@MainActor
|
@MainActor
|
||||||
@@ -18,58 +18,58 @@ struct TodoFetchDescriptorTests {
|
|||||||
// GIVEN
|
// GIVEN
|
||||||
let container = try modelContainer
|
let container = try modelContainer
|
||||||
let context = container.mainContext
|
let context = container.mainContext
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
let descriptor = FetchDescriptor<Todo>(
|
let descriptor = FetchDescriptor<Todo>(
|
||||||
group: .today,
|
group: .today,
|
||||||
showCompleted: .random()
|
showCompleted: .random()
|
||||||
)
|
)
|
||||||
let results = try context.fetch(descriptor)
|
let results = try context.fetch(descriptor)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
#expect(results.isEmpty)
|
#expect(results.isEmpty)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(
|
@Test(
|
||||||
"initializes with group and show completed disabled when filled tasks",
|
"initialization with show completed disabled",
|
||||||
arguments: TodoGroup.allCases
|
arguments: TodoGroup.allCases
|
||||||
)
|
)
|
||||||
@MainActor
|
@MainActor
|
||||||
func initializationWhenFilledTasks(for group: TodoGroup) throws {
|
func initializationWithNoShowCompleted(for group: TodoGroup) throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let container = try modelContainer
|
let container = try modelContainer
|
||||||
let context = container.mainContext
|
let context = container.mainContext
|
||||||
|
|
||||||
insert(into: context)
|
insert(into: context)
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
let descriptor = FetchDescriptor<Todo>(
|
let descriptor = FetchDescriptor<Todo>(
|
||||||
group: group,
|
group: group,
|
||||||
showCompleted: false
|
showCompleted: false
|
||||||
)
|
)
|
||||||
let results = try context.fetch(descriptor)
|
let results = try context.fetch(descriptor)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
#expect(results.count == 2)
|
#expect(results.count == 2)
|
||||||
|
|
||||||
switch group {
|
switch group {
|
||||||
case .completed:
|
case .completed:
|
||||||
#expect(results[0].title == "Completed task #2")
|
#expect(results[0].title == "Completed task #2")
|
||||||
#expect(results[1].title == "Completed task #1")
|
#expect(results[1].title == "Completed task #1")
|
||||||
case .overdue:
|
case .overdue:
|
||||||
#expect(results[0].title == "Overdue task #2")
|
#expect(results[0].title == "Overdue task #2")
|
||||||
#expect(results[1].title == "Overdue task #1")
|
#expect(results[1].title == "Overdue task #1")
|
||||||
case .today:
|
case .today:
|
||||||
#expect(results[0].title == "Today task #1")
|
#expect(results[0].title == "Today task #1")
|
||||||
#expect(results[1].title == "Today task #2")
|
#expect(results[1].title == "Today task #2")
|
||||||
case .upcoming:
|
case .upcoming:
|
||||||
#expect(results[0].title == "Upcoming task #1")
|
#expect(results[0].title == "Upcoming task #1")
|
||||||
#expect(results[1].title == "Upcoming task #2")
|
#expect(results[1].title == "Upcoming task #2")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(
|
@Test(
|
||||||
"initializes with group and show completed enabled when filled tasks",
|
"initialization with show completed enabled",
|
||||||
arguments: TodoGroup.allCases
|
arguments: TodoGroup.allCases
|
||||||
)
|
)
|
||||||
@MainActor
|
@MainActor
|
||||||
@@ -77,38 +77,39 @@ struct TodoFetchDescriptorTests {
|
|||||||
// GIVEN
|
// GIVEN
|
||||||
let container = try modelContainer
|
let container = try modelContainer
|
||||||
let context = container.mainContext
|
let context = container.mainContext
|
||||||
|
|
||||||
insert(into: context)
|
insert(into: context)
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
let descriptor = FetchDescriptor<Todo>(
|
let descriptor = FetchDescriptor<Todo>(
|
||||||
group: group,
|
group: group,
|
||||||
showCompleted: true
|
showCompleted: true
|
||||||
)
|
)
|
||||||
let results = try context.fetch(descriptor)
|
let results = try context.fetch(descriptor)
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
if group == .overdue || group == .upcoming {
|
if group == .overdue || group == .upcoming {
|
||||||
#expect(results.count == 3)
|
#expect(results.count == 3)
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
#expect(results.count == 2)
|
#expect(results.count == 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch group {
|
switch group {
|
||||||
case .completed:
|
case .completed:
|
||||||
#expect(results[0].title == "Completed task #2")
|
#expect(results[0].title == "Completed task #2")
|
||||||
#expect(results[1].title == "Completed task #1")
|
#expect(results[1].title == "Completed task #1")
|
||||||
case .overdue:
|
case .overdue:
|
||||||
#expect(results[0].title == "Completed task #1")
|
#expect(results[0].title == "Overdue task #2")
|
||||||
#expect(results[1].title == "Overdue task #2")
|
#expect(results[1].title == "Completed task #1")
|
||||||
#expect(results[2].title == "Overdue task #1")
|
#expect(results[2].title == "Overdue task #1")
|
||||||
case .today:
|
case .today:
|
||||||
#expect(results[0].title == "Today task #1")
|
#expect(results[0].title == "Today task #1")
|
||||||
#expect(results[1].title == "Today task #2")
|
#expect(results[1].title == "Today task #2")
|
||||||
case .upcoming:
|
case .upcoming:
|
||||||
#expect(results[0].title == "Upcoming task #1")
|
#expect(results[0].title == "Completed task #2")
|
||||||
#expect(results[1].title == "Upcoming task #2")
|
#expect(results[1].title == "Upcoming task #1")
|
||||||
#expect(results[2].title == "Completed task #2")
|
#expect(results[2].title == "Upcoming task #2")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +117,7 @@ struct TodoFetchDescriptorTests {
|
|||||||
|
|
||||||
// MARK: - Helpers
|
// MARK: - Helpers
|
||||||
|
|
||||||
private extension TodoFetchDescriptorTests {
|
private extension TodoFetchDescriptor_InitsTests {
|
||||||
|
|
||||||
// MARK: Computed
|
// MARK: Computed
|
||||||
|
|
||||||
@@ -138,40 +139,48 @@ private extension TodoFetchDescriptorTests {
|
|||||||
let tasks: [Todo] = [
|
let tasks: [Todo] = [
|
||||||
.init(
|
.init(
|
||||||
title: "Today task #1",
|
title: "Today task #1",
|
||||||
due: .now
|
due: .now,
|
||||||
|
order: 0
|
||||||
),
|
),
|
||||||
.init(
|
.init(
|
||||||
title: "Today task #2",
|
title: "Today task #2",
|
||||||
due: .now
|
due: .now,
|
||||||
|
order: 1
|
||||||
),
|
),
|
||||||
.init(
|
.init(
|
||||||
title: "Upcoming task #1",
|
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(
|
.init(
|
||||||
title: "Upcoming task #2",
|
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(
|
.init(
|
||||||
title: "Overdue task #1",
|
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(
|
.init(
|
||||||
title: "Overdue task #2",
|
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(
|
.init(
|
||||||
title: "Completed task #1",
|
title: "Completed task #1",
|
||||||
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
|
due: calendar.date(byAdding: .day, value: -1, to: .now) ?? .now,
|
||||||
completed: .now
|
completed: .now,
|
||||||
|
order: 1
|
||||||
),
|
),
|
||||||
.init(
|
.init(
|
||||||
title: "Completed task #2",
|
title: "Completed task #2",
|
||||||
due: calendar.date(byAdding: .day, value: 1, to: .now) ?? .now,
|
due: calendar.date(byAdding: .day, value: 1, to: .now) ?? .now,
|
||||||
completed: .now
|
completed: .now,
|
||||||
)
|
order: 0
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
for task in tasks {
|
for task in tasks {
|
||||||
context.insert(task)
|
context.insert(task)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user