-
-
Notifications
You must be signed in to change notification settings - Fork 515
/
AggregateWithWhenTests.cs
145 lines (121 loc) · 3.6 KB
/
AggregateWithWhenTests.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Core.Aggregates;
using FluentAssertions;
using Xunit;
namespace Core.Tests;
public record Person(
string Name,
string Address
);
public record InvoiceInitiated(
double Amount,
string Number,
Person IssuedTo,
DateTime InitiatedAt
);
public record InvoiceIssued(
string IssuedBy,
DateTime IssuedAt
);
public enum InvoiceSendMethod
{
Email,
Post
}
public record InvoiceSent(
InvoiceSendMethod SentVia,
DateTime SentAt
);
public enum InvoiceStatus
{
Initiated = 1,
Issued = 2,
Sent = 3
}
public class Invoice: Aggregate<object, string>
{
public double Amount { get; private set; }
public string Number { get; private set; } = default!;
public InvoiceStatus Status { get; private set; }
public Person IssuedTo { get; private set; } = default!;
public DateTime InitiatedAt { get; private set; }
public string? IssuedBy { get; private set; }
public DateTime IssuedAt { get; private set; }
public InvoiceSendMethod SentVia { get; private set; }
public DateTime SentAt { get; private set; }
public override void Apply(object @event)
{
switch (@event)
{
case InvoiceInitiated invoiceInitiated:
Apply(invoiceInitiated);
break;
case InvoiceIssued invoiceIssued:
Apply(invoiceIssued);
break;
case InvoiceSent invoiceSent:
Apply(invoiceSent);
break;
}
}
private void Apply(InvoiceInitiated @event)
{
Id = @event.Number;
Amount = @event.Amount;
Number = @event.Number;
IssuedTo = @event.IssuedTo;
InitiatedAt = @event.InitiatedAt;
Status = InvoiceStatus.Initiated;
}
private void Apply(InvoiceIssued @event)
{
IssuedBy = @event.IssuedBy;
IssuedAt = @event.IssuedAt;
Status = InvoiceStatus.Issued;
}
private void Apply(InvoiceSent @event)
{
SentVia = @event.SentVia;
SentAt = @event.SentAt;
Status = InvoiceStatus.Sent;
}
}
public class AggregateWithWhenTests
{
[Fact]
public void AggregationWithWhenShouldGetTheCurrentState()
{
var invoiceInitiated = new InvoiceInitiated(
34.12,
"INV/2021/11/01",
new Person("Oscar the Grouch", "123 Sesame Street"),
DateTime.UtcNow
);
var invoiceIssued = new InvoiceIssued(
"Cookie Monster",
DateTime.UtcNow
);
var invoiceSent = new InvoiceSent(
InvoiceSendMethod.Email,
DateTime.UtcNow
);
// 1. Get all events and sort them in the order of appearance
var events = new object[] {invoiceInitiated, invoiceIssued, invoiceSent};
// 2. Construct empty Invoice object
var invoice = new Invoice();
// 3. Apply each event on the entity.
foreach (var @event in events)
{
invoice.Apply(@event);
}
invoice.Id.Should().Be(invoiceInitiated.Number);
invoice.Amount.Should().Be(invoiceInitiated.Amount);
invoice.Number.Should().Be(invoiceInitiated.Number);
invoice.Status.Should().Be(InvoiceStatus.Sent);
invoice.IssuedTo.Should().Be(invoiceInitiated.IssuedTo);
invoice.InitiatedAt.Should().Be(invoiceInitiated.InitiatedAt);
invoice.IssuedBy.Should().Be(invoiceIssued.IssuedBy);
invoice.IssuedAt.Should().Be(invoiceIssued.IssuedAt);
invoice.SentVia.Should().Be(invoiceSent.SentVia);
invoice.SentAt.Should().Be(invoiceSent.SentAt);
}
}