Skip to content

Commit 3ad425b

Browse files
authored
chore: Update examples implementations (#1379)
1 parent adbd9a7 commit 3ad425b

File tree

9 files changed

+53
-46
lines changed

9 files changed

+53
-46
lines changed

examples/Flyway/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ItemGroup>
77
<!-- Unit and integration test dependencies: -->
88
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
9-
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0"/>
9+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
1010
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
1111
<PackageVersion Include="xunit" Version="2.9.2"/>
1212
<!-- Third-party client dependencies to connect and interact with the containers: -->

examples/Respawn/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ItemGroup>
77
<!-- Unit and integration test dependencies: -->
88
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
9-
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0"/>
9+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
1010
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
1111
<PackageVersion Include="xunit" Version="2.9.2"/>
1212
<!-- Third-party client dependencies to connect and interact with the containers: -->

examples/WeatherForecast/Directory.Packages.props

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.1"/>
8-
<PackageVersion Include="Microsoft.Fast.Components.FluentUI" Version="3.5.4"/>
9-
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2"/>
7+
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.13"/>
8+
<PackageVersion Include="Microsoft.Fast.Components.FluentUI" Version="3.5.5"/>
9+
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11"/>
1010
<!-- Unit and integration test dependencies: -->
11-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
12-
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.1"/>
13-
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.8.0"/>
14-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7"/>
15-
<PackageVersion Include="xunit" Version="2.7.0"/>
11+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
12+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.13"/>
13+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
14+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
15+
<PackageVersion Include="xunit" Version="2.9.2"/>
1616
<!-- Third-party client dependencies to connect and interact with the containers: -->
1717
<PackageVersion Include="Selenium.WebDriver.ChromeDriver" Version="106.0.5249.6100"/>
1818
<PackageVersion Include="Selenium.WebDriver" Version="4.9.1"/>
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
global using System;
22
global using System.Collections.Generic;
33
global using System.Linq;
4-
global using System.Threading;
54
global using System.Threading.Tasks;
65
global using JetBrains.Annotations;
76
global using WeatherForecast.Entities;

examples/WeatherForecast/src/WeatherForecast/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
builder.Services.AddRazorPages();
55
builder.Services.AddServerSideBlazor();
66

7-
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
7+
var postgreSqlConnectionString = builder.Configuration.GetConnectionString("PostgreSQL");
88

9-
if (string.IsNullOrWhiteSpace(connectionString))
9+
if (string.IsNullOrWhiteSpace(postgreSqlConnectionString))
1010
{
1111
// The application configuration does not include a database connection string, use Testcontainers for .NET to create, start and seed the dependent database.
1212
builder.Services.AddSingleton<DatabaseContainer>();
@@ -20,7 +20,7 @@
2020
else
2121
{
2222
// The application configuration includes a database connection string, use it to establish a connection and seed the database.
23-
builder.Services.AddDbContext<WeatherDataContext>((_, options) => options.UseNpgsql(connectionString));
23+
builder.Services.AddDbContext<WeatherDataContext>((_, options) => options.UseNpgsql(postgreSqlConnectionString));
2424
}
2525

2626
builder.Services.AddScoped<IWeatherDataReadOnlyRepository, WeatherDataReadOnlyContext>();

examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/Usings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
global using System.Collections.Generic;
33
global using System.Linq;
44
global using System.Net;
5-
global using System.Net.Http;
65
global using System.Text.Json;
76
global using System.Threading.Tasks;
87
global using JetBrains.Annotations;
8+
global using Microsoft.AspNetCore.Hosting;
99
global using Microsoft.AspNetCore.Mvc.Testing;
1010
global using Microsoft.Extensions.DependencyInjection;
1111
global using Testcontainers.PostgreSql;

examples/WeatherForecast/tests/WeatherForecast.InProcess.Tests/WeatherForecastTest.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,19 @@ public Task DisposeAsync()
1515
return _postgreSqlContainer.DisposeAsync().AsTask();
1616
}
1717

18-
public sealed class Api : IClassFixture<WeatherForecastTest>, IDisposable
18+
public sealed class Api : WebApplicationFactory<Program>, IClassFixture<WeatherForecastTest>
1919
{
20-
private readonly WebApplicationFactory<Program> _webApplicationFactory;
21-
22-
private readonly IServiceScope _serviceScope;
23-
24-
private readonly HttpClient _httpClient;
20+
private readonly string _postgreSqlConnectionString;
2521

2622
public Api(WeatherForecastTest weatherForecastTest)
2723
{
28-
// Instead of using environment variables to bootstrap our application configuration, we can implement a custom WebApplicationFactory<TEntryPoint>
29-
// that overrides the ConfigureWebHost(IWebHostBuilder) method to add a WeatherDataContext to the service collection.
30-
Environment.SetEnvironmentVariable("ASPNETCORE_URLS", "https://+");
31-
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path", "certificate.crt");
32-
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Password", "password");
33-
Environment.SetEnvironmentVariable("ConnectionStrings__DefaultConnection", weatherForecastTest._postgreSqlContainer.GetConnectionString());
34-
_webApplicationFactory = new WebApplicationFactory<Program>();
35-
_serviceScope = _webApplicationFactory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
36-
_httpClient = _webApplicationFactory.CreateClient();
24+
_postgreSqlConnectionString = weatherForecastTest._postgreSqlContainer.GetConnectionString();
3725
}
3826

39-
public void Dispose()
27+
protected override void ConfigureWebHost(IWebHostBuilder builder)
4028
{
41-
_httpClient.Dispose();
42-
_serviceScope.Dispose();
43-
_webApplicationFactory.Dispose();
29+
builder.UseSetting("URLS", "https://+");
30+
builder.UseSetting("ConnectionStrings:PostgreSQL", _postgreSqlConnectionString);
4431
}
4532

4633
[Fact]
@@ -50,8 +37,10 @@ public async Task Get_WeatherForecast_ReturnsSevenDays()
5037
// Given
5138
const string path = "api/WeatherForecast";
5239

40+
using var httpClient = CreateClient();
41+
5342
// When
54-
var response = await _httpClient.GetAsync(path)
43+
var response = await httpClient.GetAsync(path)
5544
.ConfigureAwait(true);
5645

5746
var weatherForecastStream = await response.Content.ReadAsStreamAsync()
@@ -72,7 +61,9 @@ public async Task Get_WeatherForecast_ReturnsThreeDays()
7261
// Given
7362
const int threeDays = 3;
7463

75-
var weatherDataReadOnlyRepository = _serviceScope.ServiceProvider.GetRequiredService<IWeatherDataReadOnlyRepository>();
64+
using var serviceScope = Services.CreateScope();
65+
66+
var weatherDataReadOnlyRepository = serviceScope.ServiceProvider.GetRequiredService<IWeatherDataReadOnlyRepository>();
7667

7768
// When
7869
var weatherForecast = await weatherDataReadOnlyRepository.GetAllAsync(string.Empty, string.Empty, DateTime.Today, DateTime.Today.AddDays(threeDays))

examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public WeatherForecastContainer()
2222
{
2323
const string weatherForecastStorage = "weatherForecastStorage";
2424

25-
const string connectionString = $"Host={weatherForecastStorage};Username={PostgreSqlBuilder.DefaultUsername};Password={PostgreSqlBuilder.DefaultPassword};Database={PostgreSqlBuilder.DefaultDatabase}";
25+
const string postgreSqlConnectionString = $"Host={weatherForecastStorage};Username={PostgreSqlBuilder.DefaultUsername};Password={PostgreSqlBuilder.DefaultPassword};Database={PostgreSqlBuilder.DefaultDatabase}";
2626

2727
_weatherForecastNetwork = new NetworkBuilder()
2828
.Build();
@@ -39,7 +39,7 @@ public WeatherForecastContainer()
3939
.WithEnvironment("ASPNETCORE_URLS", "https://+")
4040
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", WeatherForecastImage.CertificateFilePath)
4141
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", WeatherForecastImage.CertificatePassword)
42-
.WithEnvironment("ConnectionStrings__DefaultConnection", connectionString)
42+
.WithEnvironment("ConnectionStrings__PostgreSQL", postgreSqlConnectionString)
4343
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(WeatherForecastImage.HttpsPort))
4444
.Build();
4545
}

examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastImage.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@ public sealed class WeatherForecastImage : IImage, IAsyncLifetime
1111

1212
private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
1313

14-
private readonly IImage _image = new DockerImage("localhost/testcontainers", "weather-forecast", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
14+
private readonly IImage _image = new DockerImage("localhost/testcontainers/weather-forecast", tag: DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
15+
16+
public string Repository => _image.Repository;
17+
18+
public string Registry => _image.Registry;
19+
20+
public string Tag => _image.Tag;
21+
22+
public string Digest => _image.Digest;
23+
24+
public string FullName => _image.FullName;
1525

1626
public async Task InitializeAsync()
1727
{
@@ -41,16 +51,23 @@ public Task DisposeAsync()
4151
return Task.CompletedTask;
4252
}
4353

44-
public string Repository => _image.Repository;
45-
46-
public string Name => _image.Name;
54+
public string GetHostname()
55+
{
56+
return _image.GetHostname();
57+
}
4758

48-
public string Tag => _image.Tag;
59+
public bool MatchLatestOrNightly()
60+
{
61+
return _image.MatchLatestOrNightly();
62+
}
4963

50-
public string FullName => _image.FullName;
64+
public bool MatchVersion(Predicate<string> predicate)
65+
{
66+
return _image.MatchVersion(predicate);
67+
}
5168

52-
public string GetHostname()
69+
public bool MatchVersion(Predicate<Version> predicate)
5370
{
54-
return _image.GetHostname();
71+
return _image.MatchVersion(predicate);
5572
}
5673
}

0 commit comments

Comments
 (0)