Skip to content

Added test for MediatR/Toponyms - #53

Open
M1R4MI wants to merge 50 commits into
devfrom
tests/27/MediatR/Toponyms
Open

Added test for MediatR/Toponyms#53
M1R4MI wants to merge 50 commits into
devfrom
tests/27/MediatR/Toponyms

Conversation

@M1R4MI

@M1R4MI M1R4MI commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

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

  • GetToponymsByStreetcodeIdHandlerTests
  • GetToponymByIdHandlerTest
  • GetAllToponymItemsHandlerTests

The tests cover:

  • successful results;
  • missing Toponym and Streetcode entities;
  • empty toponym collections;
  • repository interactions;
  • AutoMapper calls;

Testing approach

Executed all Toponyms unit tests with:

     dotnet test Streetcode.XUnitTest/Streetcode.XUnitTest.csproj \
           --filter "FullyQualifiedName~Streetcode.XUnitTest.MediatRTests.Toponyms"

Closes #27

CHECK LIST

  • СI passed
  • Сode coverage >=95%
  • PR is reviewed manually again (to make sure you have 100% ready code)
  • All reviewers agreed to merge the PR
  • I've checked new feature as logged in and logged out user if needed
  • PR meets all conventions

@DrFaust555 DrFaust555 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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().

@M1R4MI
M1R4MI requested a review from DrFaust555 August 14, 2026 08:46

@Laminate32 Laminate32 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. In GetToponymsByStreetcodeIdHandlerTests you 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 mock IEnumerable<Toponym> to IEnumerable<ToponymDTO>, just like you did in the GetAll tests

  2. In Handle_WhenToponymRepositoryReturnsNull_ShouldReturnFailure you 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 emil720a1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

emil720a1 and others added 23 commits August 21, 2026 16:53
…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.
@M1R4MI
M1R4MI requested review from Laminate32 and emil720a1 and removed request for Laminate32 August 23, 2026 11:38
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@M1R4MI
M1R4MI removed the request for review from VitaliyKorostil August 25, 2026 21:11
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.

Write unit tests for MeadiatR/Toponyms

6 participants