Skip to content

Commit

Permalink
Added tests for multiple events' schemas with CLR upcasters using cla…
Browse files Browse the repository at this point in the history
…sses
  • Loading branch information
oskardudycz committed Sep 2, 2022
1 parent 800d735 commit fd71ef7
Show file tree
Hide file tree
Showing 4 changed files with 598 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<V1.ShoppingCartOpened, V2.WithTheSameName.ShoppingCartOpened>
{
protected override V2.WithTheSameName.ShoppingCartOpened Upcast(
V1.ShoppingCartOpened @event
) =>
new V2.WithTheSameName.ShoppingCartOpened(
@event.ShoppingCartId,
@event.ClientId
);
}

public class ProductItemAddedToShoppingCartV1toV2Upcaster:
EventUpcaster<V1.ProductItemAddedToShoppingCart, V2.WithTheSameName.ProductItemAddedToShoppingCart>
{
protected override V2.WithTheSameName.ProductItemAddedToShoppingCart Upcast(
V1.ProductItemAddedToShoppingCart @event
) =>
new V2.WithTheSameName.ProductItemAddedToShoppingCart(
@event.ShoppingCartId,
@event.ProductId,
@event.Quantity
);
}

public class ShoppingCartOpenedV1toV3Upcaster:
EventUpcaster<V1.ShoppingCartOpened, V3.WithTheSameName.ShoppingCartOpened>
{
protected override V3.WithTheSameName.ShoppingCartOpened Upcast(
V1.ShoppingCartOpened @event
) =>
new V3.WithTheSameName.ShoppingCartOpened(
@event.ShoppingCartId,
new V3.Client(@event.ClientId)
);
}

public class ProductItemAddedToShoppingCartV1toV3Upcaster:
EventUpcaster<V1.ProductItemAddedToShoppingCart, V3.WithTheSameName.ProductItemAddedToShoppingCart>
{
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<V2.WithTheSameName.ShoppingCartOpened, V3.WithTheSameName.ShoppingCartOpened>
{
public override string EventTypeName =>
GetEventTypeNameWithSchemaVersion<V1.ShoppingCartOpened>(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<V2.WithTheSameName.ProductItemAddedToShoppingCart,
V3.WithTheSameName.ProductItemAddedToShoppingCart>
{
public override string EventTypeName =>
GetEventTypeNameWithSchemaVersion<V1.ProductItemAddedToShoppingCart>(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<V1.ShoppingCartOpened, V2.WithDifferentName.ShoppingCartOpenedV2>
{
protected override V2.WithDifferentName.ShoppingCartOpenedV2 Upcast(
V1.ShoppingCartOpened @event
) =>
new V2.WithDifferentName.ShoppingCartOpenedV2(
@event.ShoppingCartId,
@event.ClientId
);
}

public class ProductItemAddedToShoppingCartV1toV2Upcaster:
EventUpcaster<V1.ProductItemAddedToShoppingCart, V2.WithDifferentName.ProductItemAddedToShoppingCartV2>
{
protected override V2.WithDifferentName.ProductItemAddedToShoppingCartV2 Upcast(
V1.ProductItemAddedToShoppingCart @event
) =>
new V2.WithDifferentName.ProductItemAddedToShoppingCartV2(
@event.ShoppingCartId,
@event.ProductId,
@event.Quantity
);
}

public class ShoppingCartOpenedV1toV3Upcaster:
EventUpcaster<V1.ShoppingCartOpened, V3.WithDifferentName.ShoppingCartOpenedV3>
{
protected override V3.WithDifferentName.ShoppingCartOpenedV3 Upcast(
V1.ShoppingCartOpened @event
) =>
new V3.WithDifferentName.ShoppingCartOpenedV3(
@event.ShoppingCartId,
new V3.Client(@event.ClientId)
);
}

public class ProductItemAddedToShoppingCartV1toV3Upcaster:
EventUpcaster<V1.ProductItemAddedToShoppingCart, V3.WithDifferentName.ProductItemAddedToShoppingCartV3>
{
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<V2.WithDifferentName.ShoppingCartOpenedV2, V3.WithDifferentName.ShoppingCartOpenedV3>
{
public override string EventTypeName =>
GetEventTypeNameWithSchemaVersion<V1.ShoppingCartOpened>(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<V2.WithDifferentName.ProductItemAddedToShoppingCartV2,
V3.WithDifferentName.ProductItemAddedToShoppingCartV3>
{
public override string EventTypeName =>
GetEventTypeNameWithSchemaVersion<V1.ProductItemAddedToShoppingCart>(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<StoreOptions> V2WithTheSameName =>
options => options.Events
.Upcast
(
new WithTheSameName.ShoppingCartOpenedV1toV2Upcaster(),
new WithTheSameName.ProductItemAddedToShoppingCartV1toV2Upcaster()
)
.MapEventTypeWithSchemaVersion<
V2.WithTheSameName.ShoppingCartOpened>(2)
.MapEventTypeWithSchemaVersion<
V2.WithTheSameName.ProductItemAddedToShoppingCart>(2);

public static Action<StoreOptions> 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<StoreOptions> V2WithDifferentName =>
options => options.Events
.Upcast
(
new WithDifferentName.ShoppingCartOpenedV1toV2Upcaster(),
new WithDifferentName.ProductItemAddedToShoppingCartV1toV2Upcaster()
);

public static Action<StoreOptions> V3WithDifferentName =>
options => options.Events
.Upcast
(
new WithDifferentName.ShoppingCartOpenedV1toV3Upcaster(),
new WithDifferentName.ProductItemAddedToShoppingCartV1toV3Upcaster(),
new WithDifferentName.ShoppingCartOpenedV2toV3Upcaster(),
new WithDifferentName.ProductItemAddedToShoppingCartV2toV3Upcaster()
);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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<StoreOptions> V2WithTheSameName =>
options =>
{
options.Events
.Upcast<V2.WithTheSameName.ShoppingCartOpened>(
Upcast(oldEvent =>
new V2.WithTheSameName.ShoppingCartOpened(
(Guid)oldEvent["ShoppingCartId"]!,
(Guid)oldEvent["ClientId"]!
)
)
)
.Upcast<V2.WithTheSameName.ProductItemAddedToShoppingCart>(
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<StoreOptions> V3WithTheSameName =>
options =>
{
options.Events
.Upcast<V3.WithTheSameName.ShoppingCartOpened>(
Upcast(oldEvent =>
new V3.WithTheSameName.ShoppingCartOpened(
(Guid)oldEvent["ShoppingCartId"]!,
new V3.Client((Guid)oldEvent["ClientId"]!)
))
)
.Upcast<V3.WithTheSameName.ProductItemAddedToShoppingCart>(
Upcast(oldEvent =>
new V3.WithTheSameName.ProductItemAddedToShoppingCart(
(Guid)oldEvent["ShoppingCartId"]!,
new ProductItem(
(Guid)oldEvent["ProductId"]!,
(int)oldEvent["Quantity"]!
)
))
)
.Upcast<V3.WithTheSameName.ShoppingCartOpened>(
2, Upcast(oldEvent =>
new V3.WithTheSameName.ShoppingCartOpened(
(Guid)oldEvent["ShoppingCartId"]!,
new V3.Client((Guid)oldEvent["ClientId"]!),
Enum.Parse<V3.ShoppingCartStatus>((string)oldEvent["Status"]!),
(DateTime)oldEvent["OpenedAt"]!
))
)
.Upcast<V3.WithTheSameName.ProductItemAddedToShoppingCart>(
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<StoreOptions> V2WithDifferentName =>
options =>
{
options.Events
.Upcast<V2.WithDifferentName.ShoppingCartOpenedV2>(
"shopping_cart_opened",
Upcast(oldEvent =>
new V2.WithDifferentName.ShoppingCartOpenedV2(
(Guid)oldEvent["ShoppingCartId"]!,
(Guid)oldEvent["ClientId"]!
))
)
.Upcast<V2.WithDifferentName.ProductItemAddedToShoppingCartV2>(
"product_item_added_to_shopping_cart",
Upcast(oldEvent =>
new V2.WithDifferentName.ProductItemAddedToShoppingCartV2(
(Guid)oldEvent["ShoppingCartId"]!,
(Guid)oldEvent["ProductId"]!,
(int)oldEvent["Quantity"]!
))
);
};

public static Action<StoreOptions> V3WithDifferentName =>
options =>
{
options.Events
.Upcast<V3.WithDifferentName.ShoppingCartOpenedV3>(
"shopping_cart_opened",
Upcast(oldEvent =>
new V3.WithDifferentName.ShoppingCartOpenedV3(
(Guid)oldEvent["ShoppingCartId"]!,
new V3.Client((Guid)oldEvent["ClientId"]!)
))
)
.Upcast<V3.WithDifferentName.ProductItemAddedToShoppingCartV3>(
"product_item_added_to_shopping_cart",
Upcast(oldEvent =>
new V3.WithDifferentName.ProductItemAddedToShoppingCartV3(
(Guid)oldEvent["ShoppingCartId"]!,
new ProductItem(
(Guid)oldEvent["ProductId"]!,
(int)oldEvent["Quantity"]!
)
))
)
.Upcast<V3.WithDifferentName.ShoppingCartOpenedV3>(
"shopping_cart_opened_v2",
Upcast(oldEvent =>
new V3.WithDifferentName.ShoppingCartOpenedV3(
(Guid)oldEvent["ShoppingCartId"]!,
new V3.Client((Guid)oldEvent["ClientId"]!),
Enum.Parse<V3.ShoppingCartStatus>((string)oldEvent["Status"]!),
(DateTime)oldEvent["OpenedAt"]!
))
)
.Upcast<V3.WithDifferentName.ProductItemAddedToShoppingCartV3>(
"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
Loading

0 comments on commit fd71ef7

Please sign in to comment.