From fd71ef750d8893c66cb27a50370695c2d28b86c0 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Thu, 1 Sep 2022 16:47:43 +0200 Subject: [PATCH] Added tests for multiple events' schemas with CLR upcasters using classes --- .../Classes/ClrUpcastConfiguration.cs | 228 ++++++++++++++++++ .../Classes/JsonNetUpcastConfiguration.cs | 155 ++++++++++++ .../SystemTextJsonUpcastConfiguration.cs | 207 ++++++++++++++++ .../MultipleSchemaVersions.cs | 15 +- 4 files changed, 598 insertions(+), 7 deletions(-) create mode 100644 src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/ClrUpcastConfiguration.cs create mode 100644 src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/JsonNetUpcastConfiguration.cs create mode 100644 src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/SystemTextJsonUpcastConfiguration.cs diff --git a/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/ClrUpcastConfiguration.cs b/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/ClrUpcastConfiguration.cs new file mode 100644 index 0000000000..7bc80ddbb1 --- /dev/null +++ b/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/ClrUpcastConfiguration.cs @@ -0,0 +1,228 @@ +#nullable enable +#if NET6_0_OR_GREATER +using System; +using Marten; +using Marten.Services.Json.Transformations; +using static Marten.Events.EventMappingExtensions; + +namespace EventSourcingTests.SchemaChange.MultipleVersions.Classes +{ + namespace WithTheSameName + { + public class ShoppingCartOpenedV1toV2Upcaster: + EventUpcaster + { + protected override V2.WithTheSameName.ShoppingCartOpened Upcast( + V1.ShoppingCartOpened @event + ) => + new V2.WithTheSameName.ShoppingCartOpened( + @event.ShoppingCartId, + @event.ClientId + ); + } + + public class ProductItemAddedToShoppingCartV1toV2Upcaster: + EventUpcaster + { + protected override V2.WithTheSameName.ProductItemAddedToShoppingCart Upcast( + V1.ProductItemAddedToShoppingCart @event + ) => + new V2.WithTheSameName.ProductItemAddedToShoppingCart( + @event.ShoppingCartId, + @event.ProductId, + @event.Quantity + ); + } + + public class ShoppingCartOpenedV1toV3Upcaster: + EventUpcaster + { + protected override V3.WithTheSameName.ShoppingCartOpened Upcast( + V1.ShoppingCartOpened @event + ) => + new V3.WithTheSameName.ShoppingCartOpened( + @event.ShoppingCartId, + new V3.Client(@event.ClientId) + ); + } + + public class ProductItemAddedToShoppingCartV1toV3Upcaster: + EventUpcaster + { + protected override V3.WithTheSameName.ProductItemAddedToShoppingCart Upcast( + V1.ProductItemAddedToShoppingCart @event + ) => + new V3.WithTheSameName.ProductItemAddedToShoppingCart( + @event.ShoppingCartId, + new V3.ProductItem(@event.ProductId, @event.Quantity) + ); + } + + public class ShoppingCartOpenedV2toV3Upcaster: + EventUpcaster + { + public override string EventTypeName => + GetEventTypeNameWithSchemaVersion(2); + + protected override V3.WithTheSameName.ShoppingCartOpened Upcast( + V2.WithTheSameName.ShoppingCartOpened @event + ) => + new V3.WithTheSameName.ShoppingCartOpened( + @event.ShoppingCartId, + new V3.Client(@event.ClientId), + (V3.ShoppingCartStatus)(int)@event.Status, + @event.OpenedAt + ); + } + + public class ProductItemAddedToShoppingCartV2toV3Upcaster: + EventUpcaster + { + public override string EventTypeName => + GetEventTypeNameWithSchemaVersion(2); + + protected override V3.WithTheSameName.ProductItemAddedToShoppingCart Upcast( + V2.WithTheSameName.ProductItemAddedToShoppingCart @event + ) => + new V3.WithTheSameName.ProductItemAddedToShoppingCart( + @event.ShoppingCartId, + new V3.ProductItem(@event.ProductId, @event.Quantity, @event.Price) + ); + } + } + + namespace WithDifferentName + { + public class ShoppingCartOpenedV1toV2Upcaster: + EventUpcaster + { + protected override V2.WithDifferentName.ShoppingCartOpenedV2 Upcast( + V1.ShoppingCartOpened @event + ) => + new V2.WithDifferentName.ShoppingCartOpenedV2( + @event.ShoppingCartId, + @event.ClientId + ); + } + + public class ProductItemAddedToShoppingCartV1toV2Upcaster: + EventUpcaster + { + protected override V2.WithDifferentName.ProductItemAddedToShoppingCartV2 Upcast( + V1.ProductItemAddedToShoppingCart @event + ) => + new V2.WithDifferentName.ProductItemAddedToShoppingCartV2( + @event.ShoppingCartId, + @event.ProductId, + @event.Quantity + ); + } + + public class ShoppingCartOpenedV1toV3Upcaster: + EventUpcaster + { + protected override V3.WithDifferentName.ShoppingCartOpenedV3 Upcast( + V1.ShoppingCartOpened @event + ) => + new V3.WithDifferentName.ShoppingCartOpenedV3( + @event.ShoppingCartId, + new V3.Client(@event.ClientId) + ); + } + + public class ProductItemAddedToShoppingCartV1toV3Upcaster: + EventUpcaster + { + protected override V3.WithDifferentName.ProductItemAddedToShoppingCartV3 Upcast( + V1.ProductItemAddedToShoppingCart @event + ) => + new V3.WithDifferentName.ProductItemAddedToShoppingCartV3( + @event.ShoppingCartId, + new V3.ProductItem(@event.ProductId, @event.Quantity) + ); + } + + public class ShoppingCartOpenedV2toV3Upcaster: + EventUpcaster + { + public override string EventTypeName => + GetEventTypeNameWithSchemaVersion(2); + + protected override V3.WithDifferentName.ShoppingCartOpenedV3 Upcast( + V2.WithDifferentName.ShoppingCartOpenedV2 @event + ) => + new V3.WithDifferentName.ShoppingCartOpenedV3( + @event.ShoppingCartId, + new V3.Client(@event.ClientId), + (V3.ShoppingCartStatus)(int)@event.Status, + @event.OpenedAt + ); + } + + public class ProductItemAddedToShoppingCartV2toV3Upcaster: + EventUpcaster + { + public override string EventTypeName => + GetEventTypeNameWithSchemaVersion(2); + + protected override V3.WithDifferentName.ProductItemAddedToShoppingCartV3 Upcast( + V2.WithDifferentName.ProductItemAddedToShoppingCartV2 @event + ) => + new V3.WithDifferentName.ProductItemAddedToShoppingCartV3( + @event.ShoppingCartId, + new V3.ProductItem(@event.ProductId, @event.Quantity, @event.Price) + ); + } + } + + + public class ClrUpcastConfiguration + { + public static Action V2WithTheSameName => + options => options.Events + .Upcast + ( + new WithTheSameName.ShoppingCartOpenedV1toV2Upcaster(), + new WithTheSameName.ProductItemAddedToShoppingCartV1toV2Upcaster() + ) + .MapEventTypeWithSchemaVersion< + V2.WithTheSameName.ShoppingCartOpened>(2) + .MapEventTypeWithSchemaVersion< + V2.WithTheSameName.ProductItemAddedToShoppingCart>(2); + + public static Action V3WithTheSameName => + options => options.Events + .Upcast + ( + new WithTheSameName.ShoppingCartOpenedV1toV3Upcaster(), + new WithTheSameName.ProductItemAddedToShoppingCartV1toV3Upcaster(), + new WithTheSameName.ShoppingCartOpenedV2toV3Upcaster(), + new WithTheSameName.ProductItemAddedToShoppingCartV2toV3Upcaster() + ) + .MapEventTypeWithSchemaVersion< + V3.WithTheSameName.ShoppingCartOpened>(3) + .MapEventTypeWithSchemaVersion< + V3.WithTheSameName.ProductItemAddedToShoppingCart>(3); + + public static Action V2WithDifferentName => + options => options.Events + .Upcast + ( + new WithDifferentName.ShoppingCartOpenedV1toV2Upcaster(), + new WithDifferentName.ProductItemAddedToShoppingCartV1toV2Upcaster() + ); + + public static Action V3WithDifferentName => + options => options.Events + .Upcast + ( + new WithDifferentName.ShoppingCartOpenedV1toV3Upcaster(), + new WithDifferentName.ProductItemAddedToShoppingCartV1toV3Upcaster(), + new WithDifferentName.ShoppingCartOpenedV2toV3Upcaster(), + new WithDifferentName.ProductItemAddedToShoppingCartV2toV3Upcaster() + ); + } +} +#endif diff --git a/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/JsonNetUpcastConfiguration.cs b/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/JsonNetUpcastConfiguration.cs new file mode 100644 index 0000000000..39f9f5b08f --- /dev/null +++ b/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/JsonNetUpcastConfiguration.cs @@ -0,0 +1,155 @@ +#nullable enable +#if NET6_0_OR_GREATER +using System; +using EventSourcingTests.SchemaChange.MultipleVersions.V3; +using Marten; +using static Marten.Services.Json.Transformations.JsonNet.JsonTransformations; + +namespace EventSourcingTests.SchemaChange.MultipleVersions.Classes; + +public class JsonNetUpcastConfiguration +{ + public static Action V2WithTheSameName => + options => + { + options.Events + .Upcast( + Upcast(oldEvent => + new V2.WithTheSameName.ShoppingCartOpened( + (Guid)oldEvent["ShoppingCartId"]!, + (Guid)oldEvent["ClientId"]! + ) + ) + ) + .Upcast( + Upcast(oldEvent => + new V2.WithTheSameName.ProductItemAddedToShoppingCart( + (Guid)oldEvent["ShoppingCartId"]!, + (Guid)oldEvent["ProductId"]!, + (int)oldEvent["Quantity"]! + )) + ) + .MapEventTypeWithSchemaVersion< + V2.WithTheSameName.ShoppingCartOpened>(2) + .MapEventTypeWithSchemaVersion< + V2.WithTheSameName.ProductItemAddedToShoppingCart>(2); + }; + + public static Action V3WithTheSameName => + options => + { + options.Events + .Upcast( + Upcast(oldEvent => + new V3.WithTheSameName.ShoppingCartOpened( + (Guid)oldEvent["ShoppingCartId"]!, + new V3.Client((Guid)oldEvent["ClientId"]!) + )) + ) + .Upcast( + Upcast(oldEvent => + new V3.WithTheSameName.ProductItemAddedToShoppingCart( + (Guid)oldEvent["ShoppingCartId"]!, + new ProductItem( + (Guid)oldEvent["ProductId"]!, + (int)oldEvent["Quantity"]! + ) + )) + ) + .Upcast( + 2, Upcast(oldEvent => + new V3.WithTheSameName.ShoppingCartOpened( + (Guid)oldEvent["ShoppingCartId"]!, + new V3.Client((Guid)oldEvent["ClientId"]!), + Enum.Parse((string)oldEvent["Status"]!), + (DateTime)oldEvent["OpenedAt"]! + )) + ) + .Upcast( + 2, Upcast(oldEvent => + new V3.WithTheSameName.ProductItemAddedToShoppingCart( + (Guid)oldEvent["ShoppingCartId"]!, + new ProductItem( + (Guid)oldEvent["ProductId"]!, + (int)oldEvent["Quantity"]!, + (decimal)oldEvent["Price"]! + ) + )) + ) + .MapEventTypeWithSchemaVersion< + V3.WithTheSameName.ShoppingCartOpened>(3) + .MapEventTypeWithSchemaVersion< + V3.WithTheSameName.ProductItemAddedToShoppingCart>(3); + }; + + public static Action V2WithDifferentName => + options => + { + options.Events + .Upcast( + "shopping_cart_opened", + Upcast(oldEvent => + new V2.WithDifferentName.ShoppingCartOpenedV2( + (Guid)oldEvent["ShoppingCartId"]!, + (Guid)oldEvent["ClientId"]! + )) + ) + .Upcast( + "product_item_added_to_shopping_cart", + Upcast(oldEvent => + new V2.WithDifferentName.ProductItemAddedToShoppingCartV2( + (Guid)oldEvent["ShoppingCartId"]!, + (Guid)oldEvent["ProductId"]!, + (int)oldEvent["Quantity"]! + )) + ); + }; + + public static Action V3WithDifferentName => + options => + { + options.Events + .Upcast( + "shopping_cart_opened", + Upcast(oldEvent => + new V3.WithDifferentName.ShoppingCartOpenedV3( + (Guid)oldEvent["ShoppingCartId"]!, + new V3.Client((Guid)oldEvent["ClientId"]!) + )) + ) + .Upcast( + "product_item_added_to_shopping_cart", + Upcast(oldEvent => + new V3.WithDifferentName.ProductItemAddedToShoppingCartV3( + (Guid)oldEvent["ShoppingCartId"]!, + new ProductItem( + (Guid)oldEvent["ProductId"]!, + (int)oldEvent["Quantity"]! + ) + )) + ) + .Upcast( + "shopping_cart_opened_v2", + Upcast(oldEvent => + new V3.WithDifferentName.ShoppingCartOpenedV3( + (Guid)oldEvent["ShoppingCartId"]!, + new V3.Client((Guid)oldEvent["ClientId"]!), + Enum.Parse((string)oldEvent["Status"]!), + (DateTime)oldEvent["OpenedAt"]! + )) + ) + .Upcast( + "product_item_added_to_shopping_cart_v2", + Upcast(oldEvent => + new V3.WithDifferentName.ProductItemAddedToShoppingCartV3( + (Guid)oldEvent["ShoppingCartId"]!, + new ProductItem( + (Guid)oldEvent["ProductId"]!, + (int)oldEvent["Quantity"]!, + (decimal)oldEvent["Price"]! + ) + )) + ); + }; +} +#endif diff --git a/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/SystemTextJsonUpcastConfiguration.cs b/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/SystemTextJsonUpcastConfiguration.cs new file mode 100644 index 0000000000..c8bd1b1b67 --- /dev/null +++ b/src/EventSourcingTests/SchemaChange/MultipleVersions/Classes/SystemTextJsonUpcastConfiguration.cs @@ -0,0 +1,207 @@ +#nullable enable +#if NET6_0_OR_GREATER +using System; +using EventSourcingTests.SchemaChange.MultipleVersions.V3; +using Marten; +using Marten.Services.Json; +using static Marten.Services.Json.Transformations.SystemTextJson.JsonTransformations; + +namespace EventSourcingTests.SchemaChange.MultipleVersions.Classes; + +public class SystemTextJsonUpcastConfiguration +{ + public static Action V2WithTheSameName => + options => + { + options.UseDefaultSerialization(serializerType: SerializerType.SystemTextJson); + options.Events + .Upcast( + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V2.WithTheSameName.ShoppingCartOpened( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + oldEvent.GetProperty("ClientId").GetGuid() + ); + }) + ) + .Upcast( + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V2.WithTheSameName.ProductItemAddedToShoppingCart( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + oldEvent.GetProperty("ProductId").GetGuid(), + oldEvent.GetProperty("Quantity").GetInt32() + ); + }) + ) + .MapEventTypeWithSchemaVersion< + V2.WithTheSameName.ShoppingCartOpened>(2) + .MapEventTypeWithSchemaVersion< + V2.WithTheSameName.ProductItemAddedToShoppingCart>(2); + }; + + public static Action V3WithTheSameName => + options => + { + options.UseDefaultSerialization(serializerType:SerializerType.SystemTextJson); + options.Events + .Upcast( + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithTheSameName.ShoppingCartOpened( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new V3.Client(oldEvent.GetProperty("ClientId").GetGuid()) + ); + }) + ) + .Upcast( + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithTheSameName.ProductItemAddedToShoppingCart( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new ProductItem( + oldEvent.GetProperty("ProductId").GetGuid(), + oldEvent.GetProperty("Quantity").GetInt32() + ) + ); + }) + ) + .Upcast( + 2, Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithTheSameName.ShoppingCartOpened( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new V3.Client(oldEvent.GetProperty("ClientId").GetGuid()), + Enum.Parse(oldEvent.GetProperty("Status").GetString()!), + oldEvent.GetProperty("OpenedAt").GetDateTime() + ); + }) + ) + .Upcast( + 2, Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithTheSameName.ProductItemAddedToShoppingCart( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new ProductItem( + oldEvent.GetProperty("ProductId").GetGuid(), + oldEvent.GetProperty("Quantity").GetInt32(), + oldEvent.GetProperty("Price").GetDecimal() + ) + ); + }) + ) + .MapEventTypeWithSchemaVersion< + V3.WithTheSameName.ShoppingCartOpened>(3) + .MapEventTypeWithSchemaVersion< + V3.WithTheSameName.ProductItemAddedToShoppingCart>(3); + }; + + public static Action V2WithDifferentName => + options => + { + options.UseDefaultSerialization(serializerType:SerializerType.SystemTextJson); + options.Events + .Upcast( + "shopping_cart_opened", + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V2.WithDifferentName.ShoppingCartOpenedV2( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + oldEvent.GetProperty("ClientId").GetGuid() + ); + }) + ) + .Upcast( + "product_item_added_to_shopping_cart", + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V2.WithDifferentName.ProductItemAddedToShoppingCartV2( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + oldEvent.GetProperty("ProductId").GetGuid(), + oldEvent.GetProperty("Quantity").GetInt32() + ); + }) + ); + }; + + public static Action V3WithDifferentName => + options => + { + options.UseDefaultSerialization(serializerType:SerializerType.SystemTextJson); + options.Events + .Upcast( + "shopping_cart_opened", + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithDifferentName.ShoppingCartOpenedV3( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new V3.Client(oldEvent.GetProperty("ClientId").GetGuid()) + ); + }) + ) + .Upcast( + "product_item_added_to_shopping_cart", + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithDifferentName.ProductItemAddedToShoppingCartV3( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new ProductItem( + oldEvent.GetProperty("ProductId").GetGuid(), + oldEvent.GetProperty("Quantity").GetInt32() + ) + ); + }) + ) + .Upcast( + "shopping_cart_opened_v2", + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithDifferentName.ShoppingCartOpenedV3( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new V3.Client(oldEvent.GetProperty("ClientId").GetGuid()), + Enum.Parse(oldEvent.GetProperty("Status").GetString()!), + oldEvent.GetProperty("OpenedAt").GetDateTime() + ); + }) + ) + .Upcast( + "product_item_added_to_shopping_cart_v2", + Upcast(oldEventJson => + { + var oldEvent = oldEventJson.RootElement; + + return new V3.WithDifferentName.ProductItemAddedToShoppingCartV3( + oldEvent.GetProperty("ShoppingCartId").GetGuid(), + new ProductItem( + oldEvent.GetProperty("ProductId").GetGuid(), + oldEvent.GetProperty("Quantity").GetInt32(), + oldEvent.GetProperty("Price").GetDecimal() + ) + ); + }) + ); + }; +} +#endif diff --git a/src/EventSourcingTests/SchemaChange/MultipleVersions/MultipleSchemaVersions.cs b/src/EventSourcingTests/SchemaChange/MultipleVersions/MultipleSchemaVersions.cs index 7d3efd0d04..d353322d59 100644 --- a/src/EventSourcingTests/SchemaChange/MultipleVersions/MultipleSchemaVersions.cs +++ b/src/EventSourcingTests/SchemaChange/MultipleVersions/MultipleSchemaVersions.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using EventSourcingTests.SchemaChange.MultipleVersions.Lambdas; using LamarCodeGeneration; using Marten; using Marten.Events; @@ -443,17 +442,19 @@ private async Task CheckV3WithDifferentNameUpcasting( public static TheoryData, Action> UpcastersConfigurationWithTheSameName => new() { - { ClrUpcastConfiguration.V2WithTheSameName, ClrUpcastConfiguration.V3WithTheSameName }, - { SystemTextJsonUpcastConfiguration.V2WithTheSameName, SystemTextJsonUpcastConfiguration.V3WithTheSameName }, - { JsonNetUpcastConfiguration.V2WithTheSameName, JsonNetUpcastConfiguration.V3WithTheSameName } + { Lambdas.ClrUpcastConfiguration.V2WithTheSameName, Lambdas.ClrUpcastConfiguration.V3WithTheSameName }, + { Lambdas.SystemTextJsonUpcastConfiguration.V2WithTheSameName, Lambdas.SystemTextJsonUpcastConfiguration.V3WithTheSameName }, + { Lambdas.JsonNetUpcastConfiguration.V2WithTheSameName, Lambdas.JsonNetUpcastConfiguration.V3WithTheSameName }, + { Classes.ClrUpcastConfiguration.V2WithTheSameName, Classes.ClrUpcastConfiguration.V3WithTheSameName } }; public static TheoryData, Action> UpcastersConfigurationWithDifferentName => new() { - { ClrUpcastConfiguration.V2WithDifferentName, ClrUpcastConfiguration.V3WithDifferentName }, - { SystemTextJsonUpcastConfiguration.V2WithDifferentName, SystemTextJsonUpcastConfiguration.V3WithDifferentName }, - { JsonNetUpcastConfiguration.V2WithDifferentName, JsonNetUpcastConfiguration.V3WithDifferentName } + { Lambdas.ClrUpcastConfiguration.V2WithDifferentName, Lambdas.ClrUpcastConfiguration.V3WithDifferentName }, + { Lambdas.SystemTextJsonUpcastConfiguration.V2WithDifferentName, Lambdas.SystemTextJsonUpcastConfiguration.V3WithDifferentName }, + { Lambdas.JsonNetUpcastConfiguration.V2WithDifferentName, Lambdas.JsonNetUpcastConfiguration.V3WithDifferentName }, + { Classes.JsonNetUpcastConfiguration.V2WithDifferentName, Classes.JsonNetUpcastConfiguration.V3WithDifferentName } }; }