-
-
Notifications
You must be signed in to change notification settings - Fork 516
/
AppendingEvents.cs
98 lines (81 loc) · 3.1 KB
/
AppendingEvents.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
using FluentAssertions;
using IntroductionToEventSourcing.AppendingEvents.Tools;
using Marten;
using Xunit;
namespace IntroductionToEventSourcing.AppendingEvents;
// EVENTS
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
);
public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);
public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);
public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
);
public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
);
// VALUE OBJECTS
public record PricedProductItem(
ProductItem ProductItem,
decimal UnitPrice
)
{
public Guid ProductId => ProductItem.ProductId;
public int Quantity => ProductItem.Quantity;
public decimal TotalPrice => Quantity * UnitPrice;
}
public record ProductItem(
Guid ProductId,
int Quantity
);
public class GettingStateFromEventsTests
{
// TODO: Fill append events logic here.
private static Task AppendEvents(IDocumentSession documentSession, Guid streamId, object[] events, CancellationToken ct) =>
throw new NotImplementedException();
[Fact]
[Trait("Category", "SkipCI")]
public async Task AppendingEvents_ForSequenceOfEvents_ShouldSucceed()
{
var shoppingCartId = Guid.NewGuid();
var clientId = Guid.NewGuid();
var shoesId = Guid.NewGuid();
var tShirtId = Guid.NewGuid();
var twoPairsOfShoes = new PricedProductItem(new ProductItem(shoesId, 2), 100);
var pairOfShoes = new PricedProductItem(new ProductItem(shoesId, 1), 100);
var tShirt = new PricedProductItem(new ProductItem(tShirtId, 1), 50);
var events = new object[]
{
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)
};
const string connectionString =
"PORT = 5432; HOST = localhost; TIMEOUT = 15; POOLING = True; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'";
using var documentStore = DocumentStore.For(options =>
{
options.Connection(connectionString);
options.DatabaseSchemaName = options.Events.DatabaseSchemaName = "IntroductionToEventSourcing";
});
await using var documentSession = documentStore.LightweightSession();
documentSession.Listeners.Add(MartenEventsChangesListener.Instance);
var exception = await Record.ExceptionAsync(async () =>
await AppendEvents(documentSession, shoppingCartId, events, CancellationToken.None)
);
exception.Should().BeNull();
MartenEventsChangesListener.Instance.AppendedEventsCount.Should().Be(events.Length);
}
}