public void RemoveById_AddANewIncidentThenRemoveIt_ReturnTrue() { //ARRANGE var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); IIncidentRepository incidentRepository = new IncidentRepository(context); IRoomRepository roomRepository = new RoomRepository(context); IFloorRepository floorRepository = new FloorRepository(context); IComponentTypeRepository componentTypeRepository = new ComponentTypeRepository(context); IIssueRepository issueRepository = new IssueRepository(context); //Room(and it's floor) var floor = new FloorTO { Number = 2 }; var addedFloor1 = floorRepository.Add(floor); context.SaveChanges(); RoomTO room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = addedFloor1 }; var addedRoom = roomRepository.Add(room); context.SaveChanges(); //Component var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1En", "Name1Fr", "Name1Nl") }; var addedComponentType = componentTypeRepository.Add(componentType); context.SaveChanges(); //Issue var issue = new IssueTO { Description = "prout", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = addedComponentType }; var addedIssue = issueRepository.Add(issue); context.SaveChanges(); //Incident var incident = new IncidentTO { Description = "No coffee", Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, Room = addedRoom }; var addedIncident = incidentRepository.Add(incident); context.SaveChanges(); //ACT var result = incidentRepository.Remove(addedIncident.Id); context.SaveChanges(); //ASSERT Assert.IsTrue(result); }
public IncidentTO Update(IncidentTO Entity) { if (Entity is null) { throw new ArgumentNullException(nameof(Entity)); } if (Entity.Id <= 0) { throw new ArgumentException("The Incident's ID is not in the correct format !"); } if (!facilityContext.Incidents.Any(x => x.Id == Entity.Id)) { throw new KeyNotFoundException("No incident found !"); } var attachedIncident = facilityContext.Incidents .Include(i => i.Issue) .ThenInclude(i => i.ComponentType) .Include(i => i.Room) .ThenInclude(r => r.Floor) .FirstOrDefault(x => x.Id == Entity.Id); if (attachedIncident != null) { attachedIncident.UpdateFromDetached(Entity.ToEF()); } var tracking = facilityContext.Incidents.Update(attachedIncident); tracking.State = EntityState.Detached; return(tracking.Entity.ToTransfertObject()); }
public bool CreateIncident(IncidentTO incidentTO) { if (incidentTO is null) { throw new ArgumentNullException(nameof(incidentTO)); } if (incidentTO.Id != 0) { throw new LoggedException("The incident ID has to be 0 when adding a new incident."); } // Todo : add incidentTO.Verify() or something similar if (incidentTO.Issue is null) { throw new LoggedException("The incident has to reference an existing issue."); } if (incidentTO.Room is null) { throw new LoggedException("The incident has to reference an existing room."); } if (incidentTO.Status != IncidentStatus.Waiting) { throw new LoggedException("The incident status has to be IncidentStatus.Waiting when creating a new incident."); } // Todo check unique constraints, check if room + componenttype exists, etc. var incident = unitOfWork.IncidentRepository.Add(incidentTO); return(incident.Id != 0); }
public void CreateIncident_InvalidStatus_ThrowsException() { // Arrange var floor = new FloorTO { Number = 2 }; var room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = floor }; var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = componentType }; var incident = new IncidentTO { Description = "This thing is broken !", Room = room, Issue = issue, Status = IncidentStatus.Accepted, SubmitDate = DateTime.Now, UserId = 1, }; var mockUnitOfWork = new Mock <IFSUnitOfWork>(); mockUnitOfWork.Setup(uow => uow.IncidentRepository.Add(It.IsAny <IncidentTO>())); var sut = new FSAttendeeRole(mockUnitOfWork.Object); // Act & Assert Assert.ThrowsException <LoggedException>(() => sut.CreateIncident(incident)); mockUnitOfWork.Verify(u => u.CommentRepository.Add(It.IsAny <CommentTO>()), Times.Never); }
public static Incident ToDomain(this IncidentTO IncidentTO) { return(new Incident { Id = IncidentTO.Id, Component = IncidentTO.Component.ToDomain(), Issue = IncidentTO.Issue.ToDomain(), Comment = IncidentTO.Comment, Status = IncidentTO.Status, SubmitDate = IncidentTO.SubmitDate }); }
public List <IncidentTO> GetTestsListOfIncidents() { //Floor var floor1 = new FloorTO { Number = 2 }; var floor2 = new FloorTO { Number = -1 }; //Room RoomTO room1 = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = floor1 }; RoomTO room2 = new RoomTO { Name = new MultiLanguageString("Room2", "Room2", "Room2"), Floor = floor1 }; RoomTO room3 = new RoomTO { Name = new MultiLanguageString("Room3", "Room3", "Room3"), Floor = floor2 }; //Component var componentType1 = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1En", "Name1Fr", "Name1Nl") }; var componentType2 = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name2En", "Name2Fr", "Name2Nl") }; //Issue var issue1 = new IssueTO { Description = "Plus de café", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = componentType1 }; var issue2 = new IssueTO { Description = "Fuite d'eau", Name = new MultiLanguageString("Issue2EN", "Issue2FR", "Issue2NL"), ComponentType = componentType2 }; var issue3 = new IssueTO { Description = "Plus de PQ", Name = new MultiLanguageString("Issue3EN", "Issue3FR", "Issue3NL"), ComponentType = componentType2 }; //Incidents var incident1 = new IncidentTO { Room = room1, Issue = issue1, Status = IncidentStatus.Accepted }; var incident2 = new IncidentTO { Room = room2, Issue = issue2, Status = IncidentStatus.Resolved }; var incident3 = new IncidentTO { Room = room3, Issue = issue2, Status = IncidentStatus.Accepted }; return(new List <IncidentTO> { incident1, incident2, incident3 }); }
public IncidentTO Add(IncidentTO Entity) { if (Entity is null) { throw new ArgumentNullException(nameof(Entity)); } var incident = Entity.ToEF(); incident.Issue = facilityContext.Issues.First(x => x.Id == Entity.Issue.Id && x.Archived != true); incident.Room = facilityContext.Rooms.First(x => x.Id == Entity.Room.Id && x.Archived != true); return(facilityContext.Incidents.Add(incident).Entity.ToTransfertObject()); }
public void CreateIncident_IncidentIdNotZero_ThrowsException() { // Arrange var incident = new IncidentTO { Id = 1 }; var mockUnitOfWork = new Mock <IFSUnitOfWork>(); mockUnitOfWork.Setup(uow => uow.IncidentRepository.Add(It.IsAny <IncidentTO>())); var sut = new FSAttendeeRole(mockUnitOfWork.Object); // Act & Assert Assert.ThrowsException <LoggedException>(() => sut.CreateIncident(incident)); mockUnitOfWork.Verify(u => u.CommentRepository.Add(It.IsAny <CommentTO>()), Times.Never); }
public void Update_ThrowException_WhenUnexistingIncidentIsSupplied() { //ARRANGE var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); IIncidentRepository incidentRepository = new IncidentRepository(context); var incident = new IncidentTO { Id = 999 }; //ACT & ASSERT Assert.ThrowsException <KeyNotFoundException>(() => incidentRepository.Update(incident)); }
public static IncidentEF ToEF(this IncidentTO Incident) { if (Incident is null) { throw new ArgumentNullException(nameof(Incident)); } return(new IncidentEF { Id = Incident.Id, Component = Incident.Component.ToEF(), Issue = Incident.Issue.ToEF(), Comment = Incident.Comment, Status = Incident.Status, SubmitDate = Incident.SubmitDate }); }
public bool Remove(IncidentTO entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } if (!facilityContext.Incidents.Any(x => x.Id == entity.Id)) { throw new KeyNotFoundException("No incident found !"); } var entityEF = facilityContext.Incidents.Find(entity.Id); var tracking = facilityContext.Incidents.Remove(entityEF); return(tracking.State == EntityState.Deleted); }
public static Incident ToDomain(this IncidentTO IncidentTO) { if (IncidentTO is null) { throw new NullIncidentException(nameof(IncidentTO)); } return(new Incident { Id = IncidentTO.Id, Issue = IncidentTO.Issue.ToDomain(), Status = IncidentTO.Status, SubmitDate = IncidentTO.SubmitDate, UserId = IncidentTO.UserId, Description = IncidentTO.Description, Room = IncidentTO.Room.ToDomain(), }); }
public static IncidentEF ToEF(this IncidentTO Incident) { if (Incident is null) { throw new ArgumentNullException(nameof(Incident)); } return(new IncidentEF { Id = Incident.Id, Issue = Incident.Issue.ToEF(), Status = Incident.Status, SubmitDate = Incident.SubmitDate, Description = Incident.Description, UserId = Incident.UserId, Room = Incident.Room.ToEF() }); }
public void AddComment_ValidComment_ReturnsCommentNotNull() { // Arrange var floor = new FloorTO { Number = 2 }; var room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = floor }; var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = componentType }; var incident = new IncidentTO { Description = "This thing is broken !", Room = room, Issue = issue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, }; var comment = new CommentTO { Incident = incident, Message = "I got in touch with the right people, it'll get fixed soon!", SubmitDate = DateTime.Now, UserId = 1 }; var mockUnitOfWork = new Mock <IFSUnitOfWork>(); mockUnitOfWork.Setup(uow => uow.CommentRepository.Add(It.IsAny <CommentTO>())).Returns(comment); var sut = new FSAssistantRole(mockUnitOfWork.Object); // Act var addedComment = sut.AddComment(comment); // Assert Assert.IsNotNull(addedComment); mockUnitOfWork.Verify(u => u.CommentRepository.Add(It.IsAny <CommentTO>()), Times.Once); }
public void Update_ThrowException_WhenInvalidIdIsSupplied() { //ARRANGE var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); IIncidentRepository incidentRepository = new IncidentRepository(context); var incident1 = new IncidentTO { Id = 0 }; var incident2 = new IncidentTO { Id = -1 }; //ACT & ASSERT Assert.ThrowsException <ArgumentException>(() => incidentRepository.Update(incident1)); Assert.ThrowsException <ArgumentException>(() => incidentRepository.Update(incident2)); }
public void CreateIncident_ValidIncident_ReturnsIncidentNotNull() { // Arrange var floor = new FloorTO { Number = 2 }; var room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = floor }; var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = componentType }; var incident = new IncidentTO { Description = "This thing is broken !", Room = room, Issue = issue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, }; var mockUnitOfWork = new Mock <IFSUnitOfWork>(); mockUnitOfWork.Setup(uow => uow.IncidentRepository.Add(It.IsAny <IncidentTO>())).Returns((IncidentTO incident) => { incident.Id = 1; return(incident); }); var sut = new FSAttendeeRole(mockUnitOfWork.Object); // Act var result = sut.CreateIncident(incident); // Assert Assert.IsTrue(result); mockUnitOfWork.Verify(u => u.IncidentRepository.Add(It.IsAny <IncidentTO>()), Times.Once); }
public void CreateIncident_IncompleteIncident_ThrowsException() { // Arrange var incident1 = new IncidentTO(); // Missing Issue & Room var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = componentType }; var incident2 = new IncidentTO { Issue = issue }; // Missing Room var floor = new FloorTO { Number = 2 }; var room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = floor }; var incident3 = new IncidentTO { Room = room }; // Missing Issue var mockUnitOfWork = new Mock <IFSUnitOfWork>(); mockUnitOfWork.Setup(uow => uow.IncidentRepository.Add(It.IsAny <IncidentTO>())); var sut = new FSAttendeeRole(mockUnitOfWork.Object); // Act & Assert Assert.ThrowsException <LoggedException>(() => sut.CreateIncident(incident1)); Assert.ThrowsException <LoggedException>(() => sut.CreateIncident(incident2)); Assert.ThrowsException <LoggedException>(() => sut.CreateIncident(incident3)); mockUnitOfWork.Verify(u => u.CommentRepository.Add(It.IsAny <CommentTO>()), Times.Never); }
public void GetAll_AddThreeIncidents_ReturnCorrectNumberOfIncidents() { //ARRANGE var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); IIncidentRepository incidentRepository = new IncidentRepository(context); IRoomRepository roomRepository = new RoomRepository(context); IFloorRepository floorRepository = new FloorRepository(context); IComponentTypeRepository componentTypeRepository = new ComponentTypeRepository(context); IIssueRepository issueRepository = new IssueRepository(context); //Room(and it's floor) var floor = new FloorTO { Number = 2 }; var addedFloor1 = floorRepository.Add(floor); context.SaveChanges(); RoomTO room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = addedFloor1 }; var addedRoom = roomRepository.Add(room); context.SaveChanges(); //Component var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1En", "Name1Fr", "Name1Nl") }; var addedComponentType = componentTypeRepository.Add(componentType); context.SaveChanges(); //Issue var issue = new IssueTO { Description = "prout", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = addedComponentType }; var addedIssue = issueRepository.Add(issue); context.SaveChanges(); //Incidents var incident1 = new IncidentTO { Description = "No coffee", Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, Room = addedRoom }; var incident2 = new IncidentTO { Description = "Technical issue", Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 2, Room = addedRoom }; var incident3 = new IncidentTO { Description = "No sugar", Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, Room = addedRoom }; incidentRepository.Add(incident1); incidentRepository.Add(incident2); incidentRepository.Add(incident3); context.SaveChanges(); //ACT var result = incidentRepository.GetAll(); //ASSERT Assert.IsNotNull(result); Assert.AreEqual(3, result.Count()); }
public void GetAll_AddThreeComments_ReturnsCorrectNumberOfComments() { // Arrange var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); ICommentRepository commentRepository = new CommentRepository(context); IIncidentRepository incidentRepository = new IncidentRepository(context); IRoomRepository roomRepository = new RoomRepository(context); IFloorRepository floorRepository = new FloorRepository(context); IComponentTypeRepository componentTypeRepository = new ComponentTypeRepository(context); IIssueRepository issueRepository = new IssueRepository(context); var floor = new FloorTO { Number = 2 }; var addedFloor = floorRepository.Add(floor); context.SaveChanges(); RoomTO room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = addedFloor }; var addedRoom = roomRepository.Add(room); context.SaveChanges(); var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var addedComponentType = componentTypeRepository.Add(componentType); context.SaveChanges(); var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = addedComponentType }; var addedIssue = issueRepository.Add(issue); context.SaveChanges(); var incident = new IncidentTO { Description = "This thing is broken !", Room = addedRoom, Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, }; var addedIncident = incidentRepository.Add(incident); context.SaveChanges(); var comment1 = new CommentTO { Incident = addedIncident, Message = "I got in touch with the right people, it'll get fixed soon!", SubmitDate = DateTime.Now, UserId = 2 }; var comment2 = new CommentTO { Incident = addedIncident, Message = "New ETA is Monday morning.", SubmitDate = DateTime.Now.AddDays(1), UserId = 2 }; var comment3 = new CommentTO { Incident = addedIncident, Message = "Postponed to Tuesday morning.", SubmitDate = DateTime.Now.AddDays(2), UserId = 2 }; commentRepository.Add(comment1); commentRepository.Add(comment2); commentRepository.Add(comment3); context.SaveChanges(); // Act var result = commentRepository.GetAll(); // Assert Assert.IsNotNull(result); Assert.AreEqual(3, result.Count()); }
public void GetUserIncidents_AddThreeIncidentsWithSameUserId_ThenRetrieveThem_ReturnsCorrectNumberOfIncidents() { // Arrange var userId = 1; var floor = new FloorTO { Number = 2 }; var room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = floor }; var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = componentType }; var incident1 = new IncidentTO { Description = "This thing is broken !", Room = room, Issue = issue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = userId, }; var incident2 = new IncidentTO { Description = "This thing is still broken !", Room = room, Issue = issue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now.AddDays(7), UserId = userId, }; var incident3 = new IncidentTO { Description = "This hasn't been fixed yet ?!", Room = room, Issue = issue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now.AddDays(14), UserId = userId, }; var incidents = new List <IncidentTO> { incident1, incident2, incident3 }; var mockUnitOfWork = new Mock <IFSUnitOfWork>(); mockUnitOfWork.Setup(uow => uow.IncidentRepository.Add(It.IsAny <IncidentTO>())).Returns(new IncidentTO()); mockUnitOfWork.Setup(uow => uow.IncidentRepository.GetIncidentsByUserId(It.Is <int>(i => i > 0))).Returns(incidents); var sut = new FSAttendeeRole(mockUnitOfWork.Object); // Act sut.CreateIncident(incident1); sut.CreateIncident(incident2); sut.CreateIncident(incident3); var userIncidents = sut.GetUserIncidents(userId); // Assert mockUnitOfWork.Verify(u => u.IncidentRepository.Add(It.IsAny <IncidentTO>()), Times.Exactly(3)); Assert.AreEqual(3, userIncidents.Count()); }
public void UpdateComment_AddNewCommentThenUpdateIt_ReturnsUpdatedComment() { // Arrange var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); ICommentRepository commentRepository = new CommentRepository(context); IIncidentRepository incidentRepository = new IncidentRepository(context); IRoomRepository roomRepository = new RoomRepository(context); IFloorRepository floorRepository = new FloorRepository(context); IComponentTypeRepository componentTypeRepository = new ComponentTypeRepository(context); IIssueRepository issueRepository = new IssueRepository(context); var floor = new FloorTO { Number = 2 }; var addedFloor = floorRepository.Add(floor); context.SaveChanges(); RoomTO room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = addedFloor }; var addedRoom = roomRepository.Add(room); context.SaveChanges(); var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var addedComponentType = componentTypeRepository.Add(componentType); context.SaveChanges(); var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = addedComponentType }; var addedIssue = issueRepository.Add(issue); context.SaveChanges(); var incident = new IncidentTO { Description = "This thing is broken !", Room = addedRoom, Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, }; var addedIncident = incidentRepository.Add(incident); context.SaveChanges(); var comment = new CommentTO { Incident = addedIncident, Message = "I got in touch with the right people, it'll get fixed soon!", SubmitDate = DateTime.Now, UserId = 1 }; var commentAdded = commentRepository.Add(comment); context.SaveChanges(); // Act var later = DateTime.Now.AddHours(2); commentAdded.Message = "Updated message"; commentAdded.SubmitDate = later; var result = commentRepository.Update(commentAdded); context.SaveChanges(); // Assert Assert.IsNotNull(result); Assert.AreEqual("Updated message", result.Message); Assert.AreEqual(later, result.SubmitDate); }
public IncidentTO Update(IncidentTO Entity) { throw new System.NotImplementedException(); }
public bool Remove(IncidentTO entity) { throw new System.NotImplementedException(); }
public bool CreateIncident(IncidentTO incidentTO, int attendeeId) { return(false); }
public void RemoveCommentById_AddNewCommentThenRemoveIt_ReturnsTrue() { // Arrange var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); ICommentRepository commentRepository = new CommentRepository(context); IIncidentRepository incidentRepository = new IncidentRepository(context); IRoomRepository roomRepository = new RoomRepository(context); IFloorRepository floorRepository = new FloorRepository(context); IComponentTypeRepository componentTypeRepository = new ComponentTypeRepository(context); IIssueRepository issueRepository = new IssueRepository(context); var floor = new FloorTO { Number = 2 }; var addedFloor = floorRepository.Add(floor); context.SaveChanges(); RoomTO room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = addedFloor }; var addedRoom = roomRepository.Add(room); context.SaveChanges(); var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var addedComponentType = componentTypeRepository.Add(componentType); context.SaveChanges(); var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = addedComponentType }; var addedIssue = issueRepository.Add(issue); context.SaveChanges(); var incident = new IncidentTO { Description = "This thing is broken !", Room = addedRoom, Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, }; var addedIncident = incidentRepository.Add(incident); context.SaveChanges(); var comment = new CommentTO { Incident = addedIncident, Message = "I got in touch with the right people, it'll get fixed soon!", SubmitDate = DateTime.Now, UserId = 1 }; var addedComment = commentRepository.Add(comment); context.SaveChanges(); // Act var result = commentRepository.Remove(addedComment.Id); context.SaveChanges(); // Assert Assert.IsTrue(result); Assert.ThrowsException <KeyNotFoundException>(() => commentRepository.GetById(addedComment.Id)); }
public void GetCommentsByIncidentId_AddMultipleComments_ReturnRelevantComments() { // Arrange var options = new DbContextOptionsBuilder <FacilityContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new FacilityContext(options); ICommentRepository commentRepository = new CommentRepository(context); IIncidentRepository incidentRepository = new IncidentRepository(context); IRoomRepository roomRepository = new RoomRepository(context); IFloorRepository floorRepository = new FloorRepository(context); IComponentTypeRepository componentTypeRepository = new ComponentTypeRepository(context); IIssueRepository issueRepository = new IssueRepository(context); var floor = new FloorTO { Number = 2 }; var addedFloor = floorRepository.Add(floor); context.SaveChanges(); RoomTO room = new RoomTO { Name = new MultiLanguageString("Room1", "Room1", "Room1"), Floor = addedFloor }; var addedRoom = roomRepository.Add(room); context.SaveChanges(); var componentType = new ComponentTypeTO { Archived = false, Name = new MultiLanguageString("Name1EN", "Name1FR", "Name1NL") }; var addedComponentType = componentTypeRepository.Add(componentType); context.SaveChanges(); var issue = new IssueTO { Description = "Broken thing", Name = new MultiLanguageString("Issue1EN", "Issue1FR", "Issue1NL"), ComponentType = addedComponentType }; var addedIssue = issueRepository.Add(issue); context.SaveChanges(); var incident1 = new IncidentTO { Description = "This thing is broken!", Room = addedRoom, Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now, UserId = 1, }; var incident2 = new IncidentTO { Description = "This thing is still broken after a week!", Room = addedRoom, Issue = addedIssue, Status = IncidentStatus.Waiting, SubmitDate = DateTime.Now.AddDays(7), UserId = 1, }; var addedIncident1 = incidentRepository.Add(incident1); var addedIncident2 = incidentRepository.Add(incident2); context.SaveChanges(); var comment1 = new CommentTO { Incident = addedIncident1, Message = "I got in touch with the right people, it'll get fixed soon!", SubmitDate = DateTime.Now, UserId = 2 }; var comment2 = new CommentTO { Incident = addedIncident1, Message = "New ETA is Monday morning.", SubmitDate = DateTime.Now.AddDays(1), UserId = 2 }; var comment3 = new CommentTO { Incident = addedIncident2, Message = "It should be fixed very soon, sorry for the inconvenience!", SubmitDate = DateTime.Now.AddDays(8), UserId = 2 }; commentRepository.Add(comment1); commentRepository.Add(comment2); commentRepository.Add(comment3); context.SaveChanges(); // Act var result1 = commentRepository.GetCommentsByIncident(addedIncident1.Id); var result2 = commentRepository.GetCommentsByIncident(addedIncident2.Id); // Assert Assert.IsNotNull(result1); Assert.IsNotNull(result2); Assert.AreEqual(2, result1.Count); Assert.AreEqual(1, result2.Count); }