Skip to content

Commit

Permalink
Fixing functional tests to use scheduleId in routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Apr 15, 2021
1 parent 4e5f8f2 commit 170a06b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion FrontDesk/src/FrontDesk.Api/Endpoints/Appointment/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public List(IReadRepository<Schedule> scheduleRepository,
OperationId = "appointments.List",
Tags = new[] { "AppointmentEndpoints" })
]
public override async Task<ActionResult<ListAppointmentResponse>> HandleAsync([FromQuery] ListAppointmentRequest request,
public override async Task<ActionResult<ListAppointmentResponse>> HandleAsync([FromRoute] ListAppointmentRequest request,
CancellationToken cancellationToken)
{
var response = new ListAppointmentResponse(request.CorrelationId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ public class ScheduleForClinicAndDateWithAppointmentsSpec : Specification<Schedu
{
public ScheduleForClinicAndDateWithAppointmentsSpec(int clinicId, DateTimeOffset date)
{
var endDate = date.AddDays(1);
Query
.Include(nameof(Schedule.Appointments))
.Where(schedule =>
schedule.ClinicId == clinicId &&
schedule.Appointments != null);
schedule.ClinicId == clinicId &&
schedule.Appointments != null)
.Include(s => s.Appointments.Where(a => a.TimeRange.Start > date && a.TimeRange.End < endDate));

// See: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#filtered-include

// TODO: Only include appointments for the specified date
// NOTE: This worked when using DateTime, but EF Core has
// issues with DateTimeOffset in queries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private IEnumerable<Appointment> CreateAppointments(Guid scheduleId)
_darwin.Id,
room1,
new DateTimeOffsetRange(_testDate.AddHours(10), TimeSpan.FromMinutes(30)),
"(WE) Darwin - Steve Smith"),
"(WE) Darwin - Steve Smith") { Id = Guid.NewGuid() },
new Appointment(
wellnessVisit,
scheduleId,
Expand All @@ -283,7 +283,7 @@ private IEnumerable<Appointment> CreateAppointments(Guid scheduleId)
_steve.Patients[1].Id,
room1,
new DateTimeOffsetRange(_testDate.AddHours(10).AddMinutes(30), TimeSpan.FromMinutes(30)),
"(WE) Arya - Steve Smith"),
"(WE) Arya - Steve Smith") { Id = Guid.NewGuid() },
new Appointment(
wellnessVisit,
scheduleId,
Expand All @@ -292,7 +292,7 @@ private IEnumerable<Appointment> CreateAppointments(Guid scheduleId)
_steve.Patients[2].Id,
room1,
new DateTimeOffsetRange(_testDate.AddHours(11), TimeSpan.FromMinutes(30)),
"(WE) Rosie - Steve Smith"),
"(WE) Rosie - Steve Smith") { Id = Guid.NewGuid() },
new Appointment(
diagnosticVisit,
scheduleId,
Expand All @@ -301,7 +301,7 @@ private IEnumerable<Appointment> CreateAppointments(Guid scheduleId)
_sampson.Id,
room2,
new DateTimeOffsetRange(_testDate.AddHours(11), TimeSpan.FromMinutes(60)),
"(DE) Sampson - Julie Lerman")
"(DE) Sampson - Julie Lerman") { Id = Guid.NewGuid() }
};

appointmentList.Add(CreateAppointment(scheduleId, "David Batten", "Max", room3, 10));
Expand All @@ -327,6 +327,7 @@ private Appointment CreateAppointment(Guid scheduleId, string clientName, string
var client = _context.Clients.First(c => c.FullName == clientName);
var patient = _context.Patients.First(p => p.Name == patientName);
var appt = new Appointment(diagnosticVisit, scheduleId, client.Id, patient.PreferredDoctorId.Value, patient.Id, roomId, new DateTimeOffsetRange(_testDate.AddHours(hour), TimeSpan.FromMinutes(60)), $"(DE) {patientName} - {clientName}");
appt.Id = Guid.NewGuid();
return appt;
}
catch (Exception ex)
Expand Down
18 changes: 12 additions & 6 deletions FrontDesk/tests/FunctionalTests/AppointmentEndpoints/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Ardalis.HttpClientTestExtensions;
using BlazorShared.Models.Appointment;
using BlazorShared.Models.Schedule;
using FrontDesk.Api;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -32,19 +33,24 @@ public Create(CustomWebApplicationFactory<Startup> factory, ITestOutputHelper ou
[Fact]
public async Task CreatesANewAppointment()
{
var result = await _client.GetAndDeserialize<ListAppointmentResponse>(ListAppointmentRequest.Route, _outputHelper);
// get schedule
var listResult = await _client.GetAndDeserialize<ListScheduleResponse>(ListScheduleRequest.Route, _outputHelper);
var schedule = listResult.Schedules.First();
string scheduleId = schedule.Id.ToString();

var firstAppt = result.Appointments.First();
Guid scheduleId = firstAppt.ScheduleId;
string listRoute = ListAppointmentRequest.Route.Replace("{ScheduleId}", scheduleId);

var jsonContent = GetValidNewAppointmentJson(scheduleId);
var result = await _client.GetAndDeserialize<ListAppointmentResponse>(listRoute, _outputHelper);

var rawResult = await _client.PostAsync(CreateAppointmentRequest.Route, jsonContent);
var jsonContent = GetValidNewAppointmentJson(schedule.Id);

string createRoute = CreateAppointmentRequest.Route.Replace("{ScheduleId}", scheduleId);
var rawResult = await _client.PostAsync(createRoute, jsonContent);
rawResult.EnsureSuccessStatusCode();
var stringResponse = await rawResult.Content.ReadAsStringAsync();
var endResult = stringResponse.FromJson<CreateAppointmentResponse>();

Assert.Equal(scheduleId, endResult.Appointment.ScheduleId);
Assert.Equal(schedule.Id, endResult.Appointment.ScheduleId);
}

private StringContent GetValidNewAppointmentJson(Guid scheduleId)
Expand Down
11 changes: 8 additions & 3 deletions FrontDesk/tests/FunctionalTests/AppointmentEndpoints/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ public Delete(CustomWebApplicationFactory<Startup> factory, ITestOutputHelper ou
[Fact]
public async Task DeletesExistingAppointment()
{
// get schedule
var listResult = await _client.GetAndDeserialize<ListScheduleResponse>(ListScheduleRequest.Route, _outputHelper);
var schedule = listResult.Schedules.First();
string scheduleId = schedule.Id.ToString();

string getRoute = ListAppointmentRequest.Route.Replace("{ScheduleId}", scheduleId);

// get existing appointment
var result = await _client.GetAndDeserialize<ListAppointmentResponse>(ListAppointmentRequest.Route, _outputHelper);
var result = await _client.GetAndDeserialize<ListAppointmentResponse>(getRoute, _outputHelper);

var firstAppt = result.Appointments.First();
_outputHelper.WriteLine(firstAppt.ToString());

// delete it
string route = DeleteAppointmentRequest.Route.Replace("{AppointmentId}", firstAppt.AppointmentId.ToString());
var scheduleId = firstAppt.ScheduleId.ToString();

route = route.Replace("{ScheduleId}", scheduleId);
var deleteResponse = await _client.DeleteAsync(route);
deleteResponse.EnsureSuccessStatusCode();
Expand Down
14 changes: 12 additions & 2 deletions FrontDesk/tests/FunctionalTests/AppointmentEndpoints/GetById.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Ardalis.HttpClientTestExtensions;
using BlazorShared.Models.Appointment;
using BlazorShared.Models.Schedule;
using FrontDesk.Api;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -24,13 +25,22 @@ public GetById(CustomWebApplicationFactory<Startup> factory, ITestOutputHelper o
[Fact]
public async Task GetsExistingAppointment()
{
var listResponse = await _client.GetAndDeserialize<ListAppointmentResponse>(ListAppointmentRequest.Route, _outputHelper);
// get schedule
var listResult = await _client.GetAndDeserialize<ListScheduleResponse>(ListScheduleRequest.Route, _outputHelper);
var schedule = listResult.Schedules.First();
_outputHelper.WriteLine($"Schedule: {schedule}");

string listRoute = ListAppointmentRequest.Route.Replace("{ScheduleId}", schedule.Id.ToString());

_outputHelper.WriteLine($"Route: {listRoute}");

var listResponse = await _client.GetAndDeserialize<ListAppointmentResponse>(listRoute, _outputHelper);

var firstAppt = listResponse.Appointments.First();
_outputHelper.WriteLine(firstAppt.ToString());

string route = GetByIdAppointmentRequest.Route.Replace("{AppointmentId}", firstAppt.AppointmentId.ToString());
route = route.Replace("{ScheduleId}", firstAppt.ScheduleId.ToString());
route = route.Replace("{ScheduleId}", schedule.Id.ToString());
var result = await _client.GetAndDeserialize<GetByIdAppointmentResponse>(route, _outputHelper);

Assert.Equal(firstAppt.PatientId, result.Appointment.PatientId);
Expand Down
13 changes: 12 additions & 1 deletion FrontDesk/tests/FunctionalTests/AppointmentEndpoints/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Ardalis.HttpClientTestExtensions;
using BlazorShared.Models.Appointment;
using BlazorShared.Models.Schedule;
using FrontDesk.Api;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -24,7 +25,17 @@ public List(CustomWebApplicationFactory<Startup> factory, ITestOutputHelper outp
[Fact]
public async Task ReturnsAppointmentsIncludingOneForDarwin()
{
var result = await _client.GetAndDeserialize<ListAppointmentResponse>(ListAppointmentRequest.Route, _outputHelper);
// get schedule
var listResult = await _client.GetAndDeserialize<ListScheduleResponse>(ListScheduleRequest.Route, _outputHelper);
Assert.True(listResult.Schedules.Count == 1);
var schedule = listResult.Schedules.First();
_outputHelper.WriteLine($"Schedule: {schedule}");

string route = ListAppointmentRequest.Route.Replace("{ScheduleId}", schedule.Id.ToString());

_outputHelper.WriteLine($"Route: {route}");

var result = await _client.GetAndDeserialize<ListAppointmentResponse>(route, _outputHelper);

var darwinAppt = result.Appointments.First(a => a.Title.Contains("Darwin"));
_outputHelper.WriteLine(darwinAppt.ToString());
Expand Down

0 comments on commit 170a06b

Please sign in to comment.