def test_answer_retrieval(self): """ Tests answer retrieval from the database in given text """ inquiry = set_up_inquiry() question = Question.objects.get(name="OpenQ1") # Values are retrieved through the 'q_' marker message = "The value of tech_1_score is {q_OpenQ1}" test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(q_OpenQ1="") self.assertEqual(test_message, correct_message) # Set an answer iqa = InquiryQuestionAnswer.objects.create(question=question, inquiry=inquiry, answer="An_answer") test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(q_OpenQ1=iqa.get_readable_answer()) self.assertEqual(test_message, correct_message) # Change the answer iqa.answer = "Something else" iqa.save() iqa.refresh_from_db() test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(q_OpenQ1=iqa.get_readable_answer()) self.assertEqual(test_message, correct_message)
def test_code_retrieval(self): """ Tests that __code returns the code for the selected answer """ inquiry = set_up_inquiry() question = Question.objects.get(name="ChoiceQ1") # Values are retrieved through the 'q_*__code' marker message = "The code of tech_1_score is {q_ChoiceQ1__code}" iqa = InquiryQuestionAnswer.objects.create(question=question, inquiry=inquiry, answer="An_answer") # Answer is not processed, so should return nothing test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(q_ChoiceQ1__code="") self.assertEqual(test_message, correct_message) # Set the code code_answer = "Answer number 1" option = question.answeroption_set.first() option.context_code = code_answer option.save() # set the answer option iqa.processed_answer = option iqa.save() # Answer is processed, so should return the answer test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(q_ChoiceQ1__code=code_answer) self.assertEqual(test_message, correct_message)
def test_value_retrieval(self): """ Tests retrieval of scoring values from the database """ inquiry = set_up_inquiry() declaration = ScoringDeclaration.objects.get(name="tech_1_score") score = Score.objects.create(inquiry=inquiry, declaration=declaration) # Values are retrieved through the 'v_' marker message = "The value of tech_1_score is {v_tech_1_score}" # ##### Test with default value ##### test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(v_tech_1_score=score.score) self.assertEqual(test_message, correct_message) # ##### Adjust the score to make sure it is not retrieving defaults ##### score.score = 42 score.save() score.refresh_from_db( ) # Refresh to prevent Decimal artifacts from hanging around (42 != 42.00) test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(v_tech_1_score=score.score) self.assertEqual(test_message, correct_message) # ##### Test default value if score does not exist ##### score.delete() test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(v_tech_1_score="") self.assertEqual(test_message, correct_message)
def test_question_regex_filter(self): q_name = 'q_Postcode__regex=[0-9]/{4/}' message = "The code of tech_1_score is {" + q_name + "}" inquiry = set_up_inquiry() question = Question.objects.create(name="Postcode", question_type=Question.TYPE_OPEN) test_message = format_from_database(message, inquiry=inquiry) correct_message = message.replace('{' + q_name + '}', "") self.assertEqual(test_message, correct_message) iqa = InquiryQuestionAnswer.objects.create(question=question, inquiry=inquiry, answer="5612AH") test_message = format_from_database(message, inquiry=inquiry) correct_message = message.replace('{' + q_name + '}', "5612") self.assertEqual(test_message, correct_message)
def test_choice_answer_retrieval(self): """ Tests that multiple choice questions return the selected answer and not the stored answer I.e. it does not return the reference to the answer option, but the answer option itself """ inquiry = set_up_inquiry() question = Question.objects.get(name="ChoiceQ1") # Values are retrieved through the 'q_*__code' marker message = "The answer of tech_1_score is {q_ChoiceQ1}" # There is no answer yet test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(q_ChoiceQ1="") self.assertEqual(test_message, correct_message) answer = question.answeroption_set.first() question.answer_for_inquiry(inquiry=inquiry, answer_value=answer.value, process=True) # Check that the returned solution contains the selected answer and not the selected answer value test_message = format_from_database(message, inquiry=inquiry) correct_message = message.format(q_ChoiceQ1=answer.answer) self.assertEqual(test_message, correct_message)
def get_content(self, inquiry=None): """ Returns the given content as queried :param inquiry: Inquiry model of the current inquiry :return: """ code = format_from_database(self.code_source, inquiry=inquiry) if code is None: return None if not self.local_table.is_active: # If the table is not active, there is no database table and thus no data return None DataClass = self.local_table.get_data_class() try: data_object = DataClass.objects.get( **{self.local_table.db_key_column_name: code}) data_result = data_object.__getattribute__( self.local_attribute.db_column_name) return data_result except DataClass.DoesNotExist: return None
def get_prepped_text(self, inquiry=None): from Questionaire.processors.replace_text_from_database import format_from_database # Format the text to include answers from database objects entries return format_from_database(self.text, inquiry=inquiry)
def prep_text(self, text, inquiry=None): """ Adjusts the text to better suit displaying it on screen """ from Questionaire.processors.replace_text_from_database import format_from_database return format_from_database(text, inquiry=inquiry)