public void GetUserBySession_NoUserSession() { var options = new DbContextOptionsBuilder <RegistrationContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var RSCxt = new RegistrationContext(options); IRSUserRepository userRepository = new UserRepository(RSCxt); IRSCourseRepository courseRepository = new CourseRepository(RSCxt); IRSSessionRepository sessionRepository = new SessionRepository(RSCxt); var Teacher = new UserTO() { Name = "Max", Email = "*****@*****.**", Role = UserRole.Teacher }; var Jack = new UserTO() { Name = "Jack Jack", Email = "*****@*****.**", Role = UserRole.Attendee }; var John = new UserTO() { Name = "John", Email = "*****@*****.**", Role = UserRole.Attendee }; var AddedUser0 = userRepository.Add(Teacher); var AddedUser1 = userRepository.Add(Jack); var AddedUser2 = userRepository.Add(John); RSCxt.SaveChanges(); var SQLCourse = new CourseTO() { Name = "SQL" }; var AddedCourse = courseRepository.Add(SQLCourse); RSCxt.SaveChanges(); var SQLSession = new SessionTO() { Attendees = new List <UserTO>() { }, Course = AddedCourse, Teacher = null }; var AddedSession = sessionRepository.Add(SQLSession); RSCxt.SaveChanges(); Assert.ThrowsException <NullReferenceException>(() => userRepository.GetUsersBySession(AddedSession)); }
public IEnumerable <UserTO> GetBySession(SessionTO session) { return(userContext.UserSessions .Where(x => x.SessionId == session.Id) .Select(x => x.User.ToTransfertObject()) .ToList()); }
public SessionTO Update(SessionTO session) { if (session == null) { throw new ArgumentNullException(); } if (!registrationContext.Sessions.Any(x => x.Id == session.Id)) { throw new ArgumentException("The session you are trying to update doesn't exists"); } var entity = registrationContext.Sessions.FirstOrDefault(x => x.Id == session.Id); if (entity != default) { if (registrationContext.Courses.Any(x => x.Id == session.Course.Id)) { entity.Course = registrationContext.Courses.FirstOrDefault(x => x.Id == session.Course.Id); } UpdateUserSessions(session, entity); } return(registrationContext.Sessions.Update(entity).Entity.ToTransfertObject()); }
public void GetForm_ReturnsRequestedFormTO_WhenAllValuesProvidedAreValid_FinalForm(int deltaDay1, int deltaDay2, int deltaDay3, int expectedFormId) { //ARRANGE - DATA INPUTS var TrainingDay1 = DateTime.Now.AddDays(deltaDay1); var TrainingDay2 = DateTime.Now.AddDays(deltaDay2); var TrainingDay3 = DateTime.Now.AddDays(deltaDay3); //ARRANGE - MOCKS IESUnitOfWork var mockUnitOfWork = new Mock <IESUnitOfWork>(); mockUnitOfWork.Setup(x => x.SubmissionRepository.IsAlreadySubmitted(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(false); mockUnitOfWork.Setup(x => x.FormRepository.GetById(It.IsAny <int>())) .Returns((int id) => new FormTO() { Id = id, Name = new MultiLanguageString("Form1", "Form1", "Form1") }); mockUnitOfWork.Setup(x => x.QuestionRepository.GetAllOfForm(It.IsAny <int>())) .Returns(new List <QuestionTO> { new QuestionTO() { Id = 1, Libelle = new MultiLanguageString("Q1", "Q1", "Q1"), Position = 1, Type = QuestionType.Open } }); //ARRANGE - MOCKS IRSServiceRole var mockRSServiceRole = new Mock <IRSServiceRole>(); var SessionToForRSService = new SessionTO { Id = 1, Attendees = new List <UserTO>() { new UserTO { Id = 1 } }, SessionDays = new List <SessionDayTO>() { new SessionDayTO { Id = 1, Date = TrainingDay1 }, new SessionDayTO { Id = 2, Date = TrainingDay2 }, new SessionDayTO { Id = 3, Date = TrainingDay3 } } }; mockRSServiceRole.Setup(x => x.GetSession(It.IsAny <int>())).Returns(SessionToForRSService); //ACT var attendee = new ESAttendeeRole(mockUnitOfWork.Object, mockRSServiceRole.Object); var returnedValue = attendee.GetActiveForm(1, 1); //ASSERT Assert.IsNotNull(returnedValue); Assert.AreEqual(expectedFormId, returnedValue.Id); }
private void UpdateUserSessions(SessionTO session, SessionEF entity) { entity.UserSessions.Clear(); registrationContext.RemoveRange(registrationContext.UserSessions.Where(x => x.SessionId == session.Id)); foreach (var user in session.Attendees) { var userSession = new UserSessionEF() { Session = entity, SessionId = entity.Id, User = registrationContext.Users.FirstOrDefault(x => x.Id == user.Id), UserId = user.Id }; registrationContext.UserSessions.Add(userSession); } if (session.Teacher != null) { var teacherSession = new UserSessionEF() { Session = entity, SessionId = entity.Id, User = registrationContext.Users.FirstOrDefault(x => x.Id == session.Teacher.Id), UserId = session.Teacher.Id }; entity.UserSessions.Add(teacherSession); } }
public void Should_Insert_Session_Without_Teacher_And_Users() { var options = new DbContextOptionsBuilder <RegistrationContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using (var context = new RegistrationContext(options)) { IRSSessionRepository sessionRepository = new SessionRepository(context); IRSCourseRepository courseRepository = new CourseRepository(context); var hugeCourse = new CourseTO() { Name = "Huge COurse" }; var addedCourse = courseRepository.Add(hugeCourse); var hugeSession = new SessionTO() { Course = addedCourse }; var addedSession = sessionRepository.Add(hugeSession); context.SaveChanges(); Assert.AreEqual(1, sessionRepository.GetAll().Count()); } }
public void GetForm_ThrowsSessionIsNotHeldToday_WhenValidUserValidSessionIsProvidedAndSessionDaysIsNotToday() { //ESAttendeeRole(IESUnitOfWork iESUnitOfWork, IRSServiceRole iRSServiceRole) var mockUnitOfWork = new Mock <IESUnitOfWork>(); var mockRSServiceRole = new Mock <IRSServiceRole>(); var SessionToForRSService = new SessionTO { Id = 1, Attendees = new List <UserTO>() { new UserTO { Id = 1 } }, SessionDays = new List <SessionDayTO>() { new SessionDayTO { Id = 1, Date = DateTime.Now.AddDays(-1) }, new SessionDayTO { Id = 1, Date = DateTime.Now.AddDays(+1) } } }; mockRSServiceRole.Setup(x => x.GetSession(It.IsAny <int>())).Returns(SessionToForRSService); var attendee = new ESAttendeeRole(mockUnitOfWork.Object, mockRSServiceRole.Object); Assert.ThrowsException <LoggedException>(() => attendee.GetActiveForm(1, 1)); }
public SessionTO Add(SessionTO session) { if (session is null) { throw new ArgumentNullException(nameof(session)); } if (session.Id != 0) { return(session); } if (session.Course.IsArchived) { throw new ArgumentException("Course can not be archived"); } var entity = session.ToEF(); entity.Course = registrationContext.Courses.FirstOrDefault(x => x.Id == session.Course.Id); entity.UserSessions = new List <UserSessionEF>(); entity = registrationContext.Sessions.Add(entity).Entity; UpdateUserSessions(session, entity); return(entity.ToTransfertObject()); }
public void Should_Have_One_UserSessions() { #region TOInitialization UserTO student = new UserTO() { Id = 1, Name = "Jacky Fringant", Email = "*****@*****.**", Role = UserRole.Attendee, }; UserTO teacher = new UserTO() { Id = 2, Name = "Johnny Begood", Email = "*****@*****.**", Role = UserRole.Teacher }; CourseTO sql = new CourseTO() { Id = 1, Name = "SQL" }; SessionTO sessionTO = new SessionTO() { Id = 1, Teacher = teacher, Course = sql, SessionDays = new List <SessionDayTO>() { new SessionDayTO() { Id = 1, Date = new DateTime(2020, 2, 3), PresenceType = SessionPresenceType.MorningAfternoon }, new SessionDayTO() { Id = 2, Date = new DateTime(2020, 2, 4), PresenceType = SessionPresenceType.MorningAfternoon }, new SessionDayTO() { Id = 3, Date = new DateTime(2020, 2, 5), PresenceType = SessionPresenceType.MorningAfternoon } }, Attendees = new List <UserTO>() { student, } }; #endregion TOInitialization SessionEF sessionConverted = sessionTO.ToEF(); }
public void Should_Throw_Exception_When_Course_IsArchived() { var options = new DbContextOptionsBuilder <RegistrationContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using (var context = new RegistrationContext(options)) { IRSUserRepository userRepository = new UserRepository(context); IRSSessionRepository sessionRepository = new SessionRepository(context); IRSCourseRepository courseRepository = new CourseRepository(context); var Teacher = new UserTO() { //Id = 420, Name = "Christian", Email = "*****@*****.**", Role = UserRole.Teacher }; var Michou = new UserTO() { //Id = 45, Name = "Michou Miraisin", Email = "*****@*****.**", Role = UserRole.Attendee }; var AddedTeacher = userRepository.Add(Teacher); var AddedAttendee = userRepository.Add(Michou); context.SaveChanges(); var SQLCourse = new CourseTO() { //Id = 28, Name = "SQL", IsArchived = true }; var AddedCourse = courseRepository.Add(SQLCourse); context.SaveChanges(); var SQLSession = new SessionTO() { //Id = 1, Attendees = new List <UserTO>() { AddedAttendee }, Course = AddedCourse, Teacher = AddedTeacher, }; Assert.ThrowsException <ArgumentException>(() => sessionRepository.Add(SQLSession)); } }
public static Session ToDomain(this SessionTO sessionTO) { return(new Session { ID = sessionTO.ID, Course = sessionTO.Course.ToDoamin(), Teacher = sessionTO.TeacherName.ToDomain(), Attendee = sessionTO.Attendee.ToDomain(), }); }
public static Session ToDomain(this SessionTO sessionTO) { return(new Session { Id = sessionTO.Id, Course = sessionTO.Course.ToDomain(), Teacher = sessionTO.TeacherName.ToDomain(), Attendees = sessionTO.Attendees?.Select(x => x.ToDomain()).ToList() }); }
public void AddSession_ThrowException_WhenSessionIDisDiferentThanZero() { //ARRANGE var assistant = new RSAssistantRole(new Mock <IRSUnitOfWork>().Object); var sessionToAdd = new SessionTO { Id = 1, Course = null, Teacher = null }; //ASSERT Assert.ThrowsException <Exception>(() => assistant.AddSession(sessionToAdd)); }
public bool IsInSession(UserTO user, SessionTO session) { var returnValue = false; var sessionList = GetUsersBySession(session); if (sessionList.Contains(user)) { returnValue = true; } return(returnValue); }
public void UpdateSession_ThrowException_WhenSessionIdIsZero() { //ARRANGE var sessionIdZero = new SessionTO { Id = 0, Course = null }; var assistant = new RSAssistantRole(MockUofW.Object); //ASSERT Assert.ThrowsException <Exception>(() => assistant.UpdateSession(sessionIdZero)); }
public static SessionEF ToEF(this SessionTO session) { if (session is null) { throw new ArgumentNullException(nameof(session)); } var result = new SessionEF() { Id = session.Id, Course = session.Course.ToEF(), Dates = session.SessionDays?.Select(x => x.ToEF()).ToList() }; if (session.Attendees == null) { return(result); } result.UserSessions = new List <UserSessionEF>(); foreach (var user in session.Attendees) { var userSession = new UserSessionEF() { SessionId = session.Id, Session = result, UserId = user.Id, User = user.ToEF() }; result.UserSessions.Add(userSession); } var teacherEF = new UserSessionEF() { SessionId = session.Id, Session = result, UserId = session.Teacher.Id, User = session.Teacher.ToEF() }; result.UserSessions.Add(teacherEF); //foreach (UserSessionEF item in result.UserSessions) //{ // item.User.UserSessions.Add(item); //} return(result); }
public void RemoveSession_ReturnsTrue_WhenSessionIsProvidedAndRemovedFromDB_Test() { //ARRANGE MockSessionRepository.Setup(x => x.Remove(It.IsAny <SessionTO>())); MockUofW.Setup(x => x.SessionRepository).Returns(MockSessionRepository.Object); var assistant = new RSAssistantRole(MockUofW.Object); var sessionToRemove = new SessionTO { Id = 1, Course = course, Teacher = teacher }; //ASSERT Assert.IsTrue(assistant.RemoveSession(sessionToRemove)); }
public void UpdateSession_ReturnsTrue_WhenAValidSessionIsProvidedAndUpdatedInDB() { //ARRANGE MockSessionRepository.Setup(x => x.Update(It.IsAny <SessionTO>())); MockUofW.Setup(x => x.SessionRepository).Returns(MockSessionRepository.Object); var assistant = new RSAssistantRole(MockUofW.Object); var user = new SessionTO { Id = 1, Course = course, Teacher = teacher }; //ASSERT Assert.IsTrue(assistant.UpdateSession(user)); }
public void UpdateSession_UserRepositoryIsCalledOnce_WhenAValidSessionIsProvidedAndUpdatedInDB() { //ARRANGE MockSessionRepository.Setup(x => x.Update(It.IsAny <SessionTO>())); MockUofW.Setup(x => x.SessionRepository).Returns(MockSessionRepository.Object); var ass = new RSAssistantRole(MockUofW.Object); var userToUpdate = new SessionTO { Id = 1, Course = course, Teacher = teacher }; //ACT ass.UpdateSession(userToUpdate); MockSessionRepository.Verify(x => x.Update(It.IsAny <SessionTO>()), Times.Once); }
public IEnumerable <UserTO> GetUsersBySession(SessionTO session) { if (session is null) { throw new ArgumentNullException(nameof(session)); } if ((session.Attendees == null || session.Attendees.Count() == 0) && session.Teacher == null) { throw new NullReferenceException(); } return(registrationContext.UserSessions .AsNoTracking() .Where(x => x.SessionId == session.Id) .Select(x => x.User.ToTransfertObject()) .ToList()); }
public static Session ToDomain(this SessionTO session) { if (session == null) { throw new ArgumentNullException(nameof(session)); } return(new Session { Id = session.Id, Course = session.Course.ToDomain(), Teacher = session.Teacher.ToDomain(), Attendees = session.Attendees?.Select(x => x.ToDomain()).ToList(), Dates = session.SessionDays?.Select(x => x.ToDomain()).ToList() }); }
public void AddSession_NewSession_Test() { //ARRANGE var newSession = new SessionTO { Id = 0, Course = course, Teacher = teacher, Attendees = null }; MockSessionRepository.Setup(x => x.Add(It.IsAny <SessionTO>())); var mockUofW = new Mock <IRSUnitOfWork>(); mockUofW.Setup(x => x.SessionRepository).Returns(MockSessionRepository.Object); var assistant = new RSAssistantRole(mockUofW.Object); //ASSERT Assert.IsTrue(assistant.AddSession(newSession)); }
public SessionTO Add(SessionTO Entity) { if (Entity is null) { throw new ArgumentNullException(nameof(Entity)); } if (Entity.Id != 0) { return(Entity); } var sessionEF = Entity.ToEF(); sessionEF.Course = registrationContext.Courses.First(x => x.Id == Entity.Course.Id); registrationContext.Sessions.Add(sessionEF); return(sessionEF.ToTransfertObject()); // => registrationContext.Add(Entity.ToEF()).Entity.ToTransfertObject(); }
public static SessionTO ToTransfertObject(this SessionEF session) { var sessionTO = new SessionTO() { Id = session.Id, Course = session.Course?.ToTransfertObject(), //SessionDays = session.Dates.Select(x => x.ToTransfertObject()).ToList(), }; if (session.UserSessions.Any(x => x.User.Role == UserRole.Teacher)) { sessionTO.Teacher = session.UserSessions.FirstOrDefault(x => x.User.Role == UserRole.Teacher).User.ToTransfertObject(); } if (session.UserSessions.Any(x => x.User.Role == UserRole.Attendee)) { sessionTO.Attendees = session.UserSessions.Where(x => x.User.Role == UserRole.Attendee).Select(x => x.User.ToTransfertObject()).ToList(); } return(sessionTO); }
public bool UpdateSession(SessionTO sessionTO) { if (sessionTO is null) { throw new ArgumentNullException((nameof(sessionTO))); } if (sessionTO.Id == 0) { throw new Exception("User does not exist"); } try { iRSUnitOfWork.SessionRepository.Update(sessionTO.ToDomain().ToTransfertObject()); return(true); } catch (Exception) { throw; } }
public void GetForm_ThrowExceptionFormAlreadySubmitted_WhenTodaHasAnActiveFormNotSubmmittedButSubmittedDay2Previously_day1() { //ESAttendeeRole(IESUnitOfWork iESUnitOfWork, IRSServiceRole iRSServiceRole) var mockUnitOfWork = new Mock <IESUnitOfWork>(); mockUnitOfWork.SetupSequence(x => x.SubmissionRepository.IsAlreadySubmitted(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(false) //Form 1 not submitted .Returns(true); //Form 2 submitted var mockRSServiceRole = new Mock <IRSServiceRole>(); var SessionToForRSService = new SessionTO { Id = 1, Attendees = new List <UserTO>() { new UserTO { Id = 1 } }, SessionDays = new List <SessionDayTO>() { new SessionDayTO { Id = 1, Date = DateTime.Now.AddDays(+1) }, new SessionDayTO { Id = 1, Date = DateTime.Now }, new SessionDayTO { Id = 1, Date = DateTime.Now.AddDays(+2) } } }; mockRSServiceRole.Setup(x => x.GetSession(It.IsAny <int>())).Returns(SessionToForRSService); var attendee = new ESAttendeeRole(mockUnitOfWork.Object, mockRSServiceRole.Object); Assert.ThrowsException <LoggedException>(() => attendee.GetActiveForm(1, 1)); }
public void GetForm_ThrowsSessionUserNotInSession_WhenValidUserIsProvidedButNotInSession() { //ESAttendeeRole(IESUnitOfWork iESUnitOfWork, IRSServiceRole iRSServiceRole) var mockUnitOfWork = new Mock <IESUnitOfWork>(); var mockRSServiceRole = new Mock <IRSServiceRole>(); var SessionToForRSService = new SessionTO { Id = 1, Attendees = new List <UserTO>() { new UserTO { Id = 1 } } }; mockRSServiceRole.Setup(x => x.GetSession(It.IsAny <int>())).Returns(SessionToForRSService); var attendee = new ESAttendeeRole(mockUnitOfWork.Object, mockRSServiceRole.Object); Assert.ThrowsException <LoggedException>(() => attendee.GetActiveForm(1, 4)); }
public bool AddSession(SessionTO sessionTO) { if (sessionTO is null) { throw new ArgumentNullException(nameof(sessionTO)); } if (sessionTO.Id != 0) { throw new Exception("Existing Session"); } try { iRSUnitOfWork.SessionRepository.Add(sessionTO.ToDomain().ToTransfertObject()); return(true); } catch (Exception) { throw; } }
public static SessionEF ToEF(this SessionTO session) { if (session is null) { throw new ArgumentNullException(nameof(session)); } var result = new SessionEF() { Id = session.Id, Course = session.Course.ToEF(), Dates = session.SessionDays?.Select(x => x.ToEF()).ToList() }; if (session.Attendees == null) { return(result); } result.UserSessions = new List <UserSessionEF>(); return(result); }
public void GetForm_ReturnsNULL_WhenTodayIsDoesNotHaveAnActiveForm() { //ESAttendeeRole(IESUnitOfWork iESUnitOfWork, IRSServiceRole iRSServiceRole) var mockUnitOfWork = new Mock <IESUnitOfWork>(); var mockRSServiceRole = new Mock <IRSServiceRole>(); var SessionToForRSService = new SessionTO { Id = 1, Attendees = new List <UserTO>() { new UserTO { Id = 1 } }, SessionDays = new List <SessionDayTO>() { new SessionDayTO { Id = 1, Date = DateTime.Now.AddDays(-1) }, new SessionDayTO { Id = 1, Date = DateTime.Now }, new SessionDayTO { Id = 1, Date = DateTime.Now.AddDays(+1) } } }; mockRSServiceRole.Setup(x => x.GetSession(It.IsAny <int>())).Returns(SessionToForRSService); var attendee = new ESAttendeeRole(mockUnitOfWork.Object, mockRSServiceRole.Object); var returnedValue = attendee.GetActiveForm(1, 1); Assert.IsNull(returnedValue); }