using System.Net; using System.Net.Http.Json; using Microsoft.EntityFrameworkCore; using Xunit; namespace PatientProfile.AcceptanceTests; public sealed class UpdatePatientTests(PatientApiFixture fixture) : IClassFixture { private sealed record Profile(Guid PatientId, string Name, DateOnly DateOfBirth, string? ContactInfo); [Fact] public async Task Updated_profile_preserves_identity_and_date_of_birth_and_survives_restart() { using var createdResponse = await fixture.Client.PostAsJsonAsync("/api/patients", new { name = "Steffen Example", dateOfBirth = "1980-01-01", contactInfo = "old@example.test" }); Assert.Equal(HttpStatusCode.Created, createdResponse.StatusCode); var created = (await createdResponse.Content.ReadFromJsonAsync())!; var path = $"/api/patients/{created.PatientId}"; using var response = await fixture.Client.PutAsJsonAsync(path, new { name = "Steffen Updated", contactInfo = "new@example.test" }); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var expected = created with { Name = "Steffen Updated", ContactInfo = "new@example.test" }; Assert.Equal(expected, await response.Content.ReadFromJsonAsync()); await fixture.RestartHostsAsync(); Assert.Equal(expected, await fixture.Client.GetFromJsonAsync(path)); } [Fact] public async Task Updating_a_missing_patient_is_rejected_without_creating_a_profile() { await using var db = fixture.CreateDbContext(); var countBefore = await db.Patients.CountAsync(); var path = $"/api/patients/{Guid.NewGuid()}"; using var response = await fixture.Client.PutAsJsonAsync(path, new { name = "Steffen Example", contactInfo = "new@example.test" }); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Assert.Equal(countBefore, await db.Patients.CountAsync()); using var retrieval = await fixture.Client.GetAsync(path); Assert.Equal(HttpStatusCode.NotFound, retrieval.StatusCode); } [Theory] [InlineData(null)] [InlineData("")] [InlineData(" ")] public async Task Invalid_name_is_rejected_without_changing_the_profile(string? name) { using var createdResponse = await fixture.Client.PostAsJsonAsync("/api/patients", new { name = "Steffen Example", dateOfBirth = "1980-01-01", contactInfo = "old@example.test" }); Assert.Equal(HttpStatusCode.Created, createdResponse.StatusCode); var created = (await createdResponse.Content.ReadFromJsonAsync())!; var path = $"/api/patients/{created.PatientId}"; using var response = await fixture.Client.PutAsJsonAsync(path, new { name, contactInfo = "new@example.test" }); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Equal(created, await fixture.Client.GetFromJsonAsync(path)); } }