Record Architecture B approval and verified pinned baseline
This commit is contained in:
1 parent
20ab461c01
commit
cee98e7035
158 files changed
+33575
No files matched your search
@@ -0,0 +1,121 @@
|
||||
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 UpdateObservationTests(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<Observation> RecordAsync()
|
||||
{
|
||||
using var patient = await fixture.Client.PostAsJsonAsync("/api/patients", new
|
||||
{
|
||||
name = "Correction Example", dateOfBirth = "1980-01-01"
|
||||
});
|
||||
Assert.Equal(HttpStatusCode.Created, patient.StatusCode);
|
||||
var patientId = (await patient.Content.ReadFromJsonAsync<Profile>())!.PatientId;
|
||||
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new
|
||||
{
|
||||
measuredAt = "2026-09-12T08:00:00", systolic = 182, diastolic = 82, pulse = 68,
|
||||
context = "Resting", note = "Before breakfast"
|
||||
});
|
||||
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||||
return (await response.Content.ReadFromJsonAsync<Observation>())!;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Correction_preserves_other_information_and_is_visible_in_all_retrievals_after_restart()
|
||||
{
|
||||
var original = await RecordAsync();
|
||||
var other = await RecordAsync();
|
||||
await using var db = fixture.CreateObservationDbContext();
|
||||
var before = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId);
|
||||
using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{original.ObservationId}",
|
||||
new { systolic = 128, diastolic = 82, pulse = 68 });
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var expected = original with { Systolic = 128 };
|
||||
Assert.Equal(expected, await response.Content.ReadFromJsonAsync<Observation>());
|
||||
await fixture.RestartHostsAsync();
|
||||
Assert.Equal(expected, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{original.ObservationId}"));
|
||||
Assert.Equal(expected, await fixture.Client.GetFromJsonAsync<Observation>(
|
||||
$"/api/patients/{original.PatientId}/observations/latest"));
|
||||
var history = await fixture.Client.GetFromJsonAsync<Observation[]>(
|
||||
$"/api/patients/{original.PatientId}/observations?from=2026-09-12T00:00:00&to=2026-09-12T23:59:59");
|
||||
Assert.Equal(expected, Assert.Single(history!));
|
||||
Assert.Equal(other, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{other.ObservationId}"));
|
||||
var after = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId);
|
||||
Assert.Equal(before.CreatedAt, after.CreatedAt);
|
||||
Assert.True(after.UpdatedAt > before.UpdatedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task All_three_measured_values_can_be_corrected()
|
||||
{
|
||||
var original = await RecordAsync();
|
||||
using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{original.ObservationId}",
|
||||
new { systolic = 120, diastolic = 80, pulse = 60 });
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal(original with { Systolic = 120, Diastolic = 80, Pulse = 60 },
|
||||
await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{original.ObservationId}"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 82, 68)]
|
||||
[InlineData(128, 0, 68)]
|
||||
[InlineData(128, 82, 0)]
|
||||
[InlineData(-1, 82, 68)]
|
||||
[InlineData(128, -1, 68)]
|
||||
[InlineData(128, 82, -1)]
|
||||
public async Task Non_positive_values_are_rejected_without_changing_observation(int systolic, int diastolic, int pulse)
|
||||
{
|
||||
var original = await RecordAsync();
|
||||
await using var db = fixture.CreateObservationDbContext();
|
||||
var before = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId);
|
||||
using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{original.ObservationId}",
|
||||
new { systolic, diastolic, pulse });
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
Assert.Equal(original, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{original.ObservationId}"));
|
||||
var after = await db.Observations.AsNoTracking().SingleAsync(o => o.ObservationId == original.ObservationId);
|
||||
Assert.Equal(before.UpdatedAt, after.UpdatedAt);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{\"diastolic\":82,\"pulse\":68}")]
|
||||
[InlineData("{\"systolic\":128,\"pulse\":68}")]
|
||||
[InlineData("{\"systolic\":128,\"diastolic\":82}")]
|
||||
[InlineData("{\"systolic\":null,\"diastolic\":82,\"pulse\":68}")]
|
||||
[InlineData("{\"systolic\":\"invalid\",\"diastolic\":82,\"pulse\":68}")]
|
||||
[InlineData("{")]
|
||||
public async Task Missing_or_malformed_values_are_rejected_without_changes(string json)
|
||||
{
|
||||
var original = await RecordAsync();
|
||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
using var response = await fixture.Client.PutAsync($"/api/observations/{original.ObservationId}", 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(original, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{original.ObservationId}"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Missing_observation_is_rejected_without_inserting_a_new_observation()
|
||||
{
|
||||
await using var db = fixture.CreateObservationDbContext();
|
||||
var count = await db.Observations.CountAsync();
|
||||
var observationId = Guid.NewGuid();
|
||||
using var response = await fixture.Client.PutAsJsonAsync($"/api/observations/{observationId}",
|
||||
new { systolic = 128, diastolic = 82, pulse = 68 });
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
Assert.Equal(count, await db.Observations.CountAsync());
|
||||
using var retrieval = await fixture.Client.GetAsync($"/api/observations/{observationId}");
|
||||
Assert.Equal(HttpStatusCode.NotFound, retrieval.StatusCode);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user