Files
HealthAppRepository/tests/Observation.AcceptanceTests/LatestObservationTests.cs
T

63 lines
2.7 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Xunit;
namespace Observation.AcceptanceTests;
public sealed class LatestObservationTests(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 = "Latest Example", dateOfBirth = "1980-01-01"
});
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Profile>())!.PatientId;
}
private async Task<Observation> RecordAsync(Guid patientId, string measuredAt, int systolic, int diastolic, int pulse)
{
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new
{
measuredAt, systolic, diastolic, pulse, context = "Resting", note = "Before breakfast"
});
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Observation>())!;
}
[Fact]
public async Task Latest_is_selected_by_measurement_time_for_the_requested_patient_after_restart()
{
var patientId = await CreatePatientAsync();
await RecordAsync(patientId, "2026-09-10T08:00:00", 130, 84, 70);
var latest = await RecordAsync(patientId, "2026-09-12T08:00:00", 126, 80, 66);
await RecordAsync(patientId, "2026-09-11T08:00:00", 128, 82, 68);
var otherPatientId = await CreatePatientAsync();
var otherLatest = await RecordAsync(otherPatientId, "2026-09-13T08:00:00", 140, 90, 75);
await fixture.RestartHostsAsync();
Assert.Equal(latest, await fixture.Client.GetFromJsonAsync<Observation>(
$"/api/patients/{patientId}/observations/latest"));
Assert.Equal(otherLatest, await fixture.Client.GetFromJsonAsync<Observation>(
$"/api/patients/{otherPatientId}/observations/latest"));
}
[Fact]
public async Task Patient_without_observations_returns_not_found_even_when_another_patient_has_observations()
{
var patientId = await CreatePatientAsync();
await RecordAsync(await CreatePatientAsync(), "2026-09-12T08:00:00", 128, 82, 68);
using var response = await fixture.Client.GetAsync($"/api/patients/{patientId}/observations/latest");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.DoesNotContain("observationId", await response.Content.ReadAsStringAsync());
}
}