Skip to content

Commit

Permalink
Merge pull request #26 from msx752/development
Browse files Browse the repository at this point in the history
TestContainer feature is improved on the test project
  • Loading branch information
msx752 authored Sep 3, 2024
2 parents c739d82 + 052448f commit 70d5954
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 268 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
namespace SampleDotnet.RepositoryFactory.Tests.Cases.Application.Sagas.SagaTests;

public class SagaTests : IAsyncLifetime
[Collection("Shared Collection")]
public class SagaTests
{
// A container for running SQL Server in Docker for testing purposes.
private readonly MsSqlContainer _sqlContainer;
private readonly SharedContainerFixture _shared;

// Constructor initializes the SQL container with specific configurations.
public SagaTests()
public SagaTests(SharedContainerFixture fixture)
{
_sqlContainer = new MsSqlBuilder()
.WithPassword("Admin123!") // Set the password for the SQL Server.
.WithCleanUp(true) // automatically clean up the container.
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433)) // Wait strategy to ensure SQL Server is ready.
.Build(); // Build the container.
}

// DisposeAsync stops and disposes of the SQL container asynchronously after each test.
public async Task DisposeAsync()
{
await _sqlContainer.StopAsync(); // Stop the SQL Server container.
await _sqlContainer.DisposeAsync(); // Dispose of the SQL Server container.
}

// InitializeAsync starts the SQL container asynchronously before each test.
public async Task InitializeAsync()
{
await _sqlContainer.StartAsync(); // Start the SQL Server container.
_shared = fixture;
}

[Fact]
Expand All @@ -37,7 +21,7 @@ public async Task DistributedTransaction_SagaCommitAndRollback()
// Configure CartDbContext with SQL Server settings.
services.AddDbContextFactory<CartDbContext>(options =>
{
var cnnBuilder = new SqlConnectionStringBuilder(_sqlContainer.GetConnectionString());
var cnnBuilder = new SqlConnectionStringBuilder(_shared.SqlContainer.GetConnectionString());
cnnBuilder.InitialCatalog = "CartDbContext_DistributedTransaction_SagaCommitAndRollback";
cnnBuilder.TrustServerCertificate = true;
cnnBuilder.MultipleActiveResultSets = true;
Expand All @@ -51,7 +35,7 @@ public async Task DistributedTransaction_SagaCommitAndRollback()
// Configure SecondDbContext with SQL Server settings.
services.AddDbContextFactory<PaymentDbContext>(options =>
{
var cnnBuilder = new SqlConnectionStringBuilder(_sqlContainer.GetConnectionString());
var cnnBuilder = new SqlConnectionStringBuilder(_shared.SqlContainer.GetConnectionString());
cnnBuilder.InitialCatalog = "PaymentDbContext_DistributedTransaction_SagaCommitAndRollback"; // Set the initial catalog (database name).
cnnBuilder.TrustServerCertificate = true; // Trust the server certificate.
cnnBuilder.MultipleActiveResultSets = true; // Allow multiple active result sets.
Expand Down Expand Up @@ -83,13 +67,8 @@ public async Task DistributedTransaction_SagaCommitAndRollback()

using (IHost build = host.Build())
{
var CartDbContextFactory = build.Services.GetRequiredService<IDbContextFactory<CartDbContext>>();
using (var context = CartDbContextFactory.CreateDbContext())
context.Database.EnsureCreated();

var paymentDbContextFactory = build.Services.GetRequiredService<IDbContextFactory<PaymentDbContext>>();
using (var context = paymentDbContextFactory.CreateDbContext())
context.Database.EnsureCreated();
build.Services.EnsureDatabaseExists<CartDbContext>();
build.Services.EnsureDatabaseExists<PaymentDbContext>();

var harness = build.Services.CreateScope().ServiceProvider.GetRequiredService<ITestHarness>();
await harness.Start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
namespace SampleDotnet.RepositoryFactory.Tests.Cases.Core.Entities.DateTimeOffsetTests;

public class DateTimeOffsetAsyncTests : IAsyncLifetime
[Collection("Shared Collection")]
public class DateTimeOffsetAsyncTests
{
private readonly MsSqlContainer _sqlContainer;
// A container for running SQL Server in Docker for testing purposes.
private readonly SharedContainerFixture _shared;

public DateTimeOffsetAsyncTests()
// Constructor initializes the SQL container with specific configurations.
public DateTimeOffsetAsyncTests(SharedContainerFixture fixture)
{
_sqlContainer = new MsSqlBuilder()
.WithPassword("Admin123!")
.WithCleanUp(true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433))
.Build();
}

public async Task InitializeAsync()
{
await _sqlContainer.StartAsync();
}

public async Task DisposeAsync()
{
await _sqlContainer.StopAsync();
await _sqlContainer.DisposeAsync();
_shared = fixture;
}

[Fact]
Expand All @@ -31,7 +19,7 @@ public async Task Case_set_CreatedAt_DateTimeOffsetAsync()
{
services.AddDbContextFactory<DateTimeOffsetDbContext>(options =>
{
var cnnBuilder = new SqlConnectionStringBuilder(_sqlContainer.GetConnectionString());
var cnnBuilder = new SqlConnectionStringBuilder(_shared.SqlContainer.GetConnectionString());
cnnBuilder.InitialCatalog = "Case_set_CreatedAt_DateTimeOffsetAsync";
cnnBuilder.TrustServerCertificate = true;
cnnBuilder.MultipleActiveResultSets = true;
Expand All @@ -48,17 +36,12 @@ public async Task Case_set_CreatedAt_DateTimeOffsetAsync()

using (IHost build = host.Build())
{
build.Services.EnsureDatabaseExists<DateTimeOffsetDbContext>();

//scope1
using (IServiceScope scope = build.Services.CreateScope())
using (var cancellationTokenSource = new CancellationTokenSource())
{
var testApplicationDbContextFactory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<DateTimeOffsetDbContext>>();
using (var context = testApplicationDbContextFactory.CreateDbContext())
{
context.Database.EnsureCreated();
await context.CLEAN_TABLES_DO_NOT_USE_IN_PRODUCTION();
}

var uow = scope.ServiceProvider.GetRequiredService<IUnitOfWork>();
using (IRepository<DateTimeOffsetDbContext> repo = uow.CreateRepository<DateTimeOffsetDbContext>())
{
Expand All @@ -82,7 +65,7 @@ public async Task Case_set_UpdatedAt_DateTimeOffsettAsync()
{
services.AddDbContextFactory<DateTimeOffsetDbContext>(options =>
{
var cnnBuilder = new SqlConnectionStringBuilder(_sqlContainer.GetConnectionString());
var cnnBuilder = new SqlConnectionStringBuilder(_shared.SqlContainer.GetConnectionString());
cnnBuilder.InitialCatalog = "Case_set_UpdatedAt_DateTimeOffsetAsync";
cnnBuilder.TrustServerCertificate = true;
cnnBuilder.MultipleActiveResultSets = true;
Expand All @@ -99,12 +82,7 @@ public async Task Case_set_UpdatedAt_DateTimeOffsettAsync()

using (IHost build = host.Build())
{
var testApplicationDbContextFactory = build.Services.GetRequiredService<IDbContextFactory<DateTimeOffsetDbContext>>();
using (var context = testApplicationDbContextFactory.CreateDbContext())
{
context.Database.EnsureCreated();
await context.CLEAN_TABLES_DO_NOT_USE_IN_PRODUCTION();
}
build.Services.EnsureDatabaseExists<DateTimeOffsetDbContext>();

//scope1
using (IServiceScope scope = build.Services.CreateScope())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
namespace SampleDotnet.RepositoryFactory.Tests.Cases.Core.Entities.DateTimeOffsetTests;

public class DateTimeOffsetTests : IAsyncLifetime
[Collection("Shared Collection")]
public class DateTimeOffsetTests
{
private readonly MsSqlContainer _sqlContainer;
// A container for running SQL Server in Docker for testing purposes.
private readonly SharedContainerFixture _shared;

public DateTimeOffsetTests()
// Constructor initializes the SQL container with specific configurations.
public DateTimeOffsetTests(SharedContainerFixture fixture)
{
_sqlContainer = new MsSqlBuilder()
.WithPassword("Admin123!") // Set the password for the SQL Server.
.WithCleanUp(true) // automatically clean up the container.
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433)) // Wait strategy to ensure SQL Server is ready.
.Build(); // Build the container.
}

public async Task InitializeAsync()
{
await _sqlContainer.StartAsync();
}

public async Task DisposeAsync()
{
await _sqlContainer.StopAsync();
await _sqlContainer.DisposeAsync();
_shared = fixture;
}

[Fact]
Expand All @@ -31,7 +19,7 @@ public async void Case_set_CreatedAt_DateTimeOffset()
{
services.AddDbContextFactory<DateTimeOffsetDbContext>(options =>
{
var cnnBuilder = new SqlConnectionStringBuilder(_sqlContainer.GetConnectionString());
var cnnBuilder = new SqlConnectionStringBuilder(_shared.SqlContainer.GetConnectionString());
cnnBuilder.InitialCatalog = "Case_set_CreatedAt_DateTimeOffset";
cnnBuilder.TrustServerCertificate = true;
cnnBuilder.MultipleActiveResultSets = true;
Expand All @@ -48,12 +36,7 @@ public async void Case_set_CreatedAt_DateTimeOffset()

using (IHost build = host.Build())
{
var testApplicationDbContextFactory = build.Services.GetRequiredService<IDbContextFactory<DateTimeOffsetDbContext>>();
using (var context = testApplicationDbContextFactory.CreateDbContext())
{
context.Database.EnsureCreated();
await context.CLEAN_TABLES_DO_NOT_USE_IN_PRODUCTION();
}
build.Services.EnsureDatabaseExists<DateTimeOffsetDbContext>();

//scope1
using (IServiceScope scope = build.Services.CreateScope())
Expand Down Expand Up @@ -81,7 +64,7 @@ public async Task Case_set_UpdatedAt_DateTimeOffset()
{
services.AddDbContextFactory<DateTimeOffsetDbContext>(options =>
{
var cnnBuilder = new SqlConnectionStringBuilder(_sqlContainer.GetConnectionString());
var cnnBuilder = new SqlConnectionStringBuilder(_shared.SqlContainer.GetConnectionString());
cnnBuilder.InitialCatalog = "Case_set_UpdatedAt_DateTimeOffset";
cnnBuilder.TrustServerCertificate = true;
cnnBuilder.MultipleActiveResultSets = true;
Expand All @@ -98,12 +81,7 @@ public async Task Case_set_UpdatedAt_DateTimeOffset()

using (IHost build = host.Build())
{
var testApplicationDbContextFactory = build.Services.GetRequiredService<IDbContextFactory<DateTimeOffsetDbContext>>();
using (var context = testApplicationDbContextFactory.CreateDbContext())
{
context.Database.EnsureCreated();
await context.CLEAN_TABLES_DO_NOT_USE_IN_PRODUCTION();
}
build.Services.EnsureDatabaseExists<DateTimeOffsetDbContext>();

//scope1
using (IServiceScope scope = build.Services.CreateScope())
Expand Down
Loading

0 comments on commit 70d5954

Please sign in to comment.