-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathNewRequiredPropertyFromMetadata.cs
78 lines (66 loc) · 2.24 KB
/
NewRequiredPropertyFromMetadata.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
using System.Text.Json;
using FluentAssertions;
using V1 = ECommerce.V1;
namespace EventsVersioning.Tests.Upcasters;
public class NewRequiredPropertyFromMetadata
{
public record EventMetadata(
Guid UserId
);
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId,
Guid InitializedBy
);
public static ShoppingCartOpened Upcast(
V1.ShoppingCartOpened oldEvent,
EventMetadata eventMetadata
) =>
new(
oldEvent.ShoppingCartId,
oldEvent.ClientId,
eventMetadata.UserId
);
public static ShoppingCartOpened Upcast(
string oldEventJson,
string eventMetadataJson
)
{
var oldEvent = JsonDocument.Parse(oldEventJson);
var eventMetadata = JsonDocument.Parse(eventMetadataJson);
return new ShoppingCartOpened(
oldEvent.RootElement.GetProperty("ShoppingCartId").GetGuid(),
oldEvent.RootElement.GetProperty("ClientId").GetGuid(),
eventMetadata.RootElement.GetProperty("UserId").GetGuid()
);
}
[Fact]
public void UpcastObjects_Should_BeForwardCompatible()
{
// Given
var oldEvent = new V1.ShoppingCartOpened(Guid.NewGuid(), Guid.NewGuid());
var eventMetadata = new EventMetadata(Guid.NewGuid());
// When
var @event = Upcast(oldEvent, eventMetadata);
@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(oldEvent.ShoppingCartId);
@event.ClientId.Should().Be(oldEvent.ClientId);
@event.InitializedBy.Should().Be(eventMetadata.UserId);
}
[Fact]
public void UpcastJson_Should_BeForwardCompatible()
{
// Given
var oldEvent = new V1.ShoppingCartOpened(Guid.NewGuid(), Guid.NewGuid());
var eventMetadata = new EventMetadata(Guid.NewGuid());
// When
var @event = Upcast(
JsonSerializer.Serialize(oldEvent),
JsonSerializer.Serialize(eventMetadata)
);
@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(oldEvent.ShoppingCartId);
@event.ClientId.Should().Be(oldEvent.ClientId);
@event.InitializedBy.Should().Be(eventMetadata.UserId);
}
}