feature: Warning Alert Consumer, Orders API & Input Validation
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
[Collection("Integration")]
|
||||
public class OrderLifecycleTests : IAsyncLifetime
|
||||
{
|
||||
private readonly ApiFixture _fixture;
|
||||
private readonly HttpClient _client;
|
||||
private Guid _encounterId;
|
||||
|
||||
public OrderLifecycleTests(ApiFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
_client = fixture.CreateClient();
|
||||
}
|
||||
|
||||
public async Task InitializeAsync() => await ResetAndSeedAsync();
|
||||
|
||||
public Task DisposeAsync() => Task.CompletedTask;
|
||||
|
||||
private async Task ResetAndSeedAsync()
|
||||
{
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await DbResetHelper.ResetAsync(db);
|
||||
|
||||
var patient = new Patient
|
||||
{
|
||||
Id = Guid.NewGuid(), Mrn = "MRN-ORD-001", FirstName = "Order", LastName = "Test",
|
||||
DateOfBirth = new DateOnly(1988, 2, 10), Gender = "F", CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
var encounter = new Encounter
|
||||
{
|
||||
Id = Guid.NewGuid(), PatientId = patient.Id, EncounterType = EncounterType.Inpatient,
|
||||
Status = EncounterStatus.Active, Department = Department.GeneralMedicine,
|
||||
AttendingPhysician = "Dr. Order", AdmittedAt = DateTimeOffset.UtcNow,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
db.Patients.Add(patient);
|
||||
db.Encounters.Add(encounter);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
_encounterId = encounter.Id;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateOrder_Returns201()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
var resp = await _client.PostAsJsonAsync(
|
||||
$"/api/v1/encounters/{_encounterId}/orders",
|
||||
new CreateOrderRequest(OrderType.Lab, "CBC with differential", "Dr. Test"));
|
||||
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||
var body = await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
body!.RootElement.GetProperty("data").GetProperty("status").GetString()
|
||||
.Should().Be("Pending");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListOrders_ReturnsPaginated()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
await _client.PostAsJsonAsync(
|
||||
$"/api/v1/encounters/{_encounterId}/orders",
|
||||
new CreateOrderRequest(OrderType.Lab, "BMP", "Dr. A"));
|
||||
await _client.PostAsJsonAsync(
|
||||
$"/api/v1/encounters/{_encounterId}/orders",
|
||||
new CreateOrderRequest(OrderType.Imaging, "Chest X-ray", "Dr. B"));
|
||||
|
||||
var resp = await _client.GetAsync($"/api/v1/encounters/{_encounterId}/orders");
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var body = await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
body!.RootElement.GetProperty("data").GetProperty("totalCount").GetInt32()
|
||||
.Should().Be(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RecordResult_TransitionsToResulted()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
var createResp = await _client.PostAsJsonAsync(
|
||||
$"/api/v1/encounters/{_encounterId}/orders",
|
||||
new CreateOrderRequest(OrderType.Lab, "Potassium", "Dr. Test"));
|
||||
var createBody = await createResp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
var orderId = createBody!.RootElement.GetProperty("data").GetProperty("id").GetGuid();
|
||||
|
||||
var resultResp = await _client.PatchAsJsonAsync(
|
||||
$"/api/v1/orders/{orderId}/result",
|
||||
new RecordOrderResultRequest("Potassium 4.2 mEq/L — within normal limits"));
|
||||
|
||||
resultResp.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var body = await resultResp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
body!.RootElement.GetProperty("data").GetProperty("status").GetString()
|
||||
.Should().Be("Resulted");
|
||||
body.RootElement.GetProperty("data").GetProperty("resultSummary").GetString()
|
||||
.Should().Contain("4.2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CancelResultedOrder_Returns409()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
var createResp = await _client.PostAsJsonAsync(
|
||||
$"/api/v1/encounters/{_encounterId}/orders",
|
||||
new CreateOrderRequest(OrderType.Lab, "Glucose", "Dr. Test"));
|
||||
var orderId = (await createResp.Content.ReadFromJsonAsync<JsonDocument>())!
|
||||
.RootElement.GetProperty("data").GetProperty("id").GetGuid();
|
||||
|
||||
await _client.PatchAsJsonAsync(
|
||||
$"/api/v1/orders/{orderId}/result",
|
||||
new RecordOrderResultRequest("Normal"));
|
||||
|
||||
var resp = await _client.PatchAsJsonAsync(
|
||||
$"/api/v1/orders/{orderId}/status",
|
||||
new TransitionOrderStatusRequest(OrderStatus.Cancelled));
|
||||
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.Conflict);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using FluentAssertions;
|
||||
|
||||
[Collection("Integration")]
|
||||
public class ValidationTests
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public ValidationTests(ApiFixture fixture) => _client = fixture.CreateClient();
|
||||
|
||||
[Fact]
|
||||
public async Task EmptyFirstName_Returns400()
|
||||
{
|
||||
var resp = await _client.PostAsJsonAsync("/api/v1/patients", new
|
||||
{
|
||||
firstName = "",
|
||||
lastName = "Valid",
|
||||
dateOfBirth = "1990-01-01",
|
||||
gender = "M"
|
||||
});
|
||||
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThresholdInvalidOrder_Returns400()
|
||||
{
|
||||
var resp = await _client.PostAsJsonAsync("/api/v1/alert-thresholds", new
|
||||
{
|
||||
observationCode = "TEST_CODE",
|
||||
displayName = "Test",
|
||||
unit = "units",
|
||||
criticalLow = 50,
|
||||
warningLow = 30
|
||||
});
|
||||
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OrderEmptyDescription_Returns400()
|
||||
{
|
||||
var resp = await _client.PostAsJsonAsync(
|
||||
$"/api/v1/encounters/{Guid.NewGuid()}/orders",
|
||||
new { orderType = "Lab", description = "", orderedBy = "Dr. Test" });
|
||||
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StackExchange.Redis;
|
||||
|
||||
[Collection("Integration")]
|
||||
public class WarningAlertTests : IAsyncLifetime
|
||||
{
|
||||
private readonly ApiFixture _fixture;
|
||||
private Guid _patientId;
|
||||
private Guid _encounterId;
|
||||
|
||||
public WarningAlertTests(ApiFixture fixture) => _fixture = fixture;
|
||||
|
||||
public async Task InitializeAsync() => await ResetAndSeedAsync();
|
||||
|
||||
public Task DisposeAsync() => Task.CompletedTask;
|
||||
|
||||
private async Task ResetAndSeedAsync()
|
||||
{
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await DbResetHelper.ResetAsync(db);
|
||||
|
||||
var patient = new Patient
|
||||
{
|
||||
Id = Guid.NewGuid(), Mrn = "MRN-WA-001", FirstName = "Warning", LastName = "Test",
|
||||
DateOfBirth = new DateOnly(1975, 5, 20), Gender = "M", CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
var encounter = new Encounter
|
||||
{
|
||||
Id = Guid.NewGuid(), PatientId = patient.Id, EncounterType = EncounterType.Inpatient,
|
||||
Status = EncounterStatus.Active, Department = Department.Icu,
|
||||
AttendingPhysician = "Dr. Warning", AdmittedAt = DateTimeOffset.UtcNow,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
db.Patients.Add(patient);
|
||||
db.Encounters.Add(encounter);
|
||||
db.AlertThresholds.Add(new AlertThreshold
|
||||
{
|
||||
Id = Guid.NewGuid(), ObservationCode = "HEART_RATE",
|
||||
DisplayName = "Heart Rate", Unit = "bpm",
|
||||
CriticalLow = 30, WarningLow = 50, WarningHigh = 100, CriticalHigh = 150,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
var redis = scope.ServiceProvider.GetRequiredService<IConnectionMultiplexer>();
|
||||
var cache = redis.GetDatabase(1);
|
||||
await cache.StringSetAsync("threshold:HEART_RATE",
|
||||
"""{"ObservationCode":"HEART_RATE","CriticalLow":30,"WarningLow":50,"WarningHigh":100,"CriticalHigh":150}""");
|
||||
|
||||
_patientId = patient.Id;
|
||||
_encounterId = encounter.Id;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WarningHeartRate_AlertCreated()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var evaluator = scope.ServiceProvider.GetRequiredService<WarningEvaluator>();
|
||||
|
||||
var created = await evaluator.EvaluateAsync(
|
||||
Guid.NewGuid(), _encounterId, _patientId, "HEART_RATE", 105m);
|
||||
|
||||
created.Should().BeTrue();
|
||||
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
var alert = await db.ClinicalAlerts.SingleAsync();
|
||||
alert.AlertType.Should().Be(AlertType.WarningHeartRate);
|
||||
alert.Severity.Should().Be(AlertSeverity.Warning);
|
||||
alert.Status.Should().Be(AlertStatus.Open);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NormalHeartRate_NoAlert()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var evaluator = scope.ServiceProvider.GetRequiredService<WarningEvaluator>();
|
||||
|
||||
var created = await evaluator.EvaluateAsync(
|
||||
Guid.NewGuid(), _encounterId, _patientId, "HEART_RATE", 78m);
|
||||
|
||||
created.Should().BeFalse();
|
||||
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
(await db.ClinicalAlerts.CountAsync()).Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CriticalHeartRate_NoWarningAlert()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var evaluator = scope.ServiceProvider.GetRequiredService<WarningEvaluator>();
|
||||
|
||||
var created = await evaluator.EvaluateAsync(
|
||||
Guid.NewGuid(), _encounterId, _patientId, "HEART_RATE", 160m);
|
||||
|
||||
created.Should().BeFalse();
|
||||
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
(await db.ClinicalAlerts.CountAsync()).Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DuplicateWarning_Idempotent()
|
||||
{
|
||||
await ResetAndSeedAsync();
|
||||
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var evaluator = scope.ServiceProvider.GetRequiredService<WarningEvaluator>();
|
||||
|
||||
await evaluator.EvaluateAsync(Guid.NewGuid(), _encounterId, _patientId, "HEART_RATE", 105m);
|
||||
await evaluator.EvaluateAsync(Guid.NewGuid(), _encounterId, _patientId, "HEART_RATE", 108m);
|
||||
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
(await db.ClinicalAlerts.CountAsync()).Should().Be(1,
|
||||
"second warning for same type must be idempotent while first is still open");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user