-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for GroupCheckoutSaga
- Loading branch information
1 parent
53edb0a
commit 1d1d79d
Showing
2 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
Sample/HotelManagement/HotelManagement.Tests/GroupCheckouts/GroupCheckoutSagaTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using Core.Commands; | ||
using FluentAssertions; | ||
using HotelManagement.GroupCheckouts; | ||
using HotelManagement.GuestStayAccounts; | ||
using Xunit; | ||
using GuestCheckoutFailed = HotelManagement.GuestStayAccounts.GuestCheckoutFailed; | ||
|
||
namespace HotelManagement.Tests.GroupCheckouts; | ||
|
||
using static GuestCheckoutFailed; | ||
|
||
public class GroupCheckoutSagaTests | ||
{ | ||
private readonly GroupCheckoutSaga saga; | ||
private readonly AsyncCommandBusStub commandBus = new(); | ||
private readonly Guid groupCheckoutId = Guid.NewGuid(); | ||
private readonly Guid clerkId = Guid.NewGuid(); | ||
private readonly DateTimeOffset now = DateTimeOffset.UtcNow; | ||
private readonly CancellationToken ct = CancellationToken.None; | ||
|
||
public GroupCheckoutSagaTests() => | ||
saga = new GroupCheckoutSaga(commandBus); | ||
|
||
[Fact] | ||
public async Task GroupCheckoutInitiated_ShouldSchedule_CheckOutGuestForEachGuesAndRecordGuestCheckoutsInitiation() | ||
{ | ||
// Given | ||
var guestStayAccountIds = new[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }; | ||
var @event = new GroupCheckoutInitiated(groupCheckoutId, clerkId, guestStayAccountIds, now); | ||
|
||
// When | ||
await saga.Handle(@event, ct); | ||
|
||
// Then | ||
commandBus.ShouldHaveScheduled( | ||
new CheckOutGuest(guestStayAccountIds[0], groupCheckoutId), | ||
new CheckOutGuest(guestStayAccountIds[1], groupCheckoutId), | ||
new CheckOutGuest(guestStayAccountIds[2], groupCheckoutId), | ||
new RecordGuestCheckoutsInitiation(groupCheckoutId, guestStayAccountIds) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task GuestCheckedOutWithGroupCheckOutId_ShouldSchedule_RecordGuestCheckoutCompletion() | ||
{ | ||
// Given | ||
var guestStayAccountId = Guid.NewGuid(); | ||
var @event = new GuestCheckedOut(guestStayAccountId, now, groupCheckoutId); | ||
|
||
// When | ||
await saga.Handle(@event, ct); | ||
|
||
// Then | ||
commandBus.ShouldHaveScheduled( | ||
new RecordGuestCheckoutCompletion(groupCheckoutId, guestStayAccountId, now) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task GuestCheckedOutWithoutGroupCheckOutId_ShouldNotSchedule_RecordsGuestCheckoutCompletion() | ||
{ | ||
// Given | ||
var guestStayAccountId = Guid.NewGuid(); | ||
var @event = new GuestCheckedOut(guestStayAccountId, now); | ||
|
||
// When | ||
await saga.Handle(@event, ct); | ||
|
||
// Then | ||
commandBus.ShouldNotHaveScheduledCommands( | ||
new RecordGuestCheckoutCompletion(groupCheckoutId, guestStayAccountId, now) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task GuestCheckoutFailedWithGroupCheckOutId_ShouldSchedule_RecordGuestCheckoutFailure() | ||
{ | ||
// Given | ||
var guestStayAccountId = Guid.NewGuid(); | ||
var @event = new GuestCheckoutFailed(guestStayAccountId, FailureReason.BalanceNotSettled, now, groupCheckoutId); | ||
|
||
// When | ||
await saga.Handle(@event, ct); | ||
|
||
// Then | ||
commandBus.ShouldHaveScheduled( | ||
new RecordGuestCheckoutFailure(groupCheckoutId, guestStayAccountId, now) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task GuestCheckoutFailedWithoutGroupCheckOutId_ShouldNotSchedule_RecordGuestCheckoutFailure() | ||
{ | ||
// Given | ||
var guestStayAccountId = Guid.NewGuid(); | ||
var @event = new GuestCheckoutFailed(guestStayAccountId, FailureReason.BalanceNotSettled, now); | ||
|
||
// When | ||
await saga.Handle(@event, ct); | ||
|
||
// Then | ||
commandBus.ShouldNotHaveScheduledCommands( | ||
new RecordGuestCheckoutFailure(groupCheckoutId, guestStayAccountId, now) | ||
); | ||
} | ||
} | ||
|
||
internal class AsyncCommandBusStub: IAsyncCommandBus | ||
{ | ||
private readonly List<object> commands = new(); | ||
|
||
public Task Schedule<TCommand>(TCommand command, CancellationToken ct = default) where TCommand : notnull | ||
{ | ||
commands.Add(command); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public void ShouldNotHaveScheduledCommands(params object[] expected) | ||
{ | ||
commands.Should().NotContain(expected); | ||
} | ||
|
||
public void ShouldHaveScheduled(params object[] expected) | ||
{ | ||
commands.Should().Contain(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters