/
afanasevn
/
RedisLab
Обзор
Документация
Войти
/
afanasevn
/
RedisLab
Код
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
tests/RedisLab.IntegrationTests/Support/RedisLabIntegrationFixture.cs
60 строк
2 KB
IBS\NAfanasev
Solution commit
24 июн 2026, 16:45
24 июн 2026, 16:45
bab80b7
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Testcontainers.PostgreSql; using Testcontainers.Redis; namespace RedisLab.IntegrationTests.Support; /// <summary> /// Общая фикстура: PostgreSQL + Redis через Testcontainers и WebApplicationFactory. /// </summary> public sealed class RedisLabIntegrationFixture : IAsyncLifetime { private readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder() .WithImage("postgres:16-alpine") .WithDatabase("redislab") .WithUsername("redislab") .WithPassword("redislab") .Build(); private readonly RedisContainer _redis = new RedisBuilder() .WithImage("redis:7-alpine") .Build(); public WebApplicationFactory<Program> Factory { get; private set; } = null!; public HttpClient CreateClient() => Factory.CreateClient(); /// <inheritdoc /> public async Task InitializeAsync() { await _postgres.StartAsync(); await _redis.StartAsync(); // Перекрываем .env до старта Program (DotNetEnv не перезаписывает уже заданные переменные). Environment.SetEnvironmentVariable("ConnectionStrings__DefaultConnection", _postgres.GetConnectionString()); Environment.SetEnvironmentVariable("ConnectionStrings__Redis", _redis.GetConnectionString()); Environment.SetEnvironmentVariable("Jwt__Issuer", "RedisLab.Tests"); Environment.SetEnvironmentVariable("Jwt__Audience", "RedisLab.Tests"); Environment.SetEnvironmentVariable("Jwt__Key", "integration-test-secret-key-32chars!"); Environment.SetEnvironmentVariable("Jwt__ExpirationHours", "1"); Factory = new WebApplicationFactory<Program>() .WithWebHostBuilder(builder => { builder.UseEnvironment("Development"); }); // Прогрев: миграции + seed при старте host. _ = Factory.CreateClient(); await Task.Delay(TimeSpan.FromSeconds(2)); } /// <inheritdoc /> public async Task DisposeAsync() { await Factory.DisposeAsync(); await _redis.DisposeAsync(); await _postgres.DisposeAsync(); } }