Example #1
0
    def test_get_next_question_with_choices_predefined(self):
        # Update QuestionGraph with predefined choices
        c2 = ChoiceFactory.create(payload='q2')
        c3 = ChoiceFactory.create(payload='q3')

        q_graph = create_diamond_plus()
        edge_to_q2 = Edge.objects.filter(question=q_graph.first_question,
                                         graph=q_graph,
                                         next_question__analysis_key='q2')
        edge_to_q3 = Edge.objects.filter(question=q_graph.first_question,
                                         graph=q_graph,
                                         next_question__analysis_key='q3')

        self.assertEqual(edge_to_q2.count(), 1)
        self.assertEqual(edge_to_q3.count(), 1)

        edge_to_q2.update(choice=c2)
        edge_to_q3.update(choice=c3)

        # ---
        session = SessionFactory.create(questionnaire__graph=q_graph)
        service = SessionService(session)
        service.question_graph_service.refresh_from_db()
        self.assertEqual(len(service.question_graph_service._questions_by_id),
                         7)

        a1 = Answer.objects.create(
            session=session,
            question=q_graph.first_question,
            payload=
            'NOT A PREDEFINED CHOICE',  # This is something we should not allow!
        )
        next_question_1 = service._get_next_question(
            service.question_graph_service._nx_graph,
            service.question_graph_service._questions_by_id,
            q_graph.first_question, a1.payload)
        self.assertIsNone(next_question_1)

        a2 = Answer.objects.create(session=session,
                                   question=q_graph.first_question,
                                   payload='q2')
        next_question_2 = service._get_next_question(
            service.question_graph_service._nx_graph,
            service.question_graph_service._questions_by_id,
            q_graph.first_question, a2.payload)
        self.assertEqual(next_question_2.analysis_key, 'q2')

        a3 = Answer.objects.create(session=session,
                                   question=q_graph.first_question,
                                   payload='q3')
        next_question_3 = service._get_next_question(
            service.question_graph_service._nx_graph,
            service.question_graph_service._questions_by_id,
            q_graph.first_question, a3.payload)
        self.assertEqual(next_question_3.analysis_key, 'q3')
Example #2
0
    def test_get_next_question_no_choices_predefined(self):
        q_graph = create_diamond_plus()
        session = SessionFactory.create(questionnaire__graph=q_graph)
        service = SessionService(session)
        service.question_graph_service.refresh_from_db()
        self.assertEqual(len(service.question_graph_service._questions_by_id),
                         7)

        # First question in "diamond_plus" QuestionGraph is a decision point,
        # the create_diamond_plus function does not set choices or edge order
        # explicitly. We test the decision point here by answering the first
        # question, determining the next question and reordering the edges
        # and checking we get the other branch.
        a = Answer.objects.create(session=session,
                                  question=q_graph.first_question,
                                  payload='answer')
        next_question_1 = service._get_next_question(
            service.question_graph_service._nx_graph,
            service.question_graph_service._questions_by_id,
            q_graph.first_question, a.payload)

        # First set order to the old order, nothing should change.
        edge_ids_before = q_graph.get_edge_order(q_graph.first_question)
        edge_ids_after = q_graph.set_edge_order(q_graph.first_question,
                                                edge_ids_before)
        service.question_graph_service.refresh_from_db(
        )  # reload, because cache is now stale

        self.assertEqual(list(edge_ids_before), list(edge_ids_after))
        next_question_2 = service._get_next_question(
            service.question_graph_service._nx_graph,
            service.question_graph_service._questions_by_id,
            q_graph.first_question, a.payload)
        self.assertEqual(next_question_1.id,
                         next_question_2.id)  # nothing should change

        # Now change the order of outgoing edges from q_graph.first_question
        edge_ids_before = q_graph.get_edge_order(q_graph.first_question)
        new_order = list(reversed(edge_ids_before))
        edge_ids_after = q_graph.set_edge_order(q_graph.first_question,
                                                new_order)
        self.assertNotEqual(list(edge_ids_after), list(edge_ids_before))
        service.question_graph_service.refresh_from_db(
        )  # reload, because cache is now stale

        self.assertEqual(list(edge_ids_after), list(new_order))
        next_question_3 = service._get_next_question(
            service.question_graph_service._nx_graph,
            service.question_graph_service._questions_by_id,
            q_graph.first_question, a.payload)
        self.assertNotEqual(next_question_1.id,
                            next_question_3.id)  # should have changed