public void CreateFromTask_AsTaskIdempotent() { Task<int> source = Task.FromResult(42); ValueTask<int> t = new ValueTask<int>(source); Assert.Same(source, t.AsTask()); Assert.Same(t.AsTask(), t.AsTask()); }
public async Task ShouldThrowValidationExceptionOnRegisterWhenStudentUserIdIsInvalidAndLogItAsync( string invalidStudentUserId) { // given Student randomStudent = CreateRandomStudent(); Student invalidStudent = randomStudent; invalidStudent.UserId = invalidStudentUserId; var invalidStudentException = new InvalidStudentException( parameterName: nameof(Student.UserId), parameterValue: invalidStudent.UserId); var expectedStudentValidationException = new StudentValidationException(invalidStudentException); // when ValueTask <Student> registerStudentTask = this.studentService.RegisterStudentAsync(invalidStudent); // then await Assert.ThrowsAsync <StudentValidationException>(() => registerStudentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentAsync(It.IsAny <Student>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenIdIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Calendar randomCalendar = CreateRandomCalendar(dateTime); Calendar inputCalendar = randomCalendar; inputCalendar.Id = default; var invalidCalendarInputException = new InvalidCalendarInputException( parameterName: nameof(Calendar.Id), parameterValue: inputCalendar.Id); var expectedCalendarValidationException = new CalendarValidationException(invalidCalendarInputException); // when ValueTask <Calendar> registerCalendarTask = this.calendarService.AddCalendarAsync(inputCalendar); // then await Assert.ThrowsAsync <CalendarValidationException>(() => registerCalendarTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCalendarValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCalendarAsync(It.IsAny <Calendar>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomStudentId = Guid.NewGuid(); Guid inputStudentId = randomStudentId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedStudentException = new LockedStudentException(databaseUpdateConcurrencyException); var expectedStudentDependencyException = new StudentDependencyException(lockedStudentException); this.storageBrokerMock.Setup(broker => broker.SelectStudentByIdAsync(inputStudentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Student> deleteStudentTask = this.studentService.DeleteStudentAsync(inputStudentId); // then await Assert.ThrowsAsync <StudentDependencyException>(() => deleteStudentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentByIdAsync(inputStudentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenAttachmentIdIsInvalidAndLogItAsync() { // given TeacherAttachment randomTeacherAttachment = CreateRandomTeacherAttachment(); TeacherAttachment inputTeacherAttachment = randomTeacherAttachment; inputTeacherAttachment.AttachmentId = default; var invalidTeacherAttachmentInputException = new InvalidTeacherAttachmentException( parameterName: nameof(TeacherAttachment.AttachmentId), parameterValue: inputTeacherAttachment.AttachmentId); var expectedTeacherAttachmentValidationException = new TeacherAttachmentValidationException(invalidTeacherAttachmentInputException); // when ValueTask <TeacherAttachment> addTeacherAttachmentTask = this.teacherAttachmentService.AddTeacherAttachmentAsync(inputTeacherAttachment); // then await Assert.ThrowsAsync <TeacherAttachmentValidationException>(() => addTeacherAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertTeacherAttachmentAsync(It.IsAny <TeacherAttachment>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnDeleteWhenIdIsInvalidAndLogItAsync() { // given Guid randomTagId = default; Guid inputTagId = randomTagId; var invalidTagInputException = new InvalidTagException( parameterName: nameof(Tag.Id), parameterValue: inputTagId); var expectedTagValidationException = new TagValidationException(invalidTagInputException); // when ValueTask <Tag> deleteTagTask = this.tagService.RemoveTagByIdAsync(inputTagId); // then await Assert.ThrowsAsync <TagValidationException>(() => deleteTagTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedTagValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTagByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteTagAsync(It.IsAny <Tag>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someCourseId = Guid.NewGuid(); SqlException sqlException = GetSqlException(); var expectedCourseAttachmentDependencyException = new CourseAttachmentDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectCourseAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(sqlException); // when ValueTask <CourseAttachment> retrieveCourseAttachmentTask = this.courseAttachmentService.RetrieveCourseAttachmentByIdAsync( someCourseId, someAttachmentId); // then await Assert.ThrowsAsync <CourseAttachmentDependencyException>(() => retrieveCourseAttachmentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectCourseAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedCourseAttachmentDependencyException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); CalendarEntry randomCalendarEntry = CreateRandomCalendarEntry(dateTime); CalendarEntry inputCalendarEntry = randomCalendarEntry; inputCalendarEntry.UpdatedDate = default; var invalidCalendarEntryInputException = new InvalidCalendarEntryException( parameterName: nameof(CalendarEntry.UpdatedDate), parameterValue: inputCalendarEntry.UpdatedDate); var expectedCalendarEntryValidationException = new CalendarEntryValidationException(invalidCalendarEntryInputException); // when ValueTask <CalendarEntry> modifyCalendarEntryTask = this.calendarEntryService.ModifyCalendarEntryAsync(inputCalendarEntry); // then await Assert.ThrowsAsync <CalendarEntryValidationException>(() => modifyCalendarEntryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCalendarEntryValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnDeleteWhenIdIsInvalidAndLogItAsync() { // given Guid randomAttendanceId = default; Guid inputAttendanceId = randomAttendanceId; var invalidAttendanceException = new InvalidAttendanceException( parameterName: nameof(Attendance.Id), parameterValue: inputAttendanceId); var expectedAttendanceValidationException = new AttendanceValidationException(invalidAttendanceException); // when ValueTask <Attendance> actualAttendanceTask = this.attendanceService.DeleteAttendanceAsync(inputAttendanceId); // then await Assert.ThrowsAsync <AttendanceValidationException>(() => actualAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAttendanceValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteAttendanceAsync(It.IsAny <Attendance>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public Task <bool> MoveNextAsync() { ValueTask <T> result = _channel.ReadAsync(_cancellationToken); if (result.IsCompletedSuccessfully) { _current = result.Result; return(s_trueTask); } return(result.AsTask().ContinueWith((t, s) => { AsyncEnumerator <T> thisRef = (AsyncEnumerator <T>)s; try { thisRef._current = t.GetAwaiter().GetResult(); return true; } catch (ClosedChannelException) { return false; } }, this, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); }
public async Task ShouldThrowValidatonExceptionOnRetrieveWhenAttachmentIdIsInvalidAndLogItAsync() { // given Guid randomAttachmentId = default; Guid randomCalendarEntryId = Guid.NewGuid(); Guid inputAttachmentId = randomAttachmentId; Guid inputCalendarEntryId = randomCalendarEntryId; var invalidCalendarEntryAttachmentInputException = new InvalidCalendarEntryAttachmentException( parameterName: nameof(CalendarEntryAttachment.AttachmentId), parameterValue: inputAttachmentId); var expectedCalendarEntryAttachmentValidationException = new CalendarEntryAttachmentValidationException(invalidCalendarEntryAttachmentInputException); // when ValueTask <CalendarEntryAttachment> retrieveCalendarEntryAttachmentTask = this.calendarEntryAttachmentService.RetrieveCalendarEntryAttachmentByIdAsync( inputCalendarEntryId, inputAttachmentId); // then await Assert.ThrowsAsync <CalendarEntryAttachmentValidationException>(() => retrieveCalendarEntryAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCalendarEntryAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Comment randomComment = CreateRandomComment(dateTime); Comment inputComment = randomComment; inputComment.CreatedDate = default; var invalidCommentException = new InvalidCommentException( parameterName: nameof(Comment.CreatedDate), parameterValue: inputComment.CreatedDate); var expectedCommentValidationException = new CommentValidationException(invalidCommentException); // when ValueTask <Comment> createCommentTask = this.commentService.AddCommentAsync(inputComment); // then await Assert.ThrowsAsync <CommentValidationException>(() => createCommentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCommentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCommentAsync(It.IsAny <Comment>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogIt() { // given var exception = new Exception(); var expectedAssignmentServiceException = new AssignmentServiceException(exception); var guid = Guid.NewGuid(); this.storageBrokerMock.Setup(broker => broker.SelectAssignmentByIdAsync(guid)) .Throws(exception); // when ValueTask <Assignment> retrieveTask = this.assignmentService.RetrieveAssignmentById(guid); // then await Assert.ThrowsAsync <AssignmentServiceException>(() => retrieveTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAssignmentServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAssignmentByIdAsync(guid), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyWhenCourseNameIsInvalidAndLogItAsync( string invalidCourseName) { // given Course randomCourse = CreateRandomCourse(DateTime.Now); Course invalidCourse = randomCourse; invalidCourse.Name = invalidCourseName; var invalidCourseException = new InvalidCourseException( parameterName: nameof(Course.Name), parameterValue: invalidCourse.Name); var expectedCourseValidationException = new CourseValidationException(invalidCourseException); // when ValueTask <Course> modifyCourseTask = this.courseService.ModifyCourseAsync(invalidCourse); // then await Assert.ThrowsAsync <CourseValidationException>(() => modifyCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateCourseAsync(It.IsAny <Course>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyWhenCreatedByIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Classroom randomClassroom = CreateRandomClassroom(dateTime); Classroom inputClassroom = randomClassroom; inputClassroom.CreatedBy = default; var invalidClassroomInputException = new InvalidClassroomInputException( parameterName: nameof(Classroom.CreatedBy), parameterValue: inputClassroom.CreatedBy); var expectedClassroomValidationException = new ClassroomValidationException(invalidClassroomInputException); // when ValueTask <Classroom> modifyClassroomTask = this.classroomService.ModifyClassroomAsync(inputClassroom); // then await Assert.ThrowsAsync <ClassroomValidationException>(() => modifyClassroomTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedClassroomValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectClassroomByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowCriticalDependencyExceptionOnDeleteWhenSqlErrorOccursAndLogItAsync() { // given Guid randomClassroomId = Guid.NewGuid(); Guid inputClassroomId = randomClassroomId; SqlException sqlException = GetSqlException(); var failedClassroomStorageException = new FailedClassroomStorageException(sqlException); var expectedClassroomDependencyException = new ClassroomDependencyException(failedClassroomStorageException); this.storageBrokerMock.Setup(broker => broker.SelectClassroomByIdAsync(inputClassroomId)) .ThrowsAsync(sqlException); // when ValueTask <Classroom> deleteClassroomTask = this.classroomService.RemoveClassroomAsync(inputClassroomId); // then await Assert.ThrowsAsync <ClassroomDependencyException>(() => deleteClassroomTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs( expectedClassroomDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectClassroomByIdAsync(inputClassroomId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomAttendanceId = Guid.NewGuid(); Guid inputAttendanceId = randomAttendanceId; var serviceException = new Exception(); var failedAttendanceServiceException = new FailedAttendanceServiceException(serviceException); var expectedAttendanceServiceException = new AttendanceServiceException(failedAttendanceServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId)) .ThrowsAsync(serviceException); // when ValueTask <Attendance> deleteAttendanceTask = this.attendanceService.RemoveAttendanceByIdAsync(inputAttendanceId); // then await Assert.ThrowsAsync <AttendanceServiceException>(() => deleteAttendanceTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedAttendanceServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttendanceByIdAsync(inputAttendanceId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnDeleteWhenUserIdIsInvalidAndLogItAsync() { // given Guid randomUserId = default; Guid inputUserId = randomUserId; var invalidUserException = new InvalidUserException( parameterName: nameof(User.Id), parameterValue: inputUserId); var expectedUserValidationException = new UserValidationException(invalidUserException); // when ValueTask <User> actualUserTask = this.userService.RemoveUserByIdAsync(inputUserId); // then await Assert.ThrowsAsync <UserValidationException>(() => actualUserTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedUserValidationException))), Times.Once); this.userManagementBrokerMock.Verify(broker => broker.SelectUserByIdAsync(It.IsAny <Guid>()), Times.Never); this.userManagementBrokerMock.Verify(broker => broker.DeleteUserAsync(It.IsAny <User>()), Times.Never); this.userManagementBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnRemoveIfIdIsInvalidAndLogItAsync() { // given Guid invalidTaskCategotyId = Guid.Empty; var invalidRegistrationException = new InvalidRegistrationException( parameterName: nameof(Registration.Id), parameterValue: invalidTaskCategotyId); var expectedRegistrationValidationException = new RegistrationValidationException(invalidRegistrationException); // when ValueTask <Registration> deleteRegistrationTask = this.registrationService.RemoveRegistrationByIdAsync(invalidTaskCategotyId); // then await Assert.ThrowsAsync <RegistrationValidationException>(() => deleteRegistrationTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedRegistrationValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteRegistrationAsync(It.IsAny <Registration>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync() { // given Guid randomStudentGuardianId = Guid.NewGuid(); Guid randomStudentId = Guid.NewGuid(); Guid inputStudentGuardianId = randomStudentGuardianId; Guid inputStudentId = randomStudentId; var exception = new Exception(); var expectedStudentGuardianException = new StudentGuardianServiceException(exception); this.storageBrokerMock.Setup(broker => broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId)) .ThrowsAsync(exception); // when ValueTask <StudentGuardian> deleteStudentGuardianTask = this.studentGuardianService.DeleteStudentGuardianAsync(inputStudentGuardianId, inputStudentId); // then await Assert.ThrowsAsync <StudentGuardianServiceException>(() => deleteStudentGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentGuardianException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentGuardianByIdAsync(inputStudentGuardianId, inputStudentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid someAttachmentId = Guid.NewGuid(); Guid someTeacherId = Guid.NewGuid(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedAttachmentException = new LockedTeacherAttachmentException(databaseUpdateConcurrencyException); var expectedTeacherAttachmentException = new TeacherAttachmentDependencyException(lockedAttachmentException); this.storageBrokerMock.Setup(broker => broker.SelectTeacherAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <TeacherAttachment> retrieveTeacherAttachmentTask = this.teacherAttachmentService.RetrieveTeacherAttachmentByIdAsync(someTeacherId, someAttachmentId); // then await Assert.ThrowsAsync <TeacherAttachmentDependencyException>(() => retrieveTeacherAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedTeacherAttachmentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTeacherAttachmentByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnDeleteWhenSqlExceptionOccursAndLogItAsync() { // given Guid randomSemesterCourseId = Guid.NewGuid(); Guid inputSemesterCourseId = randomSemesterCourseId; Guid randomStudentId = Guid.NewGuid(); Guid inputStudentId = randomStudentId; SqlException sqlException = GetSqlException(); var expectedStudentSemesterCourseDependencyException = new StudentSemesterCourseDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectStudentSemesterCourseByIdAsync(inputSemesterCourseId, inputStudentId)) .ThrowsAsync(sqlException); // when ValueTask <StudentSemesterCourse> deleteStudentSemesterCourseTask = this.studentSemesterCourseService.RemoveStudentSemesterCourseByIdsAsync(inputSemesterCourseId, inputStudentId); // then await Assert.ThrowsAsync <StudentSemesterCourseDependencyException>(() => deleteStudentSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedStudentSemesterCourseDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(inputSemesterCourseId, inputStudentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid someRegistrationId = Guid.NewGuid(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedRegistrationException = new LockedRegistrationException(databaseUpdateConcurrencyException); var expectedRegistrationDependencyException = new RegistrationDependencyException(lockedRegistrationException); this.storageBrokerMock.Setup(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Registration> retrieveByIdRegistrationTask = this.registrationService.RetrieveRegistrationByIdAsync(someRegistrationId); // then await Assert.ThrowsAsync <RegistrationDependencyException>(() => retrieveByIdRegistrationTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedRegistrationDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateWhenIdIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Attachment randomAttachment = CreateRandomAttachment(dateTime); Attachment inputAttachment = randomAttachment; inputAttachment.Id = default; var invalidAttachmentInputException = new InvalidAttachmentException( parameterName: nameof(Attachment.Id), parameterValue: inputAttachment.Id); var expectedAttachmentValidationException = new AttachmentValidationException(invalidAttachmentInputException); // when ValueTask <Attachment> registerAttachmentTask = this.attachmentService.AddAttachmentAsync(inputAttachment); // then await Assert.ThrowsAsync <AttachmentValidationException>(() => registerAttachmentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAttachmentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAttachmentByIdAsync(It.IsAny <Guid>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentExam randomStudentExam = CreateRandomStudentExam(dateTime); StudentExam inputStudentExam = randomStudentExam; inputStudentExam.UpdatedDate = default; var invalidStudentExamInputException = new InvalidStudentExamInputException( parameterName: nameof(StudentExam.UpdatedDate), parameterValue: inputStudentExam.UpdatedDate); var expectedStudentExamValidationException = new StudentExamValidationException(invalidStudentExamInputException); // when ValueTask <StudentExam> modifyStudentExamTask = this.studentExamService.ModifyStudentExamAsync(inputStudentExam); // then await Assert.ThrowsAsync <StudentExamValidationException>(() => modifyStudentExamTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentExamValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentExamByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnRegisterWhenUpdatedByIsNotSameToCreatedByAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Student randomStudent = CreateRandomStudent(dateTime); Student inputStudent = randomStudent; inputStudent.UpdatedBy = Guid.NewGuid(); var invalidStudentInputException = new InvalidStudentException( parameterName: nameof(Student.UpdatedBy), parameterValue: inputStudent.UpdatedBy); var expectedStudentValidationException = new StudentValidationException(invalidStudentInputException); // when ValueTask <Student> registerStudentTask = this.studentService.RegisterStudentAsync(inputStudent); // then await Assert.ThrowsAsync <StudentValidationException>(() => registerStudentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentAsync(It.IsAny <Student>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnRetrieveWhenIdIsInvalidAndLogItAsync() { //given Guid randomAssignmentId = default; Guid inputAssignmentId = randomAssignmentId; var invalidAssignmentInputException = new InvalidAssignmentException( parameterName: nameof(Assignment.Id), parameterValue: inputAssignmentId); var expectedAssignmentValidationException = new AssignmentValidationException(invalidAssignmentInputException); //when ValueTask <Assignment> retrieveAssignmentByIdTask = this.assignmentService.RetrieveAssignmentByIdAsync(inputAssignmentId); //then await Assert.ThrowsAsync <AssignmentValidationException>(() => retrieveAssignmentByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedAssignmentValidationException))), Times.Once ); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.Verify(broker => broker.SelectAssignmentByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyWhenCreatedByIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian inputGuardian = randomGuardian; inputGuardian.CreatedBy = default; var invalidGuardianInputException = new InvalidGuardianException( parameterName: nameof(Guardian.CreatedBy), parameterValue: inputGuardian.CreatedBy); var expectedGuardianValidationException = new GuardianValidationException(invalidGuardianInputException); // when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(inputGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => modifyGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateWhenUpdatedByIsNotSameToCreatedByAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Teacher randomTeacher = CreateRandomTeacher(dateTime); Teacher inputTeacher = randomTeacher; inputTeacher.UpdatedBy = Guid.NewGuid(); var invalidTeacherInputException = new InvalidTeacherInputException( parameterName: nameof(Teacher.UpdatedBy), parameterValue: inputTeacher.UpdatedBy); var expectedTeacherValidationException = new TeacherValidationException(invalidTeacherInputException); // when ValueTask <Teacher> createTeacherTask = this.teacherService.CreateTeacherAsync(inputTeacher); // then await Assert.ThrowsAsync <TeacherValidationException>(() => createTeacherTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedTeacherValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTeacherByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync() { // given Guid someContactId = Guid.NewGuid(); Guid someTeacherId = Guid.NewGuid(); var serviceException = new Exception(); var failedTeacherContactException = new FailedTeacherContactServiceException(serviceException); var expectedTeacherContactException = new TeacherContactServiceException(failedTeacherContactException); this.storageBrokerMock.Setup(broker => broker.SelectTeacherContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(serviceException); // when ValueTask <TeacherContact> retrieveTeacherContactTask = this.teacherContactService.RetrieveTeacherContactByIdAsync(someTeacherId, someContactId); // then await Assert.ThrowsAsync <TeacherContactServiceException>(() => retrieveTeacherContactTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedTeacherContactException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectTeacherContactByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentSemesterCourse randomStudentSemesterCourse = CreateRandomStudentSemesterCourse(dateTime); StudentSemesterCourse inputStudentSemesterCourse = randomStudentSemesterCourse; inputStudentSemesterCourse.CreatedDate = default; var invalidStudentSemesterCourseInputException = new InvalidStudentSemesterCourseInputException( parameterName: nameof(StudentSemesterCourse.CreatedDate), parameterValue: inputStudentSemesterCourse.CreatedDate); var expectedStudentSemesterCourseValidationException = new StudentSemesterCourseValidationException(invalidStudentSemesterCourseInputException); // when ValueTask <StudentSemesterCourse> createStudentSemesterCourseTask = this.studentSemesterCourseService.CreateStudentSemesterCourseAsync(inputStudentSemesterCourse); // then await Assert.ThrowsAsync <StudentSemesterCourseValidationException>(() => createStudentSemesterCourseTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentSemesterCourseValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentSemesterCourseByIdAsync(It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void CreateFromValue_AsTaskNotIdempotent() { ValueTask<int> t = new ValueTask<int>(42); Assert.NotSame(Task.FromResult(42), t.AsTask()); Assert.NotSame(t.AsTask(), t.AsTask()); }
public void GetHashCode_ContainsTask() { ValueTask<string> t = new ValueTask<string>(Task.FromResult("42")); Assert.Equal(t.AsTask().GetHashCode(), t.GetHashCode()); }