public async Task <ExerciseTestRunInfoDto> GetUserTestRunInfoForExercise(int exerciseId, int userId, DateTime?dateUtc) { //TODO: write unit test var testRunInfo = new ExerciseTestRunInfoDto(); var testRuns = await _testRunRepository.GetUserTestRunsForExercise(exerciseId, userId, dateUtc); if (testRuns.Any()) { var firstTestRun = testRuns.First(); var lastTestRun = testRuns.Last(); testRunInfo.FirstRunDateTime = firstTestRun.CreateDateTime; testRunInfo.LastRunDateTime = lastTestRun.CreateDateTime; testRunInfo.SourceCode = lastTestRun.SourceCode; testRunInfo.NumberOfRuns = testRuns.Count; } return(testRunInfo); }
public void GetExerciseResultsForUserShouldReturnExerciseDetailsIfParamatersAreValid() { //Arrange var existingExercise = new ExerciseBuilder().Build(); var userId = _random.NextPositive(); _exerciseRepositoryMock.Setup(repo => repo.GetSingleWithTestsAndCourseAsync(It.IsAny <int>())) .ReturnsAsync(existingExercise); var returnedExerciseResultDto = new AssignmentResultDto(); _exerciseServiceMock.Setup(service => service.GetResultsForUserAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <DateTime?>())) .ReturnsAsync(returnedExerciseResultDto); var returnedTestRunInfo = new ExerciseTestRunInfoDto(); _exerciseServiceMock .Setup(service => service.GetUserTestRunInfoForExercise(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <DateTime?>())) .ReturnsAsync(returnedTestRunInfo); var returnedModel = new ExerciseDetailModel(); _exerciseConverterMock .Setup(converter => converter.ToExerciseDetailModel(It.IsAny <Exercise>(), It.IsAny <AssignmentResultDto>(), It.IsAny <ExerciseTestRunInfoDto>())) .Returns(returnedModel); _controller.ControllerContext = new ControllerContextBuilder().WithUser(userId.ToString()).WithRole(Role.Constants.Student).Build(); //Act var actionResult = _controller.GetExerciseResultsForUser(existingExercise.Id, userId, null).Result as OkObjectResult; //Assert Assert.That(actionResult, Is.Not.Null); Assert.That(actionResult.Value, Is.EqualTo(returnedModel)); _exerciseRepositoryMock.Verify(repo => repo.GetSingleWithTestsAndCourseAsync(existingExercise.Id), Times.Once); _exerciseServiceMock.Verify(service => service.GetResultsForUserAsync(existingExercise.Id, userId, null), Times.Once); _exerciseConverterMock.Verify(converter => converter.ToExerciseDetailModel(existingExercise, returnedExerciseResultDto, returnedTestRunInfo), Times.Once); }
public void ToExerciseDetailModel_ShouldCorrectlyConvertTestRunInfoFields() { //Arrange var chapter = new ChapterBuilder().WithCourse().Build(); var exercise = new ExerciseBuilder().WithChapter(chapter).Build(); var utcNow = DateTime.UtcNow; var testRunInfo = new ExerciseTestRunInfoDto { FirstRunDateTime = utcNow.AddDays(-1), LastRunDateTime = utcNow, NumberOfRuns = _random.NextPositive() }; //Act var model = _converter.ToExerciseDetailModel(exercise, new AssignmentResultDto(), testRunInfo); //Assert Assert.That(model, Is.Not.Null); Assert.That(model.FirstRun, Is.EqualTo(testRunInfo.FirstRunDateTime)); Assert.That(model.LastRun, Is.EqualTo(testRunInfo.LastRunDateTime)); Assert.That(model.NumberOfRuns, Is.EqualTo(testRunInfo.NumberOfRuns)); }
public ExerciseDetailModel ToExerciseDetailModel(Exercise exercise, AssignmentResultDto results, ExerciseTestRunInfoDto testRunInfo) { if (exercise.Chapter?.Course == null) { throw new ArgumentException("Exercise should have chapter and course loaded", nameof(exercise)); } if (exercise.Tests == null) { throw new ArgumentException("Exercise should have tests loaded", nameof(exercise)); } if (testRunInfo == null) { throw new ArgumentNullException(nameof(testRunInfo)); } var model = new ExerciseDetailModel { ChapterNumber = exercise.Chapter.Number, Code = exercise.Code, ExerciseId = exercise.Id, CourseName = exercise.Chapter.Course.Name, CourseId = exercise.Chapter.CourseId, TestResults = new List <TestResultModel>(), FirstRun = testRunInfo.FirstRunDateTime, LastRun = testRunInfo.LastRunDateTime, NumberOfRuns = testRunInfo.NumberOfRuns, SourceCode = testRunInfo.SourceCode }; foreach (var test in exercise.Tests) { var testResultModel = new TestResultModel { TestName = test.TestName, Runned = false, Passed = false, Message = string.Empty }; var matchingResult = results?.TestResults?.FirstOrDefault(r => r.TestId == test.Id); if (matchingResult != null) { testResultModel.Runned = true; testResultModel.Passed = matchingResult.Passed; testResultModel.Message = matchingResult.Message; } model.TestResults.Add(testResultModel); } return(model); }