/
GettingStateFromEventsTests.cs
124 lines (105 loc) · 3.65 KB
/
GettingStateFromEventsTests.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using FluentAssertions;
using Xunit;
namespace IntroductionToEventSourcing.GettingStateFromEvents.Mutable;
using static ShoppingCartEvent;
// EVENTS
public abstract record ShoppingCartEvent
{
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
): ShoppingCartEvent;
public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;
public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;
public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
): ShoppingCartEvent;
public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
): ShoppingCartEvent;
// This won't allow
private ShoppingCartEvent(){}
}
// VALUE OBJECTS
public class PricedProductItem
{
public Guid ProductId { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public decimal TotalPrice => Quantity * UnitPrice;
}
// ENTITY
public class ShoppingCart
{
public Guid Id { get; set; }
public Guid ClientId { get; set; }
public ShoppingCartStatus Status { get; set; }
public IList<PricedProductItem> ProductItems { get; set; } = new List<PricedProductItem>();
public DateTime? ConfirmedAt { get; set; }
public DateTime? CanceledAt { get; set; }
}
public enum ShoppingCartStatus
{
Pending = 1,
Confirmed = 2,
Canceled = 4
}
public class GettingStateFromEventsTests
{
private static ShoppingCart GetShoppingCart(IEnumerable<ShoppingCartEvent> events)
{
// 1. Add logic here
throw new NotImplementedException();
}
[Fact]
[Trait("Category", "SkipCI")]
public void GettingState_ForSequenceOfEvents_ShouldSucceed()
{
var shoppingCartId = Guid.NewGuid();
var clientId = Guid.NewGuid();
var shoesId = Guid.NewGuid();
var tShirtId = Guid.NewGuid();
var twoPairsOfShoes =
new PricedProductItem
{
ProductId = shoesId, Quantity = 2, UnitPrice = 100
};
var pairOfShoes =
new PricedProductItem
{
ProductId = shoesId, Quantity = 1, UnitPrice = 100
};
var tShirt =
new PricedProductItem
{
ProductId = tShirtId, Quantity = 1, UnitPrice = 50
};
var events = new ShoppingCartEvent[]
{
new ShoppingCartOpened(shoppingCartId, clientId),
new ProductItemAddedToShoppingCart(shoppingCartId, twoPairsOfShoes),
new ProductItemAddedToShoppingCart(shoppingCartId, tShirt),
new ProductItemRemovedFromShoppingCart(shoppingCartId, pairOfShoes),
new ShoppingCartConfirmed(shoppingCartId, DateTime.UtcNow),
new ShoppingCartCanceled(shoppingCartId, DateTime.UtcNow)
};
var shoppingCart = GetShoppingCart(events);
shoppingCart.Id.Should().Be(shoppingCartId);
shoppingCart.ClientId.Should().Be(clientId);
shoppingCart.ProductItems.Should().HaveCount(2);
shoppingCart.ProductItems[0].ProductId.Should().Be(shoesId);
shoppingCart.ProductItems[0].Quantity.Should().Be(pairOfShoes.Quantity);
shoppingCart.ProductItems[0].UnitPrice.Should().Be(pairOfShoes.UnitPrice);
shoppingCart.ProductItems[1].ProductId.Should().Be(tShirtId);
shoppingCart.ProductItems[1].Quantity.Should().Be(tShirt.Quantity);
shoppingCart.ProductItems[1].UnitPrice.Should().Be(tShirt.UnitPrice);
}
}