Skip to content

Commit

Permalink
chore: Update examples implementations (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Feb 22, 2025
1 parent adbd9a7 commit 3ad425b
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion examples/Flyway/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ItemGroup>
<!-- Unit and integration test dependencies: -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0"/>
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
<PackageVersion Include="xunit" Version="2.9.2"/>
<!-- Third-party client dependencies to connect and interact with the containers: -->
Expand Down
2 changes: 1 addition & 1 deletion examples/Respawn/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ItemGroup>
<!-- Unit and integration test dependencies: -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0"/>
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
<PackageVersion Include="xunit" Version="2.9.2"/>
<!-- Third-party client dependencies to connect and interact with the containers: -->
Expand Down
16 changes: 8 additions & 8 deletions examples/WeatherForecast/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.1"/>
<PackageVersion Include="Microsoft.Fast.Components.FluentUI" Version="3.5.4"/>
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2"/>
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.13"/>
<PackageVersion Include="Microsoft.Fast.Components.FluentUI" Version="3.5.5"/>
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11"/>
<!-- Unit and integration test dependencies: -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.1"/>
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.8.0"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7"/>
<PackageVersion Include="xunit" Version="2.7.0"/>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.13"/>
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
<PackageVersion Include="xunit" Version="2.9.2"/>
<!-- Third-party client dependencies to connect and interact with the containers: -->
<PackageVersion Include="Selenium.WebDriver.ChromeDriver" Version="106.0.5249.6100"/>
<PackageVersion Include="Selenium.WebDriver" Version="4.9.1"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions examples/WeatherForecast/src/WeatherForecast/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatabaseContainer>();
Expand All @@ -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<WeatherDataContext>((_, options) => options.UseNpgsql(connectionString));
builder.Services.AddDbContext<WeatherDataContext>((_, options) => options.UseNpgsql(postgreSqlConnectionString));
}

builder.Services.AddScoped<IWeatherDataReadOnlyRepository, WeatherDataReadOnlyContext>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,19 @@ public Task DisposeAsync()
return _postgreSqlContainer.DisposeAsync().AsTask();
}

public sealed class Api : IClassFixture<WeatherForecastTest>, IDisposable
public sealed class Api : WebApplicationFactory<Program>, IClassFixture<WeatherForecastTest>
{
private readonly WebApplicationFactory<Program> _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<TEntryPoint>
// 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<Program>();
_serviceScope = _webApplicationFactory.Services.GetRequiredService<IServiceScopeFactory>().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]
Expand All @@ -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()
Expand All @@ -72,7 +61,9 @@ public async Task Get_WeatherForecast_ReturnsThreeDays()
// Given
const int threeDays = 3;

var weatherDataReadOnlyRepository = _serviceScope.ServiceProvider.GetRequiredService<IWeatherDataReadOnlyRepository>();
using var serviceScope = Services.CreateScope();

var weatherDataReadOnlyRepository = serviceScope.ServiceProvider.GetRequiredService<IWeatherDataReadOnlyRepository>();

// When
var weatherForecast = await weatherDataReadOnlyRepository.GetAllAsync(string.Empty, string.Empty, DateTime.Today, DateTime.Today.AddDays(threeDays))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<string> predicate)
{
return _image.MatchVersion(predicate);
}

public string GetHostname()
public bool MatchVersion(Predicate<Version> predicate)
{
return _image.GetHostname();
return _image.MatchVersion(predicate);
}
}

0 comments on commit 3ad425b

Please sign in to comment.