Example #1
0
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)
Example #2
0
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 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')
Example #4
0
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 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')
Example #6
0
 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_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)
Example #8
0
 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())
Example #9
0
    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_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')
Example #11
0
 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())
Example #12
0
 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])
Example #13
0
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_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_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_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_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_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')