Skip to content
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

Delete Favorites #2

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Tests/HttpDebug/debug-tests.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
@host={{hostname}}:{{port}}

### get current weather request
GET https://{{host}}/weather/v1/current?latitude=38.5&longtitude=-78.5
GET https://{{host}}/weather/v1/current?latitude=38.5&longitude=-78.5
Content-Type: application/json

### get forecast weather request
GET https://{{host}}/weather/v1/forecast?latitude=38.5&longtitude=-78.5
GET https://{{host}}/weather/v1/forecast?latitude=38.5&longitude=-78.5
Content-Type: application/json

### get favorites weather request
Expand All @@ -23,4 +23,8 @@ Content-Type: application/json
"latitude": 38.5,
"longitude": -78.5
}
}
}

### add favorites weather request
DELETE https://{{host}}/weather/v1/favorite/1
Content-Type: application/json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Weather.API.SystemTests
public class WeatherSystemTests
{
private readonly double latitude = 38.5;
private readonly double longtitude = -78.5;
private readonly double longitude = -78.5;
private readonly string cityName = "Stanley";

private readonly HttpClient _httpClient;
Expand All @@ -26,7 +26,7 @@ public async Task GetCurrentWeather()
{
//Arrange
//Act
var response = await _httpClient.GetAsync($"weather/v1/current?latitude={latitude}&longtitude={longtitude}");
var response = await _httpClient.GetAsync($"weather/v1/current?latitude={latitude}&longitude={longitude}");

//Assert
response.EnsureSuccessStatusCode();
Expand All @@ -41,7 +41,7 @@ public async Task GetForecastWeather()
{
//Arrange
//Act
var response = await _httpClient.GetAsync($"weather/v1/forecast?latitude={latitude}&longtitude={longtitude}");
var response = await _httpClient.GetAsync($"weather/v1/forecast?latitude={latitude}&longitude={longitude}");

//Assert
response.EnsureSuccessStatusCode();
Expand Down Expand Up @@ -82,6 +82,27 @@ public async Task GetWeatherFavorites()
Assert.Equal(cityName, resultDto.Data.FavoriteWeathers.First().CityName);
}

[Fact]
public async Task DeleteWeatherFavorites()
{
//Arrange
var addResponse = await AddFavorite();

addResponse.EnsureSuccessStatusCode();

var content = await addResponse.Content.ReadAsStringAsync();
var addResult = JsonConvert.DeserializeObject<DataResponse<int>>(content);
//Act
var response = await _httpClient.DeleteAsync($"weather/v1/favorite/{addResult!.Data}");

//Assert
response.EnsureSuccessStatusCode();
var stringResult = await response.Content.ReadAsStringAsync();
var resultDto = JsonConvert.DeserializeObject<DataResponse<bool>>(stringResult);
Assert.NotNull(resultDto?.Data);
Assert.True(resultDto.Data);
}

private async Task<HttpResponseMessage> AddFavorite()
{
//Arrange
Expand All @@ -90,7 +111,7 @@ private async Task<HttpResponseMessage> AddFavorite()
Location = new LocationDto
{
Latitude = latitude,
Longitude = longtitude,
Longitude = longitude,
}
});
var content = new StringContent(body, Encoding.UTF8, "application/json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public async Task InvalidLocation()
//Assert
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
Assert.Single(result.Errors);
Assert.False(result.Data);
_addFavoriteCommandValidatorMock.Verify(x => x.IsValid(It.Is<AddFavoriteCommand>(y => y.Equals(addFavoriteCommand))), Times.Once);
}

Expand All @@ -58,7 +57,6 @@ public async Task AddFavoriteLocation_Failed()
//Assert
Assert.Equal(HttpStatusCode.InternalServerError, result.StatusCode);
Assert.Single(result.Errors);
Assert.False(result.Data);
Assert.Equal(ErrorMessages.CantStoreLocation, result.Errors.Single());
_addFavoriteCommandValidatorMock.Verify(x => x.IsValid(It.Is<AddFavoriteCommand>(y => y.Equals(addFavoriteCommand))), Times.Once);
_weatherCommandsRepositoryMock.Verify(x => x.AddFavoriteLocation(It.Is<AddFavoriteCommand>(y=>y.Equals(addFavoriteCommand)), It.IsAny<CancellationToken>()), Times.Once);
Expand All @@ -81,7 +79,7 @@ public async Task Success()
//Assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Empty(result.Errors);
Assert.True(result.Data);
Assert.Equal(locationId, result.Data);
_addFavoriteCommandValidatorMock.Verify(x => x.IsValid(It.Is<AddFavoriteCommand>(y => y.Equals(addFavoriteCommand))), Times.Once);
_weatherCommandsRepositoryMock.Verify(x => x.AddFavoriteLocation(It.Is<AddFavoriteCommand>(y => y.Equals(addFavoriteCommand)), It.IsAny<CancellationToken>()), Times.Once);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Weather.Core.Abstractions;
using Weather.Core.Queries;
using Weather.Core.Resources;
using Weather.Domain.BusinessEntities;
using Weather.Domain.Dtos;
using Weather.Domain.Http;
using Weather.Domain.Logging;
Expand Down Expand Up @@ -37,7 +38,7 @@ public async Task GetFavorites_Empty()
{
//Arrange
_weatherRepositoryMock.Setup(x => x.GetFavorites(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LocationDto>());
.ReturnsAsync(new List<FavoriteLocation>());

//Act
var result = await _uut.HandleAsync(EmptyRequest.Instance, CancellationToken.None);
Expand All @@ -52,10 +53,10 @@ public async Task GetFavorites_Empty()
public async Task InvalidLocation()
{
//Arrange
var locationDto = new LocationDto { Latitude = 1, Longitude = 1 };
var locationDto = new FavoriteLocation { Id =0, Latitude = 1, Longitude = 1 };

_weatherRepositoryMock.Setup(x => x.GetFavorites(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LocationDto>
.ReturnsAsync(new List<FavoriteLocation>
{
locationDto,
});
Expand All @@ -79,10 +80,10 @@ public async Task EmptyResult_GetCurrentWeather_Fail()
{
//Arrange
var failMessage = "Some fail message";
var locationDto = new LocationDto { Latitude = 1, Longitude = 1 };
var locationDto = new FavoriteLocation { Id = 0, Latitude = 1, Longitude = 1 };

_weatherRepositoryMock.Setup(x => x.GetFavorites(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LocationDto>
.ReturnsAsync(new List<FavoriteLocation>
{
locationDto,
});
Expand All @@ -107,13 +108,13 @@ public async Task One_Of_GetCurrentWeather_Failed()
{
//Arrange
var failMessage = "Some fail message";
var locationDto = new LocationDto { Latitude = 1, Longitude = 1 };
var locationDto = new FavoriteLocation { Id = 0, Latitude = 1, Longitude = 1 };

_weatherRepositoryMock.Setup(x => x.GetFavorites(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LocationDto>
.ReturnsAsync(new List<FavoriteLocation>
{
locationDto,
new LocationDto(),
new FavoriteLocation(),
});

_locationValidatorMock.Setup(x => x.IsValid(It.IsAny<LocationDto>())).Returns(true);
Expand All @@ -122,8 +123,8 @@ public async Task One_Of_GetCurrentWeather_Failed()

_currentWeatherValidatorMock.Setup(x => x.IsValid(It.IsAny<CurrentWeatherDto>())).Returns(true);

_weatherServiceMock.Setup(x => x.GetCurrentWeather(It.Is<LocationDto>(y=> y.Equals(locationDto)), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Fail(failMessage));
_weatherServiceMock.Setup(x => x.GetCurrentWeather(It.Is<LocationDto>(y => !y.Equals(locationDto)), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Ok(currentWeather));
_weatherServiceMock.Setup(x => x.GetCurrentWeather(It.Is<FavoriteLocation>(y=> y.Equals(locationDto)), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Fail(failMessage));
_weatherServiceMock.Setup(x => x.GetCurrentWeather(It.Is<FavoriteLocation>(y => !y.Equals(locationDto)), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Ok(currentWeather));
//Act
var result = await _uut.HandleAsync(EmptyRequest.Instance, CancellationToken.None);

Expand All @@ -132,7 +133,7 @@ public async Task One_Of_GetCurrentWeather_Failed()
Assert.Single(result.Errors);
Assert.NotNull(result.Data);
Assert.Single(result.Data.FavoriteWeathers);
Assert.Equal(currentWeather, result.Data.FavoriteWeathers.Single());
Assert.Equal(currentWeather.CityName, result.Data.FavoriteWeathers.Single().CityName);
_weatherRepositoryMock.Verify(x => x.GetFavorites(It.IsAny<CancellationToken>()), Times.Once);
_weatherServiceMock.Verify(x => x.GetCurrentWeather(It.IsAny<LocationDto>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
_loggerMock.VerifyLog(LogLevel.Warning, LogEvents.FavoriteWeathersGeneral, failMessage, Times.Once());
Expand All @@ -144,10 +145,10 @@ public async Task One_Of_GetCurrentWeather_Failed()
public async Task GetCurrentWeather_Validation_Fail()
{
//Arrange
var locationDto = new LocationDto { Latitude = 1, Longitude = 1 };
var locationDto = new FavoriteLocation { Id = 0, Latitude = 1, Longitude = 1 };

_weatherRepositoryMock.Setup(x => x.GetFavorites(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LocationDto>
.ReturnsAsync(new List<FavoriteLocation>
{
locationDto,
});
Expand All @@ -174,10 +175,10 @@ public async Task GetCurrentWeather_Validation_Fail()
public async Task Success()
{
//Arrange
var locationDto = new LocationDto { Latitude = 1, Longitude = 1 };
var locationDto = new FavoriteLocation { Latitude = 1, Longitude = 1 };

_weatherRepositoryMock.Setup(x => x.GetFavorites(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<LocationDto>
.ReturnsAsync(new List<FavoriteLocation>
{
locationDto,
});
Expand All @@ -195,7 +196,7 @@ public async Task Success()
Assert.Empty(result.Errors);
Assert.NotNull(result.Data);
Assert.Single(result.Data.FavoriteWeathers);
Assert.Equal(currentWeather, result.Data.FavoriteWeathers.Single());
Assert.Equal(currentWeather.CityName, result.Data.FavoriteWeathers.Single().CityName);
_weatherRepositoryMock.Verify(x => x.GetFavorites(It.IsAny<CancellationToken>()), Times.Once);
_weatherServiceMock.Verify(x => x.GetCurrentWeather(It.Is<LocationDto>(y=>y.Equals(locationDto)), It.IsAny<CancellationToken>()), Times.Once);
_locationValidatorMock.Verify(x => x.IsValid(It.Is<LocationDto>(y => y.Equals(locationDto))), Times.Once);
Expand Down
Loading
Loading