Skip to content

test: add comprehensive SortingHandler tests#68

Merged
Erlend Ellefsen (erlendellefsen) merged 1 commit into
mainfrom
test/3.1-sorting-handler-tests
Jan 24, 2026
Merged

test: add comprehensive SortingHandler tests#68
Erlend Ellefsen (erlendellefsen) merged 1 commit into
mainfrom
test/3.1-sorting-handler-tests

Conversation

@erlendellefsen

@erlendellefsen Erlend Ellefsen (erlendellefsen) commented Jan 24, 2026

Copy link
Copy Markdown
Collaborator
  • Add 27 tests covering basic sorting, multi-field, property mapping, invalid fields, data types, and edge cases
  • Fix bug where invalid first sort field caused ArgumentNullException

- Add 27 tests covering basic sorting, multi-field, property mapping,
invalid fields, data types, and edge cases
- Fix bug where invalid first sort field caused ArgumentNullException"
Copilot AI review requested due to automatic review settings January 24, 2026 22:28
@erlendellefsen Erlend Ellefsen (erlendellefsen) merged commit 8757de7 into main Jan 24, 2026
6 checks passed
@erlendellefsen Erlend Ellefsen (erlendellefsen) deleted the test/3.1-sorting-handler-tests branch January 24, 2026 22:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds comprehensive unit coverage for SortingHandler.ApplySorting and adjusts the sorting implementation to correctly handle leading invalid sort fields.

Changes:

  • Fix sorting method selection to use OrderBy* for the first valid sort field and ThenBy* thereafter.
  • Add a new SortingHandlerTests suite covering single/multi-field sorting, name mapping, invalid fields, multiple data types, and edge cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
JsonApiToolkit/Extensions/Querying/Handlers/SortingHandler.cs Fixes ordering method selection so invalid leading sort fields don’t cause ThenBy on a null ordered query.
JsonApiToolkit.Tests/Extensions/Sorting/SortingHandlerTests.cs Adds extensive unit tests for sorting behavior, mapping, invalid inputs, and edge cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +593 to +595
// All have same name, order should be stable (original order preserved)
Assert.Equal(3, result.Count);
Assert.All(result, e => Assert.Equal("Same", e.Name));

Copilot AI Jan 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is named/worded as verifying a stable sort, but it doesn’t assert stability (it only asserts that all names are "Same"). Either assert the original relative order (e.g., by Id sequence) when values are equal, or rename the test/comment to match what’s actually being validated.

Suggested change
// All have same name, order should be stable (original order preserved)
Assert.Equal(3, result.Count);
Assert.All(result, e => Assert.Equal("Same", e.Name));
// All have same name; verify stable sort by checking original Id order is preserved
Assert.Equal(3, result.Count);
Assert.All(result, e => Assert.Equal("Same", e.Name));
Assert.Equal(1, result[0].Id);
Assert.Equal(2, result[1].Id);
Assert.Equal(3, result[2].Id);

Copilot uses AI. Check for mistakes.
Comment on lines +564 to +582
var testData = new List<TestEntity>
{
new TestEntity
{
Id = 1,
Name = "Same",
CreatedAt = DateTime.Now,
},
new TestEntity
{
Id = 2,
Name = "Same",
CreatedAt = DateTime.Now,
},
new TestEntity
{
Id = 3,
Name = "Same",
CreatedAt = DateTime.Now,

Copilot AI Jan 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using DateTime.Now in test data makes failures harder to reproduce and can introduce non-determinism if future assertions start depending on these values. Prefer fixed DateTime values here.

Suggested change
var testData = new List<TestEntity>
{
new TestEntity
{
Id = 1,
Name = "Same",
CreatedAt = DateTime.Now,
},
new TestEntity
{
Id = 2,
Name = "Same",
CreatedAt = DateTime.Now,
},
new TestEntity
{
Id = 3,
Name = "Same",
CreatedAt = DateTime.Now,
var createdAt = new DateTime(2023, 1, 1);
var testData = new List<TestEntity>
{
new TestEntity
{
Id = 1,
Name = "Same",
CreatedAt = createdAt,
},
new TestEntity
{
Id = 2,
Name = "Same",
CreatedAt = createdAt,
},
new TestEntity
{
Id = 3,
Name = "Same",
CreatedAt = createdAt,

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants