2024-08-23 18:02:18 +02:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http.Json;
|
2025-06-12 18:22:37 +02:00
|
|
|
|
using Vegasco.Server.Api.Cars;
|
|
|
|
|
|
using Vegasco.Server.Api.Consumptions;
|
|
|
|
|
|
using Vegasco.Server.Api.Persistence;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
2025-06-12 18:22:37 +02:00
|
|
|
|
namespace Vegasco.Server.Api.Tests.Integration.Consumptions;
|
2024-08-23 18:02:18 +02:00
|
|
|
|
|
|
|
|
|
|
[Collection(SharedTestCollection.Name)]
|
|
|
|
|
|
public class DeleteConsumptionTests : IAsyncLifetime
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly WebAppFactory _factory;
|
|
|
|
|
|
private readonly IServiceScope _scope;
|
|
|
|
|
|
private readonly ApplicationDbContext _dbContext;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly CarFaker _carFaker = new();
|
|
|
|
|
|
private readonly ConsumptionFaker _consumptionFaker = new();
|
|
|
|
|
|
|
|
|
|
|
|
public DeleteConsumptionTests(WebAppFactory factory)
|
|
|
|
|
|
{
|
|
|
|
|
|
_factory = factory;
|
|
|
|
|
|
_scope = _factory.Services.CreateScope();
|
|
|
|
|
|
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task DeleteConsumption_ShouldDeleteConsumption_WhenConsumptionExists()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/consumptions/{createdConsumption.Id}");
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
|
|
|
|
|
|
_dbContext.Consumptions.Should().NotContain(x => x.Id.Value == createdConsumption.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task DeleteConsumption_ShouldReturnNotFound_WhenConsumptionDoesNotExist()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
var consumptionId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/consumptions/{consumptionId}");
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<CreateConsumption.Response> CreateConsumptionAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateCar.Response createdCarResponse = await CreateCarAsync();
|
|
|
|
|
|
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(createdCarResponse.Id);
|
|
|
|
|
|
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
var createdConsumption = await response.Content.ReadFromJsonAsync<CreateConsumption.Response>();
|
|
|
|
|
|
return createdConsumption!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<CreateCar.Response> CreateCarAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateCar.Request createCarRequest = new CarFaker().CreateCarRequest();
|
|
|
|
|
|
using HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
|
|
|
|
|
createCarResponse.EnsureSuccessStatusCode();
|
|
|
|
|
|
var createdCarResponse = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
|
|
|
|
|
return createdCarResponse!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task InitializeAsync() => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DisposeAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
_scope.Dispose();
|
|
|
|
|
|
await _dbContext.DisposeAsync();
|
|
|
|
|
|
await _factory.ResetDatabaseAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|