コード例 #1
0
 private FindChoiceAnswerCandidateDto TransformeModelToChoiceDtoCandidate(ChoiceAnswer choice)
 {
     return(new FindChoiceAnswerCandidateDto(
                choice.IdChoiceAnswer,
                choice.TextAnswer
                ));
 }
コード例 #2
0
        public Answer MapToAnswer(Dtos.Wizard.IQuestion dto)
        {
            Answer answer;

            if (dto is Dtos.Wizard.IQuestionChoice choiceAnswer)
            {
                var choices = choiceAnswer.Choices
                              .Select(x => new Choice {
                    Content = x.Content, Valid = x.Valid
                })
                              .ToList();

                answer = new ChoiceAnswer(choices,
                                          choiceAnswer.ChoiceAnswerType,
                                          choiceAnswer.Score,
                                          choiceAnswer.AllValidChoicesRequired);
            }
            else if (dto is Dtos.Wizard.IQuestionWritten writtenAnswer)
            {
                answer = new WrittenAnswer(writtenAnswer.Answer, writtenAnswer.Score);
            }
            else
            {
                throw new Exception("Cannot convert into valid Answer");
            }

            return(answer);
        }
コード例 #3
0
        public async Task <IActionResult> CreateChoiceAnswer([Bind("ChoiceAnswerId,Answer,IsRight,QuestionId,CreationDate")] ChoiceAnswer choiceAnswer)
        {
            choiceAnswer.Question = await _context.Questions.Where(q => q.QuestionId == choiceAnswer.QuestionId).FirstOrDefaultAsync();

            if (ModelState.IsValid)
            {
                _context.Add(choiceAnswer);
                await _context.SaveChangesAsync();
            }

            var model = new CreateAnswerViewModel
            {
                IsChoiceAnswer   = true,
                Question         = choiceAnswer.Question,
                ListChoiceAnswer = _context.ChoiceAnswers.Where(a => a.QuestionId == choiceAnswer.QuestionId).ToList()
            };

            // For single choice, check if one answer is already True
            // TODO :: Replace Name by a key value in app config
            var query = from answer in _context.ChoiceAnswers
                        join question in _context.Questions
                        on answer.QuestionId equals question.QuestionId
                        join type in _context.TypeQuestions
                        on question.TypeQuestionId equals type.TypeQuestionId
                        where type.Name == "Unique" && question.QuestionId == choiceAnswer.QuestionId && answer.IsRight
                        select answer;

            ViewData["ChoiceAnswersIsRight"] = query.Count() > 0 ? true : false;

            // Return to the add answer
            return(View("CreateAnswer", model));
        }
コード例 #4
0
ファイル: AnswerTests.cs プロジェクト: Krztk/KtTest
        public void ChoiceAnswer_NotAllValidChoicesRequired_ReturnValidScore(float maxScore, float expectedScore, int numericAnswer)
        {
            int questionId = 7;
            var choices    = new List <Choice>
            {
                new Choice {
                    Content = "1", Valid = true
                },
                new Choice {
                    Content = "2", Valid = false
                },
                new Choice {
                    Content = "3", Valid = false
                },
                new Choice {
                    Content = "3", Valid = true
                }
            };

            Answer answer = new ChoiceAnswer(questionId, choices, ChoiceAnswerType.MultipleChoice, maxScore, false);

            var score = answer.GetScore(new ChoiceUserAnswer(numericAnswer, 1, questionId, 1));

            score.Should().Be(expectedScore);
        }
コード例 #5
0
ファイル: ChoiceAnswerTests.cs プロジェクト: Krztk/KtTest
        public void Constructor_ValidData_CreatesObjectWithCalculatedNumericValue(List <Choice> choices, ChoiceAnswerType choiceAnswerType, int expectedNumericValue, float maxScore)
        {
            //act
            ChoiceAnswer choiceAnswer = new ChoiceAnswer(choices, choiceAnswerType, maxScore);

            //assert
            choiceAnswer.NumericValue.Should().Be(expectedNumericValue);
        }
コード例 #6
0
        private static Question GetQuestion(ChoiceAnswerType choiceAnswerType, List <Choice> choices, bool allRequired = true)
        {
            int questionId = 5;
            var authorId   = 1;
            var score      = 1f;
            var answer     = new ChoiceAnswer(choices, choiceAnswerType, score, allRequired);
            var question   = new Question(questionId, "Question content", answer, authorId);

            return(question);
        }
コード例 #7
0
        public async Task <IActionResult> CreateChoice([Bind("ChoiceAnswerId,Answer,IsRight,QuestionId,CreationDate")] ChoiceAnswer choiceAnswer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(choiceAnswer);
                await _context.SaveChangesAsync();

                // return RedirectToAction(nameof(Crea), "Questions", new { id = choiceAnswer.QuestionId });
            }

            // Return to the add answer
            return(RedirectToAction("CreateAnswer", "Answers", choiceAnswer));
        }
コード例 #8
0
ファイル: QuestionReader.cs プロジェクト: Krztk/KtTest
        public async Task <OperationResult <Paginated <QuestionHeaderDto> > > GetQuestionHeaders(int authorId, int offset, int limit)
        {
            var questionTests = await dbContext
                                .Questions
                                .FromSqlInterpolated($"SELECT Id, Content from Questions WHERE AuthorId = {authorId} ORDER BY Id DESC OFFSET {offset} ROWS FETCH NEXT {limit + 1} ROWS ONLY")
                                .Include(x => x.Answer)
                                .GroupJoin(dbContext.TestItems,
                                           x => x.Id,
                                           y => y.QuestionId,
                                           (x, y) => new { x.Id, x.Content, x.Answer, TestItem = y })
                                .SelectMany(x => x.TestItem.DefaultIfEmpty(),
                                            (x, y) => new { x.Id, x.Content, x.Answer, TestItem = y })
                                .ToArrayAsync();

            var idHeaderDtos = new Dictionary <int, QuestionHeaderDto>();

            foreach (var question in questionTests)
            {
                if (idHeaderDtos.ContainsKey(question.Id))
                {
                    if (question.TestItem != null)
                    {
                        idHeaderDtos[question.Id].NumberOfTimesUsedInTests++;
                    }
                }
                else
                {
                    var questionHeaderDto = new QuestionHeaderDto
                    {
                        Id      = question.Id,
                        Content = question.Content,
                        NumberOfTimesUsedInTests = question.TestItem == null ? 0 : 1,
                        Type = question.Answer switch
                        {
                            WrittenAnswer writtenAnswer => "Written",
                            ChoiceAnswer choiceAnswer => choiceAnswer.ChoiceAnswerType == ChoiceAnswerType.SingleChoice
                                ? "Single choice"
                                : "Multiple choice",
                        _ => throw new NotImplementedException()
                        }
                    };
                    idHeaderDtos.Add(question.Id, questionHeaderDto);
                }
            }

            return(new Paginated <QuestionHeaderDto>(limit, idHeaderDtos.Values));
        }
コード例 #9
0
        private Task AddQuestions()
        {
            var choices = new List <Choice>
            {
                new Choice {
                    Content = "32", Valid = false
                },
                new Choice {
                    Content = "64", Valid = true
                },
                new Choice {
                    Content = "81", Valid = false
                },
            };
            var answer   = new ChoiceAnswer(choices, ChoiceAnswerType.SingleChoice, 2f);
            var question = new Question("What is the total number of squares on a chess board?", answer, UserId);

            Questions.Add(question);

            choices = new List <Choice>
            {
                new Choice {
                    Content = "2", Valid = true
                },
                new Choice {
                    Content = "3", Valid = true
                },
                new Choice {
                    Content = "4", Valid = false
                },
                new Choice {
                    Content = "5", Valid = true
                },
            };
            answer   = new ChoiceAnswer(choices, ChoiceAnswerType.MultipleChoice, 3f);
            question = new Question("Select prime numbers", answer, UserId);
            Questions.Add(question);

            question = new Question("5 + 5 = ?", new WrittenAnswer("10", 1f), UserId);
            Questions.Add(question);

            return(fixture.ExecuteDbContext(db =>
            {
                db.Questions.AddRange(Questions);
                return db.SaveChangesAsync();
            }));
        }
コード例 #10
0
        public void MapToAnswer_QuestionWithChoiceAnswersDto_ValidAnswer(ChoiceAnswerType choiceAnswerType, bool allRequired)
        {
            //arrange
            var score = 5f;

            Dtos.Wizard.QuestionDto dto = new Dtos.Wizard.QuestionWithChoiceAnswersDto
            {
                Id = 1,
                ChoiceAnswerType        = choiceAnswerType,
                AllValidChoicesRequired = allRequired,
                Choices = new List <Dtos.Wizard.ChoiceDto>
                {
                    new Dtos.Wizard.ChoiceDto {
                        Content = "1", Valid = true
                    },
                    new Dtos.Wizard.ChoiceDto {
                        Content = "2", Valid = false
                    },
                },
                Categories = new List <int>(),
                Question   = "Question content",
                Score      = score
            };
            var expectedChoices = new List <Choice>
            {
                new Choice
                {
                    Content = "1",
                    Valid   = true
                },
                new Choice
                {
                    Content = "2",
                    Valid   = false
                }
            };
            var expectedAnswer = new ChoiceAnswer(expectedChoices, choiceAnswerType, score, allRequired);

            //act
            var mapper = new QuestionServiceMapper();
            var answer = mapper.MapToAnswer(dto);

            //assert
            answer.Should().BeEquivalentTo(expectedAnswer);
        }
コード例 #11
0
ファイル: AnswerTests.cs プロジェクト: Krztk/KtTest
        public void ChoiceAnswer_WrittenUserAnswer_ThrowsException()
        {
            int questionId = 7;
            var choices    = new List <Choice>
            {
                new Choice {
                    Content = "1", Valid = true
                },
                new Choice {
                    Content = "2", Valid = false
                },
                new Choice {
                    Content = "3", Valid = false
                },
            };
            Answer answer = new ChoiceAnswer(questionId, choices, ChoiceAnswerType.SingleChoice, 6f);

            Action act = () => answer.GetScore(new WrittenUserAnswer("written answer", 1, questionId, 1));

            act.Should().Throw <WrongAnswerTypeException>();
        }
コード例 #12
0
ファイル: QuestionServiceTests.cs プロジェクト: Krztk/KtTest
        public async Task UpdateQuestion_ChangeWrittenAnswerToChoiceAnswer_UpdatesQuestion()
        {
            //arrange
            var question = GetNewQuestionWithWrittenAnswer();

            InsertQuestion(question);
            var service = new QuestionService(dbContext, userContext);
            var choices = new List <Choice>
            {
                new Choice
                {
                    Content = "First choice",
                    Valid   = false
                },
                new  Choice
                {
                    Content = "Second choice",
                    Valid   = true
                },
            };
            float newMaxScore = 10f;
            var   newAnswer   = new ChoiceAnswer(choices, ChoiceAnswerType.SingleChoice, newMaxScore);

            //act
            var result = await service.UpdateQuestion(question.Id, "New content", newAnswer, null);

            //assert
            result.Succeeded.Should().BeTrue();
            var persistedAnswer = dbContext.Answers.Include(x => ((ChoiceAnswer)x).Choices).First(x => x.QuestionId == question.Id);
            var choiceAnswer    = persistedAnswer as ChoiceAnswer;

            choiceAnswer.Should().NotBeNull();
            choiceAnswer.NumericValue.Should().Be(1);
            choiceAnswer.Choices.Should().BeEquivalentTo(choices);
            choiceAnswer.MaxScore.Should().Be(newMaxScore);
        }
コード例 #13
0
        public async Task ShouldReturnAllQuestions()
        {
            int authorId  = fixture.UserId;
            var questions = new List <Question>();
            var choices   = new List <Choice>()
            {
                new Choice {
                    Valid = false, Content = "1957"
                },
                new Choice {
                    Valid = true, Content = "1958"
                },
                new Choice {
                    Valid = false, Content = "1959"
                },
                new Choice {
                    Valid = false, Content = "1960"
                },
                new Choice {
                    Valid = false, Content = "1961"
                },
                new Choice {
                    Valid = false, Content = "1962"
                }
            };

            var choiceAnswer = new ChoiceAnswer(choices, ChoiceAnswerType.SingleChoice, 1f);

            questions.Add(new Question("When was Nasa founded?", choiceAnswer, authorId));

            choices = new List <Choice>()
            {
                new Choice {
                    Valid = false, Content = "99 years"
                },
                new Choice {
                    Valid = false, Content = "100 years"
                },
                new Choice {
                    Valid = false, Content = "113 years"
                },
                new Choice {
                    Valid = true, Content = "116 years"
                }
            };

            choiceAnswer = new ChoiceAnswer(choices, ChoiceAnswerType.SingleChoice, 1f);
            questions.Add(new Question("How many years did the 100 years war last?", choiceAnswer, authorId));

            var writtenAnswer = new WrittenAnswer("George Washington", 1f);

            questions.Add(new Question("Who was the first president of the United States?", writtenAnswer, authorId));

            await fixture.ExecuteDbContext(db =>
            {
                foreach (var question in questions)
                {
                    db.Questions.Add(question);
                }

                return(db.SaveChangesAsync());
            });

            var mapper       = new QuestionServiceMapper();
            var questionDtos = questions.Select(mapper.MapToWizardQuestionDto);

            int offset      = 0;
            int limit       = 10;
            var queryString = $"?Offset={offset}&Limit={limit}";
            var response    = await fixture.RequestSender.GetAsync("questions" + queryString);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            var responseData = await response.Content.ReadAsStringAsync();

            var result = fixture.Deserialize <Paginated <QuestionDto> >(responseData);

            result.Data.Should().NotBeEmpty();
            foreach (var questionDto in questionDtos)
            {
                result.Data.Should().ContainEquivalentOf(questionDto);
            }
        }