Skip to content

Commit

Permalink
Disable projections in Api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed May 25, 2024
1 parent 049fb9b commit b85e27d
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Admin/NCafe.Admin.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Add services to the container.
builder.Services.AddEventStoreRepository(builder.Configuration)
.AddEventStoreProjectionService<Product>()
.AddEventStoreProjectionService<Product>(builder.Configuration)
.AddInMemoryReadModelRepository<Product>()
.AddHostedService<ProductProjectionService>();

Expand Down
3 changes: 3 additions & 0 deletions src/Admin/NCafe.Admin.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"AllowedHosts": "*",
"ConnectionStrings": {
"EventStore": "esdb://localhost:2113?tls=false"
},
"EventStore": {
"EnableProjections": "true"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public async Task GivenValidProductInformation_ShouldCreateAndStoreProduct()

// Assert
A.CallTo(() => _repository.Save(A<Product>.That.Matches(p => p.Name == command.Name && p.Price == command.Price)))
.MustHaveHappenedOnceExactly(); ;
.MustHaveHappenedOnceExactly();
}
}
2 changes: 1 addition & 1 deletion src/Barista/NCafe.Barista.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

// Add services to the container.
builder.Services.AddEventStoreRepository(builder.Configuration)
.AddEventStoreProjectionService<BaristaOrder>()
.AddEventStoreProjectionService<BaristaOrder>(builder.Configuration)
.AddInMemoryReadModelRepository<BaristaOrder>()
.AddHostedService<BaristaOrderProjectionService>();

Expand Down
3 changes: 3 additions & 0 deletions src/Barista/NCafe.Barista.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"EventStore": "esdb://localhost:2113?tls=false",
"RabbitMq": "host=localhost;username=myuser;password=mypassword"
},
"EventStore": {
"EnableProjections": "true"
},
"RabbitMq": {
"queuePrefix": "barista-service"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using NCafe.Cashier.Domain.ReadModels;
using NCafe.Core.MessageBus;
using NCafe.Core.Projections;
using NCafe.Core.ReadModels;
using NCafe.Core.Repositories;

Expand All @@ -15,6 +16,7 @@ public class CashierWebApplicationFactory<TProgram>
private readonly IRepository _repository;
private readonly IReadModelRepository<Product> _productRepository;
private readonly IBusPublisher _busPublisher;
private readonly IProjectionService<Product> _productProjectionService;

public IRepository FakeRepository => _repository;
public IReadModelRepository<Product> FakeProductRepository => _productRepository;
Expand All @@ -25,10 +27,12 @@ public CashierWebApplicationFactory()
_repository = A.Fake<IRepository>();
_productRepository = A.Fake<IReadModelRepository<Product>>();
_busPublisher = A.Fake<IBusPublisher>();
_productProjectionService = A.Fake<IProjectionService<Product>>();
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
Environment.SetEnvironmentVariable("EventStore__EnableProjections", "false");
Environment.SetEnvironmentVariable("RabbitMq__InitializeExchange", "false");

builder.ConfigureServices(services =>
Expand All @@ -45,9 +49,14 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
d => d.ServiceType == typeof(IBusPublisher));
services.Remove(busPublisherDescriptor!);

var productProjectionServiceDescriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(IProjectionService<Product>));
services.Remove(productProjectionServiceDescriptor!);

services.AddSingleton(_repository);
services.AddSingleton(_productRepository);
services.AddSingleton(_busPublisher);
services.AddSingleton(_productProjectionService);
});

builder.UseEnvironment("Tests");
Expand Down
4 changes: 4 additions & 0 deletions src/Cashier/NCafe.Cashier.Api.Tests/PlaceOrderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FakeItEasy;
using NCafe.Cashier.Domain.Entities;
using NCafe.Cashier.Domain.Messages;
using Shouldly;
using System.Net;
using System.Net.Http.Json;
Expand Down Expand Up @@ -28,5 +29,8 @@ public async Task PlaceOrder_ShouldReturnAcceptedStatus()

// Assert
response.StatusCode.ShouldBe(HttpStatusCode.Accepted);

A.CallTo(() => _factory.FakeBusPublisher.Publish(A<OrderPlacedMessage>.That.Matches(p => p.Id == orderId)))
.MustHaveHappenedOnceExactly();
}
}
2 changes: 1 addition & 1 deletion src/Cashier/NCafe.Cashier.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// Add services to the container.
builder.Services.AddEventStoreRepository(builder.Configuration)
.AddEventStoreProjectionService<Product>()
.AddEventStoreProjectionService<Product>(builder.Configuration)
.AddInMemoryReadModelRepository<Product>()
.AddHostedService<ProductProjectionService>();

Expand Down
3 changes: 3 additions & 0 deletions src/Cashier/NCafe.Cashier.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"EventStore": "esdb://localhost:2113?tls=false",
"RabbitMq": "amqp://myuser:mypassword@localhost:5672"
},
"EventStore": {
"EnableProjections": "true"
},
"RabbitMq": {
"ExchangeName": "cashier",
"InitializeExchange": "true"
Expand Down
9 changes: 7 additions & 2 deletions src/Common/NCafe.Infrastructure/DependencyRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ public static IServiceCollection AddEventStoreRepository(this IServiceCollection
return services;
}

public static IServiceCollection AddEventStoreProjectionService<TModel>(this IServiceCollection services)
public static IServiceCollection AddEventStoreProjectionService<TModel>(this IServiceCollection services, IConfiguration configuration)
where TModel : ReadModel
{
return services.AddSingleton<IProjectionService<TModel>, EventStoreProjectionService<TModel>>();
if (configuration.GetValue("EventStore:EnableProjections", false))
{
services.AddSingleton<IProjectionService<TModel>, EventStoreProjectionService<TModel>>();
}

return services;
}

public static IServiceCollection AddRabbitMqPublisher(this IServiceCollection services, IConfiguration configuration)
Expand Down

0 comments on commit b85e27d

Please sign in to comment.