Add consumption logic and endpoints
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.WebApi.Cars;
|
||||
using Vegasco.WebApi.Consumptions;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace WebApi.Tests.Integration.Consumptions;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetConsumptionsTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
private readonly ConsumptionFaker _consumptionFaker = new();
|
||||
|
||||
public GetConsumptionsTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptions_ShouldReturnConsumptions_WhenConsumptionsExist()
|
||||
{
|
||||
// Arrange
|
||||
List<CreateConsumption.Response> createdConsumptions = [];
|
||||
const int numberOfConsumptions = 3;
|
||||
for (var i = 0; i < numberOfConsumptions; i++)
|
||||
{
|
||||
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
||||
createdConsumptions.Add(createdConsumption);
|
||||
}
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/consumptions");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var consumptions = await response.Content.ReadFromJsonAsync<List<GetConsumptions.Response>>();
|
||||
consumptions.Should().BeEquivalentTo(createdConsumptions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptions_ShouldReturnEmptyList_WhenNoConsumptionsExist()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/consumptions");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var consumptions = await response.Content.ReadFromJsonAsync<List<GetConsumptions.Response>>();
|
||||
consumptions.Should().BeEmpty();
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
FluentAssertionConfiguration.SetupGlobalConfig();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user