public async Task <CodeResponse> ExecuteCodeSnippetAsync(int attendeeId, bool runOnlyDefault, TestAnswerAC testAnswer) { var allTestCasePassed = true; var countPassedTest = 0; var errorEncounter = false; var errorMessage = ""; var score = 0d; var code = testAnswer.Code; var codeResponse = new CodeResponse(); var testCases = new List <CodeSnippetQuestionTestCases>(); var results = new List <Result>(); var testCaseResults = new List <TestCaseResult>(); var testCaseChecks = await _dbContext.CodeSnippetQuestion.SingleOrDefaultAsync(x => x.Id == testAnswer.QuestionId); var completeTestCases = await _dbContext.CodeSnippetQuestionTestCases.Where(x => x.CodeSnippetQuestionId == testAnswer.QuestionId).ToListAsync(); //Filter Test Cases testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Default).ToList()); if (testCaseChecks.RunBasicTestCase && !runOnlyDefault) { testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Basic).ToList()); } if (testCaseChecks.RunCornerTestCase && !runOnlyDefault) { testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Corner).ToList()); } if (testCaseChecks.RunNecessaryTestCase && !runOnlyDefault) { testCases.AddRange(completeTestCases.Where(x => x.TestCaseType == TestCaseType.Necessary).ToList()); } //End of filter foreach (var testCase in testCases) { code.Input = testCase.TestCaseInput; var result = await ExecuteCodeAsync(code); if (result.ExitCode != 0) { errorEncounter = true; errorMessage = result.Output; } //Trim newline character result.Output = result.Output.TrimEnd(new char[] { '\r', '\n' }); if (result.Output != testCase.TestCaseOutput) { allTestCasePassed = false; } else { countPassedTest++; //Calculate score score += testCase.TestCaseMarks; } var testCaseResult = new TestCaseResult() { Memory = result.MemoryConsumed, Output = result.Output, CodeSnippetQuestionTestCasesId = testCase.Id, Processing = result.RunTime }; testCaseResults.Add(testCaseResult); } //Add score to the TestCodeSolution table //Final score is calculated using the following formula: //Total score of this question = Sum(1st test case marks + 2nd test case marks .....) / Sum of total marks of all test cases var totalTestCaseScore = testCases.Sum(x => x.TestCaseMarks); var codeSolution = new TestCodeSolution() { TestAttendeeId = attendeeId, QuestionId = testAnswer.QuestionId, Solution = testAnswer.Code.Source, Language = testAnswer.Code.Language, Score = score / totalTestCaseScore }; await _dbContext.TestCodeSolution.AddAsync(codeSolution); await _dbContext.SaveChangesAsync(); //Add result to TestCaseResult Table foreach (var testCaseResult in testCaseResults) { testCaseResult.TestCodeSolution = codeSolution; } await _dbContext.TestCaseResult.AddRangeAsync(testCaseResults); await _dbContext.SaveChangesAsync(); codeResponse.ErrorOccurred = false; if (allTestCasePassed && !errorEncounter) { codeResponse.Message = runOnlyDefault ? _stringConstants.DefaultTestCasePassed : _stringConstants.AllTestCasePassed; } else if (!errorEncounter) { if (countPassedTest > 0) { codeResponse.Message = runOnlyDefault ? _stringConstants.DefaultTestCaseFailed : _stringConstants.SomeTestCasePassed; } else { codeResponse.Message = runOnlyDefault ? _stringConstants.DefaultTestCaseFailed : _stringConstants.NoTestCasePassed; } } else { codeResponse.ErrorOccurred = true; codeResponse.Error = errorMessage; } //Add answer to the database testAnswer.Code.CodeResponse = codeResponse; codeResponse.TotalTestCases = testCases.Count(); codeResponse.TotalTestCasePassed = countPassedTest; await AddAnswerAsync(attendeeId, testAnswer, 0.0); return(codeResponse); }
public async Task GetTotalMarksOfCodeSnippetQuestionAsyncTest() { var test = CreateTest("Programming"); await _testRepository.CreateTestAsync(test, "5"); var testAttendee = CreateTestAttendee(test.Id); await _testConductRepository.RegisterTestAttendeesAsync(testAttendee); //create category var category = CreateCategory("programming"); await _categoryRepository.AddCategoryAsync(category); //create coding question var question = CreateCodingQuestionAc(true, category.Id, 2, QuestionType.Programming); await _questionRepository.AddCodeSnippetQuestionAsync(question, test.CreatedByUserId); var questionId = (await _trappistDbContext.Question.SingleAsync(x => x.QuestionDetail == question.Question.QuestionDetail)).Id; var answer = new TestAnswerAC() { OptionChoice = new List <int>(), QuestionId = questionId, Code = new Code() { Input = "input", Source = "source", Language = ProgrammingLanguage.C }, QuestionStatus = QuestionStatus.answered, IsAnswered = true }; var list = new List <TestCodeSolution>(); await _testConductRepository.AddAnswerAsync(testAttendee.Id, answer, 0.0); //add test code solution var codeSolution1 = new TestCodeSolution() { Id = 1, TestAttendeeId = testAttendee.Id, QuestionId = questionId, Solution = answer.Code.Source, Language = answer.Code.Language, Score = 1, }; await _trappistDbContext.TestCodeSolution.AddAsync(codeSolution1); await _trappistDbContext.SaveChangesAsync(); var codeSolution2 = new TestCodeSolution() { Id = 2, TestAttendeeId = testAttendee.Id, QuestionId = questionId, Solution = answer.Code.Source, Language = answer.Code.Language, Score = 0, }; await Task.Delay(500); await _trappistDbContext.TestCodeSolution.AddAsync(codeSolution2); await _trappistDbContext.SaveChangesAsync(); var marksScoredInCodeSnippetQuestion = await _reportRepository.GetTotalMarksOfCodeSnippetQuestionAsync(testAttendee.Id, questionId); var marksScoredWhenQuestionIdAbsent = await _reportRepository.GetTotalMarksOfCodeSnippetQuestionAsync(testAttendee.Id, 3); Assert.Equal(0, marksScoredInCodeSnippetQuestion); Assert.Equal(-1, marksScoredWhenQuestionIdAbsent); }
public async Task GetTestCodeSolutionDetailsAsyncTest() { var test = CreateTest("Coding Test"); await _testRepository.CreateTestAsync(test, "5"); var testAttendee = CreateTestAttendee(test.Id); await _testConductRepository.RegisterTestAttendeesAsync(testAttendee); //create category var category = CreateCategory("programming"); await _categoryRepository.AddCategoryAsync(category); //create coding question var question = CreateCodingQuestionAc(true, category.Id, 2, QuestionType.Programming); await _questionRepository.AddCodeSnippetQuestionAsync(question, test.CreatedByUserId); var questionId = (await _trappistDbContext.Question.SingleAsync(x => x.QuestionDetail == question.Question.QuestionDetail)).Id; var answer = new TestAnswerAC() { OptionChoice = new List <int>(), QuestionId = questionId, Code = new Code() { Input = "input", Source = "source", Language = ProgrammingLanguage.C }, QuestionStatus = QuestionStatus.answered }; await _testConductRepository.AddAnswerAsync(testAttendee.Id, answer, 0.0); //add test code solution var codeSolution1 = new TestCodeSolution() { Id = 1, TestAttendeeId = testAttendee.Id, QuestionId = questionId, Solution = answer.Code.Source, Language = answer.Code.Language, Score = 1 }; var codeSolution2 = new TestCodeSolution() { Id = 2, TestAttendeeId = testAttendee.Id, QuestionId = questionId, Solution = answer.Code.Source, Language = answer.Code.Language, Score = 0 }; await _trappistDbContext.TestCodeSolution.AddAsync(codeSolution1); await _trappistDbContext.TestCodeSolution.AddAsync(codeSolution2); await _trappistDbContext.SaveChangesAsync(); var codeSolutionObject = await _reportRepository.GetTestCodeSolutionDetailsAsync(testAttendee.Id, 3); var codeSolutionAcObject = await _reportRepository.GetTestCodeSolutionDetailsAsync(testAttendee.Id, questionId); Assert.Equal(null, codeSolutionObject); Assert.Equal(answer.Code.Language, codeSolutionAcObject.Language); Assert.Equal(answer.Code.Source, codeSolutionAcObject.CodeSolution); Assert.Equal(1, codeSolutionAcObject.NumberOfSuccessfulAttempts); Assert.Equal(2, codeSolutionAcObject.TotalNumberOfAttempts); }
public async Task GetAllAttendeeMarksDetailsAsyncTest() { //create test var createTest = await CreateTestAsync(); //create category var category = CreateCategory("History"); await _categoryRepository.AddCategoryAsync(category); //create question var question1 = CreateQuestionAc(true, "first Question", category.Id, 1, QuestionType.Multiple); var question2 = CreateCodingQuestionAc(true, category.Id, 2, QuestionType.Programming); await _questionRepository.AddSingleMultipleAnswerQuestionAsync(question1, createTest.CreatedByUserId); await _questionRepository.AddCodeSnippetQuestionAsync(question2, createTest.CreatedByUserId); var questionId1 = (await _trappistDbContext.Question.SingleAsync(x => x.QuestionDetail == question1.Question.QuestionDetail)).Id; var questionId2 = (await _trappistDbContext.Question.SingleAsync(x => x.QuestionDetail == question2.Question.QuestionDetail)).Id; //add test category var categoryList = new List <DomainModel.Models.Category.Category>(); categoryList.Add(category); var testCategoryList = new List <TestCategoryAC> { new TestCategoryAC { CategoryId = category.Id, IsSelect = true, } }; await _testRepository.AddTestCategoriesAsync(createTest.Id, testCategoryList); //add test Question var questionList = new List <TestQuestionAC> { new TestQuestionAC() { Id = question1.Question.Id, CategoryID = question1.Question.CategoryID, IsSelect = question1.Question.IsSelect }, new TestQuestionAC() { Id = question2.Question.Id, IsSelect = question2.Question.IsSelect, CategoryID = question2.Question.CategoryID } }; await _testRepository.AddTestQuestionsAsync(questionList, createTest.Id); //create test attednee var testAttendee = CreateTestAttendee(createTest.Id); await _testConductRepository.RegisterTestAttendeesAsync(testAttendee); //AddTestAnswer var answer1 = CreateAnswerAc(questionId1); await _testConductRepository.AddAnswerAsync(testAttendee.Id, answer1, 0.0); var answer2 = new TestAnswerAC() { OptionChoice = new List <int>(), QuestionId = questionId2, Code = new Code() { Input = "input", Source = "source", Language = ProgrammingLanguage.C }, QuestionStatus = QuestionStatus.answered }; await _testConductRepository.AddAnswerAsync(testAttendee.Id, answer2, 0.0); //create test conduct var testConduct1 = new DomainModel.Models.TestConduct.TestConduct() { Id = 1, QuestionId = answer1.QuestionId, QuestionStatus = answer1.QuestionStatus, TestAttendeeId = testAttendee.Id }; var testConduct2 = new DomainModel.Models.TestConduct.TestConduct() { Id = 2, QuestionId = answer2.QuestionId, QuestionStatus = answer2.QuestionStatus, TestAttendeeId = testAttendee.Id }; await _trappistDbContext.TestConduct.AddAsync(testConduct1); await _trappistDbContext.TestConduct.AddAsync(testConduct2); await _trappistDbContext.SaveChangesAsync(); AddTestAnswer(answer1, testConduct1.Id); AddTestAnswer(answer2, testConduct2.Id); //add test code solution var codeSolution1 = new TestCodeSolution() { TestAttendeeId = testAttendee.Id, QuestionId = questionId2, Solution = answer2.Code.Source, Language = answer2.Code.Language, Score = 1 }; var codeSolution2 = new TestCodeSolution() { TestAttendeeId = testAttendee.Id, QuestionId = questionId2, Solution = answer2.Code.Source, Language = answer2.Code.Language, Score = 0 }; await _trappistDbContext.TestCodeSolution.AddAsync(codeSolution1); await _trappistDbContext.TestCodeSolution.AddAsync(codeSolution2); await _trappistDbContext.SaveChangesAsync(); var allAttendeeMarksDetails = await _reportRepository.GetAllAttendeeMarksDetailsAsync(createTest.Id); var totalQuestionAttempted = allAttendeeMarksDetails.First().NoOfQuestionAttempted; var easyQuestionAttempted = allAttendeeMarksDetails.First().EasyQuestionAttempted; Assert.Equal(2, easyQuestionAttempted); Assert.Equal(2, totalQuestionAttempted); }