/
EventsDefinitionTests.cs
119 lines (99 loc) · 2.93 KB
/
EventsDefinitionTests.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
using FluentAssertions;
using Xunit;
namespace IntroductionToEventSourcing.EventsDefinition.Solution2;
using static ShoppingCartEvent;
// 1. Define your events and entity here
// 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;
}
public record ImmutablePricedProductItem(
Guid ProductId,
int Quantity,
decimal UnitPrice
)
{
public decimal TotalPrice => Quantity * UnitPrice;
}
// ENTITY
// regular one
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; }
}
// immutable one
public record ImmutableShoppingCart(
Guid Id,
Guid ClientId,
ShoppingCartStatus Status,
PricedProductItem[] ProductItems,
DateTime? ConfirmedAt = null,
DateTime? CanceledAt = null
);
public enum ShoppingCartStatus
{
Pending = 1,
Confirmed = 2,
Canceled = 4
}
public class EventsDefinitionTests
{
[Fact]
public void AllEventTypes_ShouldBeDefined()
{
var shoppingCartId = Guid.NewGuid();
var clientId = Guid.NewGuid();
var pairOfShoes = new PricedProductItem
{
ProductId = Guid.NewGuid(),
Quantity = 1,
UnitPrice = 100
};
var events = new object[]
{
new ShoppingCartOpened(shoppingCartId, clientId),
new ProductItemAddedToShoppingCart(shoppingCartId, pairOfShoes),
new ProductItemRemovedFromShoppingCart(shoppingCartId, pairOfShoes),
new ShoppingCartConfirmed(shoppingCartId, DateTime.UtcNow),
new ShoppingCartCanceled(shoppingCartId, DateTime.UtcNow)
};
const int expectedEventTypesCount = 5;
events.Should().HaveCount(expectedEventTypesCount);
events.GroupBy(e => e.GetType()).Should().HaveCount(expectedEventTypesCount);
}
}