Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Es versioning talk #290

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Sample/EventsVersioning/Talk/EventsVersioning.Talk.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotelManagement", "HotelManagement\HotelManagement.csproj", "{8B5F91C2-572B-4B2D-B67B-3EA98584888E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotelManagement.Tests", "HotelManagement.Tests\HotelManagement.Tests.csproj", "{79C84CFC-D0C4-4341-B262-92A99A372242}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8B5F91C2-572B-4B2D-B67B-3EA98584888E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B5F91C2-572B-4B2D-B67B-3EA98584888E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B5F91C2-572B-4B2D-B67B-3EA98584888E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B5F91C2-572B-4B2D-B67B-3EA98584888E}.Release|Any CPU.Build.0 = Release|Any CPU
{79C84CFC-D0C4-4341-B262-92A99A372242}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79C84CFC-D0C4-4341-B262-92A99A372242}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79C84CFC-D0C4-4341-B262-92A99A372242}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79C84CFC-D0C4-4341-B262-92A99A372242}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Text.Json;
using FluentAssertions;
using V1 = HotelManagement.GuestStayAccounts;

namespace HotelManagement.Tests.Downcasters;

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);
}
}
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));
}
}
Loading
Loading