diff --git a/test/Aktabook.Bus.IntegrationTest/Fixtures/BusEndpointFixture.cs b/test/Aktabook.Bus.IntegrationTest/Fixtures/BusEndpointFixture.cs index c4a0177..30f1e65 100644 --- a/test/Aktabook.Bus.IntegrationTest/Fixtures/BusEndpointFixture.cs +++ b/test/Aktabook.Bus.IntegrationTest/Fixtures/BusEndpointFixture.cs @@ -22,7 +22,7 @@ public class BusEndpointFixture public BusEndpointFixture() { - string rabbitMqBusConnectionString = new ConfigurationFixture() + string rabbitMqBusConnectionString = new ConfigurationFactory() .Configuration .GetRabbitMqBusConnectionString(BusConfiguration.RequesterServiceBusSection); diff --git a/test/Aktabook.Data.Testing/Fixtures/ConfigurationFixture.cs b/test/Aktabook.Data.Testing/Fixtures/ConfigurationFactory.cs similarity index 83% rename from test/Aktabook.Data.Testing/Fixtures/ConfigurationFixture.cs rename to test/Aktabook.Data.Testing/Fixtures/ConfigurationFactory.cs index 4cbe4fa..fc3b60d 100644 --- a/test/Aktabook.Data.Testing/Fixtures/ConfigurationFixture.cs +++ b/test/Aktabook.Data.Testing/Fixtures/ConfigurationFactory.cs @@ -8,7 +8,7 @@ namespace Aktabook.Data.Testing.Fixtures; -public class ConfigurationFixture +public class ConfigurationFactory { private IConfiguration? _configuration; @@ -16,14 +16,13 @@ public IConfiguration Configuration { get { - if (_configuration is { }) + if (_configuration is not null) { return _configuration; } string? environmentName = - Environment.GetEnvironmentVariable( - "AKTABOOK_INTEGRATION_TEST_ENVIRONMENT"); + Environment.GetEnvironmentVariable("AKTABOOK_INTEGRATION_TEST_ENVIRONMENT"); ConfigurationBuilder configBuilder = new(); configBuilder.AddJsonFile("appsettings.json", true); diff --git a/test/Aktabook.Data.Testing/Fixtures/RequesterServiceDbContextSqlServerDestructiveFixture.cs b/test/Aktabook.Data.Testing/Fixtures/RequesterServiceDbContextSqlServerDestructiveFixture.cs index 21519e2..46d8286 100644 --- a/test/Aktabook.Data.Testing/Fixtures/RequesterServiceDbContextSqlServerDestructiveFixture.cs +++ b/test/Aktabook.Data.Testing/Fixtures/RequesterServiceDbContextSqlServerDestructiveFixture.cs @@ -11,50 +11,47 @@ namespace Aktabook.Data.Testing.Fixtures; -public sealed class RequesterServiceDbContextSqlServerDestructiveFixture : IDisposable +public sealed class RequesterServiceDbContextSqlServerDestructiveFixture { private static readonly object Lock = new(); - private static bool _dbInitialized; + private string? _connectionString; + public RequesterServiceDbContextSqlServerDestructiveFixture() { lock (Lock) { - if (_dbInitialized) + if (!_dbInitialized) { - throw new InvalidOperationException("Database fixture already initialized"); - } - - SqlConnectionStringBuilder builder = new ConfigurationFixture() - .Configuration - .GetRequiredSection(DbContextConstants.RequesterServiceDbContextSqlServerSection) - .Get(options => options.ErrorOnUnknownConfiguration = true); + using (RequesterServiceDbContext dbContext = CreateDbContext()) + { + dbContext.Database.EnsureDeleted(); + dbContext.Database.EnsureCreated(); + } - RequesterServiceDbContext dbContext = CreateDbContext(builder.ConnectionString); - dbContext.Database.EnsureDeleted(); - dbContext.Database.EnsureCreated(); - - DbContext = dbContext; #pragma warning disable S3010 - _dbInitialized = true; + _dbInitialized = true; #pragma warning restore S3010 + } } } - public RequesterServiceDbContext DbContext { get; } - - public void Dispose() + public RequesterServiceDbContext CreateDbContext() { - DbContext.Dispose(); - } + if (_connectionString is null) + { + _connectionString = new ConfigurationFactory() + .Configuration + .GetRequiredSection(DbContextConstants.RequesterServiceDbContextSqlServerSection) + .Get(options => options.ErrorOnUnknownConfiguration = true) + .ConnectionString; + } - private static RequesterServiceDbContext CreateDbContext(string connectionString) - { return new RequesterServiceDbContext( new DbContextOptionsBuilder() .ConfigureWarnings(b => b.Throw()) - .UseSqlServer(connectionString) + .UseSqlServer(_connectionString) .Options); } } diff --git a/test/Aktabook.Services.BookInfoRequestService.IntegrationTest/BookInfoRequesterTest.cs b/test/Aktabook.Services.BookInfoRequestService.IntegrationTest/BookInfoRequesterTest.cs index d7c89e9..b9b796e 100644 --- a/test/Aktabook.Services.BookInfoRequestService.IntegrationTest/BookInfoRequesterTest.cs +++ b/test/Aktabook.Services.BookInfoRequestService.IntegrationTest/BookInfoRequesterTest.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using Aktabook.Data; using Aktabook.Data.Testing.Fixtures; using Aktabook.Domain.Models; using FluentAssertions; @@ -18,10 +19,8 @@ namespace Aktabook.Services.BookInfoRequestService.IntegrationTest; -[CollectionDefinition("NonParallelDbTests", DisableParallelization = true)] [Trait("Category", "Ephemeral")] -public class BookInfoRequesterTest - : IClassFixture +public class BookInfoRequesterTest : IClassFixture { private readonly RequesterServiceDbContextSqlServerDestructiveFixture _dbDestructiveFixture; @@ -34,7 +33,8 @@ public BookInfoRequesterTest( [Fact] public async Task GivenPlaceRequest_WhenIsbn_ThenReturnBookInfoRequestId() { - BookInfoRequester bookInfoRequester = new(_dbDestructiveFixture.DbContext); + await using RequesterServiceDbContext dbContext = _dbDestructiveFixture.CreateDbContext(); + BookInfoRequester bookInfoRequester = new(dbContext); Guid bookInfoRequestId = await bookInfoRequester.PlaceRequest("dummy isbn", CancellationToken.None); @@ -44,13 +44,13 @@ public async Task GivenPlaceRequest_WhenIsbn_ThenReturnBookInfoRequestId() [Fact] public async Task GivenPlaceRequest_WhenIsbn_ThenCreateOneBookInfoRequest() { - BookInfoRequester bookInfoRequester = new(_dbDestructiveFixture.DbContext); + await using RequesterServiceDbContext dbContext = _dbDestructiveFixture.CreateDbContext(); + BookInfoRequester bookInfoRequester = new(dbContext); Guid bookInfoRequestId = await bookInfoRequester.PlaceRequest("Dummy ISBN", CancellationToken.None); - List bookInfoRequests = await _dbDestructiveFixture - .DbContext + List bookInfoRequests = await dbContext .BookInfoRequests .AsNoTracking() .Where(x => x.BookInfoRequestId == bookInfoRequestId) @@ -64,13 +64,13 @@ public async Task GivenPlaceRequest_WhenIsbn_ThenCreateOneBookInfoRequest() public async Task GivenPlaceRequest_WhenIsbn_ThenCreateOneBookInfoRequestAndOneBookInfoRequestLogEntryWithStatusSetToRequested() { - BookInfoRequester bookInfoRequester = new(_dbDestructiveFixture.DbContext); + await using RequesterServiceDbContext dbContext = _dbDestructiveFixture.CreateDbContext(); + BookInfoRequester bookInfoRequester = new(dbContext); Guid bookInfoRequestId = await bookInfoRequester.PlaceRequest("Dummy ISBN", CancellationToken.None); - List bookInfoRequests = await _dbDestructiveFixture - .DbContext + List bookInfoRequests = await dbContext .BookInfoRequests .AsNoTracking() .Where(x => x.BookInfoRequestId == bookInfoRequestId) @@ -97,12 +97,13 @@ public async Task [Fact] public async Task GivenPlaceRequest_WhenIsbn_ThenRequestStatusIsRequested() { - BookInfoRequester bookInfoRequester = new(_dbDestructiveFixture.DbContext); + await using RequesterServiceDbContext dbContext = _dbDestructiveFixture.CreateDbContext(); + BookInfoRequester bookInfoRequester = new(dbContext); Guid bookInfoRequestId = await bookInfoRequester.PlaceRequest("Dummy ISBN", CancellationToken.None); List bookInfoRequestLogEntries = - await _dbDestructiveFixture.DbContext + await dbContext .BookInfoRequestLogEntries .AsNoTracking() .Where(x => x.BookInfoRequestId == bookInfoRequestId) @@ -116,7 +117,8 @@ await _dbDestructiveFixture.DbContext public async Task GivenChangeRequestStatus_WhenNewStatus_ThenNewBookInfoRequestLogEntryWithNewStatusAndReturnTrue() { - BookInfoRequester bookInfoRequester = new(_dbDestructiveFixture.DbContext); + await using RequesterServiceDbContext dbContext = _dbDestructiveFixture.CreateDbContext(); + BookInfoRequester bookInfoRequester = new(dbContext); Guid bookInfoRequestId = await bookInfoRequester.PlaceRequest("Dummy ISBN", CancellationToken.None); @@ -125,7 +127,7 @@ await bookInfoRequester .ChangeRequestStatus(bookInfoRequestId, "Dummy BookInfoRequestStatus", CancellationToken.None); List bookInfoRequestLogEntries = - await _dbDestructiveFixture.DbContext + await dbContext .BookInfoRequestLogEntries .AsNoTracking() .Where(x => x.BookInfoRequestId == bookInfoRequestId) @@ -141,7 +143,8 @@ await _dbDestructiveFixture.DbContext public async Task GivenChangeRequestStatus_WhenSameStatus_ThenReturnFalseWithoutAddingBookInfoRequestLogEntry() { - BookInfoRequester bookInfoRequester = new(_dbDestructiveFixture.DbContext); + await using RequesterServiceDbContext dbContext = _dbDestructiveFixture.CreateDbContext(); + BookInfoRequester bookInfoRequester = new(dbContext); Guid bookInfoRequestId = await bookInfoRequester.PlaceRequest("Dummy ISBN", CancellationToken.None); @@ -150,7 +153,7 @@ await bookInfoRequester .ChangeRequestStatus(bookInfoRequestId, BookInfoRequestStatus.Requested, CancellationToken.None); List bookInfoRequestLogEntries = - await _dbDestructiveFixture.DbContext + await dbContext .BookInfoRequestLogEntries .AsNoTracking() .Where(x => x.BookInfoRequestId == bookInfoRequestId) @@ -164,7 +167,8 @@ await _dbDestructiveFixture.DbContext [Fact] public async Task GivenChangeRequestStatus_WhenMultipleCalls_ThenCorrectEntries() { - BookInfoRequester bookInfoRequester = new(_dbDestructiveFixture.DbContext); + await using RequesterServiceDbContext dbContext = _dbDestructiveFixture.CreateDbContext(); + BookInfoRequester bookInfoRequester = new(dbContext); Guid bookInfoRequestId = await bookInfoRequester.PlaceRequest("Dummy ISBN", CancellationToken.None); @@ -182,7 +186,7 @@ await bookInfoRequester .ChangeRequestStatus(bookInfoRequestId, "Dummy status 004", CancellationToken.None); List bookInfoRequestLogEntries = - await _dbDestructiveFixture.DbContext + await dbContext .BookInfoRequestLogEntries .AsNoTracking() .Where(x => x.BookInfoRequestId == bookInfoRequestId)