Victor H
Victor H
CC#
Created by Victor H on 1/3/2025 in #help
Configuration of Testcontainer, WebApplicationFactory and ConfigurationSource in ASP.NET Core.
Not anything particularly annoying but just always thinking about improving my code. For instance, as you said, it becomes a bit boilerplate to add new containers and I don't like having to type strings into a dictionary. But nothing is terrible or a pain point just looking for feedback 😄 Thanks for your response ❤️
7 replies
CC#
Created by Victor H on 1/3/2025 in #help
Configuration of Testcontainer, WebApplicationFactory and ConfigurationSource in ASP.NET Core.
And an example of my Testcontainer class:
public class PostgreSqlTestContainer : ITestContainer {

private readonly PostgreSqlContainer _container = new PostgreSqlBuilder()
.WithDatabase(Database)
.WithUsername(Username)
.WithPassword(Password)
.WithPortBinding(ContainerPort, true)
.Build();

public const string Database = "myDb";
public const string Username = "username";
public const string Password = "password";
private const ushort ContainerPort = 5432; // PostgreSQL default port
public string ConnectionString => _container.GetConnectionString();
public ushort HostPort => _container.GetMappedPublicPort(ContainerPort);
public string Hostname => _container.Hostname;

public Task InitializeAsync() => _container.StartAsync();
public Task DisposeAsync() => _container.DisposeAsync().AsTask();
}
public class PostgreSqlTestContainer : ITestContainer {

private readonly PostgreSqlContainer _container = new PostgreSqlBuilder()
.WithDatabase(Database)
.WithUsername(Username)
.WithPassword(Password)
.WithPortBinding(ContainerPort, true)
.Build();

public const string Database = "myDb";
public const string Username = "username";
public const string Password = "password";
private const ushort ContainerPort = 5432; // PostgreSQL default port
public string ConnectionString => _container.GetConnectionString();
public ushort HostPort => _container.GetMappedPublicPort(ContainerPort);
public string Hostname => _container.Hostname;

public Task InitializeAsync() => _container.StartAsync();
public Task DisposeAsync() => _container.DisposeAsync().AsTask();
}
7 replies
CC#
Created by Victor H on 1/3/2025 in #help
Configuration of Testcontainer, WebApplicationFactory and ConfigurationSource in ASP.NET Core.
This is my setup:
public class ApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
private readonly RabbitMqTestContainer _rabbitMqTestContainer = new();
private readonly PostgreSqlTestContainer _postgreSqlTestContainer = new();

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
var rabbitMqTestConfiguration = new Dictionary<string, string>
{
["RabbitMqConnectionOptions:HostName"] = _rabbitMqTestContainer.Hostname,
["RabbitMqConnectionOptions:Port"] = _rabbitMqTestContainer.HostPort.ToString(),
["RabbitMqConnectionOptions:UserName"] = RabbitMqTestContainer.UserName,
["RabbitMqConnectionOptions:Password"] = RabbitMqTestContainer.Password
};
config.AddInMemoryCollection(rabbitMqTestConfiguration!);

var postgreSqlTestConfiguration = new Dictionary<string, string>
{
["PostgreSqlConnectionOptions:Host"] = _postgreSqlTestContainer.Hostname,
["PostgreSqlConnectionOptions:Database"] = PostgreSqlTestContainer.Database,
["PostgreSqlConnectionOptions:Username"] = PostgreSqlTestContainer.Username,
["PostgreSqlConnectionOptions:Password"] = PostgreSqlTestContainer.Password,
["PostgreSqlConnectionOptions:Port"] = _postgreSqlTestContainer.HostPort.ToString()
};
config.AddInMemoryCollection(postgreSqlTestConfiguration!);
});
}
}
public class ApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
private readonly RabbitMqTestContainer _rabbitMqTestContainer = new();
private readonly PostgreSqlTestContainer _postgreSqlTestContainer = new();

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
var rabbitMqTestConfiguration = new Dictionary<string, string>
{
["RabbitMqConnectionOptions:HostName"] = _rabbitMqTestContainer.Hostname,
["RabbitMqConnectionOptions:Port"] = _rabbitMqTestContainer.HostPort.ToString(),
["RabbitMqConnectionOptions:UserName"] = RabbitMqTestContainer.UserName,
["RabbitMqConnectionOptions:Password"] = RabbitMqTestContainer.Password
};
config.AddInMemoryCollection(rabbitMqTestConfiguration!);

var postgreSqlTestConfiguration = new Dictionary<string, string>
{
["PostgreSqlConnectionOptions:Host"] = _postgreSqlTestContainer.Hostname,
["PostgreSqlConnectionOptions:Database"] = PostgreSqlTestContainer.Database,
["PostgreSqlConnectionOptions:Username"] = PostgreSqlTestContainer.Username,
["PostgreSqlConnectionOptions:Password"] = PostgreSqlTestContainer.Password,
["PostgreSqlConnectionOptions:Port"] = _postgreSqlTestContainer.HostPort.ToString()
};
config.AddInMemoryCollection(postgreSqlTestConfiguration!);
});
}
}
7 replies
CC#
Created by Victor H on 4/9/2023 in #help
❔ Switch-case on byte as characters like in C
Thanks 🙂
4 replies