Skip to content

Commit

Permalink
Change environment variables for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTedder committed Oct 13, 2023
1 parent e5152e0 commit fd1b1c7
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions LeaderboardBackend.Test/TestApi/TestApiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,19 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
// Set the environment for the run to Staging
builder.UseEnvironment(Environments.Staging);

base.ConfigureWebHost(builder);

builder.ConfigureServices(services =>
if (PostgresDatabaseFixture.PostgresContainer is null)
{
if (PostgresDatabaseFixture.PostgresContainer is null)
{
throw new InvalidOperationException("Postgres container is not initialized.");
}

ServiceDescriptor? dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<ApplicationContext>));

if (dbContextDescriptor is not null)
{
services.Remove(dbContextDescriptor);
}
throw new InvalidOperationException("Postgres container is not initialized.");
}

services.Configure<ApplicationContextConfig>(conf =>
conf.Pg = new PostgresConfig
{
Db = PostgresDatabaseFixture.Database!,
Port = (ushort)PostgresDatabaseFixture.Port,
Host = PostgresDatabaseFixture.PostgresContainer.Hostname,
User = PostgresDatabaseFixture.Username!,
Password = PostgresDatabaseFixture.Password!
});

services.AddDbContext<ApplicationContext>(options =>
{
options.UseNpgsql(PostgresDatabaseFixture.DataSource!, o => o.UseNodaTime()).UseSnakeCaseNamingConvention();
});
Environment.SetEnvironmentVariable("ApplicationContext__PG__DB", PostgresDatabaseFixture.Database);
Environment.SetEnvironmentVariable("ApplicationContext__PG__PORT", PostgresDatabaseFixture.Port.ToString());
Environment.SetEnvironmentVariable("ApplicationContext__PG__HOST", PostgresDatabaseFixture.PostgresContainer!.Hostname);
Environment.SetEnvironmentVariable("ApplicationContext__PG__USER", PostgresDatabaseFixture.Username);
Environment.SetEnvironmentVariable("ApplicationContext__PG__PASSWORD", PostgresDatabaseFixture.Password);

builder.ConfigureServices(services =>
{
// mock SMTP client
services.Replace(ServiceDescriptor.Transient(_ => _mock.Object));

Expand Down

0 comments on commit fd1b1c7

Please sign in to comment.