Added test for MediatR/Toponyms - #53
Conversation
DrFaust555
left a comment
There was a problem hiding this comment.
Handle_WhenTitleProvided_ShouldFilterAndDistinctByStreetName doesn't actually test filtering. The mapper is stubbed with It.IsAny<IEnumerable>() and returns a ready-made single-item list, and the assertions then check that list - so they're checking the mock, not the handler. The test would still pass if FindStreetcodesWithMatchTitle were deleted.
It matters here, because with that data the handler really returns four toponyms, not one: the Where matches "First Street", "first street", "fIrst strEet" and "FirSt StreeT", and then GroupBy(s => s.StreetName) is case-sensitive, so those are four separate keys. Your data was clearly written to check case-insensitive dedup, and that isn't what the handler does.
The commented-out capturedItems/Callback block is exactly the tool for this - worth finishing rather than removing:
IEnumerable<Toponym>? captured = null;
_mapperMock
.Setup(m => m.Map<IEnumerable<ToponymDTO>>(It.IsAny<IEnumerable<Toponym>>()))
.Callback<object>(o => captured = ((IEnumerable<Toponym>)o).ToList())
.Returns(expectedDtos);
...
Assert.Single(captured!);
That will fail, and it's a useful failure - then either adjust the data to the actual behaviour or open a separate task to group by StreetName.ToLower().
…est to work properly
Laminate32
left a comment
There was a problem hiding this comment.
-
In
GetToponymsByStreetcodeIdHandlerTestsyou mock the mapper for a single Toponym object (It.IsAny<Toponym>()). If your handler maps the collection directly (e.g.,_mapper.Map<IEnumerable<ToponymDTO>>(toponyms)), this mock will fail. Change it to mockIEnumerable<Toponym>toIEnumerable<ToponymDTO>, just like you did in the GetAll tests -
In
Handle_WhenToponymRepositoryReturnsNull_ShouldReturnFailureyou test for a null return from the repository. Entity Framework Core typically returns an empty list ([]), not null, when no records are found. You should add a test simulating an empty collection and ensure your handler checks!toponyms.Any()
emil720a1
left a comment
There was a problem hiding this comment.
A few things still need attention:
Handle_WhenTitleProvided_ShouldFilterAndDistinctByStreetName groups capturedItems again inside the assertion, so the test passes even though the handler sends four differently-cased items to the mapper. Please verify the handler output directly with Assert.Single(capturedItems!), then either adjust the test data or track case-insensitive deduplication separately.
The tests use It.IsAny for repository predicates. Please verify that GetToponymById and GetToponymsByStreetcodeId actually use the requested IDs.
GetAllAsync() returns an empty collection when nothing is found, not null. Please add an empty-collection scenario and clarify whether it should return success or failure.
…t handlers 1. Rename Fact.Index / FactDto.Index to DisplayOrder — Index is a reserved word in T-SQL and collides with database indexes. 2. Regenerate migration as AddDisplayOrderToFact; backfill existing rows via ROW_NUMBER() OVER (PARTITION BY StreetcodeId ORDER BY Id) so DisplayOrder reflects creation order instead of being uniformly 0. 3. Add OrderBy(DisplayOrder) in GetAllFactsHandler and GetFactByStreetcodeIdHandler so the new column is actually consumed. Note: this PR covers schema + read-side ordering only. The reordering endpoint is not included and will land in a separate PR — do not close #55 on merge.
|


Code reviewers
Second Level Review
Summary of issue
MediatR/Toponyms handlers did not have unit tests. Their successful, failure, not-found and empty-result execution paths were not covered.
Summary of change
The tests cover:
Testing approach
Executed all Toponyms unit tests with:
Closes #27
CHECK LIST