File tree Expand file tree Collapse file tree 8 files changed +52
-13
lines changed
Sample/EventStoreDB/Simple/ECommerce.Api.Tests Expand file tree Collapse file tree 8 files changed +52
-13
lines changed Original file line number Diff line number Diff line change @@ -7,20 +7,21 @@ public class EventsLog
77 public List < object > PublishedEvents { get ; } = new ( ) ;
88}
99
10- public class EventListener < TEvent > : IEventHandler < TEvent >
11- where TEvent : notnull
10+ public class EventListener : IEventBus
1211{
12+ private readonly IEventBus eventBus ;
1313 private readonly EventsLog eventsLog ;
1414
15- public EventListener ( EventsLog eventsLog )
15+ public EventListener ( EventsLog eventsLog , IEventBus eventBus )
1616 {
17+ this . eventBus = eventBus ;
1718 this . eventsLog = eventsLog ;
1819 }
1920
20- public Task Handle ( TEvent @event , CancellationToken cancellationToken )
21+ public async Task Publish ( IEventEnvelope eventEnvelope , CancellationToken ct )
2122 {
22- eventsLog . PublishedEvents . Add ( @event ) ;
23-
24- return Task . CompletedTask ;
23+ eventsLog . PublishedEvents . Add ( eventEnvelope ) ;
24+ await eventBus . Publish ( eventEnvelope , ct ) ;
2525 }
2626}
27+
Original file line number Diff line number Diff line change @@ -22,11 +22,13 @@ protected override IHost CreateHost(IHostBuilder builder)
2222 builder . ConfigureServices ( services =>
2323 {
2424 services . AddSingleton ( eventsLog )
25- . AddSingleton ( typeof ( IEventHandler < > ) , typeof ( EventListener < > ) )
2625 . AddSingleton < IExternalEventProducer > ( externalEventProducer )
2726 . AddSingleton < IEventBus > ( sp =>
28- new EventBusDecoratorWithExternalProducer ( sp . GetRequiredService < EventBus > ( ) ,
29- sp . GetRequiredService < IExternalEventProducer > ( ) ) )
27+ new EventListener ( eventsLog ,
28+ new EventBusDecoratorWithExternalProducer ( sp . GetRequiredService < EventBus > ( ) ,
29+ sp . GetRequiredService < IExternalEventProducer > ( ) )
30+ )
31+ )
3032 . AddSingleton < IExternalCommandBus > ( externalCommandBus )
3133 . AddSingleton < IExternalEventConsumer , DummyExternalEventConsumer > ( ) ;
3234 } ) ;
Original file line number Diff line number Diff line change 33using ECommerce . ShoppingCarts . GettingCartById ;
44using FluentAssertions ;
55using Ogooreck . API ;
6+ using Warehouse . Api . Tests ;
67using static Ogooreck . API . ApiSpecification ;
78using Xunit ;
89
910namespace Carts . Api . Tests . ShoppingCarts . AddingProduct ;
1011
1112public class AddProductFixture : ApiSpecification < Program > , IAsyncLifetime
1213{
14+ public AddProductFixture ( ) : base ( new ShoppingCartsApplicationFactory ( ) ) { }
15+
1316 public Guid ShoppingCartId { get ; private set ; }
1417
1518 public readonly Guid ClientId = Guid . NewGuid ( ) ;
Original file line number Diff line number Diff line change 33using ECommerce . ShoppingCarts . GettingCartById ;
44using FluentAssertions ;
55using Ogooreck . API ;
6+ using Warehouse . Api . Tests ;
67using Xunit ;
78using static Ogooreck . API . ApiSpecification ;
89
910namespace Carts . Api . Tests . ShoppingCarts . Canceling ;
1011
1112public class CancelShoppingCartFixture : ApiSpecification < Program > , IAsyncLifetime
1213{
14+ public CancelShoppingCartFixture ( ) : base ( new ShoppingCartsApplicationFactory ( ) ) { }
15+
1316 public Guid ShoppingCartId { get ; private set ; }
1417
1518 public readonly Guid ClientId = Guid . NewGuid ( ) ;
Original file line number Diff line number Diff line change 44using FluentAssertions ;
55using Xunit ;
66using Ogooreck . API ;
7+ using Warehouse . Api . Tests ;
78using static Ogooreck . API . ApiSpecification ;
89
910namespace Carts . Api . Tests . ShoppingCarts . Confirming ;
1011
1112public class ConfirmShoppingCartFixture : ApiSpecification < Program > , IAsyncLifetime
1213{
14+ public ConfirmShoppingCartFixture ( ) : base ( new ShoppingCartsApplicationFactory ( ) ) { }
15+
1316 public Guid ShoppingCartId { get ; private set ; }
1417
1518 public readonly Guid ClientId = Guid . NewGuid ( ) ;
Original file line number Diff line number Diff line change 55using ECommerce . ShoppingCarts . ProductItems ;
66using FluentAssertions ;
77using Ogooreck . API ;
8+ using Warehouse . Api . Tests ;
89using Xunit ;
910using static Ogooreck . API . ApiSpecification ;
1011
1112namespace Carts . Api . Tests . ShoppingCarts . Opening ;
1213
13- public class OpenShoppingCartTests : IClassFixture < TestWebApplicationFactory < Program > >
14+ public class OpenShoppingCartTests : IClassFixture < ShoppingCartsApplicationFactory >
1415{
1516 private readonly ApiSpecification < Program > API ;
1617
@@ -41,8 +42,8 @@ public Task Post_ShouldReturn_CreatedStatus_With_CartId() =>
4142 } ) )
4243 ) ;
4344
44- public OpenShoppingCartTests ( TestWebApplicationFactory < Program > fixture ) =>
45- API = ApiSpecification < Program > . Setup ( fixture ) ;
45+ public OpenShoppingCartTests ( ShoppingCartsApplicationFactory applicationFactory ) =>
46+ API = ApiSpecification < Program > . Setup ( applicationFactory ) ;
4647
4748 public readonly Guid ClientId = Guid . NewGuid ( ) ;
4849}
Original file line number Diff line number Diff line change 44using FluentAssertions ;
55using Xunit ;
66using Ogooreck . API ;
7+ using Warehouse . Api . Tests ;
78using static Ogooreck . API . ApiSpecification ;
89
910namespace Carts . Api . Tests . ShoppingCarts . RemovingProduct ;
1011
1112public class RemoveProductFixture : ApiSpecification < Program > , IAsyncLifetime
1213{
14+ public RemoveProductFixture ( ) : base ( new ShoppingCartsApplicationFactory ( ) ) { }
1315 public Guid ShoppingCartId { get ; private set ; }
1416
1517 public readonly Guid ClientId = Guid . NewGuid ( ) ;
Original file line number Diff line number Diff line change 1+ using Core . Testing ;
2+ using ECommerce . Storage ;
3+ using Microsoft . EntityFrameworkCore ;
4+ using Microsoft . Extensions . DependencyInjection ;
5+ using Microsoft . Extensions . Hosting ;
6+
7+ namespace Warehouse . Api . Tests ;
8+
9+ public class ShoppingCartsApplicationFactory : TestWebApplicationFactory < Program >
10+ {
11+ protected override IHost CreateHost ( IHostBuilder builder )
12+ {
13+ var host = base . CreateHost ( builder ) ;
14+
15+ using var scope = host . Services . CreateScope ( ) ;
16+ using var context = scope . ServiceProvider . GetRequiredService < ECommerceDbContext > ( ) ;
17+ var database = context . Database ;
18+ database . ExecuteSqlRaw ( "TRUNCATE TABLE \" ShoppingCartDetailsProductItem\" " ) ;
19+ database . ExecuteSqlRaw ( "TRUNCATE TABLE \" ShoppingCartShortInfo\" " ) ;
20+ database . ExecuteSqlRaw ( "TRUNCATE TABLE \" ShoppingCartDetails\" CASCADE" ) ;
21+
22+ return host ;
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments