def test_answer_factory(self): AnswerFactory.create() self.assertEqual(Answer.objects.count(), 1) self.assertEqual(Session.objects.count(), 1) self.assertEqual(Questionnaire.objects.count(), 1) self.assertEqual(Question.objects.count(), 1)
def test_validate_question_graph_with_decision_with_default_not_required( self): graph = _question_graph_with_decision_with_default_no_required_answers( ) q_yes = Question.objects.get(retrieval_key='q_yes') q_no = Question.objects.get(retrieval_key='q_no') # Test default branch by not providing an answer, that will work because # the first question is not required. session_1 = SessionFactory.create(questionnaire__graph=graph) AnswerFactory(session=session_1, question=q_yes, payload='yes happy') session_service_1 = get_session_service(session_1.uuid) session_service_1.refresh_from_db() self.assertTrue(session_service_1.can_freeze) # Test default branch (by providing a non-matching, but valid answer). # This case will fail because the default branch is chosen. session_2 = SessionFactory.create(questionnaire__graph=graph) AnswerFactory(session=session_2, question=q_no, payload='not happy') session_service_2 = get_session_service(session_2.uuid) session_service_2.refresh_from_db() self.assertFalse(session_service_2.can_freeze)
def test_get_all_answers(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) for q in service.question_graph_service._questions: AnswerFactory.create(session=session, question=q, payload='answer') answers = service._get_all_answers(session) self.assertEqual(len(answers), 7)
def test_handle_frozen_session_on_commit_triggered(self): question = QuestionFactory.create(field_type='plain_text', label='Is het goed weer?', short_label='Goed weer?') session = SessionFactory.create( questionnaire__flow=Questionnaire.REACTION_REQUEST, questionnaire__first_question=question, frozen=False, _signal=self.signal_reaction_requested) answer = AnswerFactory(session=session, payload='Het antwoord!') submit_question = Question.objects.get_by_reference('submit') with self.captureOnCommitCallbacks(execute=False) as callbacks: QuestionnairesService.create_answer( answer_payload=None, question=submit_question, questionnaire=session.questionnaire, session=session) self.assertEqual(len(callbacks), 1) callbacks[0]() self.signal_reaction_requested.refresh_from_db() self.assertEqual(self.signal_reaction_requested.status.state, workflow.REACTIE_ONTVANGEN) self.assertEqual(self.signal_reaction_requested.status.text, answer.payload)
def test_handle_frozen_session_REACTION_REQUEST_wrong_flow(self): session = SessionFactory.create( questionnaire__flow=Questionnaire.REACTION_REQUEST, frozen=True, _signal=self.signal_reaction_requested) AnswerFactory(session=session, payload='Het antwoord!') service = get_session_service(session.uuid) self.assertIsInstance(service, ReactionRequestSessionService) session.questionnaire.flow = Questionnaire.EXTRA_PROPERTIES session.questionnaire.save() service.refresh_from_db() self.assertEqual(service.session.questionnaire.flow, Questionnaire.EXTRA_PROPERTIES) with self.assertRaises(WrongFlow): # Deal with CannotFreeze service.freeze()
def test_handle_frozen_session_REACTION_REQUEST(self): question = QuestionFactory.create(field_type='plain_text', label='Is het goed weer?', short_label='Goed weer?') session = SessionFactory.create( questionnaire__flow=Questionnaire.REACTION_REQUEST, questionnaire__first_question=question, frozen=True, _signal=self.signal_reaction_requested) answer = AnswerFactory(session=session, payload='Het antwoord!') ReactionRequestService.handle_frozen_session_REACTION_REQUEST(session) self.signal_reaction_requested.refresh_from_db() self.assertEqual(self.signal_reaction_requested.status.state, workflow.REACTIE_ONTVANGEN) self.assertEqual(self.signal_reaction_requested.status.text, answer.payload)
def test_validate_question_graph_with_decision(self): graph = _question_graph_with_decision() q_yes = Question.objects.get(retrieval_key='q_yes') q_no = Question.objects.get(retrieval_key='q_no') # Test yes branch session_1 = SessionFactory.create(questionnaire__graph=graph) AnswerFactory(session=session_1, question=graph.first_question, payload='yes') AnswerFactory(session=session_1, question=q_yes, payload='yes happy') session_service_1 = get_session_service(session_1.uuid) session_service_1.refresh_from_db() self.assertTrue(session_service_1.can_freeze) # Test no branch session_2 = SessionFactory.create(questionnaire__graph=graph) AnswerFactory(session=session_2, question=graph.first_question, payload='no') AnswerFactory(session=session_2, question=q_no, payload='no unhappy') session_service_2 = get_session_service(session_2.uuid) session_service_2.refresh_from_db() self.assertTrue(session_service_2.can_freeze) # Test missing data session_3 = SessionFactory.create(questionnaire__graph=graph) AnswerFactory(session=session_3, question=graph.first_question, payload='yes') session_service_3 = get_session_service(session_3.uuid) session_service_3.refresh_from_db() self.assertFalse(session_service_3.can_freeze) # Test showing a question halfway through the questionnaire can be # considered an endpoint. # TODO: consider whether this behavior is desirable (likely not). session_4 = SessionFactory.create(questionnaire__graph=graph) AnswerFactory(session=session_4, question=graph.first_question, payload='not a choice, but valid') session_service_4 = get_session_service(session_4.uuid) session_service_4.refresh_from_db() self.assertFalse(session_service_4.can_freeze)
def test_handle_frozen_session_on_commit_triggered(self): question = QuestionFactory.create( field_type='plain_text', label='Is het goed weer?', short_label='Goed weer?', analysis_key='reaction', ) graph = QuestionGraphFactory.create(name='Reactie gevraagd.', first_question=question) session = SessionFactory.create( questionnaire__flow=Questionnaire.REACTION_REQUEST, questionnaire__graph=graph, frozen=False, _signal=self.signal_reaction_requested ) answer = AnswerFactory(session=session, payload='Het antwoord!') service = get_session_service(session.uuid) self.assertIsInstance(service, ReactionRequestSessionService) service.freeze() self.signal_reaction_requested.refresh_from_db() self.assertEqual(self.signal_reaction_requested.status.state, workflow.REACTIE_ONTVANGEN) self.assertEqual(self.signal_reaction_requested.status.text, answer.payload)
def setUp(self): self.graph = _question_graph_with_decision() self.session = SessionFactory.create(questionnaire__graph=self.graph) with freeze_time('2021-08-17 12:00:00'): AnswerFactory.create(session=self.session, question=self.graph.first_question, payload='no') with freeze_time('2021-08-17 12:10:00'): AnswerFactory.create(session=self.session, question=self.graph.first_question, payload='yes') self.q2 = Question.objects.get(retrieval_key='q_yes') with freeze_time('2021-08-17 12:20:00'): AnswerFactory.create(session=self.session, question=self.q2, payload='yes happy')
def test_answer_question_detail_not_allowed(self): answer = AnswerFactory.create() response = self.client.get(f'{self.base_endpoint}{self.question.uuid}/answer/{answer.pk}') self.assertEqual(response.status_code, 404)