Implement US-04 blood-pressure observation recording
This commit is contained in:
1 parent
a9633e49e1
commit
05f57dcba3
25 files changed
+843
-9
No files matched your search
@@ -0,0 +1,121 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Npgsql;
|
||||
using PatientProfileService.Persistence;
|
||||
using Xunit;
|
||||
|
||||
namespace Observation.AcceptanceTests;
|
||||
|
||||
public sealed class ObservationApiFixture : IAsyncLifetime
|
||||
{
|
||||
private readonly string databaseName = "us04_patient_" + Guid.NewGuid().ToString("N");
|
||||
private readonly string observationDatabaseName = "us04_observation_" + Guid.NewGuid().ToString("N");
|
||||
private WebApplicationFactory<ObservationService.Program>? observation;
|
||||
public string ObservationConnectionString { get; private set; } = "";
|
||||
private string adminConnectionString = "";
|
||||
private WebApplicationFactory<PatientProfileService.Program>? service;
|
||||
private WebApplicationFactory<ApiGateway.Program>? gateway;
|
||||
public string ConnectionString { get; private set; } = "";
|
||||
public HttpClient Client { get; private set; } = null!;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
adminConnectionString = Environment.GetEnvironmentVariable("TEST_POSTGRES_CONNECTION")
|
||||
?? throw new InvalidOperationException(
|
||||
"Set TEST_POSTGRES_CONNECTION to a PostgreSQL connection with permission to create test databases.");
|
||||
await using var connection = new NpgsqlConnection(adminConnectionString);
|
||||
await connection.OpenAsync();
|
||||
await using var command = new NpgsqlCommand($"CREATE DATABASE {databaseName}", connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
await using var observationCommand = new NpgsqlCommand($"CREATE DATABASE {observationDatabaseName}", connection);
|
||||
await observationCommand.ExecuteNonQueryAsync();
|
||||
ObservationConnectionString = new NpgsqlConnectionStringBuilder(adminConnectionString) { Database = observationDatabaseName }.ConnectionString;
|
||||
await using var observationDb = CreateObservationDbContext();
|
||||
await observationDb.Database.MigrateAsync();
|
||||
await observationDb.Database.MigrateAsync();
|
||||
ConnectionString = new NpgsqlConnectionStringBuilder(adminConnectionString) { Database = databaseName }.ConnectionString;
|
||||
await using var db = CreateDbContext();
|
||||
await db.Database.MigrateAsync();
|
||||
// Applying the committed migration twice must be harmless.
|
||||
await db.Database.MigrateAsync();
|
||||
StartHosts();
|
||||
}
|
||||
|
||||
public PatientDbContext CreateDbContext() => new(
|
||||
new DbContextOptionsBuilder<PatientDbContext>().UseNpgsql(ConnectionString).Options);
|
||||
|
||||
public ObservationService.Persistence.ObservationDbContext CreateObservationDbContext() => new(
|
||||
new DbContextOptionsBuilder<ObservationService.Persistence.ObservationDbContext>()
|
||||
.UseNpgsql(ObservationConnectionString).Options);
|
||||
|
||||
private void StartHosts()
|
||||
{
|
||||
service = new WebApplicationFactory<PatientProfileService.Program>().WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.ConfigureKestrel(options => options.Listen(IPAddress.Loopback, 0));
|
||||
builder.ConfigureAppConfiguration((_, configuration) => configuration.AddInMemoryCollection(
|
||||
new Dictionary<string, string?> { ["ConnectionStrings:PatientDatabase"] = ConnectionString }));
|
||||
});
|
||||
service.UseKestrel(0);
|
||||
using var serviceClient = service.CreateClient();
|
||||
|
||||
gateway = new WebApplicationFactory<ApiGateway.Program>().WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.ConfigureKestrel(options => options.Listen(IPAddress.Loopback, 0));
|
||||
builder.ConfigureAppConfiguration((_, configuration) => configuration.AddInMemoryCollection(
|
||||
new Dictionary<string, string?> { ["Services:PatientProfile:BaseUrl"] = serviceClient.BaseAddress!.ToString(),
|
||||
["Services:Observation:BaseUrl"] = "http://127.0.0.1:1/" }));
|
||||
});
|
||||
gateway.UseKestrel(0);
|
||||
Client = gateway.CreateClient();
|
||||
observation = new WebApplicationFactory<ObservationService.Program>().WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.ConfigureKestrel(options => options.Listen(IPAddress.Loopback, 0));
|
||||
builder.ConfigureAppConfiguration((_, configuration) => configuration.AddInMemoryCollection(
|
||||
new Dictionary<string, string?>
|
||||
{
|
||||
["ConnectionStrings:ObservationDatabase"] = ObservationConnectionString,
|
||||
["Services:ApiGateway:BaseUrl"] = Client.BaseAddress!.ToString()
|
||||
}));
|
||||
});
|
||||
observation.UseKestrel(0);
|
||||
using var observationClient = observation.CreateClient();
|
||||
gateway.Services.GetRequiredService<IConfiguration>()["Services:Observation:BaseUrl"] = observationClient.BaseAddress!.ToString();
|
||||
}
|
||||
|
||||
public async Task RestartHostsAsync()
|
||||
{
|
||||
Client.Dispose();
|
||||
await observation!.DisposeAsync();
|
||||
await gateway!.DisposeAsync();
|
||||
if (service is not null) await service.DisposeAsync();
|
||||
StartHosts();
|
||||
}
|
||||
|
||||
public async Task StopPatientServiceAsync()
|
||||
{
|
||||
await service!.DisposeAsync();
|
||||
service = null;
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
Client?.Dispose();
|
||||
if (observation is not null) await observation.DisposeAsync();
|
||||
if (gateway is not null) await gateway.DisposeAsync();
|
||||
if (service is not null) await service.DisposeAsync();
|
||||
if (string.IsNullOrEmpty(adminConnectionString)) return;
|
||||
|
||||
NpgsqlConnection.ClearAllPools();
|
||||
await using var connection = new NpgsqlConnection(adminConnectionString);
|
||||
await connection.OpenAsync();
|
||||
await using var command = new NpgsqlCommand($"DROP DATABASE IF EXISTS {databaseName} WITH (FORCE)", connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
await using var observationCommand = new NpgsqlCommand($"DROP DATABASE IF EXISTS {observationDatabaseName} WITH (FORCE)", connection);
|
||||
await observationCommand.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user