-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved the Cashier domain. TODO: update UI
Added CreateOrder, AddItemToOrder, PlaceOrder commands and its related events/handlers.
- Loading branch information
1 parent
298dbee
commit 3710d0c
Showing
27 changed files
with
498 additions
and
206 deletions.
There are no files selected for viewing
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
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
112 changes: 112 additions & 0 deletions
112
src/Cashier/NCafe.Cashier.Domain.Tests/Commands/AddItemTests.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,112 @@ | ||
using NCafe.Cashier.Domain.Commands; | ||
using NCafe.Cashier.Domain.Entities; | ||
using NCafe.Cashier.Domain.Exceptions; | ||
using NCafe.Cashier.Domain.ReadModels; | ||
using NCafe.Cashier.Domain.ValueObjects; | ||
using NCafe.Core.ReadModels; | ||
using NCafe.Core.Repositories; | ||
|
||
namespace NCafe.Cashier.Domain.Tests.Commands; | ||
|
||
public class AddItemTests | ||
{ | ||
private readonly AddItemToOrderHandler _sut; | ||
private readonly IRepository _repository; | ||
private readonly IReadModelRepository<Product> _productRepository; | ||
|
||
public AddItemTests() | ||
{ | ||
_repository = A.Fake<IRepository>(); | ||
_productRepository = A.Fake<IReadModelRepository<Product>>(); | ||
_sut = new AddItemToOrderHandler(_repository, _productRepository); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldSaveOrder() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var productId = Guid.NewGuid(); | ||
var quantity = 1; | ||
var command = new AddItemToOrder(orderId, productId, quantity); | ||
|
||
A.CallTo(() => _repository.GetById<Order>(orderId)) | ||
.Returns(Task.FromResult(new Order(orderId, "cashier-1", DateTimeOffset.UtcNow))); | ||
|
||
A.CallTo(() => _productRepository.GetById(command.ProductId)) | ||
.Returns(new Product { Name = "Latte", Price = 5 }); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); | ||
|
||
// Assert | ||
exception.ShouldBeNull(); | ||
A.CallTo(() => _repository.Save(A<Order>.That.Matches(o => o.Id == orderId && | ||
o.Items.Any(i => i.ProductId == productId && i.Quantity == quantity)))) | ||
.MustHaveHappenedOnceExactly(); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenOrderNotFound_ShouldThrow() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var productId = Guid.NewGuid(); | ||
var quantity = 1; | ||
var command = new AddItemToOrder(orderId, productId, quantity); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); | ||
|
||
// Assert | ||
exception.ShouldBeOfType<OrderNotFoundException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenProductNotFound_ShouldThrow() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var productId = Guid.NewGuid(); | ||
var quantity = 1; | ||
var command = new AddItemToOrder(orderId, productId, quantity); | ||
|
||
A.CallTo(() => _repository.GetById<Order>(orderId)) | ||
.Returns(Task.FromResult(new Order(orderId, "cashier-1", DateTimeOffset.UtcNow))); | ||
|
||
A.CallTo(() => _productRepository.GetById(command.ProductId)) | ||
.Returns(null); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); | ||
|
||
// Assert | ||
exception.ShouldBeOfType<ProductNotFoundException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenNotNewOrder_ShouldThrow() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var order = new Order(orderId, "cashier-1", DateTimeOffset.UtcNow); | ||
var productId = Guid.NewGuid(); | ||
var quantity = 1; | ||
var command = new AddItemToOrder(orderId, productId, quantity); | ||
|
||
order.AddItem(new OrderItem(Guid.NewGuid(), 1, "Latte", 5)); | ||
order.PlaceOrder(new Customer("John Doe"), DateTimeOffset.UtcNow); | ||
|
||
A.CallTo(() => _repository.GetById<Order>(orderId)) | ||
.Returns(Task.FromResult(order)); | ||
|
||
A.CallTo(() => _productRepository.GetById(command.ProductId)) | ||
.Returns(new Product { Name = "Latte", Price = 5 }); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); | ||
|
||
// Assert | ||
exception.ShouldBeOfType<CannotAddItemToOrderException>(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Cashier/NCafe.Cashier.Domain.Tests/Commands/CreateOrderTests.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,40 @@ | ||
using Microsoft.Extensions.Time.Testing; | ||
using NCafe.Cashier.Domain.Commands; | ||
using NCafe.Cashier.Domain.Entities; | ||
using NCafe.Core.Repositories; | ||
|
||
namespace NCafe.Cashier.Domain.Tests.Commands; | ||
|
||
public class CreateOrderTests | ||
{ | ||
private readonly CreateOrderHandler _sut; | ||
private readonly IRepository _repository; | ||
private readonly FakeTimeProvider _timeProvider; | ||
|
||
public CreateOrderTests() | ||
{ | ||
_repository = A.Fake<IRepository>(); | ||
_timeProvider = new FakeTimeProvider(); | ||
_sut = new CreateOrderHandler(_repository, _timeProvider); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldSaveOrder() | ||
{ | ||
// Arrange | ||
var createdAt = DateTimeOffset.UtcNow; | ||
_timeProvider.SetUtcNow(createdAt); | ||
var createdBy = "cashier-1"; | ||
var command = new CreateOrder(createdBy); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); | ||
|
||
// Assert | ||
exception.ShouldBeNull(); | ||
A.CallTo(() => _repository.Save(A<Order>.That.Matches(o => o.Status == OrderStatus.New && | ||
o.CreatedBy == createdBy && | ||
o.CreatedAt == createdAt))) | ||
.MustHaveHappenedOnceExactly(); | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PayForOrderTests.cs
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.