コード例 #1
0
    def test_validate_fieldtypes(self):
        integer_question = QuestionFactory(field_type='integer',
                                           label='integer',
                                           short_label='integer')
        plaintext_question = QuestionFactory(field_type='plain_text',
                                             label='plain_text',
                                             short_label='plain_text')
        validate_answer = AnswerService.validate_answer_payload

        # Check integer fieldtype
        self.assertEqual(validate_answer(123456, integer_question), 123456)
        with self.assertRaises(django_validation_error):
            validate_answer('THESE ARE CHARACTERS', integer_question)
        with self.assertRaises(django_validation_error):
            validate_answer({
                'some': 'thing',
                'complicated': {}
            }, integer_question)

        # check plain_text fieldtype
        self.assertEqual(validate_answer('THIS IS TEXT', plaintext_question),
                         'THIS IS TEXT')
        with self.assertRaises(django_validation_error):
            validate_answer(123456, plaintext_question)
        with self.assertRaises(django_validation_error):
            validate_answer({
                'some': 'thing',
                'complicated': {}
            }, plaintext_question)
コード例 #2
0
def _question_graph_no_required_answers():
    q1 = QuestionFactory.create(
        key='one',
        required=False,
        short_label='First not required',
        label='First not required',
        next_rules=[
            {'ref': 'two'},
        ]
    )
    q2 = QuestionFactory(
        key='two',
        required=False,
        short_label='Second not required',
        label='Second not required',
    )

    return q1, q2
コード例 #3
0
def _question_graph_no_required_answers():
    q1 = QuestionFactory.create(
        retrieval_key='one',
        required=False,
        short_label='First not required',
        label='First not required',
    )
    q2 = QuestionFactory(
        retrieval_key='two',
        required=False,
        short_label='Second not required',
        label='Second not required',
    )

    graph = QuestionGraphFactory.create(
        name='Graph with questions that are not required.', first_question=q1)
    EdgeFactory.create(graph=graph, question=q1, next_question=q2, choice=None)

    return graph
コード例 #4
0
    def test_answer_a_complete_questionnaire_branching_flow(self):
        self.assertEqual(0, Answer.objects.count())
        self.assertEqual(0, Session.objects.count())

        # Setup questions
        test_question_1 = QuestionFactory(retrieval_key='test-question-1')
        test_question_2 = QuestionFactory(retrieval_key='test-question-2')
        test_question_3 = QuestionFactory(retrieval_key='test-question-3')
        test_question_4 = QuestionFactory(retrieval_key='test-question-4')

        # Setup graph relations between questions
        graph = QuestionGraphFactory.create(first_question=test_question_1)
        EdgeFactory.create(graph=graph,
                           question=test_question_1,
                           next_question=test_question_2,
                           choice__payload='yes',
                           choice__question=test_question_1)
        EdgeFactory.create(graph=graph,
                           question=test_question_1,
                           next_question=test_question_3,
                           choice__payload='no',
                           choice__question=test_question_1)
        EdgeFactory.create(graph=graph, question=test_question_1, next_question=test_question_4, choice=None)

        # Create our questionnaire
        questionnaire = QuestionnaireFactory.create(graph=graph)

        # Answer our questionnaire
        first_post_answer_endpoint = f'{self.base_endpoint}{questionnaire.first_question.uuid}/answer'

        # Flow: question 1 -> question 2 -> done
        data = {'payload': 'yes', 'questionnaire': questionnaire.uuid}
        response = self.client.post(first_post_answer_endpoint, data=data, format='json')
        response_data = response.json()
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_data['next_question']['uuid'], str(test_question_2.uuid))

        data = {'payload': 'test', 'session': response_data['session']}
        second_post_answer_endpoint = response_data['next_question']['_links']['sia:post-answer']['href']
        response = self.client.post(second_post_answer_endpoint, data=data, format='json')
        response_data = response.json()
        self.assertEqual(response.status_code, 201)
        self.assertIsNone(response_data['next_question'])

        # Flow: question 1 -> question 3 -> done
        data = {'payload': 'no', 'questionnaire': questionnaire.uuid}
        response = self.client.post(first_post_answer_endpoint, data=data, format='json')
        response_data = response.json()
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_data['next_question']['uuid'], str(test_question_3.uuid))

        data = {'payload': 'test', 'session': response_data['session']}
        response = self.client.post(second_post_answer_endpoint, data=data, format='json')
        response_data = response.json()
        self.assertEqual(response.status_code, 201)
        self.assertIsNone(response_data['next_question'])

        # Flow: question 1 -> question 4 -> done
        data = {'payload': 'default', 'questionnaire': questionnaire.uuid}
        response = self.client.post(first_post_answer_endpoint, data=data, format='json')
        response_data = response.json()
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response_data['next_question']['uuid'], str(test_question_4.uuid))

        data = {'payload': 'test', 'session': response_data['session']}
        second_post_answer_endpoint = response_data['next_question']['_links']['sia:post-answer']['href']
        response = self.client.post(second_post_answer_endpoint, data=data, format='json')
        response_data = response.json()
        self.assertEqual(response.status_code, 201)
        self.assertIsNone(response_data['next_question'])

        self.assertEqual(3, Session.objects.count())
        self.assertEqual(6, Answer.objects.count())
コード例 #5
0
    def test_answer_a_complete_questionnaire(self):
        self.assertEqual(0, Answer.objects.count())
        self.assertEqual(0, Session.objects.count())

        # Setup questions
        test_question_1 = QuestionFactory(retrieval_key='test-question-1')
        test_question_2 = QuestionFactory(retrieval_key='test-question-2')
        test_question_3 = QuestionFactory(retrieval_key='test-question-3')
        test_question_4 = QuestionFactory(retrieval_key='test-question-4')
        test_question_5 = QuestionFactory(retrieval_key='test-question-5')

        # Setup graph relations between questions
        graph = QuestionGraphFactory.create(first_question=test_question_1)
        EdgeFactory.create(graph=graph, question=test_question_1, next_question=test_question_2, choice=None)
        EdgeFactory.create(graph=graph, question=test_question_2, next_question=test_question_3, choice=None)
        EdgeFactory.create(graph=graph, question=test_question_3, next_question=test_question_4, choice=None)
        EdgeFactory.create(graph=graph, question=test_question_4, next_question=test_question_5, choice=None)

        # Create our questionnaire
        questionnaire = QuestionnaireFactory.create(graph=graph)

        # Answer our questionnaire
        data = {'payload': 'answer-1', 'questionnaire': questionnaire.uuid}
        first_post_answer_endpoint = f'{self.base_endpoint}{questionnaire.first_question.uuid}/answer'
        response = self.client.post(first_post_answer_endpoint, data=data, format='json')
        self.assertEqual(response.status_code, 201)

        response_data = response.json()
        self.assertJsonSchema(self.post_answer_schema, response_data)
        self.assertIsNotNone(response_data['next_question'])

        self.assertEqual(1, Answer.objects.count())
        self.assertEqual(1, Session.objects.count())

        next_post_answer_endpoint = response_data['next_question']['_links']['sia:post-answer']['href']
        session = Session.objects.first()

        for x in range(2, 6):
            data = {'payload': f'answer-{x}', 'session': session.uuid}
            response = self.client.post(next_post_answer_endpoint, data=data, format='json')
            self.assertEqual(response.status_code, 201)

            response_data = response.json()
            self.assertJsonSchema(self.post_answer_schema, response_data)

            self.assertEqual(x, Answer.objects.count())
            self.assertEqual(1, Session.objects.count())

            if x < 5:
                self.assertIsNotNone(response_data['next_question'])
                next_post_answer_endpoint = response_data['next_question']['_links']['sia:post-answer']['href']
            else:
                self.assertIsNone(response_data['next_question'])  # session must be frozen using ../submit endpoint

        answer_qs = Answer.objects.filter(
            question_id__in=[
                questionnaire.graph.first_question.id,
                test_question_2.id,
                test_question_3.id,
                test_question_4.id,
                test_question_5.id,
            ],
            session_id=session.pk,
        )

        self.assertTrue(answer_qs.exists())
        self.assertEqual(5, answer_qs.count())