public override async Task UpdateWithWrongIdShouldReturnExceptionAsync() { int id = 0; int wronId = 10; Room roomSaved = null; // arrange (use the context directly - we assume that works) var options = new DbContextOptionsBuilder <Project15Context>() .UseInMemoryDatabase("db_rooms_test_update").Options; // act (for act, only use the repo, to test it) using (var db = new Project15Context(options)); // assert (for assert, once again use the context directly for verify.) using (var db = new Project15Context(options)) { var repo = new RoomRepository(db); Room room = new Room { Cost = 500, RoomType = "Standard" }; roomSaved = await repo.CreateAsync(room); await repo.SaveChangesAsync(); roomSaved = await repo.CreateAsync(room); await repo.SaveChangesAsync(); id = roomSaved.Id; } using (var db = new Project15Context(options)) { var repo = new RoomRepository(db); Room room = await repo.GetByIdAsync(id); Assert.NotEqual(0, room.Id); Assert.Equal(500, room.Cost); Assert.Equal("Standard", room.RoomType); await Assert.ThrowsAsync <ArgumentException>(() => repo.UpdateAsync(room, wronId)); } }
public override async Task CreateWorksAsync() { Customer customerSaved = null; Room roomSaved = null; Reservation reservationSaved = null; var options = new DbContextOptionsBuilder <Project15Context>() .UseInMemoryDatabase("db_reservation_test_create").Options; using (var db = new Project15Context(options)); // act (for act, only use the repo, to test it) using (var db = new Project15Context(options)) { var repo = new ReservationRepository(db); var customerRepo = new CustomerRepository(db); var roomRepo = new RoomRepository(db); //Create customer Customer customer = new Customer { Name = "Axel", Address1 = "111 Address" }; customer = await customerRepo.CreateAsync(customer); await customerRepo.SaveChangesAsync(); // create room Room room = new Room { Beds = 1, Cost = 50, RoomType = "Standard" }; room = await roomRepo.CreateAsync(room); await roomRepo.SaveChangesAsync(); Reservation eventCustomer = new Reservation { CustomerId = customer.Id, RoomId = room.Id, Paid = true }; reservationSaved = await repo.CreateAsync(eventCustomer); await repo.SaveChangesAsync(); customerSaved = customer; roomSaved = room; } // assert (for assert, once again use the context directly for verify.) using (var db = new Project15Context(options)) { var repo = new ReservationRepository(db); Reservation reservation = AutoMapper.Mapper.Map <Reservations, Reservation>(db.Reservation.First(m => m.Paid == true)); Assert.Equal(customerSaved.Id, reservation.CustomerId); Assert.Equal(roomSaved.Id, reservation.RoomId); Assert.True(reservation.Paid); Assert.NotEqual(0, reservation.Id); } }
public override async Task DeleteWorksAsync() { Room roomSaved = null; int id = 0; // arrange (use the context directly - we assume that works) var options = new DbContextOptionsBuilder <Project15Context>() .UseInMemoryDatabase("db_room_test_delete").Options; using (var db = new Project15Context(options)); // act (for act, only use the repo, to test it) using (var db = new Project15Context(options)) { var repo = new RoomRepository(db); //Create customer Room room = new Room { Beds = 1, Cost = 50, RoomType = "Standard" }; roomSaved = await repo.CreateAsync(room); await repo.SaveChangesAsync(); id = roomSaved.Id; } // assert (for assert, once again use the context directly for verify.) using (var db = new Project15Context(options)) { var repo = new RoomRepository(db); Room room = await repo.GetByIdAsync(id); Assert.NotEqual(0, room.Id); // should get some generated ID Assert.Equal(roomSaved.Id, room.Id); Assert.Equal(1, room.Beds); Assert.Equal(50, room.Cost); Assert.Equal("Standard", room.RoomType); await repo.DeleteAsync(id); await repo.SaveChangesAsync(); room = await repo.GetByIdAsync(id); Assert.Null(room); } }
public override async Task GetAllWorksAsync() { List <Room> list = new List <Room>(); Room roomSaved = null; // arrange (use the context directly - we assume that works) var options = new DbContextOptionsBuilder <Project15Context>() .UseInMemoryDatabase("db_room_test_getAll").Options; // act (for act, only use the repo, to test it) using (var db = new Project15Context(options)) { var repo = new RoomRepository(db); for (int i = 0; i < 10; i++) { Room room = new Room { Cost = 150, Beds = 3, RoomType = $"Room {i}" }; roomSaved = await repo.CreateAsync(room); await repo.SaveChangesAsync(); list.Add(roomSaved); } } // asssert (for assert, once again use the context directly for verification.) using (var db = new Project15Context(options)) { var repo = new RoomRepository(db); List <Room> rooms = (List <Room>) await repo.GetAllAsync(); // should equal the same amount of rooms Assert.Equal(list.Count, rooms.Count); for (int i = 0; i < list.Count; i++) { Assert.Equal(list[i].Cost, rooms[i].Cost); Assert.Equal(list[i].Beds, rooms[i].Beds); Assert.Equal(list[i].RoomType, rooms[i].RoomType); } } }
public async Task <IActionResult> Post([FromBody] RoomViewModel roomModel) { if (roomModel == null) { return(StatusCode(500, new InternalServerError("Room model is null"))); } roomModel.Name = roomModel.Name.Trim(); if (roomModel.Name == string.Empty) { return(StatusCode(500, new InternalServerError("Room name cannot be empty"))); } var existRooms = await _roomRepository.GetAsync(p => p.Name == roomModel.Name); if (existRooms.Any()) { var existRoom = existRooms.FirstOrDefault(); var participants = await _participantRepository.GetAsync(p => p.RoomId == existRoom.Id); if (!participants.Any() && existRoom.FirstConnectionExpired < DateTime.Now) { await _roomRepository.RemoveAsync(existRoom); } else { return(StatusCode(500, new InternalServerError("A room with the same name already exists"))); } } var room = new Room { Name = roomModel.Name, ListenSald = Guid.NewGuid().ToString(), PlayingSald = Guid.NewGuid().ToString(), FirstConnectionExpired = DateTime.Now.AddMinutes(1), OwnerGuid = Guid.NewGuid().ToString() }; await _roomRepository.CreateAsync(room); return(Ok(room.Adapt <RoomViewModel>())); }
public override async Task UpdateWorksAsync() { int id = 0; Reservation reservationSaved = null; Customer customerSaved = null; Room roomSaved = null; var options = new DbContextOptionsBuilder <Project15Context>() .UseInMemoryDatabase("db_reservation_test_getById").Options; using (var db = new Project15Context(options)); // act (for act, only use the repo, to test it) using (var db = new Project15Context(options)) { var repo = new ReservationRepository(db); var customerRepo = new CustomerRepository(db); var roomRepo = new RoomRepository(db); //Create customer Customer customer = new Customer { Name = "Axel", Address1 = "111 Address" }; customer = await customerRepo.CreateAsync(customer); await customerRepo.SaveChangesAsync(); // Create room Room room = new Room { Beds = 1, Cost = 50, RoomType = "Standard" }; room = await roomRepo.CreateAsync(room); await roomRepo.SaveChangesAsync(); // Create reservation Reservation eventCustomer = new Reservation { CustomerId = customer.Id, RoomId = room.Id, Paid = true }; reservationSaved = await repo.CreateAsync(eventCustomer); await repo.SaveChangesAsync(); customerSaved = customer; roomSaved = room; id = reservationSaved.Id; } using (var db = new Project15Context(options)) { var repo = new ReservationRepository(db); Reservation reservation = await repo.GetByIdAsync(id); Assert.NotEqual(0, reservation.Id); Assert.Equal(customerSaved.Id, reservation.CustomerId); Assert.Equal(roomSaved.Id, reservation.RoomId); Assert.True(reservation.Paid); reservation.Paid = false; await repo.UpdateAsync(reservation, id); Assert.False(reservation.Paid); } }
public override async Task GetAllWorksAsync() { List <Reservation> reservationList = new List <Reservation>(); Reservation reservationSaved = null; Customer customerSaved = null; Room roomSaved = null; var options = new DbContextOptionsBuilder <Project15Context>() .UseInMemoryDatabase("db_reservation_test_getall").Options; using (var db = new Project15Context(options)); using (var db = new Project15Context(options)) { var repo = new ReservationRepository(db); var customerRepo = new CustomerRepository(db); var roomRepo = new RoomRepository(db); //Create customer Customer customer = new Customer { Name = "Axel", Address1 = "111 Address" }; customer = await customerRepo.CreateAsync(customer); await customerRepo.SaveChangesAsync(); // Create room Room room = new Room { Beds = 1, Cost = 50, RoomType = "Standard" }; room = await roomRepo.CreateAsync(room); await roomRepo.SaveChangesAsync(); for (int i = 0; i < 5; i++) { Reservation reservation = new Reservation { CustomerId = customer.Id, RoomId = room.Id, }; reservationSaved = await repo.CreateAsync(reservation); await repo.SaveChangesAsync(); reservationList.Add(reservationSaved); } roomSaved = room; customerSaved = customer; } using (var db = new Project15Context(options)) { var repo = new ReservationRepository(db); List <Reservation> list = (List <Reservation>) await repo.GetAllAsync(); Assert.Equal(reservationList.Count, list.Count); for (int i = 0; i < 5; i++) { Assert.Equal(reservationList[i].CustomerId, list[i].CustomerId); Assert.Equal(reservationList[i].RoomId, list[i].RoomId); Assert.NotEqual(0, list[i].Id); } } }