-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShoppingCartTests.cs
82 lines (65 loc) · 2.81 KB
/
ShoppingCartTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Ogooreck.BusinessLogic;
using Ogooreck.Sample.BusinessLogic.Tests.Aggregates.EventSourced.Core;
using Ogooreck.Sample.BusinessLogic.Tests.Aggregates.EventSourced.Pricing;
using Ogooreck.Sample.BusinessLogic.Tests.Aggregates.EventSourced.Products;
using Ogooreck.Sample.BusinessLogic.Tests.Functions.EventSourced;
namespace Ogooreck.Sample.BusinessLogic.Tests.Aggregates.EventSourced;
using static ShoppingCartEventsBuilder;
using static ProductItemBuilder;
using static AggregateTestExtensions<ShoppingCart>;
public class ShoppingCartTests
{
private readonly Random random = new();
private readonly HandlerSpecification<ShoppingCart> Spec =
Specification.For<ShoppingCart>(Handle, ShoppingCart.Evolve);
private class DummyProductPriceCalculator: IProductPriceCalculator
{
private readonly decimal price;
public DummyProductPriceCalculator(decimal price) => this.price = price;
public IReadOnlyList<PricedProductItem> Calculate(params ProductItem[] productItems) =>
productItems.Select(pi => PricedProductItem.For(pi, price)).ToList();
}
[Fact]
public void GivenNonExistingShoppingCart_WhenOpenWithValidParams_ThenSucceeds()
{
var shoppingCartId = Guid.NewGuid();
var clientId = Guid.NewGuid();
Spec.Given()
.When(() => ShoppingCart.Open(shoppingCartId, clientId))
.Then(new ShoppingCartOpened(shoppingCartId, clientId));
}
[Fact]
public void GivenOpenShoppingCart_WhenAddProductWithValidParams_ThenSucceeds()
{
var shoppingCartId = Guid.NewGuid();
var productItem = ValidProductItem();
var price = random.Next(1, 1000);
var priceCalculator = new DummyProductPriceCalculator(price);
Spec.Given(ShoppingCartOpened(shoppingCartId))
.When(cart => cart.AddProduct(priceCalculator, productItem))
.Then(new ProductAdded(shoppingCartId, PricedProductItem.For(productItem, price)));
}
}
public static class ShoppingCartEventsBuilder
{
public static ShoppingCartOpened ShoppingCartOpened(Guid shoppingCartId)
{
var clientId = Guid.NewGuid();
return new ShoppingCartOpened(shoppingCartId, clientId);
}
}
public static class ProductItemBuilder
{
private static readonly Random Random = new();
public static ProductItem ValidProductItem() =>
ProductItem.From(Guid.NewGuid(), Random.Next(1, 100));
}
public static class AggregateTestExtensions<TAggregate> where TAggregate : Aggregate
{
public static DecideResult<object, TAggregate> Handle(Handler<object, TAggregate> handle, TAggregate aggregate)
{
var result = handle(aggregate);
var updatedAggregate = result.NewState ?? aggregate;
return DecideResult.For(updatedAggregate, updatedAggregate.DequeueUncommittedEvents());
}
}