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_answer_validators(self): validators = [ # supported validators 'starts_with', 'ends_with', 'equals', 'between', 'less_than', 'greater_than', 'contains', ] for func in Answer.validators(): self.assertIn(func.__name__, validators) # test Numeric Answer Validators validators = [ # supported validators 'equals', 'between', 'less_than', 'greater_than', ] for func in NumericalAnswer.validators(): self.assertIn(func.__name__, validators) # test Text Answer Validators validators = [ # supported validators 'starts_with', 'ends_with', 'equals', 'contains', ] for func in TextAnswer.validators(): self.assertIn(func.__name__, validators) # Multichoice validators = [ # supported validators 'equals', ] for func in MultiChoiceAnswer.validators(): self.assertIn(func.__name__, validators) # test Multiselect Answer Validators validators = [ # supported validators 'equals', 'contains', ] for func in MultiSelectAnswer.validators(): self.assertIn(func.__name__, validators) # test Date Answer Validators validators = [ # supported validators 'equals', 'between', 'less_than', 'greater_than', ] for func in DateAnswer.validators(): self.assertIn(func.__name__, validators) # GeoPoint Answer validators = [ # supported validators 'equals', ] for func in GeopointAnswer.validators(): self.assertIn(func.__name__, validators) # file answers should return no validators for answer_class in [VideoAnswer, AudioAnswer, ImageAnswer]: self.assertEquals(len(answer_class.validators()), 0)
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)