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,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.12" />
|
||||
<ProjectReference Include="../../src/PatientProfileService/PatientProfileService.csproj" />
|
||||
<ProjectReference Include="../../src/ApiGateway/ApiGateway.csproj" />
|
||||
<ProjectReference Include="../../src/ObservationService/ObservationService.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace Observation.AcceptanceTests;
|
||||
|
||||
public sealed class RecordObservationTests(ObservationApiFixture fixture) : IClassFixture<ObservationApiFixture>
|
||||
{
|
||||
private sealed record Profile(Guid PatientId);
|
||||
private sealed record Observation(Guid ObservationId, Guid PatientId, DateTime MeasuredAt,
|
||||
int Systolic, int Diastolic, int Pulse, string? Context, string? Note);
|
||||
|
||||
private async Task<Guid> CreatePatientAsync()
|
||||
{
|
||||
using var response = await fixture.Client.PostAsJsonAsync("/api/patients", new
|
||||
{
|
||||
name = "Steffen Example", dateOfBirth = "1980-01-01"
|
||||
});
|
||||
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||||
return (await response.Content.ReadFromJsonAsync<Profile>())!.PatientId;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Valid_observation_is_assigned_an_identifier_associated_with_the_patient_and_retrievable_after_restart()
|
||||
{
|
||||
var patientId = await CreatePatientAsync();
|
||||
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new
|
||||
{
|
||||
measuredAt = "2026-09-12T08:00:00", systolic = 128, diastolic = 82, pulse = 68,
|
||||
context = "Resting", note = "Before breakfast"
|
||||
});
|
||||
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||||
var saved = await response.Content.ReadFromJsonAsync<Observation>();
|
||||
Assert.NotNull(saved);
|
||||
Assert.NotEqual(Guid.Empty, saved.ObservationId);
|
||||
Assert.Equal(patientId, saved.PatientId);
|
||||
Assert.Equal(new DateTime(2026, 9, 12, 8, 0, 0), saved.MeasuredAt);
|
||||
Assert.Equal(128, saved.Systolic);
|
||||
Assert.Equal(82, saved.Diastolic);
|
||||
Assert.Equal(68, saved.Pulse);
|
||||
Assert.Equal("Resting", saved.Context);
|
||||
Assert.Equal("Before breakfast", saved.Note);
|
||||
Assert.Equal($"/api/observations/{saved.ObservationId}", response.Headers.Location!.ToString());
|
||||
|
||||
await fixture.RestartHostsAsync();
|
||||
Assert.Equal(saved, await fixture.Client.GetFromJsonAsync<Observation>(response.Headers.Location));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Optional_fields_can_be_omitted_and_identifiers_are_distinct()
|
||||
{
|
||||
var patientId = await CreatePatientAsync();
|
||||
var request = new { measuredAt = "2026-09-12T08:00:00", systolic = 128, diastolic = 82, pulse = 68 };
|
||||
using var first = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", request);
|
||||
using var second = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", request);
|
||||
Assert.Equal(HttpStatusCode.Created, first.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.Created, second.StatusCode);
|
||||
var saved = (await first.Content.ReadFromJsonAsync<Observation>())!;
|
||||
Assert.Null(saved.Context);
|
||||
Assert.Null(saved.Note);
|
||||
Assert.NotEqual(saved.ObservationId, (await second.Content.ReadFromJsonAsync<Observation>())!.ObservationId);
|
||||
Assert.Equal(saved, await fixture.Client.GetFromJsonAsync<Observation>(first.Headers.Location));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 82, 68)]
|
||||
[InlineData(-1, 82, 68)]
|
||||
[InlineData(128, 0, 68)]
|
||||
[InlineData(128, -1, 68)]
|
||||
[InlineData(128, 82, 0)]
|
||||
[InlineData(128, 82, -1)]
|
||||
public async Task Non_positive_values_are_rejected_without_saving(int systolic, int diastolic, int pulse)
|
||||
{
|
||||
var patientId = await CreatePatientAsync();
|
||||
await using var db = fixture.CreateObservationDbContext();
|
||||
var before = await db.Observations.CountAsync();
|
||||
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new
|
||||
{
|
||||
measuredAt = "2026-09-12T08:00:00", systolic, diastolic, pulse
|
||||
});
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
Assert.Equal(before, await db.Observations.CountAsync());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{\"systolic\":128,\"diastolic\":82,\"pulse\":68}")]
|
||||
[InlineData("{\"measuredAt\":null,\"systolic\":128,\"diastolic\":82,\"pulse\":68}")]
|
||||
[InlineData("{\"measuredAt\":\"invalid\",\"systolic\":128,\"diastolic\":82,\"pulse\":68}")]
|
||||
[InlineData("{\"measuredAt\":\"2026-09-12T08:00:00\",\"diastolic\":82,\"pulse\":68}")]
|
||||
[InlineData("{\"measuredAt\":\"2026-09-12T08:00:00\",\"systolic\":128,\"pulse\":68}")]
|
||||
[InlineData("{\"measuredAt\":\"2026-09-12T08:00:00\",\"systolic\":128,\"diastolic\":82}")]
|
||||
[InlineData("{")]
|
||||
public async Task Missing_or_malformed_information_is_rejected_without_saving(string json)
|
||||
{
|
||||
var patientId = await CreatePatientAsync();
|
||||
await using var db = fixture.CreateObservationDbContext();
|
||||
var before = await db.Observations.CountAsync();
|
||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
using var response = await fixture.Client.PostAsync($"/api/patients/{patientId}/observations", content);
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
using var problem = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
Assert.True(problem.RootElement.TryGetProperty("errors", out _));
|
||||
Assert.Equal(before, await db.Observations.CountAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Missing_patient_is_rejected_without_saving()
|
||||
{
|
||||
await using var db = fixture.CreateObservationDbContext();
|
||||
var before = await db.Observations.CountAsync();
|
||||
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{Guid.NewGuid()}/observations", new
|
||||
{
|
||||
measuredAt = "2026-09-12T08:00:00", systolic = 128, diastolic = 82, pulse = 68
|
||||
});
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
Assert.Equal(before, await db.Observations.CountAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Missing_observation_returns_not_found()
|
||||
{
|
||||
using var response = await fixture.Client.GetAsync($"/api/observations/{Guid.NewGuid()}");
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Failed_patient_lookup_is_rejected_without_saving()
|
||||
{
|
||||
var patientId = await CreatePatientAsync();
|
||||
await using var db = fixture.CreateObservationDbContext();
|
||||
var before = await db.Observations.CountAsync();
|
||||
await fixture.StopPatientServiceAsync();
|
||||
try
|
||||
{
|
||||
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new
|
||||
{
|
||||
measuredAt = "2026-09-12T08:00:00", systolic = 128, diastolic = 82, pulse = 68
|
||||
});
|
||||
Assert.Equal(HttpStatusCode.BadGateway, response.StatusCode);
|
||||
Assert.Equal(before, await db.Observations.CountAsync());
|
||||
}
|
||||
finally
|
||||
{
|
||||
await fixture.RestartHostsAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user