Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Refactor fixtures #153

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BusEndpointFixture

public BusEndpointFixture()
{
string rabbitMqBusConnectionString = new ConfigurationFixture()
string rabbitMqBusConnectionString = new ConfigurationFactory()
.Configuration
.GetRabbitMqBusConnectionString(BusConfiguration.RequesterServiceBusSection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@

namespace Aktabook.Data.Testing.Fixtures;

public class ConfigurationFixture
public class ConfigurationFactory
{
private IConfiguration? _configuration;

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,47 @@

namespace Aktabook.Data.Testing.Fixtures;

public sealed class RequesterServiceDbContextSqlServerDestructiveFixture : IDisposable
public sealed class RequesterServiceDbContextSqlServerDestructiveFixture

Check notice

Code scanning / InspectCode

Class is never instantiated: Non-private accessibility Note test

Class 'RequesterServiceDbContextSqlServerDestructiveFixture' is never instantiated
{
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<SqlConnectionStringBuilder>(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)

Check notice

Code scanning / InspectCode

'if' statement can be rewritten as '??=' assignment Note test

Convert into '??='
{
_connectionString = new ConfigurationFactory()
.Configuration
.GetRequiredSection(DbContextConstants.RequesterServiceDbContextSqlServerSection)
.Get<SqlConnectionStringBuilder>(options => options.ErrorOnUnknownConfiguration = true)
.ConnectionString;
}

private static RequesterServiceDbContext CreateDbContext(string connectionString)
{
return new RequesterServiceDbContext(
new DbContextOptionsBuilder<RequesterServiceDbContext>()
.ConfigureWarnings(b => b.Throw())
.UseSqlServer(connectionString)
.UseSqlServer(_connectionString)
.Options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,10 +19,8 @@

namespace Aktabook.Services.BookInfoRequestService.IntegrationTest;

[CollectionDefinition("NonParallelDbTests", DisableParallelization = true)]
[Trait("Category", "Ephemeral")]
public class BookInfoRequesterTest
: IClassFixture<RequesterServiceDbContextSqlServerDestructiveFixture>
public class BookInfoRequesterTest : IClassFixture<RequesterServiceDbContextSqlServerDestructiveFixture>
{
private readonly RequesterServiceDbContextSqlServerDestructiveFixture _dbDestructiveFixture;

Expand All @@ -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);

Expand All @@ -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<BookInfoRequest> bookInfoRequests = await _dbDestructiveFixture
.DbContext
List<BookInfoRequest> bookInfoRequests = await dbContext
.BookInfoRequests
.AsNoTracking()
.Where(x => x.BookInfoRequestId == bookInfoRequestId)
Expand All @@ -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<BookInfoRequest> bookInfoRequests = await _dbDestructiveFixture
.DbContext
List<BookInfoRequest> bookInfoRequests = await dbContext
.BookInfoRequests
.AsNoTracking()
.Where(x => x.BookInfoRequestId == bookInfoRequestId)
Expand All @@ -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<BookInfoRequestLogEntry> bookInfoRequestLogEntries =
await _dbDestructiveFixture.DbContext
await dbContext
.BookInfoRequestLogEntries
.AsNoTracking()
.Where(x => x.BookInfoRequestId == bookInfoRequestId)
Expand All @@ -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);
Expand All @@ -125,7 +127,7 @@ await bookInfoRequester
.ChangeRequestStatus(bookInfoRequestId, "Dummy BookInfoRequestStatus", CancellationToken.None);

List<BookInfoRequestLogEntry> bookInfoRequestLogEntries =
await _dbDestructiveFixture.DbContext
await dbContext
.BookInfoRequestLogEntries
.AsNoTracking()
.Where(x => x.BookInfoRequestId == bookInfoRequestId)
Expand All @@ -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);
Expand All @@ -150,7 +153,7 @@ await bookInfoRequester
.ChangeRequestStatus(bookInfoRequestId, BookInfoRequestStatus.Requested, CancellationToken.None);

List<BookInfoRequestLogEntry> bookInfoRequestLogEntries =
await _dbDestructiveFixture.DbContext
await dbContext
.BookInfoRequestLogEntries
.AsNoTracking()
.Where(x => x.BookInfoRequestId == bookInfoRequestId)
Expand All @@ -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);
Expand All @@ -182,7 +186,7 @@ await bookInfoRequester
.ChangeRequestStatus(bookInfoRequestId, "Dummy status 004", CancellationToken.None);

List<BookInfoRequestLogEntry> bookInfoRequestLogEntries =
await _dbDestructiveFixture.DbContext
await dbContext
.BookInfoRequestLogEntries
.AsNoTracking()
.Where(x => x.BookInfoRequestId == bookInfoRequestId)
Expand Down
Loading