-
-
Notifications
You must be signed in to change notification settings - Fork 515
/
EventsAggregation.cs
103 lines (80 loc) · 3.17 KB
/
EventsAggregation.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
using FluentAssertions;
using Marten.Integration.Tests.TestsInfrastructure;
using Xunit;
namespace Marten.Integration.Tests.EventStore.Aggregate;
public class EventsAggregation(MartenFixture fixture): MartenTest(fixture.PostgreSqlContainer)
{
public record IssueCreated(
Guid IssueId,
string Description
);
public record IssueUpdated(
Guid IssueId,
string Description
);
public record IssueRemoved(
Guid IssueId
);
public record Issue(
Guid IssueId,
string Description
);
public class IssuesList
{
public Guid Id { get; set; }
public Dictionary<Guid, Issue> Issues { get; } = new();
public void Apply(IssueCreated @event)
{
var (issueId, description) = @event;
Issues.Add(issueId, new Issue(issueId, description));
}
public void Apply(IssueUpdated @event)
{
if (!Issues.ContainsKey(@event.IssueId))
return;
Issues[@event.IssueId] = Issues[@event.IssueId]
with {Description = @event.Description};
}
public void Apply(IssueRemoved @event)
{
Issues.Remove(@event.IssueId);
}
}
[Fact]
public void GivenStreamOfEvents_WhenAggregateStreamIsCalled_ThenChangesAreAppliedProperly()
{
var streamId = Guid.NewGuid();
//1. First Issue Was Created
var issue1Id = Guid.NewGuid();
EventStore.Append(streamId, new IssueCreated(issue1Id, "Description"));
Session.SaveChanges();
var issuesList = EventStore.AggregateStream<IssuesList>(streamId)!;
issuesList.Issues.Should().HaveCount(1);
issuesList.Issues.Values.Single().IssueId.Should().Be(issue1Id);
issuesList.Issues.Values.Single().Description.Should().Be("Description");
//2. First Issue Description Was Changed
EventStore.Append(streamId, new IssueUpdated(issue1Id, "New Description"));
Session.SaveChanges();
issuesList = EventStore.AggregateStream<IssuesList>(streamId)!;
issuesList.Issues.Should().HaveCount(1);
issuesList.Issues.Values.Single().IssueId.Should().Be(issue1Id);
issuesList.Issues.Values.Single().Description.Should().Be("New Description");
//3. Two Other tasks were added
EventStore.Append(streamId, new IssueCreated(Guid.NewGuid(), "Description2"),
new IssueCreated(Guid.NewGuid(), "Description3"));
Session.SaveChanges();
issuesList = EventStore.AggregateStream<IssuesList>(streamId)!;
issuesList.Issues.Should().HaveCount(3);
issuesList.Issues.Values.Select(t => t.Description)
.Should()
.BeEquivalentTo("New Description", "Description2", "Description3");
//4. First issue was removed
EventStore.Append(streamId, new IssueRemoved(issue1Id));
Session.SaveChanges();
issuesList = EventStore.AggregateStream<IssuesList>(streamId)!;
issuesList.Issues.Should().HaveCount(2);
issuesList.Issues.Values.Select(t => t.Description)
.Should()
.BeEquivalentTo("Description2", "Description3");
}
}