-
-
Notifications
You must be signed in to change notification settings - Fork 515
/
ChangedStructure.cs
72 lines (60 loc) · 1.81 KB
/
ChangedStructure.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
using System.Text.Json;
using FluentAssertions;
using V1 = ECommerce.V1;
namespace EventsVersioning.Tests.Upcasters;
public class ChangedStructure
{
public record Client(
Guid Id,
string Name = "Unknown"
);
public record ShoppingCartOpened(
Guid ShoppingCartId,
Client Client
);
public static ShoppingCartOpened Upcast(
V1.ShoppingCartOpened oldEvent
) =>
new(
oldEvent.ShoppingCartId,
new Client(oldEvent.ClientId)
);
public static ShoppingCartOpened Upcast(
string oldEventJson
)
{
var oldEvent = JsonDocument.Parse(oldEventJson).RootElement;
return new ShoppingCartOpened(
oldEvent.GetProperty("ShoppingCartId").GetGuid(),
new Client(
oldEvent.GetProperty("ClientId").GetGuid()
)
);
}
[Fact]
public void UpcastObjects_Should_BeForwardCompatible()
{
// Given
var oldEvent = new V1.ShoppingCartOpened(Guid.NewGuid(), Guid.NewGuid());
// When
var @event = Upcast(oldEvent);
@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(oldEvent.ShoppingCartId);
@event.Client.Id.Should().Be(oldEvent.ClientId);
@event.Client.Name.Should().Be("Unknown");
}
[Fact]
public void UpcastJson_Should_BeForwardCompatible()
{
// Given
var oldEvent = new V1.ShoppingCartOpened(Guid.NewGuid(), Guid.NewGuid());
// When
var @event = Upcast(
JsonSerializer.Serialize(oldEvent)
);
@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(oldEvent.ShoppingCartId);
@event.Client.Id.Should().Be(oldEvent.ClientId);
@event.Client.Name.Should().Be("Unknown");
}
}