mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-15 05:16:45 +08:00
Merge pull request #96 from meysamhadeli/test/add-domain-unit-test
test: Add unit tests for flight domain models
This commit is contained in:
commit
535f96f639
@ -245,7 +245,7 @@ csharp_preserve_single_line_statements = false
|
||||
csharp_preserve_single_line_blocks = true
|
||||
# Namespace options
|
||||
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#namespace-options
|
||||
csharp_style_namespace_declarations = block_scoped:silent
|
||||
csharp_style_namespace_declarations = file_scoped:warning
|
||||
|
||||
##########################################
|
||||
# .NET Naming Rules
|
||||
|
||||
38
src/BuildingBlocks/Utils/ServiceLocator.cs
Normal file
38
src/BuildingBlocks/Utils/ServiceLocator.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace BuildingBlocks.Utils;
|
||||
|
||||
//ref: https://dotnetcoretutorials.com/2018/05/06/servicelocator-shim-for-net-core/
|
||||
public class ServiceLocator
|
||||
{
|
||||
private IServiceProvider _currentServiceProvider;
|
||||
private static IServiceProvider _serviceProvider;
|
||||
|
||||
public ServiceLocator(IServiceProvider currentServiceProvider)
|
||||
{
|
||||
_currentServiceProvider = currentServiceProvider;
|
||||
}
|
||||
|
||||
public static ServiceLocator Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ServiceLocator(_serviceProvider);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetLocatorProvider(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public object GetInstance(Type serviceType)
|
||||
{
|
||||
return _currentServiceProvider.GetService(serviceType);
|
||||
}
|
||||
|
||||
public TService GetInstance<TService>()
|
||||
{
|
||||
return _currentServiceProvider.GetService<TService>();
|
||||
}
|
||||
}
|
||||
14
src/Services/Flight/tests/UnitTest/Fakes/FakeFlightCreate.cs
Normal file
14
src/Services/Flight/tests/UnitTest/Fakes/FakeFlightCreate.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace Unit.Test.Fakes;
|
||||
|
||||
public static class FakeFlightCreate
|
||||
{
|
||||
public static global::Flight.Flights.Models.Flight Generate()
|
||||
{
|
||||
var command = new FakeCreateFlightCommand().Generate();
|
||||
|
||||
return 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);
|
||||
}
|
||||
}
|
||||
12
src/Services/Flight/tests/UnitTest/Fakes/FakeFlightUpdate.cs
Normal file
12
src/Services/Flight/tests/UnitTest/Fakes/FakeFlightUpdate.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace Unit.Test.Fakes;
|
||||
|
||||
using global::Flight.Flights.Models;
|
||||
|
||||
public static class FakeFlightUpdate
|
||||
{
|
||||
public static void Generate(Flight flight)
|
||||
{
|
||||
flight.Update(flight.Id, flight.FlightNumber, 3, flight.DepartureAirportId, flight.DepartureDate,
|
||||
flight.ArriveDate, 3, flight.DurationMinutes, flight.FlightDate, flight.Status, flight.Price, flight.IsDeleted);;
|
||||
}
|
||||
}
|
||||
@ -1,18 +1,15 @@
|
||||
using System;
|
||||
namespace Unit.Test.Flight.Features.Commands.CreateFlight;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Flight.Flights.Dtos;
|
||||
using Flight.Flights.Features.CreateFlight;
|
||||
using Flight.Flights.Features.CreateFlight.Commands.V1;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using NSubstitute;
|
||||
using global::Flight.Flights.Dtos;
|
||||
using global::Flight.Flights.Features.CreateFlight.Commands.V1;
|
||||
using Unit.Test.Common;
|
||||
using Unit.Test.Fakes;
|
||||
using Xunit;
|
||||
|
||||
namespace Unit.Test.Flight.Features.CreateFlight;
|
||||
|
||||
[Collection(nameof(UnitTestFixture))]
|
||||
public class CreateFlightCommandHandlerTests
|
||||
{
|
||||
@ -1,13 +1,12 @@
|
||||
using Flight.Flights.Features.CreateFlight;
|
||||
using Flight.Flights.Features.CreateFlight.Commands.V1;
|
||||
namespace Unit.Test.Flight.Features.Commands.CreateFlight;
|
||||
|
||||
using FluentAssertions;
|
||||
using FluentValidation.TestHelper;
|
||||
using global::Flight.Flights.Features.CreateFlight.Commands.V1;
|
||||
using Unit.Test.Common;
|
||||
using Unit.Test.Fakes;
|
||||
using Xunit;
|
||||
|
||||
namespace Unit.Test.Flight.Features.CreateFlight;
|
||||
|
||||
[Collection(nameof(UnitTestFixture))]
|
||||
public class CreateFlightCommandValidatorTests
|
||||
{
|
||||
@ -0,0 +1,34 @@
|
||||
namespace Unit.Test.Flight.Features.Domain
|
||||
{
|
||||
using System.Linq;
|
||||
using FluentAssertions;
|
||||
using global::Flight.Flights.Features.CreateFlight.Events.Domain.V1;
|
||||
using Unit.Test.Common;
|
||||
using Unit.Test.Fakes;
|
||||
using Xunit;
|
||||
|
||||
[Collection(nameof(UnitTestFixture))]
|
||||
public class CreateFlightTests
|
||||
{
|
||||
[Fact]
|
||||
public void can_create_valid_flight()
|
||||
{
|
||||
// Arrange + Act
|
||||
var fakeFlight = FakeFlightCreate.Generate();
|
||||
|
||||
// Assert
|
||||
fakeFlight.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void queue_domain_event_on_create()
|
||||
{
|
||||
// Arrange + Act
|
||||
var fakeFlight = FakeFlightCreate.Generate();
|
||||
|
||||
// Assert
|
||||
fakeFlight.DomainEvents.Count.Should().Be(1);
|
||||
fakeFlight.DomainEvents.FirstOrDefault().Should().BeOfType(typeof(FlightCreatedDomainEvent));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
namespace Unit.Test.Flight.Features.Domain;
|
||||
|
||||
using System.Linq;
|
||||
using FluentAssertions;
|
||||
using global::Flight.Flights.Features.UpdateFlight.Events.V1;
|
||||
using Unit.Test.Common;
|
||||
using Unit.Test.Fakes;
|
||||
using Xunit;
|
||||
|
||||
[Collection(nameof(UnitTestFixture))]
|
||||
public class UpdateFlightTests
|
||||
{
|
||||
[Fact]
|
||||
public void can_update_valid_flight()
|
||||
{
|
||||
// Arrange
|
||||
var fakeFlight = FakeFlightCreate.Generate();
|
||||
|
||||
// Act
|
||||
FakeFlightUpdate.Generate(fakeFlight);
|
||||
|
||||
// Assert
|
||||
fakeFlight.ArriveAirportId.Should().Be(3);
|
||||
fakeFlight.AircraftId.Should().Be(3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void queue_domain_event_on_update()
|
||||
{
|
||||
// Arrange
|
||||
var fakeFlight = FakeFlightCreate.Generate();
|
||||
fakeFlight.ClearDomainEvents();
|
||||
|
||||
// Act
|
||||
FakeFlightUpdate.Generate(fakeFlight);
|
||||
|
||||
// Assert
|
||||
fakeFlight.DomainEvents.Count.Should().Be(1);
|
||||
fakeFlight.DomainEvents.FirstOrDefault().Should().BeOfType(typeof(FlightUpdatedDomainEvent));
|
||||
}
|
||||
}
|
||||
@ -21,4 +21,8 @@
|
||||
<ProjectReference Include="..\..\src\Flight.Api\Flight.Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Flight\Features\Commands" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user