public void ShouldGetAll() { // Arrange var options = new DbContextOptionsBuilder <DeskBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldGetAll") .Options; var storedList = new List <Desk> { new Desk(), new Desk(), new Desk() }; using (var context = new DeskBookerContext(options)) { foreach (var desk in storedList) { context.Add(desk); context.SaveChanges(); } } // Act List <Desk> actualList; using (var context = new DeskBookerContext(options)) { var repository = new DeskRepository(context); actualList = repository.GetAll().ToList(); } // Assert Assert.Equal(storedList.Count(), actualList.Count()); }
private static void EnsureDatabaseExists(SqliteConnection connection) { var builder = new DbContextOptionsBuilder <DeskBookerContext>(); builder.UseSqlite(connection); using var context = new DeskBookerContext(builder.Options); context.Database.EnsureCreated(); }
public void ShouldReturnTheAvailableDesks() { // Arrange var date = new DateTime(2020, 1, 25); var options = new DbContextOptionsBuilder <DeskBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldReturnTheAvailableDesks") .Options; using (var context = new DeskBookerContext(options)) { context.Desk.Add(new Desk { Id = 1 }); context.Desk.Add(new Desk { Id = 2 }); context.Desk.Add(new Desk { Id = 3 }); context.DeskBooking.Add(new DeskBooking { DeskId = 1, Date = date }); context.DeskBooking.Add(new DeskBooking { DeskId = 2, Date = date.AddDays(1) }); context.SaveChanges(); } using (var context = new DeskBookerContext(options)) { var repository = new DeskRepository(context); // Act var desks = repository.GetAvailableDesks(date); // Assert Assert.Equal(2, desks.Count()); Assert.Contains(desks, d => d.Id == 2); Assert.Contains(desks, d => d.Id == 3); Assert.DoesNotContain(desks, d => d.Id == 1); } }
public void ShouldReturnTheAvailableDesks() { var date = new DateTime(2021, 5, 5); var options = new DbContextOptionsBuilder <DeskBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldReturnTheAvailableDesks") .Options; using (var context = new DeskBookerContext(options)) { context.Desk.Add(new Desk { Id = 1 }); context.Desk.Add(new Desk { Id = 2 }); context.Desk.Add(new Desk { Id = 3 }); context.DeskBookings.Add(new DeskBooking() { DeskId = 1, Date = date }); context.DeskBookings.Add(new DeskBooking() { DeskId = 2, Date = date.AddDays(1) }); context.SaveChanges(); } using (var context = new DeskBookerContext(options)) { var repository = new DeskRepository(context); var desks = repository.GetAvailableDesks(date); Assert.That(desks, Has.Count.EqualTo(2)); } }
public void ShouldGetAllOrderedByDate() { // Arrange var options = new DbContextOptionsBuilder <DeskBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldGetAllOrderedByDate") .Options; var storedList = new List <DeskBooking> { CreateDeskBooking(1, new DateTime(2020, 1, 27)), CreateDeskBooking(2, new DateTime(2020, 1, 25)), CreateDeskBooking(3, new DateTime(2020, 1, 29)) }; var expectedList = storedList .OrderBy(x => x.Date) .ToList(); using (var context = new DeskBookerContext(options)) { foreach (var deskBooking in storedList) { context.Add(deskBooking); } context.SaveChanges(); } // Act List <DeskBooking> actualList; using (var context = new DeskBookerContext(options)) { var repository = new DeskBookingRepository(context); actualList = repository.GetAll().ToList(); } // Assert CollectionAssert.AreEqual(expectedList, actualList, new DeskBookingEqualityComparer()); }
public void ShouldSaveTheDeskBooking() { //arrange var options = new DbContextOptionsBuilder <DeskBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldSaveTheDeskBooking") .Options; var deskBooking = new DeskBooking { FirstName = "John", LastName = "Doe", Date = new DateTime(2021, 5, 5), Email = "*****@*****.**", DeskId = 1 }; //act using (var context = new DeskBookerContext(options)) { var repository = new DeskBookingRepository(context); repository.Save(deskBooking); } //assert using (var context = new DeskBookerContext(options)) { var bookings = context.DeskBookings.ToList(); var storedDeskBooking = bookings.First(); Assert.That(bookings, Has.Count.EqualTo(1)); Assert.That(deskBooking.FirstName, Is.EqualTo(storedDeskBooking.FirstName)); Assert.That(deskBooking.LastName, Is.EqualTo(storedDeskBooking.LastName)); Assert.That(deskBooking.Email, Is.EqualTo(storedDeskBooking.Email)); Assert.That(deskBooking.DeskId, Is.EqualTo(storedDeskBooking.DeskId)); Assert.That(deskBooking.Date, Is.EqualTo(storedDeskBooking.Date)); } }
public void ShouldSaveTheDeskBooking() { // Arrange var options = new DbContextOptionsBuilder <DeskBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldSaveTheDeskBooking") .Options; var deskBooking = new DeskBooking { FirstName = "Thomas", LastName = "Huber", Date = new DateTime(2020, 1, 25), Email = "*****@*****.**", DeskId = 1 }; // Act using (var context = new DeskBookerContext(options)) { var repository = new DeskBookingRepository(context); repository.Save(deskBooking); } // Assert using (var context = new DeskBookerContext(options)) { var bookings = context.DeskBooking.ToList(); Assert.AreEqual(1, bookings.Count); var storedDeskBooking = bookings.Single(); Assert.AreEqual(deskBooking.FirstName, storedDeskBooking.FirstName); Assert.AreEqual(deskBooking.LastName, storedDeskBooking.LastName); Assert.AreEqual(deskBooking.Email, storedDeskBooking.Email); Assert.AreEqual(deskBooking.DeskId, storedDeskBooking.DeskId); Assert.AreEqual(deskBooking.Date, storedDeskBooking.Date); } }
public void ShouldSaveTheDeskBooking() { // Arrange var options = new DbContextOptionsBuilder <DeskBookerContext>() .UseInMemoryDatabase(databaseName: "ShouldSaveTheDeskBooking") .Options; var deskBooking = new DeskBooking { FirstName = "Fabio", LastName = "Oquendo", Date = new DateTime(2021, 4, 10), Email = "*****@*****.**", DeskId = 1 }; // Act using (var context = new DeskBookerContext(options)) { var repository = new DeskBookingRepository(context); repository.Save(deskBooking); } // Assert using (var context = new DeskBookerContext(options)) { var bookings = context.DeskBooking.ToList(); var storedDeskBooking = Assert.Single(bookings); Assert.Equal(deskBooking.FirstName, storedDeskBooking.FirstName); Assert.Equal(deskBooking.LastName, storedDeskBooking.LastName); Assert.Equal(deskBooking.Email, storedDeskBooking.Email); Assert.Equal(deskBooking.DeskId, storedDeskBooking.DeskId); Assert.Equal(deskBooking.Date, storedDeskBooking.Date); } }
public DeskBookingRepository(DeskBookerContext context) { _context = context; }
public DeskBookingRepository(DeskBookerContext deskBookerContext) { _deskBookerContext = deskBookerContext; }