public void SerializeAndDeserializeXMLTest() { StorageService ss = new StorageService(); Dictionary <String, List <Question> > questions = new Dictionary <String, List <Question> >(); questions["Algebra liniowa"] = new List <Question>(); Question question = new Question(); question.Content = "Czy zdam egzamin w pierwszym terminie?"; question.Tip1 = "1"; question.Tip2 = "2"; question.Tip3 = "3"; question.Tip4 = "4"; question.Answear = "1"; question.Category = new Category(); question.FileName = "ziomek.png"; question.Used = true; questions["Algebra liniowa"].Add(question); QuestionsSet qs = new QuestionsSet(questions); String testFilePath = @"..\..\TestsData\XML_TEST.xml"; ss.SerializeToXMLFile(qs, testFilePath); QuestionsSet qsCheck = ss.DeserializeFromXMLFile <QuestionsSet>(testFilePath); Assert.AreEqual(qs, qsCheck); }
public void SaveShouldThrowNotFoundException() { _repositoryMock.Setup(repository => repository.Get(It.IsAny <int>())).Returns((QuestionsSet)null); QuestionsSet questionsSet = new QuestionsSet { Id = 1 }; Assert.Throws <NotFoundException>(() => _service.Save(questionsSet)); }
public void ParseTextFileTest() { StorageService ss = new StorageService(); QuestionsSet qs = ss.ParseTextFile( @"..\..\TestsData\pytania.tsv", QuestionsSet.TextFormatParser); Assert.AreEqual(qs.Questions.ContainsKey("Gramatyka i ortografia"), true); }
/// <summary> /// Update OpenTriviaDB Questions Set data. /// First, fetches all categories available. /// For each category imported, it checks if that category exists or not. /// If it does not exist, then it is saved. /// Also gets <see cref="MAX_QUESTIONS_PER_CALL"/> questions for the /// imported category, and if any question does not exist for the categroy, /// it is saved too. (All questions are saved for new categories) /// </summary> public void UpdateData() { using (IUnitOfWork bUoW = _unitOfWorkFactory.GetUnitOfWork()) { QuestionsSet questionsSet = bUoW.QuestionsSetRepository .Get(bQuestionsSet => string.Equals(bQuestionsSet.Name, QuestionsSetsName.OpenTriviaDB, StringComparison.OrdinalIgnoreCase)); if (questionsSet == null) { throw new Exception("Failed to get Open Trivia DB Questions Set entity."); } foreach (Category category in GetCategories()) { int categoryQuestionCount = GetCategoryQuestionCount(category.Id); // Get minimum number of questions between categoryQuestionCount and MAX_QUESTIONS_PER_CALL // because a request with a greater number than category question count returns // a no result response IList <Question> importedQuestions = GetQuestions(category.Id, Math.Min(categoryQuestionCount, MAX_QUESTIONS_PER_CALL)); // Set correspondent Level for each importedQuestion foreach (Question importedQuestion in importedQuestions) { importedQuestion.Level = questionsSet.Levels.ToList() .Find(bLevel => string.Equals(bLevel.Name, importedQuestion.Level.Name, StringComparison.OrdinalIgnoreCase)); } // Check if the category already exists in OpenTriviaDB Questions Set Category existingCategory = questionsSet.Categories.ToList().Find(bCategory => bCategory.Id == category.Id); // If not exists any category with that id, then save the imported category // with all imported questions. if (existingCategory == null) { category.Questions = importedQuestions; questionsSet.Categories.Add(category); } else { // Only save questions which are not already saved for OpenTriviaDB Questions Set foreach (Question importedQuestion in importedQuestions) { Question existingQuestion = existingCategory.Questions.ToList().Find(bQuestion => Equals(bQuestion.Id, importedQuestion.Id)); if (existingQuestion == null) { existingCategory.Questions.Add(importedQuestion); } } } } bUoW.Complete(); } }
public void SaveShouldBeOk() { QuestionsSet questionsSet = new QuestionsSet { Id = 1 }; _repositoryMock.Setup(repository => repository.Get(It.IsAny <int>())).Returns(new QuestionsSet()); var response = _service.Save(questionsSet); Assert.IsTrue(response.Success); }
/// <summary> /// Save the specified pQuestionsSet. /// </summary> /// <param name="pQuestionsSet">Questions set DTO.</param> public ResponseDTO <object> SaveQuestionsSet(QuestionsSetDTO pQuestionsSet) { try { QuestionsSet modifiedQuestionsSet = _mapper.Map <QuestionsSet>(pQuestionsSet); return(_questionsSetService.Save(modifiedQuestionsSet)); } catch (Exception ex) { _logger.Error(ex, $"Failed to save QuestionsSet. {ex.Message}"); return(ResponseDTO.InternalError(ErrorMessageHelper.FailedOperation("saving questions set"))); } }
/// <summary> /// Calculate the score for the given session. /// </summary> /// <returns>The score.</returns> /// <param name="pSession">Session.</param> public double CalculateScore(Session pSession) { using (var bUoW = _unitOfWorkFactory.GetUnitOfWork()) { var category = bUoW.CategoryRepository.Get(pSession.Category.Id); QuestionsSet questionsSet = pSession.GetQuestionsSet() ?? throw new Exception("Invalid Questions Set."); if (this._scoreCalculators.TryGetValue(questionsSet.Name.ToUpper(), out IScoreCalculator calculator)) { return(calculator.CalculateScore(pSession)); } throw new Exception("There is no calculator defined for the Session's QuestionsSet."); } }
/// <summary> /// Save the specified pQuestionsSet into database. /// </summary> /// <param name="pQuestionsSet">Questions set.</param> public ResponseDTO <object> Save(QuestionsSet pQuestionsSet) { if (pQuestionsSet == null) { throw new NullReferenceException("Questions Set cannot be null"); } using (IUnitOfWork bUoW = _unitOfWorkFactory.GetUnitOfWork()) { QuestionsSet entity = bUoW.QuestionsSetRepository.Get(pQuestionsSet.Id); if (entity == null) { throw new NotFoundException($"No questions set found with id {pQuestionsSet.Id}"); } entity.ExpectedAnswerTime = pQuestionsSet.ExpectedAnswerTime; bUoW.Complete(); } return(ResponseDTO.Ok("Questions Set successfully saved.")); }