public async Task DeleteAppointment_NotOK() { // ARRANGE DateTime startTime = DateTime.Now, endTime = startTime.AddSeconds(1); string summary = "Test summary..", location = "Test location...."; _optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); var oldAppointment = InsertTestAppointment(startTime: startTime, endTime: endTime, location: location, summary: summary); //Assert that the appointment record was created Assert.NotNull(oldAppointment); // ACT Exception ex; using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { var appointmentService = Setup(dbContext); //ex = Assert.Throws<RepositoryException>(async () => await appointmentService.DeleteAppointment(oldAppointment.ID + -99)); ex = await Assert.ThrowsAsync <RepositoryException>(() => appointmentService.DeleteAppointment(oldAppointment.ID + -99)); } // ASSERT Assert.Equal("Appointment not found.", ex.Message.Substring(0, 22)); if (ex is RepositoryException repoEx) { Assert.Equal(RepositiryExceptionType.AppointmentNotFound, repoEx.ExceptionType); } }
public void GetAllAppointments_OK() { // ARRANGE var startTime = DateTime.Now; var endTime = startTime.AddSeconds(1); string summary = "Test summary..", location = "Test location...."; TestActionExecutor <List <Appointment> > action = new TestActionExecutor <List <Appointment> >(); _optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); var result = AddTestAppointment(startTime: startTime, endTime: endTime, location: location, summary: summary); // ACT // I'd like to make this a one liner but I need a refactor. I'd need to inject dbContext List <Appointment> response; using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { var appointmentService = Setup(dbContext); response = action.ExecuteAction(async() => await appointmentService.GetAppointments(DateTime.Now)).Result; } // ASSERT Assert.NotEmpty(response); Assert.NotEqual(0, response.First().ID); Assert.Equal(startTime, response.First().StartDate); Assert.Equal(endTime, response.First().EndDate); Assert.Equal(location, response.First().Location); Assert.Equal(summary, response.First().Summary); }
public async Task AddBadAppointment_NotOK() { // ARRANGE DateTime startTime = DateTime.Now, endTime = startTime.AddHours(-1); string summary = "Test summary..", location = "Test location...."; _optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); Appointment appointment = new Appointment { StartDate = startTime, EndDate = endTime, Location = location, Summary = summary }; // ACT Exception ex; using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { var appointmentService = Setup(dbContext); ex = await Assert.ThrowsAsync <RepositoryException>(() => appointmentService.AddAppointment(appointment)); } // ASSERT Assert.Contains("Appointment must end after it has started.", ex.Message); if (ex is RepositoryException repoEx) { Assert.Equal(RepositiryExceptionType.BadAppointmentEndDateTime, repoEx.ExceptionType); } }
private AppointmentService Setup(AppointmentsDBContext dbContext) { return( new AppointmentService( dbContext )); }
public async Task DeleteAppointment_OK() { // ARRANGE DateTime startTime = DateTime.Now, endTime = startTime.AddSeconds(1); string summary = "Test summary..", location = "Test location...."; _optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); var oldAppointment = InsertTestAppointment(startTime: startTime, endTime: endTime, location: location, summary: summary); //Assert that the appointment record was created Assert.NotNull(oldAppointment); // ACT using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { var appointmentService = Setup(dbContext); await appointmentService.DeleteAppointment(oldAppointment.ID); } // ASSERT using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { if (dbContext.Appointments.Find(oldAppointment.ID) is Appointment appointment) { Assert.Null(appointment); } } }
public async Task UpdateAppointment_OK() { // ARRANGE DateTime startTime = DateTime.Now, endTime = startTime.AddSeconds(1); string summary = "Test summary..", location = "Test location...."; _optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); var oldAppointment = InsertTestAppointment(startTime: startTime, endTime: endTime, location: location, summary: summary); // ACT Appointment updatedAppointment; using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { var appointmentService = Setup(dbContext); updatedAppointment = await appointmentService.UpdateAppointment(new Appointment() { ID = oldAppointment.ID, EndDate = endTime, // don't use oldAppointment.endDate to ensure no insert performed. StartDate = startTime, // don't use oldAppointment.startDate to ensure no insert performed. Location = "LocationUpdated", Summary = "SumamryUpdated" }); } // ASSERT Assert.Equal(oldAppointment.ID, updatedAppointment.ID); Assert.Equal(oldAppointment.StartDate, updatedAppointment.StartDate); Assert.Equal(oldAppointment.EndDate, updatedAppointment.EndDate); Assert.NotEqual(oldAppointment.Location, updatedAppointment.Location); Assert.NotEqual(oldAppointment.Summary, updatedAppointment.Summary); }
public async Task GetAppointment_OK() { // ARRANGE DateTime startTime = DateTime.Now, endTime = startTime.AddSeconds(1); string summary = "Test summary..", location = "Test location...."; _optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); var appointmentId = AddTestAppointment(startTime: startTime, endTime: endTime, location: location, summary: summary); // ACT Appointment response; using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { var appointmentService = Setup(dbContext); response = await appointmentService.GetAppointment(appointmentId); } // ASSERT Assert.NotNull(response); Assert.NotEqual(0, response.ID); Assert.Equal(startTime, response.StartDate); Assert.Equal(endTime, response.EndDate); Assert.Equal(summary, response.Summary); Assert.Equal(location, response.Location); }
private Appointment InsertTestAppointment(DateTime startTime, DateTime endTime, string location, string summary) { Appointment appointment; using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { appointment = dbContext.Appointments.Add(new Appointment() { StartDate = startTime, EndDate = endTime, Location = location, Summary = summary }).Entity; dbContext.SaveChangesAsync(); } return(appointment); }
public async Task AddAppointment_OK() { // ARRANGE DateTime startTime = DateTime.Now, endTime = startTime.AddSeconds(1); string summary = "Test summary..", location = "Test location...."; _optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); // ACT int appointmentId; using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { var appointmentService = Setup(dbContext); appointmentId = await appointmentService.AddAppointment(new Appointment() { StartDate = startTime, EndDate = endTime, Location = location, Summary = summary }); } // ASSERT Assert.NotEqual(-99, appointmentId); using (var dbContext = new AppointmentsDBContext(_optionsBuilder.Options)) { if (dbContext.Appointments.Find(appointmentId) is Appointment appointment) { Assert.Equal(startTime, appointment.StartDate); Assert.Equal(endTime, appointment.EndDate); Assert.Equal(summary, appointment.Summary); Assert.Equal(location, appointment.Location); } } }
public AppointmentService(AppointmentsDBContext dbContext) { _dbContext = dbContext; }