-
Notifications
You must be signed in to change notification settings - Fork 1
test/21: write tests for BLL/MediatR/Toponyms handlers #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PenultimateBoss
wants to merge
4
commits into
dev
Choose a base branch
from
test/21/mediatr_toponyms
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+525
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
981554d
test/21: write tests for BLL/MediatR/Toponyms handlers
itsex0tissle 5567240
fix/44: adjust tests for GetToponymsByStreetcodeIdHandler matching ch…
itsex0tissle 7885cb1
Merge branch 'dev' into test/21/mediatr_toponyms
itsex0tissle 4d5ccb6
Merge branch 'dev' into test/21/mediatr_toponyms
PenultimateBoss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
Streetcode/Streetcode.XUnitTest/BLL/MediatR/Toponyms/GetAllToponymsHandlerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| namespace Streetcode.XUnitTest.BLL.MediatR.Toponyms | ||
| { | ||
| using AutoMapper; | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using Streetcode.BLL.DTO.Toponyms; | ||
| using Streetcode.BLL.Interfaces.Logging; | ||
| using Streetcode.BLL.MediatR.Toponyms.GetAll; | ||
| using Streetcode.DAL.Entities.Toponyms; | ||
| using Streetcode.DAL.Repositories.Interfaces.Base; | ||
| using Streetcode.DAL.Repositories.Interfaces.Toponyms; | ||
| using Xunit; | ||
|
|
||
| /// <summary> | ||
| /// Contains tests for <see cref="GetAllToponymsHandler"/>. | ||
| /// </summary> | ||
| public sealed class GetAllToponymsHandlerTests | ||
| { | ||
| private readonly IMapper mapper; | ||
| private readonly Mock<ILoggerService> loggerMock; | ||
| private readonly Mock<IRepositoryWrapper> repositoryWrapperMock; | ||
| private readonly Mock<IToponymRepository> toponymRepositoryMock; | ||
| private readonly GetAllToponymsHandler handler; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GetAllToponymsHandlerTests"/> class. | ||
| /// </summary> | ||
| public GetAllToponymsHandlerTests() | ||
| { | ||
| this.mapper = new MapperConfiguration(cfg => | ||
| { | ||
| cfg.CreateMap<Toponym, ToponymDTO>(); | ||
| }).CreateMapper(); | ||
| this.loggerMock = new Mock<ILoggerService>(); | ||
| this.repositoryWrapperMock = new Mock<IRepositoryWrapper>(); | ||
| this.toponymRepositoryMock = new Mock<IToponymRepository>(); | ||
| this.repositoryWrapperMock.Setup(r => r.ToponymRepository).Returns(this.toponymRepositoryMock.Object); | ||
| this.handler = new GetAllToponymsHandler(this.repositoryWrapperMock.Object, this.mapper, this.loggerMock.Object); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Should return all toponyms when <see cref="GetAllToponymsRequestDTO.Title"/> <see langword="is null"/>. | ||
| /// </summary> | ||
| /// <returns>Awaitable task.</returns> | ||
| [Fact] | ||
| public async Task Handle_ShouldReturnAllToponyms_WhenNoTitleProvided() | ||
| { | ||
| // Arrange | ||
| IQueryable<Toponym> toponyms = new List<Toponym> | ||
| { | ||
| new() | ||
| { | ||
| Id = 1, | ||
| StreetName = "Шевченка", | ||
| }, | ||
| new() | ||
| { | ||
| Id = 2, | ||
| StreetName = "Бандери", | ||
| }, | ||
| }.AsQueryable(); | ||
| List<ToponymDTO> expected_toponyms = new() | ||
| { | ||
| this.mapper.Map<ToponymDTO>(toponyms.ElementAt(0)), | ||
| this.mapper.Map<ToponymDTO>(toponyms.ElementAt(1)), | ||
| }; | ||
| GetAllToponymsQuery query = new(new GetAllToponymsRequestDTO | ||
| { | ||
| Title = null, | ||
| }); | ||
| this.toponymRepositoryMock.Setup(r => r.FindAll(null)).Returns(toponyms); | ||
|
|
||
| // Act | ||
| var result = await this.handler.Handle(query, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.IsSuccess.Should().BeTrue(); | ||
| result.Value.Toponyms.Should().BeEquivalentTo(expected_toponyms); | ||
| this.toponymRepositoryMock.Verify(r => r.FindAll(null), Times.Once); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Should return filtered toponyms when <see cref="GetAllToponymsRequestDTO.Title"/> <see langword="is not null"/>. | ||
| /// Must be case-insensitive. | ||
| /// </summary> | ||
| /// <returns>Awaitable task.</returns> | ||
| [Fact] | ||
| public async Task Handle_ShouldFilterToponyms_WhenTitleProvided_CaseInsensitive() | ||
| { | ||
| // Arrange | ||
| IQueryable<Toponym> toponyms = new List<Toponym> | ||
| { | ||
| new() | ||
| { | ||
| Id = 1, | ||
| StreetName = "Шевченка", | ||
| }, | ||
| new() | ||
| { | ||
| Id = 2, | ||
| StreetName = "Бандери", | ||
| }, | ||
| }.AsQueryable(); | ||
| List<ToponymDTO> expected_toponyms = new() | ||
| { | ||
| this.mapper.Map<ToponymDTO>(toponyms.ElementAt(1)), | ||
| }; | ||
| GetAllToponymsQuery query = new(new GetAllToponymsRequestDTO | ||
| { | ||
| Title = "аНдЕр", | ||
| }); | ||
| this.toponymRepositoryMock.Setup(r => r.FindAll(null)).Returns(toponyms); | ||
|
|
||
| // Act | ||
| var result = await this.handler.Handle(query, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.IsSuccess.Should().BeTrue(); | ||
| result.Value.Toponyms.Should().BeEquivalentTo(expected_toponyms); | ||
| this.toponymRepositoryMock.Verify(r => r.FindAll(null), Times.Once); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Should return unique toponyms when <see cref="GetAllToponymsRequestDTO.Title"/> <see langword="is not null"/>. | ||
| /// </summary> | ||
| /// <returns>Awaitable task.</returns> | ||
| [Fact] | ||
| public async Task Handle_ShouldReturnUniqueToponyms_WhenTitleProvided() | ||
| { | ||
| // Arrange | ||
| IQueryable<Toponym> toponyms = new List<Toponym> | ||
| { | ||
| new() | ||
| { | ||
| Id = 1, | ||
| StreetName = "Шевченка", | ||
| Oblast = "Київська", | ||
| }, | ||
| new() | ||
| { | ||
| Id = 2, | ||
| StreetName = "Шевченка", | ||
| Oblast = "Львівська", | ||
| }, | ||
| }.AsQueryable(); | ||
| List<ToponymDTO> expected_toponyms = new() | ||
| { | ||
| this.mapper.Map<ToponymDTO>(toponyms.ElementAt(0)), | ||
| }; | ||
| GetAllToponymsQuery query = new(new GetAllToponymsRequestDTO | ||
| { | ||
| Title = "евч", | ||
| }); | ||
| this.toponymRepositoryMock.Setup(r => r.FindAll(null)).Returns(toponyms); | ||
|
|
||
| // Act | ||
| var result = await this.handler.Handle(query, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.IsSuccess.Should().BeTrue(); | ||
| result.Value.Toponyms.Should().BeEquivalentTo(expected_toponyms); | ||
| this.toponymRepositoryMock.Verify(r => r.FindAll(null), Times.Once); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Should return empty collection when no matches found. | ||
| /// </summary> | ||
| /// <returns>Awaitable task.</returns> | ||
| [Fact] | ||
| public async Task Handle_ShouldReturnEmptyCollection_WhenNoMatchesFound() | ||
| { | ||
| // Arrange | ||
| IQueryable<Toponym> toponyms = new List<Toponym> | ||
| { | ||
| new() | ||
| { | ||
| Id = 1, | ||
| StreetName = "Шевченка", | ||
| }, | ||
| new() | ||
| { | ||
| Id = 2, | ||
| StreetName = "Бандери", | ||
| }, | ||
| }.AsQueryable(); | ||
| List<ToponymDTO> expected_toponyms = new(); | ||
| GetAllToponymsQuery query = new(new GetAllToponymsRequestDTO | ||
| { | ||
| Title = "ийськ", | ||
| }); | ||
| this.toponymRepositoryMock.Setup(r => r.FindAll(null)).Returns(toponyms); | ||
|
|
||
| // Act | ||
| var result = await this.handler.Handle(query, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.IsSuccess.Should().BeTrue(); | ||
| result.Value.Toponyms.Should().BeEquivalentTo(expected_toponyms); | ||
| this.toponymRepositoryMock.Verify(r => r.FindAll(null), Times.Once); | ||
| } | ||
| } | ||
| } |
117 changes: 117 additions & 0 deletions
117
Streetcode/Streetcode.XUnitTest/BLL/MediatR/Toponyms/GetToponymByIdHandlerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| namespace Streetcode.XUnitTest.BLL.MediatR.Toponyms | ||
| { | ||
| using System.Linq.Expressions; | ||
| using AutoMapper; | ||
| using FluentAssertions; | ||
| using Moq; | ||
| using Streetcode.BLL.DTO.Toponyms; | ||
| using Streetcode.BLL.Interfaces.Logging; | ||
| using Streetcode.BLL.MediatR.Toponyms.GetById; | ||
| using Streetcode.DAL.Entities.Toponyms; | ||
| using Streetcode.DAL.Repositories.Interfaces.Base; | ||
| using Streetcode.DAL.Repositories.Interfaces.Toponyms; | ||
| using Xunit; | ||
|
|
||
| /// <summary> | ||
| /// Contains tests for <see cref="GetToponymByIdHandler"/>. | ||
| /// </summary> | ||
| public sealed class GetToponymByIdHandlerTests | ||
| { | ||
| private readonly IMapper mapper; | ||
| private readonly Mock<ILoggerService> loggerMock; | ||
| private readonly Mock<IRepositoryWrapper> repositoryWrapperMock; | ||
| private readonly Mock<IToponymRepository> toponymRepositoryMock; | ||
| private readonly GetToponymByIdHandler handler; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GetToponymByIdHandlerTests"/> class. | ||
| /// </summary> | ||
| public GetToponymByIdHandlerTests() | ||
| { | ||
| this.mapper = new MapperConfiguration(cfg => | ||
| { | ||
| cfg.CreateMap<Toponym, ToponymDTO>(); | ||
| }).CreateMapper(); | ||
| this.loggerMock = new Mock<ILoggerService>(); | ||
| this.repositoryWrapperMock = new Mock<IRepositoryWrapper>(); | ||
| this.toponymRepositoryMock = new Mock<IToponymRepository>(); | ||
| this.repositoryWrapperMock.Setup(r => r.ToponymRepository).Returns(this.toponymRepositoryMock.Object); | ||
| this.handler = new GetToponymByIdHandler(this.repositoryWrapperMock.Object, this.mapper, this.loggerMock.Object); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Should return toponym when found by id. | ||
| /// </summary> | ||
| /// <returns>Awaitable task.</returns> | ||
| [Fact] | ||
| public async Task Handle_ShouldReturnToponym_WhenFound() | ||
| { | ||
| // Arrange | ||
| Toponym toponym = new() | ||
| { | ||
| Id = 2, | ||
| StreetName = "Бандери", | ||
| }; | ||
| ToponymDTO expected_toponym = this.mapper.Map<ToponymDTO>(toponym); | ||
| GetToponymByIdQuery query = new(2); | ||
| this.toponymRepositoryMock.Setup( | ||
| r => r.GetFirstOrDefaultAsync( | ||
| It.IsAny<Expression<Func<Toponym, bool>>>(), | ||
| null | ||
| ) | ||
| ).ReturnsAsync(toponym); | ||
|
|
||
| // Act | ||
| var result = await this.handler.Handle(query, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.IsSuccess.Should().BeTrue(); | ||
| result.Value.Should().BeEquivalentTo(expected_toponym); | ||
| this.toponymRepositoryMock.Verify( | ||
| r => r.GetFirstOrDefaultAsync( | ||
| It.IsAny<Expression<Func<Toponym, bool>>>(), | ||
| null | ||
| ), | ||
| Times.Once | ||
| ); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Should return error and log it when toponym not found by id. | ||
| /// </summary> | ||
| /// <returns>Awaitable task.</returns> | ||
| [Fact] | ||
| public async Task Handle_ShouldReturnError_WhenNotFound() | ||
| { | ||
| // Arrange | ||
| GetToponymByIdQuery query = new(1); | ||
| this.toponymRepositoryMock.Setup( | ||
| r => r.GetFirstOrDefaultAsync( | ||
| It.IsAny<Expression<Func<Toponym, bool>>>(), | ||
| null | ||
| ) | ||
| ).ReturnsAsync(null as Toponym); | ||
| this.loggerMock.Setup( | ||
| l => l.LogError(query, It.IsAny<string>()) | ||
| ); | ||
|
|
||
| // Act | ||
| var result = await this.handler.Handle(query, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.IsSuccess.Should().BeFalse(); | ||
| result.Errors.Should().NotBeEmpty(); | ||
| this.toponymRepositoryMock.Verify( | ||
| r => r.GetFirstOrDefaultAsync( | ||
| It.IsAny<Expression<Func<Toponym, bool>>>(), | ||
| null | ||
| ), | ||
| Times.Once | ||
| ); | ||
| this.loggerMock.Verify( | ||
| l => l.LogError(query, It.IsAny<string>()), | ||
| Times.Once | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.