diff --git a/examples/Flyway/Directory.Packages.props b/examples/Flyway/Directory.Packages.props index a78fdaa45..07fab9a1f 100644 --- a/examples/Flyway/Directory.Packages.props +++ b/examples/Flyway/Directory.Packages.props @@ -6,7 +6,7 @@ - + diff --git a/examples/Respawn/Directory.Packages.props b/examples/Respawn/Directory.Packages.props index 630377148..df90779b6 100644 --- a/examples/Respawn/Directory.Packages.props +++ b/examples/Respawn/Directory.Packages.props @@ -6,7 +6,7 @@ - + diff --git a/examples/WeatherForecast/Directory.Packages.props b/examples/WeatherForecast/Directory.Packages.props index ab4b8adc6..9521eb455 100644 --- a/examples/WeatherForecast/Directory.Packages.props +++ b/examples/WeatherForecast/Directory.Packages.props @@ -4,15 +4,15 @@ true - - - + + + - - - - - + + + + + diff --git a/examples/WeatherForecast/src/WeatherForecast.Repositories/Usings.cs b/examples/WeatherForecast/src/WeatherForecast.Repositories/Usings.cs index 79083b29c..e42fb9acd 100644 --- a/examples/WeatherForecast/src/WeatherForecast.Repositories/Usings.cs +++ b/examples/WeatherForecast/src/WeatherForecast.Repositories/Usings.cs @@ -1,7 +1,6 @@ global using System; global using System.Collections.Generic; global using System.Linq; -global using System.Threading; global using System.Threading.Tasks; global using JetBrains.Annotations; global using WeatherForecast.Entities; diff --git a/examples/WeatherForecast/src/WeatherForecast/Program.cs b/examples/WeatherForecast/src/WeatherForecast/Program.cs index 4bdbaf8f8..2f09b2989 100644 --- a/examples/WeatherForecast/src/WeatherForecast/Program.cs +++ b/examples/WeatherForecast/src/WeatherForecast/Program.cs @@ -4,9 +4,9 @@ builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); -var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); +var postgreSqlConnectionString = builder.Configuration.GetConnectionString("PostgreSQL"); -if (string.IsNullOrWhiteSpace(connectionString)) +if (string.IsNullOrWhiteSpace(postgreSqlConnectionString)) { // The application configuration does not include a database connection string, use Testcontainers for .NET to create, start and seed the dependent database. builder.Services.AddSingleton(); @@ -20,7 +20,7 @@ else { // The application configuration includes a database connection string, use it to establish a connection and seed the database. - builder.Services.AddDbContext((_, options) => options.UseNpgsql(connectionString)); + builder.Services.AddDbContext((_, options) => options.UseNpgsql(postgreSqlConnectionString)); } builder.Services.AddScoped(); diff --git a/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/Usings.cs b/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/Usings.cs index 0e46232ac..069b3a044 100644 --- a/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/Usings.cs +++ b/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/Usings.cs @@ -2,10 +2,10 @@ global using System.Collections.Generic; global using System.Linq; global using System.Net; -global using System.Net.Http; global using System.Text.Json; global using System.Threading.Tasks; global using JetBrains.Annotations; +global using Microsoft.AspNetCore.Hosting; global using Microsoft.AspNetCore.Mvc.Testing; global using Microsoft.Extensions.DependencyInjection; global using Testcontainers.PostgreSql; diff --git a/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/WeatherForecastTest.cs b/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/WeatherForecastTest.cs index 15f4de9d5..d41a4c80c 100644 --- a/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/WeatherForecastTest.cs +++ b/examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/WeatherForecastTest.cs @@ -15,32 +15,19 @@ public Task DisposeAsync() return _postgreSqlContainer.DisposeAsync().AsTask(); } - public sealed class Api : IClassFixture, IDisposable + public sealed class Api : WebApplicationFactory, IClassFixture { - private readonly WebApplicationFactory _webApplicationFactory; - - private readonly IServiceScope _serviceScope; - - private readonly HttpClient _httpClient; + private readonly string _postgreSqlConnectionString; public Api(WeatherForecastTest weatherForecastTest) { - // Instead of using environment variables to bootstrap our application configuration, we can implement a custom WebApplicationFactory - // that overrides the ConfigureWebHost(IWebHostBuilder) method to add a WeatherDataContext to the service collection. - Environment.SetEnvironmentVariable("ASPNETCORE_URLS", "https://+"); - Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path", "certificate.crt"); - Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Password", "password"); - Environment.SetEnvironmentVariable("ConnectionStrings__DefaultConnection", weatherForecastTest._postgreSqlContainer.GetConnectionString()); - _webApplicationFactory = new WebApplicationFactory(); - _serviceScope = _webApplicationFactory.Services.GetRequiredService().CreateScope(); - _httpClient = _webApplicationFactory.CreateClient(); + _postgreSqlConnectionString = weatherForecastTest._postgreSqlContainer.GetConnectionString(); } - public void Dispose() + protected override void ConfigureWebHost(IWebHostBuilder builder) { - _httpClient.Dispose(); - _serviceScope.Dispose(); - _webApplicationFactory.Dispose(); + builder.UseSetting("URLS", "https://+"); + builder.UseSetting("ConnectionStrings:PostgreSQL", _postgreSqlConnectionString); } [Fact] @@ -50,8 +37,10 @@ public async Task Get_WeatherForecast_ReturnsSevenDays() // Given const string path = "api/WeatherForecast"; + using var httpClient = CreateClient(); + // When - var response = await _httpClient.GetAsync(path) + var response = await httpClient.GetAsync(path) .ConfigureAwait(true); var weatherForecastStream = await response.Content.ReadAsStreamAsync() @@ -72,7 +61,9 @@ public async Task Get_WeatherForecast_ReturnsThreeDays() // Given const int threeDays = 3; - var weatherDataReadOnlyRepository = _serviceScope.ServiceProvider.GetRequiredService(); + using var serviceScope = Services.CreateScope(); + + var weatherDataReadOnlyRepository = serviceScope.ServiceProvider.GetRequiredService(); // When var weatherForecast = await weatherDataReadOnlyRepository.GetAllAsync(string.Empty, string.Empty, DateTime.Today, DateTime.Today.AddDays(threeDays)) diff --git a/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastContainer.cs b/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastContainer.cs index 7fdec42eb..613693737 100644 --- a/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastContainer.cs +++ b/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastContainer.cs @@ -22,7 +22,7 @@ public WeatherForecastContainer() { const string weatherForecastStorage = "weatherForecastStorage"; - const string connectionString = $"Host={weatherForecastStorage};Username={PostgreSqlBuilder.DefaultUsername};Password={PostgreSqlBuilder.DefaultPassword};Database={PostgreSqlBuilder.DefaultDatabase}"; + const string postgreSqlConnectionString = $"Host={weatherForecastStorage};Username={PostgreSqlBuilder.DefaultUsername};Password={PostgreSqlBuilder.DefaultPassword};Database={PostgreSqlBuilder.DefaultDatabase}"; _weatherForecastNetwork = new NetworkBuilder() .Build(); @@ -39,7 +39,7 @@ public WeatherForecastContainer() .WithEnvironment("ASPNETCORE_URLS", "https://+") .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", WeatherForecastImage.CertificateFilePath) .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", WeatherForecastImage.CertificatePassword) - .WithEnvironment("ConnectionStrings__DefaultConnection", connectionString) + .WithEnvironment("ConnectionStrings__PostgreSQL", postgreSqlConnectionString) .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(WeatherForecastImage.HttpsPort)) .Build(); } diff --git a/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastImage.cs b/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastImage.cs index cca88d969..b2db3c98d 100644 --- a/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastImage.cs +++ b/examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastImage.cs @@ -11,7 +11,17 @@ public sealed class WeatherForecastImage : IImage, IAsyncLifetime private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); - private readonly IImage _image = new DockerImage("localhost/testcontainers", "weather-forecast", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString()); + private readonly IImage _image = new DockerImage("localhost/testcontainers/weather-forecast", tag: DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString()); + + public string Repository => _image.Repository; + + public string Registry => _image.Registry; + + public string Tag => _image.Tag; + + public string Digest => _image.Digest; + + public string FullName => _image.FullName; public async Task InitializeAsync() { @@ -41,16 +51,23 @@ public Task DisposeAsync() return Task.CompletedTask; } - public string Repository => _image.Repository; - - public string Name => _image.Name; + public string GetHostname() + { + return _image.GetHostname(); + } - public string Tag => _image.Tag; + public bool MatchLatestOrNightly() + { + return _image.MatchLatestOrNightly(); + } - public string FullName => _image.FullName; + public bool MatchVersion(Predicate predicate) + { + return _image.MatchVersion(predicate); + } - public string GetHostname() + public bool MatchVersion(Predicate predicate) { - return _image.GetHostname(); + return _image.MatchVersion(predicate); } }