Example #1
0
        private AssignmentResultDto GenerateAssignmentResult(Exercise exercise,
                                                             int numberOfPassingTests,
                                                             int numberOfFailingTests)
        {
            var exerciseResult = new AssignmentResultDto
            {
                AssignmentId = exercise.Id,
                TestResults  = new List <TestResultDto>()
            };

            foreach (var test in exercise.Tests)
            {
                if (numberOfPassingTests <= 0 && numberOfFailingTests <= 0)
                {
                    continue;
                }

                var passed = numberOfPassingTests > 0;
                exerciseResult.TestResults.Add(new TestResultDto
                {
                    TestName = test.TestName,
                    Passed   = passed
                });

                if (passed)
                {
                    numberOfPassingTests--;
                }
                else
                {
                    numberOfFailingTests--;
                }
            }
            return(exerciseResult);
        }
Example #2
0
        public void GetResultsForUserAsyncShouldRetrieveAssignmentsWithLastTestsResultsOfUserAndConvertThemToAssignmentResultDtos()
        {
            //Arrange
            var chapterId              = _random.NextPositive();
            var userId                 = _random.NextPositive();
            var assignmentWithResults  = new AssignmentWithLastResultsOfUser();
            var assignmentsWithResults = new List <AssignmentWithLastResultsOfUser> {
                assignmentWithResults
            };

            _testResultRepositoryMock.Setup(repo => repo.GetLastTestResultsOfChapterAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(assignmentsWithResults);

            var assignmentResultDto = new AssignmentResultDto();

            _testResultConverterMock
            .Setup(converter => converter.ToAssignmentResultDto(It.IsAny <AssignmentWithLastResultsOfUser>()))
            .Returns(assignmentResultDto);

            //Act
            var results = _service.GetResultsForUserAsync(chapterId, userId, null).Result;

            //Assert
            Assert.That(results, Has.Exactly(1).EqualTo(assignmentResultDto));
            _testResultRepositoryMock.Verify(repo => repo.GetLastTestResultsOfChapterAsync(chapterId, userId, null), Times.Once);
            _testResultConverterMock.Verify(converter => converter.ToAssignmentResultDto(assignmentWithResults), Times.Once);
        }
Example #3
0
        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);
        }
Example #4
0
        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 AssignmentResultDto ToAssignmentResultDto(AssignmentWithLastResultsOfUser assignmentWithLastResultsOfUser)
        {
            var resultDto = new AssignmentResultDto
            {
                AssignmentId = assignmentWithLastResultsOfUser.Assignment.Id,
                TestResults  = new List <TestResultDto>()
            };

            foreach (var testWithResults in assignmentWithLastResultsOfUser.TestsWithLastResultOfUser)
            {
                var testResultDto = new TestResultDto
                {
                    TestId   = testWithResults.Test.Id,
                    TestName = testWithResults.Test.TestName,
                    Passed   = testWithResults.TestResult.Passed,
                    Message  = testWithResults.TestResult.Message
                };
                resultDto.TestResults.Add(testResultDto);
            }

            return(resultDto);
        }