Skip to content

Commit

Permalink
Added tests for versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Nov 6, 2024
1 parent 2293cc7 commit 96e3e39
Show file tree
Hide file tree
Showing 20 changed files with 1,680 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Sample/EventsVersioning/Talk/EventsVersioning.Talk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
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
Expand All @@ -12,5 +14,9 @@ Global
{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,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);
}
}
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

0 comments on commit 96e3e39

Please sign in to comment.