Skip to content

Commit

Permalink
Gradually adding tests to cover event consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
charlessolar committed Jun 16, 2018
1 parent 29617ca commit 55e67f5
Show file tree
Hide file tree
Showing 97 changed files with 1,557 additions and 1,338 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,4 @@ src/Web/*.awcache

src/Web/node_modules
src/Web/dist
*.ncrunchsolution
7 changes: 7 additions & 0 deletions eShopOnContainersDDD.sln
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Identity.Domain.Tests", "sr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ordering.Domain.Tests", "src\Contexts\Ordering\Domain\Tests\Ordering.Domain.Tests.csproj", "{46F59608-AD45-4E86-A773-52D55099C6F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Basket.Applications.Tests", "src\Contexts\Basket\Applications\Tests\Basket.Applications.Tests.csproj", "{C60931FC-47BF-498D-8BCB-29F447BA7856}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -413,6 +415,10 @@ Global
{46F59608-AD45-4E86-A773-52D55099C6F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46F59608-AD45-4E86-A773-52D55099C6F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46F59608-AD45-4E86-A773-52D55099C6F0}.Release|Any CPU.Build.0 = Release|Any CPU
{C60931FC-47BF-498D-8BCB-29F447BA7856}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C60931FC-47BF-498D-8BCB-29F447BA7856}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C60931FC-47BF-498D-8BCB-29F447BA7856}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C60931FC-47BF-498D-8BCB-29F447BA7856}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -504,6 +510,7 @@ Global
{63E49985-9DEC-4D2B-BDE1-24D62F9C33FF} = {CD58E233-E690-4074-844C-09EE929B3178}
{C8A59E14-89E9-453C-AC3E-98EDD51F0FDB} = {EECF3AE9-7073-4149-AEE6-B30C185E5CEE}
{46F59608-AD45-4E86-A773-52D55099C6F0} = {5305EA5C-8C99-4B17-B19E-4C4C12B98045}
{C60931FC-47BF-498D-8BCB-29F447BA7856} = {B5581F10-6B8C-4FF0-8190-F15EC2401631}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4E0FE21F-D8B6-4D1E-85B5-9B2A2EFBEDAE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.13.43.295" />
<PackageReference Include="Aggregates.NET.NServiceBus" Version="0.14.17.314" />
<PackageReference Include="NEST" Version="6.1.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aggregates;
Expand All @@ -10,7 +11,7 @@

namespace eShop.Basket.Basket
{
public class Handler :
public class BasketIndex :
IHandleQueries<Queries.Baskets>,
IHandleMessages<Events.Initiated>,
IHandleMessages<Events.BasketClaimed>,
Expand All @@ -24,15 +25,15 @@ public async Task Handle(Queries.Baskets query, IMessageHandlerContext ctx)
{
var builder = new QueryBuilder();

var results = await ctx.App<Infrastructure.IUnitOfWork>().Query<Models.Basket>(builder.Build())
var results = await ctx.UoW().Query<Models.Basket>(builder.Build())
.ConfigureAwait(false);

await ctx.Result(results.Records, results.Total, results.ElapsedMs).ConfigureAwait(false);
}

public async Task Handle(Events.Initiated e, IMessageHandlerContext ctx)
{
var user = await ctx.App<Infrastructure.IUnitOfWork>().Get<Identity.User.Models.User>(e.UserName)
var user = await ctx.UoW().TryGet<Identity.User.Models.User>(e.UserName)
.ConfigureAwait(false);

var basket = new Models.BasketIndex
Expand All @@ -43,60 +44,60 @@ public async Task Handle(Events.Initiated e, IMessageHandlerContext ctx)
Created = e.Stamp,
Updated = e.Stamp
};
await ctx.App<Infrastructure.IUnitOfWork>().Add(e.BasketId, basket).ConfigureAwait(false);
await ctx.UoW().Add(e.BasketId, basket).ConfigureAwait(false);
}
public async Task Handle(Events.BasketClaimed e, IMessageHandlerContext ctx)
{
var user = await ctx.App<Infrastructure.IUnitOfWork>().Get<Identity.User.Models.User>(e.UserName)
var user = await ctx.UoW().Get<Identity.User.Models.User>(e.UserName)
.ConfigureAwait(false);
var basket = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketIndex>(e.BasketId)
var basket = await ctx.UoW().Get<Models.BasketIndex>(e.BasketId)
.ConfigureAwait(false);

basket.Customer = user.GivenName;
basket.CustomerId = user.Id;

basket.Updated = e.Stamp;

await ctx.App<Infrastructure.IUnitOfWork>().Update(e.BasketId, basket).ConfigureAwait(false);
await ctx.UoW().Update(e.BasketId, basket).ConfigureAwait(false);
}
public Task Handle(Events.Destroyed e, IMessageHandlerContext ctx)
{
return ctx.App<Infrastructure.IUnitOfWork>().Delete<Models.BasketIndex>(e.BasketId);
return ctx.UoW().Delete<Models.BasketIndex>(e.BasketId);
}

public async Task Handle(Entities.Item.Events.ItemAdded e, IMessageHandlerContext ctx)
{
var basket = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketIndex>(e.BasketId)
var basket = await ctx.UoW().Get<Models.BasketIndex>(e.BasketId)
.ConfigureAwait(false);
var product = await ctx.App<Infrastructure.IUnitOfWork>()
var product = await ctx.UoW()
.Get<Catalog.Product.Models.CatalogProductIndex>(e.ProductId).ConfigureAwait(false);
basket.TotalItems++;
basket.TotalQuantity++;
basket.SubTotal += product.Price;

await ctx.App<Infrastructure.IUnitOfWork>().Update(e.BasketId, basket).ConfigureAwait(false);
await ctx.UoW().Update(e.BasketId, basket).ConfigureAwait(false);
}

public async Task Handle(Entities.Item.Events.ItemRemoved e, IMessageHandlerContext ctx)
{
var basket = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketIndex>(e.BasketId)
var basket = await ctx.UoW().Get<Models.BasketIndex>(e.BasketId)
.ConfigureAwait(false);
var item = await ctx.App<Infrastructure.IUnitOfWork>()
.Get<Entities.Item.Models.BasketItemIndex>(Entities.Item.Handler.ItemIdGenerator(e.BasketId, e.ProductId)).ConfigureAwait(false);
var item = await ctx.UoW()
.Get<Entities.Item.Models.BasketItemIndex>(Entities.Item.BasketItemIndex.ItemIdGenerator(e.BasketId, e.ProductId)).ConfigureAwait(false);

basket.TotalItems--;
basket.TotalQuantity -= item.Quantity;
basket.SubTotal -= item.SubTotal;

await ctx.App<Infrastructure.IUnitOfWork>().Update(e.BasketId, basket).ConfigureAwait(false);
await ctx.UoW().Update(e.BasketId, basket).ConfigureAwait(false);
}

public async Task Handle(Entities.Item.Events.QuantityUpdated e, IMessageHandlerContext ctx)
{
var basket = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketIndex>(e.BasketId)
var basket = await ctx.UoW().Get<Models.BasketIndex>(e.BasketId)
.ConfigureAwait(false);
var item = await ctx.App<Infrastructure.IUnitOfWork>()
.Get<Entities.Item.Models.BasketItemIndex>(Entities.Item.Handler.ItemIdGenerator(e.BasketId, e.ProductId)).ConfigureAwait(false);
var item = await ctx.UoW()
.Get<Entities.Item.Models.BasketItemIndex>(Entities.Item.BasketItemIndex.ItemIdGenerator(e.BasketId, e.ProductId)).ConfigureAwait(false);

// Todo: verify item is the item state before IT processes QuantityUpdated
basket.TotalQuantity -= item.Quantity;
Expand All @@ -107,30 +108,32 @@ public async Task Handle(Entities.Item.Events.QuantityUpdated e, IMessageHandler
basket.TotalQuantity += item.Quantity;
basket.SubTotal += item.SubTotal;

await ctx.App<Infrastructure.IUnitOfWork>().Update(e.BasketId, basket).ConfigureAwait(false);
await ctx.UoW().Update(e.BasketId, basket).ConfigureAwait(false);
}

public async Task Handle(Catalog.Product.Events.PriceUpdated e, IMessageHandlerContext ctx)
{

var basketIds = await ctx.Service<Entities.Item.Services.ItemsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
var basketIds = await ctx.Service<Services.BasketsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
.ConfigureAwait(false);

// Update the subtotal and quantity for all baskets
foreach (var id in basketIds)
{
var item = await ctx.App<Infrastructure.IUnitOfWork>().Get<Entities.Item.Models.BasketItemIndex>(Entities.Item.Handler.ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);
var basket = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketIndex>(id)
var basket = await ctx.UoW().Get<Models.BasketIndex>(id)
.ConfigureAwait(false);
var item = await ctx.UoW()
.Get<Entities.Item.Models.BasketItemIndex>(Entities.Item.BasketItemIndex.ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);

basket.SubTotal -= item.SubTotal;

item.ProductPrice = e.Price;

basket.SubTotal += item.SubTotal;

await ctx.App<Infrastructure.IUnitOfWork>().Update(id, basket).ConfigureAwait(false);
await ctx.UoW().Update(id, basket).ConfigureAwait(false);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace eShop.Basket.Basket.Entities.Item
{
public class Handler :
public class BasketItemIndex :
IHandleQueries<Queries.Items>,
IHandleMessages<Events.ItemAdded>,
IHandleMessages<Events.ItemRemoved>,
Expand All @@ -24,17 +24,17 @@ public class Handler :
public async Task Handle(Queries.Items query, IMessageHandlerContext ctx)
{
var builder = new QueryBuilder();
builder.Add("BasketId", query.BasketId.ToString(), Operation.EQUAL);
builder.Add("BasketId", query.BasketId.ToString(), Operation.Equal);

var results = await ctx.App<Infrastructure.IUnitOfWork>().Query<Models.BasketItemIndex>(builder.Build())
var results = await ctx.UoW().Query<Models.BasketItemIndex>(builder.Build())
.ConfigureAwait(false);

await ctx.Result(results.Records, results.Total, results.ElapsedMs).ConfigureAwait(false);
}

public async Task Handle(Events.ItemAdded e, IMessageHandlerContext ctx)
{
var product = await ctx.App<Infrastructure.IUnitOfWork>().Get<Catalog.Product.Models.CatalogProductIndex>(e.ProductId)
var product = await ctx.UoW().Get<Catalog.Product.Models.CatalogProductIndex>(e.ProductId)
.ConfigureAwait(false);
var model = new Models.BasketItemIndex
{
Expand All @@ -48,66 +48,64 @@ public async Task Handle(Events.ItemAdded e, IMessageHandlerContext ctx)
ProductPrice = product.Price,
Quantity = 1,
};
await ctx.App<Infrastructure.IUnitOfWork>().Add(model.Id, model).ConfigureAwait(false);
await ctx.UoW().Add(model.Id, model).ConfigureAwait(false);
}
public Task Handle(Events.ItemRemoved e, IMessageHandlerContext ctx)
{
return ctx.App<Infrastructure.IUnitOfWork>().Delete<Models.BasketItemIndex>(ItemIdGenerator(e.BasketId, e.ProductId));
return ctx.UoW().Delete<Models.BasketItemIndex>(ItemIdGenerator(e.BasketId, e.ProductId));
}
public async Task Handle(Events.QuantityUpdated e, IMessageHandlerContext ctx)
{
var item = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketItemIndex>(ItemIdGenerator(e.BasketId, e.ProductId)).ConfigureAwait(false);
var item = await ctx.UoW().Get<Models.BasketItemIndex>(ItemIdGenerator(e.BasketId, e.ProductId)).ConfigureAwait(false);

item.Quantity = e.Quantity;

await ctx.App<Infrastructure.IUnitOfWork>().Update(ItemIdGenerator(e.BasketId, e.ProductId), item).ConfigureAwait(false);
await ctx.UoW().Update(ItemIdGenerator(e.BasketId, e.ProductId), item).ConfigureAwait(false);
}
public async Task Handle(Catalog.Product.Events.DescriptionUpdated e, IMessageHandlerContext ctx)
{
var product = await ctx.App<Infrastructure.IUnitOfWork>().Get<Catalog.Product.Models.CatalogProductIndex>(e.ProductId)
.ConfigureAwait(false);

var basketIds = await ctx.Service<Services.ItemsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
var basketIds = await ctx.Service<Basket.Services.BasketsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
.ConfigureAwait(false);

// Update the description for all baskets
foreach (var id in basketIds)
{
var item = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketItemIndex>(ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);
var item = await ctx.UoW().Get<Models.BasketItemIndex>(ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);
item.ProductDescription = e.Description;

await ctx.App<Infrastructure.IUnitOfWork>().Update(ItemIdGenerator(id, e.ProductId), item).ConfigureAwait(false);
await ctx.UoW().Update(ItemIdGenerator(id, e.ProductId), item).ConfigureAwait(false);
}
}
public async Task Handle(Catalog.Product.Events.PictureSet e, IMessageHandlerContext ctx)
{

var basketIds = await ctx.Service<Services.ItemsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
var basketIds = await ctx.Service<Basket.Services.BasketsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
.ConfigureAwait(false);

// Update the description for all baskets
foreach (var id in basketIds)
{
var item = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketItemIndex>(ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);
var item = await ctx.UoW().Get<Models.BasketItemIndex>(ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);
item.ProductPictureContents = e.Content;
item.ProductPictureContentType = e.ContentType;

await ctx.App<Infrastructure.IUnitOfWork>().Update(ItemIdGenerator(id, e.ProductId), item).ConfigureAwait(false);
await ctx.UoW().Update(ItemIdGenerator(id, e.ProductId), item).ConfigureAwait(false);
}
}
public async Task Handle(Catalog.Product.Events.PriceUpdated e, IMessageHandlerContext ctx)
{

var basketIds = await ctx.Service<Services.ItemsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
var basketIds = await ctx.Service<Basket.Services.BasketsUsingProduct, Guid[]>(x => { x.ProductId = e.ProductId; })
.ConfigureAwait(false);

// Update the description for all baskets
foreach (var id in basketIds)
{
var item = await ctx.App<Infrastructure.IUnitOfWork>().Get<Models.BasketItemIndex>(ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);
var item = await ctx.UoW().Get<Models.BasketItemIndex>(ItemIdGenerator(id, e.ProductId)).ConfigureAwait(false);
item.ProductPrice = e.Price;

await ctx.App<Infrastructure.IUnitOfWork>().Update(ItemIdGenerator(id, e.ProductId), item).ConfigureAwait(false);
await ctx.UoW().Update(ItemIdGenerator(id, e.ProductId), item).ConfigureAwait(false);
}
}

Expand Down

This file was deleted.

Loading

0 comments on commit 55e67f5

Please sign in to comment.