public void ShouldGetAll() { // Arrange var options = new DbContextOptionsBuilder <VetBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldGetAll") .Options; var storedList = new List <Vet> { new Vet(), new Vet(), new Vet() }; using (var context = new VetBookerContext(options)) { foreach (var vet in storedList) { context.Add(vet); context.SaveChanges(); } } // Act List <Vet> actualList; using (var context = new VetBookerContext(options)) { var repository = new VetRepository(context); actualList = repository.GetAll().ToList(); } // Assert Assert.Equal(storedList.Count(), actualList.Count()); }
private static void EnsureDatabaseExists(SqliteConnection connection) { var builder = new DbContextOptionsBuilder <VetBookerContext>(); builder.UseSqlite(connection); using var context = new VetBookerContext(builder.Options); context.Database.EnsureCreated(); }
public void ShouldReturnTheAvailableVets() { // Arrange var date = new DateTime(2020, 1, 25); var options = new DbContextOptionsBuilder <VetBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldReturnTheAvailableVets") .Options; using (var context = new VetBookerContext(options)) { context.Vet.Add(new Vet { Id = 1 }); context.Vet.Add(new Vet { Id = 2 }); context.Vet.Add(new Vet { Id = 3 }); context.VetBooking.Add(new VetBooking { VetId = 1, Date = date }); context.VetBooking.Add(new VetBooking { VetId = 2, Date = date.AddDays(1) }); context.SaveChanges(); } using (var context = new VetBookerContext(options)) { var repository = new VetRepository(context); // Act var vets = repository.GetAvailableVets(date); // Assert Assert.Equal(2, vets.Count()); Assert.Contains(vets, d => d.Id == 2); Assert.Contains(vets, d => d.Id == 3); Assert.DoesNotContain(vets, d => d.Id == 1); } }
public void ShouldSaveTheVetBooking() { // Arrange var options = new DbContextOptionsBuilder <VetBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldSaveTheVetBooking") .Options; var vetBooking = new VetBooking { FirstName = "Paul", LastName = "Simons", Date = new DateTime(2020, 1, 25), Email = "*****@*****.**", Pet = "Katinas", VetId = 1 }; // Act using (var context = new VetBookerContext(options)) { var repository = new VetBookingRepository(context); repository.Save(vetBooking); } // Assert using (var context = new VetBookerContext(options)) { var bookings = context.VetBooking.ToList(); var storedVetBooking = Assert.Single(bookings); Assert.Equal(vetBooking.FirstName, storedVetBooking.FirstName); Assert.Equal(vetBooking.LastName, storedVetBooking.LastName); Assert.Equal(vetBooking.Email, storedVetBooking.Email); Assert.Equal(vetBooking.Pet, storedVetBooking.Pet); Assert.Equal(vetBooking.VetId, storedVetBooking.VetId); Assert.Equal(vetBooking.Date, storedVetBooking.Date); } }
public void ShouldGetAllOrderedByDate() { // Arrange var options = new DbContextOptionsBuilder <VetBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldGetAllOrderedByDate") .Options; var storedList = new List <VetBooking> { CreateVetBooking(1, new DateTime(2020, 1, 27)), CreateVetBooking(2, new DateTime(2020, 1, 25)), CreateVetBooking(3, new DateTime(2020, 1, 29)) }; var expectedList = storedList.OrderBy(x => x.Date).ToList(); using (var context = new VetBookerContext(options)) { foreach (var vetBooking in storedList) { context.Add(vetBooking); context.SaveChanges(); } } // Act List <VetBooking> actualList; using (var context = new VetBookerContext(options)) { var repository = new VetBookingRepository(context); actualList = repository.GetAll().ToList(); } // Assert Assert.Equal(expectedList, actualList, new VetBookingEqualityComparer()); }
public VetRepository(VetBookerContext context) { _context = context; }