def test_should_not_add_answer_rule_twice_on_same_value_of_numeric_question(self): batch = Batch.objects.create(order=1) question_1 = Question.objects.create(text="How many members are there in this household?", answer_type=Question.NUMBER, order=1) value_1 = 0 value_2 = 20 sub_question_1 = Question.objects.create(text="Specify others", answer_type=Question.TEXT, subquestion=True, parent=question_1) # rule = AnswerRule.objects.create(question=question_1, action=AnswerRule.ACTIONS['ASK_SUBQUESTION'], # condition=AnswerRule.CONDITIONS['EQUALS'], # validate_with_value=value_1, next_question=sub_question_1, batch=batch) question_1.batches.add(batch) sub_question_1.batches.add(batch) data = dict(action='ASK_SUBQUESTION', condition='EQUALS', value='value_1', next_question=sub_question_1) logic_form = LogicForm(question = question_1, data = data, batch=batch) self.assertFalse(logic_form.is_valid()) another_data = dict(action='END_INTERVIEW', condition='GREATER_THAN_VALUE', value=value_1) logic_form = LogicForm(question = question_1, data = another_data, batch=batch) self.assertTrue(logic_form.is_valid())
def test_skip_logic_selection_in_form_question_creates_skip_flow(self): ''' :return: ''' q1 = Question.objects.create(qset=self.qset, response_validation=self.rsp, identifier='test1', text='test1', answer_type=NumericalAnswer.choice_name()) q2 = Question.objects.create(qset=self.qset, response_validation=self.rsp, identifier='test2', text='test2', answer_type=NumericalAnswer.choice_name()) q3 = Question.objects.create(qset=self.qset, response_validation=self.rsp, identifier='test3', text='test3', answer_type=NumericalAnswer.choice_name()) q4 = Question.objects.create(qset=self.qset, response_validation=self.rsp, identifier='test4', text='test4', answer_type=NumericalAnswer.choice_name()) q5 = Question.objects.create(qset=self.qset, response_validation=self.rsp, identifier='test5', text='test5', answer_type=NumericalAnswer.choice_name()) test_condition = NumericalAnswer.validators()[0].__name__ test_param = '15' form_data = { 'action': LogicForm.SKIP_TO, 'condition': test_condition, 'value': test_param } self.qset.start_question = q1 self.qset.save() QuestionFlow.objects.create(question=q1, next_question=q2) QuestionFlow.objects.create(question=q2, next_question=q3) QuestionFlow.objects.create(question=q3, next_question=q4) QuestionFlow.objects.create(question=q4, next_question=q5) form = LogicForm(q1, data=form_data) self.assertFalse(form.is_valid()) self.assertIn('next_question', form.errors) form_data['next_question'] = q4.pk form = LogicForm(q1, data=form_data) self.assertTrue(form.is_valid()) form.save() self.assertTrue( QuestionFlow.objects.filter(question_id=q1.id, next_question_id=q4.id).exists()) qf = QuestionFlow.objects.get(question_id=q1.id, next_question_id=q4.id) self.assertTrue(qf.text_arguments.filter(param=test_param).exists())
def test_reanswer_selection_in_form_question_creates_flow_to_same_question(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=DateAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = 'between' test_param_upper = datetime.now() test_param_lower = datetime.now() - timedelta(days=3) form_data = { 'action': LogicForm.REANSWER, 'condition': test_condition, 'min_value': test_param_lower, 'max_value': test_param_upper } l = LogicForm(q2, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get( question_id=q2.id, next_question_id=q2.id) TextArgument.objects.get( flow=qf, position=0, param=test_param_lower) TextArgument.objects.create( flow=qf, position=1, param=test_param_upper) QuestionFlow.objects.get( question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.get( question_id=q2.id, next_question_id=q3.id) self.assertTrue(True) return except QuestionFlow.DoesNotExist: self.assertFalse(False, 'flow not existing') pass except TextArgument: self.assertTrue(False, 'text agrunments not saved') pass else: self.assertTrue(False, 'Invalid form')
def test_form_has_validation_error_if_between_condition_is_selected_and_min_and_max_value_range_includes_range_of_existing_rule( self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create( text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) rule = AnswerRule.objects.create( question=question_without_option, action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['BETWEEN'], validate_with_min_value=3, validate_with_max_value=8, batch=batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['BETWEEN'], attribute='value', min_value=1, max_value=10) logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn( 'Rule on this condition BETWEEN within range %s - %s already exists.' % (data['min_value'], data['max_value']), logic_form.errors['__all__'])
def add_logic(request, batch_id, question_id): question = Question.objects.get(id=question_id) batch = Batch.objects.get(id=batch_id) response = None logic_form = LogicForm(question=question) question_rules_for_batch = {} # question_rules_for_batch[question] = question.rules_for_batch(batch) if request.method == "POST": logic_form = LogicForm(data=request.POST, question=question) if logic_form.is_valid(): logic_form.save() messages.success(request, 'Logic successfully added.') response = HttpResponseRedirect('/batches/%s/questions/' % batch_id) request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))), (batch.name, reverse('batch_questions_page', args=(batch.pk, ))), ]) context = {'logic_form': logic_form, 'button_label': 'Save', 'question': question, 'rules_for_batch': question_rules_for_batch, 'questionform': QuestionForm(parent_question=question, batch=batch), 'modal_action': reverse('add_batch_subquestion_page', args=(batch.pk, )), 'class': 'question-form', 'batch_id': batch_id, 'batch': batch, 'cancel_url': '/batches/%s/questions/' % batch_id} return response or render(request, "questions/logic.html", context)
def add_logic(request, qset_id, question_id): question = Question.get(id=question_id) batch = QuestionSet.get(id=qset_id) QuestionForm = get_question_form(batch.question_model()) response = None cancel_url = '../' logic_form = LogicForm(question) question_rules_for_batch = {} # question_rules_for_batch[question] = question.rules_for_batch(batch) if request.method == "POST": logic_form = LogicForm(question, data=request.POST) if logic_form.is_valid(): logic_form.save() messages.success(request, 'Logic successfully added.') response = HttpResponseRedirect( reverse('qset_questions_page', args=(batch.pk, ))) breadcrumbs = Question.edit_breadcrumbs(qset=batch) if breadcrumbs: request.breadcrumbs(breadcrumbs) cancel_url = breadcrumbs[-1][1] context = { 'logic_form': logic_form, 'button_label': 'Save', 'question': question, 'USSD_MAX_CHARS': settings.USSD_MAX_CHARS, 'rules_for_batch': question_rules_for_batch, 'questionform': QuestionForm(batch, parent_question=question), 'modal_action': reverse('add_qset_subquestion_page', args=(batch.pk, )), 'class': 'question-form', 'batch_id': qset_id, 'batch': batch, 'cancel_url': cancel_url } return response or render(request, "set_questions/logic.html", context)
def add_logic(request, batch_id, question_id): question = Question.objects.get(id=question_id) batch = Batch.objects.get(id=batch_id) logic_form = LogicForm(question=question, batch=batch) response = None question_rules_for_batch = {} question_rules_for_batch[question] = question.rules_for_batch(batch) if request.method == "POST": logic_form = LogicForm(data=request.POST, question=question, batch=batch) if logic_form.is_valid(): AnswerRule.objects.create(question=question, batch=batch, **_get_post_values(request.POST)) messages.success(request, "Logic successfully added.") response = HttpResponseRedirect("/batches/%s/questions/" % batch_id) context = { "logic_form": logic_form, "button_label": "Save", "question": question, "rules_for_batch": question_rules_for_batch, "questionform": QuestionForm(parent_question=question), "modal_action": "/questions/%s/sub_questions/new/" % question.id, "class": "question-form", "batch_id": batch_id, "batch": batch, "cancel_url": "/batches/%s/questions/" % batch_id, } return response or render(request, "questions/logic.html", context)
def test_should_not_add_answer_rule_twice_on_same_option_of_multichoice_question(self): batch = Batch.objects.create(order=1) question_1 = Question.objects.create(text="How many members are there in this household?", answer_type=Question.MULTICHOICE, order=1) option_1_1 = QuestionOption.objects.create(question=question_1, text="OPTION 1", order=1) option_1_2 = QuestionOption.objects.create(question=question_1, text="OPTION 2", order=2) sub_question_1 = Question.objects.create(text="Specify others", answer_type=Question.TEXT, subquestion=True, parent=question_1) question_1.batches.add(batch) sub_question_1.batches.add(batch) # rule = AnswerRule.objects.create(action=AnswerRule.ACTIONS['ASK_SUBQUESTION'], # condition=AnswerRule.CONDITIONS['EQUALS_OPTION'], # validate_with_option=option_1_1, next_question=sub_question_1, batch=batch) value_1 = 0 value_2 = 20 sub_question_1 = Question.objects.create(text="Specify others", answer_type=Question.TEXT, subquestion=True, parent=question_1) data = dict(action='ASK_SUBQUESTION', condition='EQUALS_OPTION', option=option_1_1, next_question=sub_question_1) logic_form = LogicForm(question = question_1, data = data, batch=batch) self.assertFalse(logic_form.is_valid())
def add_logic(request, batch_id, question_id): question = Question.objects.get(id=question_id) batch = Batch.objects.get(id=batch_id) logic_form = LogicForm(question=question, batch=batch) response = None question_rules_for_batch = {} question_rules_for_batch[question] = question.rules_for_batch(batch) if request.method == "POST": logic_form = LogicForm(data=request.POST, question=question, batch=batch) if logic_form.is_valid(): AnswerRule.objects.create(question=question, batch=batch, **_get_post_values(request.POST)) messages.success(request, 'Logic successfully added.') response = HttpResponseRedirect('/batches/%s/questions/' % batch_id) context = { 'logic_form': logic_form, 'button_label': 'Save', 'question': question, 'rules_for_batch': question_rules_for_batch, 'questionform': QuestionForm(parent_question=question), 'modal_action': '/questions/%s/sub_questions/new/' % question.id, 'class': 'question-form', 'batch_id': batch_id, 'batch': batch, 'cancel_url': '/batches/%s/questions/' % batch_id } return response or render(request, "questions/logic.html", context)
def test_should_not_add_answer_rule_twice_on_same_value_of_numeric_question( self): batch = Batch.objects.create(order=1) question_1 = Question.objects.create( text="How many members are there in this household?", answer_type=Question.NUMBER, order=1) value_1 = 0 value_2 = 20 sub_question_1 = Question.objects.create(text="Specify others", answer_type=Question.TEXT, subquestion=True, parent=question_1) rule = AnswerRule.objects.create( question=question_1, action=AnswerRule.ACTIONS['ASK_SUBQUESTION'], condition=AnswerRule.CONDITIONS['EQUALS'], validate_with_value=value_1, next_question=sub_question_1, batch=batch) question_1.batches.add(batch) sub_question_1.batches.add(batch) data = dict(action=rule.action, condition=rule.condition, value=rule.validate_with_value, next_question=rule.next_question) logic_form = LogicForm(question=question_1, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) another_data = dict( action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['GREATER_THAN_VALUE'], value=rule.validate_with_value) logic_form = LogicForm(question=question_1, data=another_data, batch=batch) self.assertTrue(logic_form.is_valid())
def test_end_interview_selection_in_form_question_creates_flow_to_with_no_next_question(self): ''' :return: ''' yes = 'yes' no = 'no' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=MultiChoiceAnswer.choice_name()) q_o1 = QuestionOption.objects.create(question_id=q2.id, text=yes, order=1) QuestionOption.objects.create(question_id=q2.id, text=no, order=2) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = MultiChoiceAnswer.validators()[0].__name__ form_data = { 'action': LogicForm.END_INTERVIEW, 'condition': test_condition, 'option': q_o1.order } l = LogicForm(q2, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get( question_id=q2.id, next_question_id__isnull=True) TextArgument.objects.get(flow=qf, position=0, param=q_o1.order) QuestionFlow.objects.get( question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.get( question_id=q2.id, next_question_id=q3.id) self.assertTrue(True) return except QuestionFlow.DoesNotExist: self.assertTrue(False, 'flow not existing') pass except TextArgument: self.assertTrue(False, 'text agrunments not saved') pass else: self.assertFalse(False, 'Invalid form')
def test_should_not_add_answer_rule_twice_on_same_question_value_of_numeric_question(self): batch = Batch.objects.create(order=1) question_1 = Question.objects.create(text="How many members are there in this household?", answer_type=Question.NUMBER, order=1) question_2 = Question.objects.create(text="How many members are above 18 years?", answer_type=Question.NUMBER, order=2) question_3 = Question.objects.create(text="Some random question", answer_type=Question.NUMBER, order=3) sub_question_1 = Question.objects.create(text="Specify others", answer_type=Question.TEXT, subquestion=True, parent=question_1) # rule = AnswerRule.objects.create(question=question_1, action='ASK_SUBQUESTION', # condition='EQUALS', # validate_with_question=question_2, next_question=sub_question_1, batch=batch) question_1.batches.add(batch) question_2.batches.add(batch) question_3.batches.add(batch) BatchQuestionOrder.objects.create(question=question_1, batch=batch, order=1) BatchQuestionOrder.objects.create(question=question_2, batch=batch, order=2) BatchQuestionOrder.objects.create(question=question_3, batch=batch, order=3) sub_question_1.batches.add(batch) data = dict(action='ASK_SUBQUESTION', condition='EQUALS', validate_with_question=question_2, next_question=sub_question_1) logic_form = LogicForm(question = question_1, data = data, batch=batch) self.assertFalse(logic_form.is_valid()) another_data = dict(action='ASK_SUBQUESTION', condition='EQUALS', validate_with_question=question_3.pk) logic_form = LogicForm(question = question_1, data = another_data, batch=batch) self.assertTrue(logic_form.is_valid())
def test_skip_logic_btween_question_groups_not_allowed(self): ''' :return: ''' group = mommy.make(RespondentGroup) q1 = BatchQuestion.objects.create( qset=self.batch, response_validation=self.rsp, identifier='test1', text='test1', answer_type=NumericalAnswer.choice_name()) q2 = BatchQuestion.objects.create( qset=self.batch, response_validation=self.rsp, identifier='test2', text='test2', answer_type=NumericalAnswer.choice_name()) q3 = BatchQuestion.objects.create( qset=self.batch, response_validation=self.rsp, identifier='test3', text='test3', answer_type=NumericalAnswer.choice_name()) q4 = BatchQuestion.objects.create( qset=self.batch, response_validation=self.rsp, identifier='test45', text='test45', answer_type=NumericalAnswer.choice_name(), group=group) q5 = BatchQuestion.objects.create( qset=self.batch, response_validation=self.rsp, identifier='test5', text='test5', answer_type=NumericalAnswer.choice_name()) test_condition = NumericalAnswer.validators()[0].__name__ test_param = '15' form_data = { 'action': LogicForm.SKIP_TO, 'next_question': q4.pk, 'condition': test_condition, 'value': test_param } self.batch.start_question = q1 self.batch.save() QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) form = LogicForm(q1, data=form_data) self.assertFalse(form.is_valid()) self.assertIn('between questions of different groups is not allowed', form.errors['next_question'][0])
def test_attempt_to_set_incorrect_value_gives_form_error(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=NumericalAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=NumericalAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=NumericalAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=NumericalAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=NumericalAnswer.choice_name()) test_condition = NumericalAnswer.validators()[0].__name__ test_param = '6267fe' form_data = { 'action': LogicForm.SKIP_TO, 'next_question': q4.pk, 'condition': test_condition, 'value': test_param } self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) l = LogicForm(q1, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get( question_id=q1.id, next_question_id=q4.id) TextArgument.objects.get(flow=qf, param=test_param) self.assertTrue(False, 'completely wrong. value saved as good') return except QuestionFlow.DoesNotExist: self.assertTrue(False, 'form valid but flow not existing') pass except TextArgument: self.assertTrue( False, 'form valid but text agrunments not saved') pass else: self.assertTrue(True)
def test_reanswer_selection_in_form_question_creates_flow_to_same_question( self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=DateAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.qset.start_question = q1 self.qset.save() QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = 'between' test_param_upper = datetime.now() test_param_lower = datetime.now() - timedelta(days=3) form_data = { 'action': LogicForm.REANSWER, 'condition': test_condition, 'min_value': test_param_lower, 'max_value': test_param_upper } form = LogicForm(q2, data=form_data) self.assertTrue(form.is_valid()) form.save() self.assertTrue( QuestionFlow.objects.filter(question_id=q2.id, next_question_id=q2.id).exists())
def test_end_interview_selection_in_form_question_creates_flow_to_with_no_next_question( self): yes = 'yes' no = 'no' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create( qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=MultiChoiceAnswer.choice_name()) q_o1 = QuestionOption.objects.create(question_id=q2.id, text=yes, order=1) QuestionOption.objects.create(question_id=q2.id, text=no, order=2) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.qset.start_question = q1 self.qset.save() QuestionFlow.objects.create(question=q1, next_question=q2) QuestionFlow.objects.create(question=q2, next_question=q3) QuestionFlow.objects.create(question=q3, next_question=q4) QuestionFlow.objects.create(question=q4, next_question=q5) test_condition = MultiChoiceAnswer.validators()[0].__name__ form_data = { 'action': LogicForm.END_INTERVIEW, 'condition': test_condition, 'option': q_o1.text } logic_form = LogicForm(q2, data=form_data) self.assertTrue(logic_form.is_valid()) logic_form.save() self.assertTrue( QuestionFlow.objects.filter(question=q2, next_question__isnull=True).exists())
def test_form_is_valid_if_between_condition_is_selected_and_rule_with_range_does_not_exist(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action='END_INTERVIEW', condition='BETWEEN', attribute = 'value', min_value='200', max_value='1000') logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertTrue(logic_form.is_valid())
def test_subquestion_selection_in_form_question_creates_branch_flow(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=TextAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=TextAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=TextAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=TextAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=TextAnswer.choice_name()) self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q5.id) test_condition = TextAnswer.validators()[0].__name__ test_param = 'Hey you!!' form_data = { 'action': LogicForm.ASK_SUBQUESTION, 'next_question': q4.pk, 'condition': test_condition, 'value': test_param } l = LogicForm(q1, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get( question_id=q1.id, next_question_id=q4.id) TextArgument.objects.get(flow=qf, param=test_param) qf = QuestionFlow.objects.get( question_id=q1.id, next_question_id=q3.id) self.assertTrue(True) return except QuestionFlow.DoesNotExist: #self.assertTrue(False, 'flow not existing') pass except TextArgument: self.assertTrue(False, 'text agrunments not saved') pass else: self.assertTrue(False, 'Invalid form')
def test_form_has_validation_error_if_value_attribute_is_selected_and_value_field_is_empty(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['EQUALS'], attribute = 'value', value="") logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn('Field is required.', logic_form.errors['value'])
def test_subquestion_selection_in_form_question_creates_branch_flow(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=TextAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=TextAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=TextAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=TextAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=TextAnswer.choice_name()) self.qset.start_question = q1 self.qset.save() QuestionFlow.objects.create(question_id=q1.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q5.id) test_condition = TextAnswer.validators()[0].__name__ test_param = 'Hey you!!' form_data = { 'action': LogicForm.ASK_SUBQUESTION, 'next_question': q4.pk, 'condition': test_condition, 'value': test_param } form = LogicForm(q1, data=form_data) self.assertTrue(form.is_valid()) form.save() self.assertTrue( QuestionFlow.objects.filter(question_id=q1.id, next_question_id=q4.id).exists()) qf = QuestionFlow.objects.get(question_id=q1.id, next_question_id=q4.id) self.assertTrue(qf.text_arguments.filter(param=test_param).exists())
def test_form_is_invalid_if_between_condition_is_selected_and_max_value_is_less_than_min_value(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action='END_INTERVIEW', condition='BETWEEN', attribute = 'value', min_value=6, max_value=3) logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn('Logic not created max value must be greater than min value.', logic_form.errors['__all__'])
def test_form_returns_true_for_validate_max_greater_than_min_if_non_integer_type_is_input_as_values(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action='END_INTERVIEW', condition='BETWEEN', attribute = 'value', min_value='Not Digit', max_value='Not digit') logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertTrue(logic_form._validate_max_greater_than_min())
def test_form_has_validation_error_if_between_condition_is_selected_and_max_value_field_is_empty(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action='END_INTERVIEW', condition='BETWEEN', attribute = 'value', max_value="") logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn('Field is required.', logic_form.errors['max_value'])
def test_form_has_validation_error_if_between_condition_is_selected_and_max_value_field_is_alpha_numeric(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['BETWEEN'], attribute = 'value', max_value="abc123") logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn('Max value %s invalid, must be an integer.' % data['max_value'], logic_form.errors['__all__'])
def test_form_has_validation_error_if_between_condition_is_selected_and_max_value_field_is_negative(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action='END_INTERVIEW', condition='BETWEEN', attribute = 'value', max_value="-1") logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn('Max value %s invalid, must be greater than zero.' % data['max_value'], logic_form.errors['__all__'])
def test_form_has_validation_error_if_value_attribute_is_selected_and_value_field_is_empty( self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create( text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['EQUALS'], attribute='value', value="") logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn('Field is required.', logic_form.errors['value'])
def test_form_is_valid_if_between_condition_is_selected_and_rule_with_range_does_not_exist( self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create( text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['BETWEEN'], attribute='value', min_value='200', max_value='1000') logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertTrue(logic_form.is_valid())
def test_specify_wrong_min_value_gives_form_error(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=DateAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.qset.start_question = q1 self.qset.save() QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = 'between' test_param_upper = datetime.now() test_param_lower = 'some time ago' form_data = { 'action': LogicForm.REANSWER, 'condition': test_condition, 'min_value': test_param_lower, 'max_value': test_param_upper } l = LogicForm(q2, data=form_data) self.assertFalse(l.is_valid())
def test_attempt_to_set_incorrect_value_gives_form_error(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=NumericalAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=NumericalAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=NumericalAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=NumericalAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=NumericalAnswer.choice_name()) test_condition = NumericalAnswer.validators()[0].__name__ test_param = '6267fe' form_data = { 'action': LogicForm.SKIP_TO, 'next_question': q4.pk, 'condition': test_condition, 'value': test_param } self.qset.start_question = q1 self.qset.save() QuestionFlow.objects.create(question=q1, next_question=q2) QuestionFlow.objects.create(question=q2, next_question=q3) QuestionFlow.objects.create(question=q3, next_question=q4) QuestionFlow.objects.create(question=q4, next_question=q5) l = LogicForm(q1, data=form_data) self.assertFalse(l.is_valid())
def test_get_batch_question_as_dump(self): self._create_ussd_group_questions() numeric_question = Question.objects.filter(answer_type=NumericalAnswer.choice_name()).first() last_question = Question.objects.last() test_condition = 'equals' test_param = '15' form_data = { 'action': LogicForm.SKIP_TO, 'next_question': last_question.id, 'condition': test_condition, 'value': test_param } logic_form = LogicForm(numeric_question, data=form_data) self.assertTrue(logic_form.is_valid()) logic_form.save() content = get_batch_question_as_dump(self.qset.flow_questions) for idx, question in enumerate(self.qset.flow_questions): self.assertIn(question.identifier, content[idx+1])
def test_form_returns_true_for_validate_max_greater_than_min_if_non_integer_type_is_input_as_values( self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create( text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['BETWEEN'], attribute='value', min_value='Not Digit', max_value='Not digit') logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertTrue(logic_form._validate_max_greater_than_min())
def test_form_has_validation_error_if_between_condition_is_selected_and_min_and_max_value_range_includes_range_of_existing_rule(self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create(text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) # rule = AnswerRule.objects.create(question=question_without_option, action=AnswerRule.ACTIONS['END_INTERVIEW'], # condition=AnswerRule.CONDITIONS['BETWEEN'], # validate_with_min_value=3, validate_with_max_value=8, batch=batch) data = dict(action='END_INTERVIEW', condition='BETWEEN', attribute = 'value', min_value=1, max_value=10) logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn('Rule on this condition BETWEEN within range %s - %s already exists.' %(data['min_value'], data['max_value']), logic_form.errors['__all__'])
def test_form_has_validation_error_if_between_condition_is_selected_and_max_value_field_is_negative( self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create( text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['BETWEEN'], attribute='value', max_value="-1") logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn( 'Max value %s invalid, must be greater than zero.' % data['max_value'], logic_form.errors['__all__'])
def test_form_is_invalid_if_between_condition_is_selected_and_max_value_is_less_than_min_value( self): batch = Batch.objects.create(order=1) question_without_option = Question.objects.create( text="Question 1?", answer_type=Question.NUMBER, order=1) question_without_option.batches.add(batch) data = dict(action=AnswerRule.ACTIONS['END_INTERVIEW'], condition=AnswerRule.CONDITIONS['BETWEEN'], attribute='value', min_value=6, max_value=3) logic_form = LogicForm(question=question_without_option, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) self.assertIn( 'Logic not created max value must be greater than min value.', logic_form.errors['__all__'])
def add_logic(request, qset_id, question_id): question = Question.get(id=question_id) batch = QuestionSet.get(id=qset_id) QuestionForm = get_question_form(batch.question_model()) response = None cancel_url = '../' logic_form = LogicForm(question) question_rules_for_batch = {} # question_rules_for_batch[question] = question.rules_for_batch(batch) if request.method == "POST": logic_form = LogicForm(question, data=request.POST) if logic_form.is_valid(): logic_form.save() messages.success(request, 'Logic successfully added.') response = HttpResponseRedirect( reverse('qset_questions_page', args=(batch.pk, ))) breadcrumbs = Question.edit_breadcrumbs(qset=batch) if breadcrumbs: request.breadcrumbs(breadcrumbs) cancel_url = breadcrumbs[-1][1] context = { 'logic_form': logic_form, 'button_label': 'Save', 'question': question, 'USSD_MAX_CHARS': settings.USSD_MAX_CHARS, 'rules_for_batch': question_rules_for_batch, 'questionform': QuestionForm( batch, parent_question=question), 'modal_action': reverse( 'add_qset_subquestion_page', args=( batch.pk, )), 'class': 'question-form', 'batch_id': qset_id, 'batch': batch, 'cancel_url': cancel_url} return response or render(request, "set_questions/logic.html", context)
def test_should_not_add_answer_rule_twice_on_same_option_of_multichoice_question( self): batch = Batch.objects.create(order=1) question_1 = Question.objects.create( text="How many members are there in this household?", answer_type=Question.MULTICHOICE, order=1) option_1_1 = QuestionOption.objects.create(question=question_1, text="OPTION 1", order=1) option_1_2 = QuestionOption.objects.create(question=question_1, text="OPTION 2", order=2) sub_question_1 = Question.objects.create(text="Specify others", answer_type=Question.TEXT, subquestion=True, parent=question_1) question_1.batches.add(batch) sub_question_1.batches.add(batch) rule = AnswerRule.objects.create( action=AnswerRule.ACTIONS['ASK_SUBQUESTION'], condition=AnswerRule.CONDITIONS['EQUALS_OPTION'], validate_with_option=option_1_1, next_question=sub_question_1, batch=batch) data = dict(action=rule.action, condition=rule.condition, option=rule.validate_with_option, next_question=rule.next_question) logic_form = LogicForm(question=question_1, data=data, batch=batch) self.assertFalse(logic_form.is_valid())
def test_download_question_with_flow(self): self._create_ussd_non_group_questions() numeric_question = Question.objects.filter( answer_type=NumericalAnswer.choice_name()).first() last_question = Question.objects.last() test_condition = 'equals' test_param = '15' form_data = { 'action': LogicForm.SKIP_TO, 'next_question': last_question.id, 'condition': test_condition, 'value': test_param } logic_form = LogicForm(numeric_question, data=form_data) self.assertTrue(logic_form.is_valid()) logic_form.save() self.assertEquals( QuestionFlow.objects.filter(question=numeric_question, next_question=last_question).count(), 1) url = reverse('download_odk_batch_form', args=(self.qset.id, )) response = self._make_odk_request(url=url) self.assertIn("/qset/qset1/questions/surveyQuestions/q1 = '15'", response.content)
def test_should_not_add_answer_rule_twice_on_same_question_value_of_numeric_question( self): batch = Batch.objects.create(order=1) question_1 = Question.objects.create( text="How many members are there in this household?", answer_type=Question.NUMBER, order=1) question_2 = Question.objects.create( text="How many members are above 18 years?", answer_type=Question.NUMBER, order=2) question_3 = Question.objects.create(text="Some random question", answer_type=Question.NUMBER, order=3) sub_question_1 = Question.objects.create(text="Specify others", answer_type=Question.TEXT, subquestion=True, parent=question_1) rule = AnswerRule.objects.create( question=question_1, action=AnswerRule.ACTIONS['ASK_SUBQUESTION'], condition=AnswerRule.CONDITIONS['EQUALS'], validate_with_question=question_2, next_question=sub_question_1, batch=batch) question_1.batches.add(batch) question_2.batches.add(batch) question_3.batches.add(batch) BatchQuestionOrder.objects.create(question=question_1, batch=batch, order=1) BatchQuestionOrder.objects.create(question=question_2, batch=batch, order=2) BatchQuestionOrder.objects.create(question=question_3, batch=batch, order=3) sub_question_1.batches.add(batch) data = dict(action=rule.action, condition=rule.condition, validate_with_question=rule.validate_with_question, next_question=rule.next_question) logic_form = LogicForm(question=question_1, data=data, batch=batch) self.assertFalse(logic_form.is_valid()) another_data = dict(action=rule.action, condition=rule.condition, validate_with_question=question_3.pk) logic_form = LogicForm(question=question_1, data=another_data, batch=batch) self.assertTrue(logic_form.is_valid())
def test_specify_wrong_min_value_gives_form_error(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=DateAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = 'between' test_param_upper = datetime.now() test_param_lower = 'some time ago' form_data = { 'action': LogicForm.REANSWER, 'condition': test_condition, 'min_value': test_param_lower, 'max_value': test_param_upper } l = LogicForm(q2, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get(question_id=q2.id, next_question_id=q2.id) TextArgument.objects.get(flow=qf, position=0, param=test_param_lower) TextArgument.objects.create(flow=qf, position=1, param=test_param_upper) QuestionFlow.objects.get(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.get(question_id=q2.id, next_question_id=q3.id) self.assertTrue( False, 'completely wrong. bad values was saved as good!!') return except QuestionFlow.DoesNotExist: self.assertTrue(False, 'form valid flow not existing') pass except TextArgument: self.assertTrue(False, 'Form valid but text agrunments not saved') pass else: self.assertTrue(True)
def test_skip_logic_selection_in_form_question_creates_skip_flow(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=NumericalAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=NumericalAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=NumericalAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=NumericalAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=NumericalAnswer.choice_name()) test_condition = NumericalAnswer.validators()[0].__name__ test_param = '15' form_data = { 'action': LogicForm.SKIP_TO, 'next_question': q4.pk, 'condition': test_condition, 'value': test_param } self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) l = LogicForm(q1, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get(question_id=q1.id, next_question_id=q4.id) TextArgument.objects.get(flow=qf, param=test_param) self.assertTrue(True) return except QuestionFlow.DoesNotExist: #self.assertTrue(False, 'flow not existing') pass except TextArgument: self.assertTrue(False, 'text agrunments not saved') pass else: self.assertTrue(False, 'Invalid form %s' % l.errors)
def test_end_interview_selection_in_form_question_creates_flow_to_with_no_next_question( self): ''' :return: ''' yes = 'yes' no = 'no' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create( qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=MultiChoiceAnswer.choice_name()) q_o1 = QuestionOption.objects.create(question_id=q2.id, text=yes, order=1) QuestionOption.objects.create(question_id=q2.id, text=no, order=2) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = MultiChoiceAnswer.validators()[0].__name__ form_data = { 'action': LogicForm.END_INTERVIEW, 'condition': test_condition, 'option': q_o1.order } l = LogicForm(q2, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get(question_id=q2.id, next_question_id__isnull=True) TextArgument.objects.get(flow=qf, position=0, param=q_o1.order) QuestionFlow.objects.get(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.get(question_id=q2.id, next_question_id=q3.id) self.assertTrue(True) return except QuestionFlow.DoesNotExist: self.assertTrue(False, 'flow not existing') pass except TextArgument: self.assertTrue(False, 'text agrunments not saved') pass else: self.assertFalse(False, 'Invalid form')
def test_specify_wrong_min_value_gives_form_error(self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=DateAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = 'between' test_param_upper = datetime.now() test_param_lower = 'some time ago' form_data = { 'action': LogicForm.REANSWER, 'condition': test_condition, 'min_value': test_param_lower, 'max_value': test_param_upper } l = LogicForm(q2, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get( question_id=q2.id, next_question_id=q2.id) TextArgument.objects.get( flow=qf, position=0, param=test_param_lower) TextArgument.objects.create( flow=qf, position=1, param=test_param_upper) QuestionFlow.objects.get( question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.get( question_id=q2.id, next_question_id=q3.id) self.assertTrue( False, 'completely wrong. bad values was saved as good!!') return except QuestionFlow.DoesNotExist: self.assertTrue(False, 'form valid flow not existing') pass except TextArgument: self.assertTrue( False, 'Form valid but text agrunments not saved') pass else: self.assertTrue(True)
def test_reanswer_selection_in_form_question_creates_flow_to_same_question( self): ''' :return: ''' q1 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test1', text='test1', answer_type=DateAnswer.choice_name()) q2 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test2', text='test2', answer_type=DateAnswer.choice_name()) q3 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test3', text='test3', answer_type=DateAnswer.choice_name()) q4 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test4', text='test4', answer_type=DateAnswer.choice_name()) q5 = Question.objects.create(qset_id=self.qset.id, response_validation_id=1, identifier='test5', text='test5', answer_type=DateAnswer.choice_name()) self.batch.start_question = q1 QuestionFlow.objects.create(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.create(question_id=q2.id, next_question_id=q3.id) QuestionFlow.objects.create(question_id=q3.id, next_question_id=q4.id) QuestionFlow.objects.create(question_id=q4.id, next_question_id=q5.id) test_condition = 'between' test_param_upper = datetime.now() test_param_lower = datetime.now() - timedelta(days=3) form_data = { 'action': LogicForm.REANSWER, 'condition': test_condition, 'min_value': test_param_lower, 'max_value': test_param_upper } l = LogicForm(q2, data=form_data) if l.is_valid(): l.save() # now check if equivalent Question flow and test arguments were # created try: qf = QuestionFlow.objects.get(question_id=q2.id, next_question_id=q2.id) TextArgument.objects.get(flow=qf, position=0, param=test_param_lower) TextArgument.objects.create(flow=qf, position=1, param=test_param_upper) QuestionFlow.objects.get(question_id=q1.id, next_question_id=q2.id) QuestionFlow.objects.get(question_id=q2.id, next_question_id=q3.id) self.assertTrue(True) return except QuestionFlow.DoesNotExist: self.assertFalse(False, 'flow not existing') pass except TextArgument: self.assertTrue(False, 'text agrunments not saved') pass else: self.assertTrue(False, 'Invalid form')