add some integration tests for flight

This commit is contained in:
meysamhadeli 2022-05-19 22:11:33 +04:30
parent 1971267b8e
commit c7c9b2dd9e
9 changed files with 171 additions and 4 deletions

View File

@ -26,7 +26,7 @@ public class CreateFlightCommandHandler : IRequestHandler<CreateFlightCommand, F
{
Guard.Against.Null(command, nameof(command));
var flight = await _flightDbContext.Flights.SingleOrDefaultAsync(x => x.FlightNumber == command.FlightNumber && !x.IsDeleted,
var flight = await _flightDbContext.Flights.SingleOrDefaultAsync(x => x.Id == command.Id && !x.IsDeleted,
cancellationToken);
if (flight is not null)

View File

@ -27,7 +27,7 @@ public class UpdateFlightCommandHandler : IRequestHandler<UpdateFlightCommand, F
{
Guard.Against.Null(command, nameof(command));
var flight = await _flightDbContext.Flights.SingleOrDefaultAsync(x => x.FlightNumber == command.FlightNumber && !x.IsDeleted,
var flight = await _flightDbContext.Flights.SingleOrDefaultAsync(x => x.Id == command.Id && !x.IsDeleted,
cancellationToken);
if (flight is null)

View File

@ -54,6 +54,18 @@ public class Flight : Aggregate<long>
long arriveAirportId, decimal durationMinutes, DateTime flightDate, FlightStatus status,
decimal price, bool isDeleted = false)
{
FlightNumber = flightNumber;
AircraftId = aircraftId;
DepartureAirportId = departureAirportId;
DepartureDate = departureDate;
arriveDate = ArriveDate;
ArriveAirportId = arriveAirportId;
DurationMinutes = durationMinutes;
FlightDate = flightDate;
Status = status;
Price = price;
IsDeleted = isDeleted;
var @event = new FlightUpdatedDomainEvent(id, flightNumber, aircraftId, departureDate, departureAirportId,
arriveDate, arriveAirportId, durationMinutes, flightDate, status, price, isDeleted);

View File

@ -7,6 +7,8 @@ public sealed class FakeCreateFlightCommand : AutoFaker<CreateFlightCommand>
{
public FakeCreateFlightCommand()
{
RuleFor(r => r.Id, r => r.Random.Number(50, 100000));
RuleFor(r => r.FlightNumber, r => r.Random.String());
RuleFor(r => r.DepartureAirportId, _ => 1);
RuleFor(r => r.ArriveAirportId, _ => 2);
RuleFor(r => r.AircraftId, _ => 1);

View File

@ -0,0 +1,17 @@
using AutoBogus;
using Flight.Flights.Features.UpdateFlight;
namespace Integration.Test.Fakes;
public class FakeUpdateFlightCommand : AutoFaker<UpdateFlightCommand>
{
public FakeUpdateFlightCommand(long id)
{
RuleFor(r => r.Id, _ => id);
RuleFor(r => r.DepartureAirportId, _ => 2);
RuleFor(r => r.ArriveAirportId, _ => 1);
RuleFor(r => r.AircraftId, _ => 2);
RuleFor(r => r.FlightNumber, _ => "12BB");
RuleFor(r => r.Price, _ => 800);
}
}

View File

@ -8,11 +8,11 @@ using Xunit;
namespace Integration.Test.Flight;
[Collection(nameof(TestFixture))]
public class CreateFlightTest
public class CreateFlightTests
{
private readonly TestFixture _fixture;
public CreateFlightTest(TestFixture fixture)
public CreateFlightTests(TestFixture fixture)
{
_fixture = fixture;
}

View File

@ -0,0 +1,51 @@
using System.Linq;
using System.Threading.Tasks;
using AutoBogus;
using Bogus;
using BuildingBlocks.IdsGenerator;
using Flight.Flights.Features.CreateFlight;
using Flight.Flights.Features.GetAvailableFlights;
using Flight.Flights.Features.GetFlightById;
using FluentAssertions;
using Integration.Test.Fakes;
using Xunit;
namespace Integration.Test.Flight;
[Collection(nameof(TestFixture))]
public class GetAvailableFlightsTests
{
private readonly TestFixture _fixture;
public GetAvailableFlightsTests(TestFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task should_return_available_flights()
{
// Arrange
var flightCommand1 = new FakeCreateFlightCommand().Generate();
var flightCommand2 = new FakeCreateFlightCommand().Generate();
var flightEntity1 = global::Flight.Flights.Models.Flight.Create(
flightCommand1.Id, flightCommand1.FlightNumber, flightCommand1.AircraftId, flightCommand1.DepartureAirportId, flightCommand1.DepartureDate,
flightCommand1.ArriveDate, flightCommand1.ArriveAirportId, flightCommand1.DurationMinutes, flightCommand1.FlightDate, flightCommand1.Status, flightCommand1.Price);
var flightEntity2 = global::Flight.Flights.Models.Flight.Create(
flightCommand2.Id, flightCommand2.FlightNumber, flightCommand2.AircraftId, flightCommand2.DepartureAirportId, flightCommand2.DepartureDate,
flightCommand2.ArriveDate, flightCommand2.ArriveAirportId, flightCommand2.DurationMinutes, flightCommand2.FlightDate, flightCommand2.Status, flightCommand2.Price);
await _fixture.InsertAsync(flightEntity1, flightEntity2);
var query = new GetAvailableFlightsQuery();
// Act
var flightResponse = await _fixture.SendAsync(query);
// Assert
flightResponse?.Should().NotBeNull();
flightResponse?.Count().Should().BeGreaterOrEqualTo(2);
}
}

View File

@ -0,0 +1,40 @@
using System.Threading.Tasks;
using BuildingBlocks.Contracts.EventBus.Messages;
using Flight.Flights.Features.CreateFlight;
using Flight.Flights.Features.GetFlightById;
using FluentAssertions;
using Integration.Test.Fakes;
using Xunit;
namespace Integration.Test.Flight;
[Collection(nameof(TestFixture))]
public class GetFlightByIdTests
{
private readonly TestFixture _fixture;
public GetFlightByIdTests(TestFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task should_retrive_a_flight_by_id_currectly()
{
// Arrange
var command = new FakeCreateFlightCommand().Generate();
var flightEntity = global::Flight.Flights.Models.Flight.Create(
command.Id, command.FlightNumber, command.AircraftId, command.DepartureAirportId, command.DepartureDate,
command.ArriveDate, command.ArriveAirportId, command.DurationMinutes, command.FlightDate, command.Status, command.Price);
await _fixture.InsertAsync(flightEntity);
var query = new GetFlightByIdQuery(flightEntity.Id);
// Act
var flightResponse = await _fixture.SendAsync(query);
// Assert
flightResponse.Should().NotBeNull();
flightResponse?.Id.Should().Be(flightEntity.Id);
}
}

View File

@ -0,0 +1,45 @@
using System.Linq;
using System.Threading.Tasks;
using BuildingBlocks.Contracts.EventBus.Messages;
using Flight.Flights.Features.CreateFlight;
using FluentAssertions;
using Integration.Test.Fakes;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace Integration.Test.Flight;
[Collection(nameof(TestFixture))]
public class UpdateFlightTests
{
private readonly TestFixture _fixture;
public UpdateFlightTests(TestFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task should_update_flight_to_db_and_publish_message_to_broker()
{
// Arrange
var fakeCreateCommandFlight = new FakeCreateFlightCommand().Generate();
var flightEntity = global::Flight.Flights.Models.Flight.Create(fakeCreateCommandFlight.Id, fakeCreateCommandFlight.FlightNumber,
fakeCreateCommandFlight.AircraftId, fakeCreateCommandFlight.DepartureAirportId, fakeCreateCommandFlight.DepartureDate,
fakeCreateCommandFlight.ArriveDate, fakeCreateCommandFlight.ArriveAirportId, fakeCreateCommandFlight.DurationMinutes,
fakeCreateCommandFlight.FlightDate, fakeCreateCommandFlight.Status, fakeCreateCommandFlight.Price);
await _fixture.InsertAsync(flightEntity);
var command = new FakeUpdateFlightCommand(flightEntity.Id).Generate();
// Act
var flightResponse = await _fixture.SendAsync(command);
// Assert
flightResponse.Should().NotBeNull();
flightResponse?.Id.Should().Be(flightEntity?.Id);
flightResponse?.Price.Should().NotBe(flightEntity?.Price);
(await _fixture.IsFaultyPublished<FlightUpdated>()).Should().BeFalse();
(await _fixture.IsPublished<FlightUpdated>()).Should().BeTrue();
}
}