public async Task ShouldAssignServicesToRoomAndRemoveCurrentConnections() { //Arrange var context = new iReceptionDbContext(DbOptions); var repository = new RoomToServiceRepository(context); var existingServices = new RoomToService() { RoomId = 1, ServiceId = 21 }; await context.RoomToServices.AddAsync(existingServices); await context.SaveChangesAsync(); int roomId = 1; int[] serviceIds = { 2, 3, 4 }; //Act var actual = await repository.AssignAsync(roomId, serviceIds); var expected = serviceIds; var deletedRecord = await context.RoomToServices.FindAsync(1, 21); //Assert actual.Should().BeEquivalentTo(expected); deletedRecord.Should().BeNull(); }
public async Task <int[]> AssignAsync(int roomId, int[] serviceIds) { var currentRelations = _db.RoomToServices.Where(rts => rts.RoomId == roomId); _db.RemoveRange(currentRelations); foreach (var serviceId in serviceIds) { var relation = new RoomToService { RoomId = roomId, ServiceId = serviceId }; await _db.RoomToServices.AddAsync(relation); } await _db.SaveChangesAsync(); return(serviceIds); }
public async Task ShouldClearExistingConnections() { //Arrange var context = new iReceptionDbContext(DbOptions); var repository = new RoomToServiceRepository(context); var existingServices = new RoomToService() { RoomId = 1, ServiceId = 21 }; await context.RoomToServices.AddAsync(existingServices); await context.SaveChangesAsync(); //Act await repository.DeleteAsync(1); var actual = context.RoomToServices.ToList(); //Assert actual.Should().BeEmpty(); }