Repository navigation
Fix Build for Tests and Update Image Version #1017
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4547455
Fix build by adding image parameter to testcontainer builder constructor
KyleMcMaster 6e27261
Update image to sql server 2025
KyleMcMaster d0088fd
Merge remote-tracking branch 'origin/main' into kyle/fix-test-contain…
KyleMcMaster 3d79a0a
Merge branch 'main' into kyle/fix-test-container-image
ardalis 8c4dad2
Merge branch 'main' into kyle/fix-test-container-image
ardalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
271 changes: 135 additions & 136 deletions
271
tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,136 +1,135 @@ | ||
| using Clean.Architecture.Infrastructure.Data; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Testcontainers.MsSql; | ||
|
|
||
| namespace Clean.Architecture.FunctionalTests; | ||
|
|
||
| public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>, IAsyncLifetime where TProgram : class | ||
| { | ||
| private MsSqlContainer? _dbContainer; | ||
|
|
||
| public async Task InitializeAsync() | ||
| { | ||
| try | ||
| { | ||
| _dbContainer = new MsSqlBuilder() | ||
| .WithImage("mcr.microsoft.com/mssql/server:2022-latest") | ||
| .WithPassword("Your_password123!") | ||
| .Build(); | ||
| await _dbContainer.StartAsync(); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| // Docker is not available; fall back to SQLite (configured via appsettings.Testing.json) | ||
| _dbContainer = null; | ||
| } | ||
| } | ||
|
|
||
| public new async Task DisposeAsync() | ||
| { | ||
| // Clean up environment variable | ||
| Environment.SetEnvironmentVariable("USE_SQL_SERVER", null); | ||
| if (_dbContainer != null) | ||
| { | ||
| await _dbContainer.DisposeAsync(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Overriding CreateHost to avoid creating a separate ServiceProvider per this thread: | ||
| /// https://github.com/dotnet-architecture/eShopOnWeb/issues/465 | ||
| /// </summary> | ||
| /// <param name="builder"></param> | ||
| /// <returns></returns> | ||
| protected override IHost CreateHost(IHostBuilder builder) | ||
| { | ||
| builder.UseEnvironment("Testing"); // will not send real emails | ||
| var host = builder.Build(); | ||
| host.Start(); | ||
|
|
||
| // Get service provider. | ||
| var serviceProvider = host.Services; | ||
|
|
||
| // Create a scope to obtain a reference to the database | ||
| // context (AppDbContext). | ||
| using (var scope = serviceProvider.CreateScope()) | ||
| { | ||
| var scopedServices = scope.ServiceProvider; | ||
| var db = scopedServices.GetRequiredService<AppDbContext>(); | ||
|
|
||
| var logger = scopedServices | ||
| .GetRequiredService<ILogger<CustomWebApplicationFactory<TProgram>>>(); | ||
|
|
||
| try | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // SQL Server via Testcontainers: apply migrations to create the schema | ||
| db.Database.Migrate(); | ||
| } | ||
| else | ||
| { | ||
| // SQLite fallback: EnsureCreated is used because the migrations use SQL Server syntax | ||
| db.Database.EnsureCreated(); | ||
| } | ||
|
|
||
| // Seed the database with test data only if it has not been seeded yet. | ||
| // This is safe for container reuse across test runs and multiple fixture instances. | ||
| SeedData.InitializeAsync(db).Wait(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "An error occurred seeding the " + | ||
| "database with test messages. Error: {exceptionMessage}", ex.Message); | ||
| } | ||
| } | ||
|
|
||
| return host; | ||
| } | ||
|
|
||
| protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // Force SQL Server mode even on non-Windows platforms for functional tests | ||
| Environment.SetEnvironmentVariable("USE_SQL_SERVER", "true"); | ||
| } | ||
|
|
||
| builder | ||
| .ConfigureAppConfiguration((context, config) => | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // Set the connection string to use the Testcontainer | ||
| config.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| ["ConnectionStrings:DefaultConnection"] = _dbContainer.GetConnectionString() | ||
| }); | ||
| } | ||
| }) | ||
| .ConfigureServices(services => | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // Remove the app's ApplicationDbContext registration | ||
| var descriptors = services.Where( | ||
| d => d.ServiceType == typeof(AppDbContext) || | ||
| d.ServiceType == typeof(DbContextOptions<AppDbContext>)) | ||
| .ToList(); | ||
|
|
||
| foreach (var descriptor in descriptors) | ||
| { | ||
| services.Remove(descriptor); | ||
| } | ||
|
|
||
| // Add ApplicationDbContext using the Testcontainers SQL Server instance | ||
| services.AddDbContext<AppDbContext>((provider, options) => | ||
| { | ||
| options.UseSqlServer(_dbContainer.GetConnectionString()); | ||
| var interceptor = provider.GetRequiredService<EventDispatchInterceptor>(); | ||
| options.AddInterceptors(interceptor); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| using Clean.Architecture.Infrastructure.Data; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Testcontainers.MsSql; | ||
|
|
||
| namespace Clean.Architecture.FunctionalTests; | ||
|
|
||
| public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>, IAsyncLifetime where TProgram : class | ||
| { | ||
| private MsSqlContainer? _dbContainer; | ||
|
|
||
| public async Task InitializeAsync() | ||
| { | ||
| try | ||
| { | ||
| _dbContainer = new MsSqlBuilder("mcr.microsoft.com/mssql/server:2025-latest") | ||
| .WithPassword("Your_password123!") | ||
| .Build(); | ||
| await _dbContainer.StartAsync(); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| // Docker is not available; fall back to SQLite (configured via appsettings.Testing.json) | ||
| _dbContainer = null; | ||
| } | ||
| } | ||
|
|
||
| public new async Task DisposeAsync() | ||
| { | ||
| // Clean up environment variable | ||
| Environment.SetEnvironmentVariable("USE_SQL_SERVER", null); | ||
| if (_dbContainer != null) | ||
| { | ||
| await _dbContainer.DisposeAsync(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Overriding CreateHost to avoid creating a separate ServiceProvider per this thread: | ||
| /// https://github.com/dotnet-architecture/eShopOnWeb/issues/465 | ||
| /// </summary> | ||
| /// <param name="builder"></param> | ||
| /// <returns></returns> | ||
| protected override IHost CreateHost(IHostBuilder builder) | ||
| { | ||
| builder.UseEnvironment("Testing"); // will not send real emails | ||
| var host = builder.Build(); | ||
| host.Start(); | ||
|
|
||
| // Get service provider. | ||
| var serviceProvider = host.Services; | ||
|
|
||
| // Create a scope to obtain a reference to the database | ||
| // context (AppDbContext). | ||
| using (var scope = serviceProvider.CreateScope()) | ||
| { | ||
| var scopedServices = scope.ServiceProvider; | ||
| var db = scopedServices.GetRequiredService<AppDbContext>(); | ||
|
|
||
| var logger = scopedServices | ||
| .GetRequiredService<ILogger<CustomWebApplicationFactory<TProgram>>>(); | ||
|
|
||
| try | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // SQL Server via Testcontainers: apply migrations to create the schema | ||
| db.Database.Migrate(); | ||
| } | ||
| else | ||
| { | ||
| // SQLite fallback: EnsureCreated is used because the migrations use SQL Server syntax | ||
| db.Database.EnsureCreated(); | ||
| } | ||
|
|
||
| // Seed the database with test data only if it has not been seeded yet. | ||
| // This is safe for container reuse across test runs and multiple fixture instances. | ||
| SeedData.InitializeAsync(db).Wait(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "An error occurred seeding the " + | ||
| "database with test messages. Error: {exceptionMessage}", ex.Message); | ||
| } | ||
| } | ||
|
|
||
| return host; | ||
| } | ||
|
|
||
| protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // Force SQL Server mode even on non-Windows platforms for functional tests | ||
| Environment.SetEnvironmentVariable("USE_SQL_SERVER", "true"); | ||
| } | ||
|
|
||
| builder | ||
| .ConfigureAppConfiguration((context, config) => | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // Set the connection string to use the Testcontainer | ||
| config.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| ["ConnectionStrings:DefaultConnection"] = _dbContainer.GetConnectionString() | ||
| }); | ||
| } | ||
| }) | ||
| .ConfigureServices(services => | ||
| { | ||
| if (_dbContainer != null) | ||
| { | ||
| // Remove the app's ApplicationDbContext registration | ||
| var descriptors = services.Where( | ||
| d => d.ServiceType == typeof(AppDbContext) || | ||
| d.ServiceType == typeof(DbContextOptions<AppDbContext>)) | ||
| .ToList(); | ||
|
|
||
| foreach (var descriptor in descriptors) | ||
| { | ||
| services.Remove(descriptor); | ||
| } | ||
|
|
||
| // Add ApplicationDbContext using the Testcontainers SQL Server instance | ||
| services.AddDbContext<AppDbContext>((provider, options) => | ||
| { | ||
| options.UseSqlServer(_dbContainer.GetConnectionString()); | ||
| var interceptor = provider.GetRequiredService<EventDispatchInterceptor>(); | ||
| options.AddInterceptors(interceptor); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.