-
-
Notifications
You must be signed in to change notification settings - Fork 515
/
InlineAggregationStorage.cs
83 lines (67 loc) · 2.59 KB
/
InlineAggregationStorage.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
using FluentAssertions;
using Marten.Events.Projections;
using Marten.Integration.Tests.TestsInfrastructure;
using Xunit;
namespace Marten.Integration.Tests.EventStore.Aggregate;
public class InlineAggregationStorage(MartenFixture fixture): MartenTest(fixture.PostgreSqlContainer)
{
public record IssueCreated(
Guid IssueId,
string Description
);
public record IssueUpdated(
Guid IssueId,
string Description
);
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)
{
Issues[@event.IssueId] = Issues[@event.IssueId]
with
{
Description = @event.Description
};
}
}
protected override IDocumentSession CreateSession(Action<StoreOptions>? storeOptions = null) =>
base.CreateSession(options =>
{
//It's needed to manually set that inline aggregation should be applied
options.Projections.Snapshot<IssuesList>(SnapshotLifecycle.Inline);
});
[Fact]
public void GivenEvents_WhenInlineTransformationIsApplied_ThenReturnsSameNumberOfTransformedItems()
{
var issue1Id = Guid.NewGuid();
var issue2Id = Guid.NewGuid();
var events = new object[]
{
new IssueCreated(issue1Id, "Description 1"), new IssueUpdated(issue1Id, "Description 1 New"),
new IssueCreated(issue2Id, "Description 2"), new IssueUpdated(issue1Id, "Description 1 Super New"),
new IssueUpdated(issue2Id, "Description 2 New"),
};
//1. Create events
var streamId = EventStore.StartStream<IssuesList>(events).Id;
Session.SaveChanges();
//2. Get live aggregation
var issuesListFromLiveAggregation = EventStore.AggregateStream<IssuesList>(streamId)!;
//3. Get inline aggregation
var issuesListFromInlineAggregation = Session.Load<IssuesList>(streamId)!;
issuesListFromLiveAggregation.Should().NotBeNull();
issuesListFromInlineAggregation.Should().NotBeNull();
issuesListFromLiveAggregation.Issues.Count.Should().Be(2);
issuesListFromLiveAggregation.Issues.Count.Should().Be(issuesListFromInlineAggregation.Issues.Count);
}
}