Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gramli committed Mar 12, 2024
1 parent e1de992 commit 259bc83
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 7 deletions.
@@ -0,0 +1,78 @@
using Weather.Core.Abstractions;
using Weather.Core.Commands;
using Weather.Domain.Commands;

namespace Weather.Core.UnitTests.Commands
{
public class DeleteFavoriteHandlerTests
{
private readonly Mock<IWeatherCommandsRepository> _weatherCommandsRepositoryMock;
private readonly Mock<IValidator<DeleteFavoriteCommand>> _validatorMock;

private readonly IDeleteFavoriteHandler _uut;
public DeleteFavoriteHandlerTests()
{
_weatherCommandsRepositoryMock = new();
_validatorMock = new();

_uut = new DeleteFavoriteHandler(_weatherCommandsRepositoryMock.Object, _validatorMock.Object);
}

[Fact]
public async Task InvalidRequest()
{
//Arrange
var deleteFavoriteCommand = new DeleteFavoriteCommand { Id = 5 };

_validatorMock.Setup(x => x.IsValid(It.IsAny<DeleteFavoriteCommand>())).Returns(false);

//Act
var result = await _uut.HandleAsync(deleteFavoriteCommand, CancellationToken.None);

//Assert
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
Assert.Single(result.Errors);
_validatorMock.Verify(x => x.IsValid(It.Is<DeleteFavoriteCommand>(y => y.Equals(deleteFavoriteCommand))), Times.Once);
}

[Fact]
public async Task DeleteFavoriteLocationSafeAsync_Failed()
{
//Arrange
var deleteFavoriteCommand = new DeleteFavoriteCommand { Id = 5 };

_validatorMock.Setup(x => x.IsValid(deleteFavoriteCommand)).Returns(true);
_weatherCommandsRepositoryMock.Setup(x => x.DeleteFavoriteLocationSafeAsync(deleteFavoriteCommand, CancellationToken.None))
.ReturnsAsync(Result.Fail(string.Empty));

//Act
var result = await _uut.HandleAsync(deleteFavoriteCommand, CancellationToken.None);

//Assert
Assert.Equal(HttpStatusCode.InternalServerError, result.StatusCode);
Assert.Single(result.Errors);
_validatorMock.Verify(x => x.IsValid(deleteFavoriteCommand), Times.Once);
_weatherCommandsRepositoryMock.Verify(x => x.DeleteFavoriteLocationSafeAsync(deleteFavoriteCommand, CancellationToken.None), Times.Once);
}

[Fact]
public async Task DeleteFavoriteLocationSafeAsync_Success()
{
//Arrange
var deleteFavoriteCommand = new DeleteFavoriteCommand { Id = 5 };

_validatorMock.Setup(x => x.IsValid(deleteFavoriteCommand)).Returns(true);
_weatherCommandsRepositoryMock.Setup(x => x.DeleteFavoriteLocationSafeAsync(deleteFavoriteCommand, CancellationToken.None))
.ReturnsAsync(Result.Ok());

//Act
var result = await _uut.HandleAsync(deleteFavoriteCommand, CancellationToken.None);

//Assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Empty(result.Errors);
_validatorMock.Verify(x => x.IsValid(deleteFavoriteCommand), Times.Once);
_weatherCommandsRepositoryMock.Verify(x => x.DeleteFavoriteLocationSafeAsync(deleteFavoriteCommand, CancellationToken.None), Times.Once);
}
}
}
Expand Up @@ -35,13 +35,13 @@ public WeatherCommandsRepositoryTests()
public async Task AddFavoriteLocation_Success()
{
//Arrange
var addFacoriteCommand = new AddFavoriteCommand { Location = new LocationDto { Latitude = 1, Longitude = 1 } };
var addFavoriteCommand = new AddFavoriteCommand { Location = new LocationDto { Latitude = 1, Longitude = 1 } };
var favoriteLocationEntity = new FavoriteLocationEntity();

_mapperMock.Setup(x => x.Map<FavoriteLocationEntity>(It.IsAny<LocationDto>())).Returns(favoriteLocationEntity);

//Act
var result = await _uut.AddFavoriteLocation(addFacoriteCommand, CancellationToken.None);
var result = await _uut.AddFavoriteLocation(addFavoriteCommand, CancellationToken.None);

//Assert
Assert.True(result.IsSuccess);
Expand All @@ -54,14 +54,14 @@ public async Task AddFavoriteLocation_Success()
public async Task AddFavoriteLocation_Failed()
{
//Arrange
var addFacoriteCommand = new AddFavoriteCommand { Location = new LocationDto { Latitude = 1, Longitude = 1 } };
var addFavoriteCommand = new AddFavoriteCommand { Location = new LocationDto { Latitude = 1, Longitude = 1 } };
var favoriteLocationEntity = new FavoriteLocationEntity();

_mapperMock.Setup(x => x.Map<FavoriteLocationEntity>(It.IsAny<LocationDto>())).Returns(favoriteLocationEntity);
_favoriteLocationEntityDbSetMock.Setup(x => x.AddAsync(It.IsAny<FavoriteLocationEntity>(), It.IsAny<CancellationToken>())).Throws(new DbUpdateException());

//Act
var result = await _uut.AddFavoriteLocation(addFacoriteCommand, CancellationToken.None);
var result = await _uut.AddFavoriteLocation(addFavoriteCommand, CancellationToken.None);

//Assert
Assert.True(result.IsFailed);
Expand All @@ -75,21 +75,82 @@ public async Task AddFavoriteLocation_Failed()
public async Task AddFavoriteLocation_Throw()
{
//Arrange
var addFacoriteCommand = new AddFavoriteCommand { Location = new LocationDto { Latitude = 1, Longitude = 1 } };
var addFavoriteCommand = new AddFavoriteCommand { Location = new LocationDto { Latitude = 1, Longitude = 1 } };
var favoriteLocationEntity = new FavoriteLocationEntity();
var exception = new ArgumentException("some message");

_mapperMock.Setup(x => x.Map<FavoriteLocationEntity>(It.IsAny<LocationDto>())).Returns(favoriteLocationEntity);
_favoriteLocationEntityDbSetMock.Setup(x => x.AddAsync(It.IsAny<FavoriteLocationEntity>(), It.IsAny<CancellationToken>())).Throws(exception);

//Act
var exceptionResult = await Assert.ThrowsAsync<ArgumentException>(() => _uut.AddFavoriteLocation(addFacoriteCommand, CancellationToken.None));
var exceptionResult = await Assert.ThrowsAsync<ArgumentException>(() => _uut.AddFavoriteLocation(addFavoriteCommand, CancellationToken.None));

//Assert
Assert.Equivalent(exception, exceptionResult);
_mapperMock.Verify(x => x.Map<FavoriteLocationEntity>(It.IsAny<LocationDto>()), Times.Once);
_weatherDbContextMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
_favoriteLocationEntityDbSetMock.Verify(x => x.AddAsync(It.IsAny<FavoriteLocationEntity>(), It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task DeleteFavoriteLocationSafeAsync_Success()
{
//Arrange
var deleteFavoriteCommand = new DeleteFavoriteCommand { Id = 1 };
var favoriteLocationEntity = new FavoriteLocationEntity();
_favoriteLocationEntityDbSetMock.Setup(x => x.FindAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(favoriteLocationEntity);

//Act
var result = await _uut.DeleteFavoriteLocationSafeAsync(deleteFavoriteCommand, CancellationToken.None);

//Assert
Assert.True(result.IsSuccess);
_weatherDbContextMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
_favoriteLocationEntityDbSetMock.Verify(x => x.FindAsync(deleteFavoriteCommand.Id, CancellationToken.None), Times.Once);
_favoriteLocationEntityDbSetMock.Verify(x => x.Remove(favoriteLocationEntity), Times.Once);
}

[Fact]
public async Task DeleteFavoriteLocationSafeAsync_Failed()
{
//Arrange
var deleteFavoriteCommand = new DeleteFavoriteCommand { Id = 1 };
var favoriteLocationEntity = new FavoriteLocationEntity();
_favoriteLocationEntityDbSetMock.Setup(x => x.FindAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(favoriteLocationEntity);
_weatherDbContextMock.Setup(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()))
.ThrowsAsync(new DbUpdateException());

//Act
var result = await _uut.DeleteFavoriteLocationSafeAsync(deleteFavoriteCommand, CancellationToken.None);

//Assert
Assert.True(result.IsFailed);
_weatherDbContextMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
_favoriteLocationEntityDbSetMock.Verify(x => x.FindAsync(deleteFavoriteCommand.Id, CancellationToken.None), Times.Once);
_favoriteLocationEntityDbSetMock.Verify(x => x.Remove(favoriteLocationEntity), Times.Once);
}

[Fact]
public async Task DeleteFavoriteLocationSafeAsync_Throw()
{
//Arrange
var deleteFavoriteCommand = new DeleteFavoriteCommand { Id = 1 };
var favoriteLocationEntity = new FavoriteLocationEntity();
_favoriteLocationEntityDbSetMock.Setup(x => x.FindAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(favoriteLocationEntity);

_weatherDbContextMock.Setup(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()))
.ThrowsAsync(new ArgumentException());

//Act
var _ = await Assert.ThrowsAsync<ArgumentException>(() => _uut.DeleteFavoriteLocationSafeAsync(deleteFavoriteCommand, CancellationToken.None));

//Assert
_weatherDbContextMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
_favoriteLocationEntityDbSetMock.Verify(x => x.FindAsync(deleteFavoriteCommand.Id, CancellationToken.None), Times.Once);
_favoriteLocationEntityDbSetMock.Verify(x => x.Remove(favoriteLocationEntity), Times.Once);
}
}
}
Expand Up @@ -41,7 +41,7 @@ public async Task<Result> DeleteFavoriteLocationSafeAsync(DeleteFavoriteCommand
try
{
var location = await _weatherContext.FavoriteLocations.FindAsync(command.Id, cancellationToken);
_weatherContext.Remove(location!);
_weatherContext.FavoriteLocations.Remove(location!);
await _weatherContext.SaveChangesAsync(cancellationToken);
return Result.Ok();
}
Expand Down

0 comments on commit 259bc83

Please sign in to comment.