-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2293cc7
commit 96e3e39
Showing
20 changed files
with
1,680 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Sample/EventsVersioning/Talk/HotelManagement.Tests/Downcasters/ChangedStructure.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using HotelManagement.GuestStayAccounts; | ||
|
||
namespace HotelManagement.Tests.Downcasters; | ||
|
||
using V1 = GuestStayAccountEvent; | ||
|
||
public class ChangedStructure | ||
{ | ||
public record Money( | ||
decimal Amount, | ||
string Currency = "CHF" | ||
); | ||
|
||
public record PaymentRecorded( | ||
string GuestStayAccountId, | ||
Money Amount, | ||
DateTimeOffset RecordedAt | ||
); | ||
|
||
public static V1.PaymentRecorded Downcast( | ||
PaymentRecorded newEvent | ||
) | ||
{ | ||
return new V1.PaymentRecorded( | ||
newEvent.GuestStayAccountId, | ||
newEvent.Amount.Amount, | ||
newEvent.RecordedAt | ||
); | ||
} | ||
|
||
public static V1.PaymentRecorded Downcast( | ||
string newEventJson | ||
) | ||
{ | ||
var newEvent = JsonDocument.Parse(newEventJson).RootElement; | ||
|
||
return new V1.PaymentRecorded( | ||
newEvent.GetProperty("GuestStayAccountId").GetString()!, | ||
newEvent.GetProperty("Amount").GetProperty("Amount").GetDecimal(), | ||
newEvent.GetProperty("RecordedAt").GetDateTimeOffset() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void DowncastObjects_Should_BeForwardCompatible() | ||
{ | ||
// Given | ||
var newEvent = new PaymentRecorded( | ||
Guid.NewGuid().ToString(), | ||
new Money((decimal)Random.Shared.NextDouble(), "USD"), | ||
DateTimeOffset.Now | ||
); | ||
|
||
// When | ||
var @event = Downcast(newEvent); | ||
|
||
@event.Should().NotBeNull(); | ||
@event.GuestStayAccountId.Should().Be(newEvent.GuestStayAccountId); | ||
@event.Amount.Should().Be(newEvent.Amount.Amount); | ||
} | ||
|
||
[Fact] | ||
public void DowncastJson_Should_BeForwardCompatible() | ||
{ | ||
// Given | ||
var newEvent = new PaymentRecorded( | ||
Guid.NewGuid().ToString(), | ||
new Money((decimal)Random.Shared.NextDouble(), "USD"), | ||
DateTimeOffset.Now | ||
); | ||
// When | ||
var @event = Downcast( | ||
JsonSerializer.Serialize(newEvent) | ||
); | ||
|
||
@event.Should().NotBeNull(); | ||
@event.GuestStayAccountId.Should().Be(newEvent.GuestStayAccountId); | ||
@event.Amount.Should().Be(newEvent.Amount.Amount); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...Versioning/Talk/HotelManagement.Tests/ExplicitSerialization/ExplicitSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
|
||
namespace HotelManagement.Tests.ExplicitSerialization; | ||
|
||
using static ShoppingCartEvent; | ||
|
||
public class ExplicitSerializationTests | ||
{ | ||
[Fact] | ||
public void ShouldSerializeAndDeserializeEvents() | ||
{ | ||
var shoppingCartId = ShoppingCartId.New(); | ||
var clientId = ClientId.New(); | ||
|
||
var tShirt = ProductId.New(); | ||
var tShirtPrice = Price.Parse(new Money(Amount.Parse(33), Currency.PLN)); | ||
|
||
var shoes = ProductId.New(); | ||
var shoesPrice = Price.Parse(new Money(Amount.Parse(77), Currency.PLN)); | ||
|
||
var events = new ShoppingCartEvent[] | ||
{ | ||
new ShoppingCartOpened( | ||
shoppingCartId, | ||
clientId | ||
), | ||
new ProductItemAddedToShoppingCart( | ||
shoppingCartId, | ||
new PricedProductItem(tShirt, Quantity.Parse(5), tShirtPrice) | ||
), | ||
new ProductItemAddedToShoppingCart( | ||
shoppingCartId, | ||
new PricedProductItem(shoes, Quantity.Parse(1), shoesPrice) | ||
), | ||
new ProductItemRemovedFromShoppingCart( | ||
shoppingCartId, | ||
new PricedProductItem(tShirt, Quantity.Parse(3), tShirtPrice) | ||
), | ||
new ShoppingCartConfirmed( | ||
shoppingCartId, | ||
LocalDateTime.Parse(DateTimeOffset.UtcNow) | ||
) | ||
}; | ||
|
||
var serde = new ShoppingCartEventsSerde(); | ||
|
||
var serializedEvents = events.Select(serde.Serialize); | ||
|
||
var deserializedEvents = serializedEvents.Select(e => | ||
serde.Deserialize(e.EventType, JsonDocument.Parse(e.Data.ToJsonString())) | ||
).ToArray(); | ||
|
||
for (var i = 0; i < deserializedEvents.Length; i++) | ||
{ | ||
deserializedEvents[i].Equals(events[i]).Should().BeTrue(); | ||
} | ||
} | ||
|
||
|
||
[Fact] | ||
public void ShouldGetCurrentShoppingCartState() | ||
{ | ||
var shoppingCartId = ShoppingCartId.New(); | ||
var clientId = ClientId.New(); | ||
|
||
var tShirt = ProductId.New(); | ||
var tShirtPrice = Price.Parse(new Money(Amount.Parse(33), Currency.PLN)); | ||
|
||
var shoes = ProductId.New(); | ||
var shoesPrice = Price.Parse(new Money(Amount.Parse(77), Currency.PLN)); | ||
|
||
var events = new ShoppingCartEvent[] | ||
{ | ||
new ShoppingCartOpened( | ||
shoppingCartId, | ||
clientId | ||
), | ||
new ProductItemAddedToShoppingCart( | ||
shoppingCartId, | ||
new PricedProductItem(tShirt, Quantity.Parse(5), tShirtPrice) | ||
), | ||
new ProductItemAddedToShoppingCart( | ||
shoppingCartId, | ||
new PricedProductItem(shoes, Quantity.Parse(1), shoesPrice) | ||
), | ||
new ProductItemRemovedFromShoppingCart( | ||
shoppingCartId, | ||
new PricedProductItem(tShirt, Quantity.Parse(3), tShirtPrice) | ||
), | ||
new ShoppingCartConfirmed( | ||
shoppingCartId, | ||
LocalDateTime.Parse(DateTimeOffset.UtcNow) | ||
) | ||
}; | ||
|
||
var shoppingCart = events.Aggregate(ShoppingCart.Default, ShoppingCart.Evolve); | ||
|
||
shoppingCart.Id.Should().Be(shoppingCartId); | ||
shoppingCart.ClientId.Should().Be(clientId); | ||
shoppingCart.Status.Should().Be(ShoppingCartStatus.Confirmed); | ||
shoppingCart.ProductItems.Should().HaveCount(2); | ||
shoppingCart.ProductItems.Keys.Should().Contain(new[] { tShirt, shoes }); | ||
shoppingCart.ProductItems[tShirt].Should().Be(Quantity.Parse(2)); | ||
shoppingCart.ProductItems[shoes].Should().Be(Quantity.Parse(1)); | ||
} | ||
} |
Oops, something went wrong.