def test_should_return_answers_for_completed_or_scheduled_runs_only(self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory( label='deliveryReceived', flow=flow, text='Was Delivery Received?', position=3) question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?', position=1) question_3 = TextQuestionFactory( label='satisfiedWithProduct', flow=flow, text='Are you satisfied with product?', position=2) option_no = OptionFactory(text='No', question=question_1) option_yes = OptionFactory(text='Yes', question=question_1) run_one = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(question=question_1, value=option_no, run=run_one) TextAnswerFactory(question=question_2, value="2014-10-10", run=run_one) TextAnswerFactory(question=question_3, value="yup", run=run_one) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]['value'], '2014-10-10') run_one.status = 'cancelled' run_one.save() answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]['value'], '') run_two = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(question=question_1, value=option_yes, run=run_two) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]['value'], '') self.assertEqual(answers[1]['value'], '') self.assertEqual(answers[2]['value'], 'Yes')
def test_should_record_an_answer_of_type_multiple_choice_for_a_node_from_request_data(self, *_): uuid = '2ff9fab3-4c12-400e-a2fe-4551fa1ebc18' question = MultipleChoiceQuestionFactory(uuids=[uuid], text='Was item received?', label='productReceived', flow=self.flow) Option.objects.get_or_create(text='Yes', question=question) Option.objects.get_or_create(text='No', question=question) delivery = DeliveryFactory() run = RunFactory(phone=self.PHONE, runnable=delivery) url_params = self._create_rapid_pro_url_params(self.PHONE, uuid, 'Yes', 'Yes', 'productReceived') response = self.client.post(HOOK_URL, url_params) expected_question = MultipleChoiceQuestion.objects.get(uuids=[uuid]) yes_option = expected_question.option_set.get(text='Yes') answers = MultipleChoiceAnswer.objects.filter(question__uuids=[uuid], run=run) created_answer = answers.first() self.assertEqual(response.status_code, 200) self.assertEqual(created_answer.value, yes_option) self.assertEqual(delivery.answers()[0]['value'], created_answer.value.text)
def test_should_return_default_answers_for_delivery(self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory(label='deliveryReceived', flow=flow, text='Was Delivery Received?') question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?') OptionFactory(text='Yes', question=question_1) RunFactory(runnable=delivery) expected_multiple_choice_answer = { 'question_label': question_1.label, 'type': 'multipleChoice', 'text': question_1.text, 'value': '', 'options': ['Yes'], 'position': question_1.position } expected_text_answer = { 'question_label': question_2.label, 'type': 'text', 'text': question_2.text, 'value': '', 'position': question_2.position } answers = delivery.answers() self.assertEqual(len(answers), 2) self.assertIn(expected_multiple_choice_answer, answers) self.assertIn(expected_text_answer, answers)
def test_should_record_an_answer_of_type_multiple_choice_for_a_node_from_request_data( self, *_): uuid = '5b0f1f19-767f-47f1-97a5-b9b32c45a47c' question = MultipleChoiceQuestionFactory(text='Was product received?', label='productReceived', flow=self.flow) Option.objects.get_or_create(text='Yes', question=question) Option.objects.get_or_create(text='No', question=question) delivery = DeliveryFactory() run = RunFactory(phone=self.PHONE, runnable=delivery) url_params = self._create_rapid_pro_url_params(self.PHONE, uuid, 'Yes', 'Yes', 'productReceived') response = self.client.post(HOOK_URL, url_params) expected_question = MultipleChoiceQuestion.objects.get( label='productReceived') yes_option = expected_question.option_set.get(text='Yes') answers = MultipleChoiceAnswer.objects.filter( question__label='productReceived', run=run) created_answer = answers.first() self.assertEqual(response.status_code, 200) self.assertEqual(created_answer.value, yes_option) self.assertEqual(delivery.answers()[0]['value'], created_answer.value.text)
def test_should_return_answers_for_delivery_in_the_same_order_as_flow(self): delivery = DeliveryFactory() flow = FlowFactory(for_runnable_type="IMPLEMENTING_PARTNER") question_1 = MultipleChoiceQuestionFactory( label="deliveryReceived", flow=flow, text="Was Delivery Received?", position=3 ) question_2 = TextQuestionFactory( label="dateOfReceipt", flow=flow, text="When was Delivery Received?", position=1 ) question_3 = TextQuestionFactory( label="satisfiedWithProduct", flow=flow, text="Are you satisfied with product?", position=2 ) OptionFactory(text="No", question=question_1) OptionFactory(text="Yes", question=question_1) RunFactory(runnable=delivery) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(question_2.label, answers[0]["question_label"]) self.assertEqual(question_3.label, answers[1]["question_label"]) self.assertEqual(question_1.label, answers[2]["question_label"])
def test_should_return_default_answers_for_delivery(self): delivery = DeliveryFactory() flow = FlowFactory(for_runnable_type="IMPLEMENTING_PARTNER") question_1 = MultipleChoiceQuestionFactory(label="deliveryReceived", flow=flow, text="Was Delivery Received?") question_2 = TextQuestionFactory(label="dateOfReceipt", flow=flow, text="When was Delivery Received?") OptionFactory(text="Yes", question=question_1) RunFactory(runnable=delivery) expected_multiple_choice_answer = { "question_label": question_1.label, "type": "multipleChoice", "text": question_1.text, "value": "", "options": ["Yes"], "position": question_1.position, } expected_text_answer = { "question_label": question_2.label, "type": "text", "text": question_2.text, "value": "", "position": question_2.position, } answers = delivery.answers() self.assertEqual(len(answers), 2) self.assertIn(expected_multiple_choice_answer, answers) self.assertIn(expected_text_answer, answers)
def test_should_return_answers_for_delivery_in_the_same_order_as_flow( self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory( label='deliveryReceived', flow=flow, text='Was Delivery Received?', position=3) question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?', position=1) question_3 = TextQuestionFactory( label='satisfiedWithProduct', flow=flow, text='Are you satisfied with product?', position=2) OptionFactory(text='No', question=question_1) OptionFactory(text='Yes', question=question_1) RunFactory(runnable=delivery) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(question_2.label, answers[0]['question_label']) self.assertEqual(question_3.label, answers[1]['question_label']) self.assertEqual(question_1.label, answers[2]['question_label'])
def test_should_return_default_answers_for_delivery(self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory( label='deliveryReceived', flow=flow, text='Was Delivery Received?') question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?') OptionFactory(text='Yes', question=question_1) RunFactory(runnable=delivery) expected_multiple_choice_answer = { 'question_label': question_1.label, 'type': 'multipleChoice', 'text': question_1.text, 'value': '', 'options': ['Yes'], 'position': question_1.position } expected_text_answer = { 'question_label': question_2.label, 'type': 'text', 'text': question_2.text, 'value': '', 'position': question_2.position } answers = delivery.answers() self.assertEqual(len(answers), 2) self.assertIn(expected_multiple_choice_answer, answers) self.assertIn(expected_text_answer, answers)
def test_should_return_answers_for_completed_or_scheduled_runs_only(self): delivery = DeliveryFactory() flow = FlowFactory(for_runnable_type="IMPLEMENTING_PARTNER") question_1 = MultipleChoiceQuestionFactory( label="deliveryReceived", flow=flow, text="Was Delivery Received?", position=3 ) question_2 = TextQuestionFactory( label="dateOfReceipt", flow=flow, text="When was Delivery Received?", position=1 ) question_3 = TextQuestionFactory( label="satisfiedWithProduct", flow=flow, text="Are you satisfied with product?", position=2 ) option_no = OptionFactory(text="No", question=question_1) option_yes = OptionFactory(text="Yes", question=question_1) run_one = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(question=question_1, value=option_no, run=run_one) TextAnswerFactory(question=question_2, value="2014-10-10", run=run_one) TextAnswerFactory(question=question_3, value="yup", run=run_one) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]["value"], "2014-10-10") run_one.status = "cancelled" run_one.save() answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]["value"], "") run_two = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(question=question_1, value=option_yes, run=run_two) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]["value"], "") self.assertEqual(answers[1]["value"], "") self.assertEqual(answers[2]["value"], "Yes")
def test_should_return_answers_for_completed_or_scheduled_runs_only(self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory(label='deliveryReceived', flow=flow, text='Was Delivery Received?', position=3) question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?', position=1) question_3 = TextQuestionFactory(label='satisfiedWithProduct', flow=flow, text='Are you satisfied with product?', position=2) option_no = OptionFactory(text='No', question=question_1) option_yes = OptionFactory(text='Yes', question=question_1) run_one = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(question=question_1, value=option_no, run=run_one) TextAnswerFactory(question=question_2, value="2014-10-10", run=run_one) TextAnswerFactory(question=question_3, value="yup", run=run_one) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]['value'], '2014-10-10') run_one.status = 'cancelled' run_one.save() answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]['value'], '') run_two = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(question=question_1, value=option_yes, run=run_two) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(answers[0]['value'], '') self.assertEqual(answers[1]['value'], '') self.assertEqual(answers[2]['value'], 'Yes')
def test_should_return_answers_for_delivery(self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory( label='deliveryReceived', flow=flow, text='Was Delivery Received?') question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?') question_3 = NumericQuestionFactory(text='How much was received?', label='amountReceived', flow=flow) option_yes = OptionFactory(text='Yes', question=question_1) run = RunFactory(runnable=delivery) multiple_choice_answer = MultipleChoiceAnswerFactory( run=run, question=question_1, value=option_yes) text_answer = TextAnswerFactory(run=run, question=question_2, value='2015-10-10') numeric_answer = NumericAnswerFactory(run=run, question=question_3, value=10) expected_multiple_choice_answer = { 'question_label': question_1.label, 'type': 'multipleChoice', 'text': question_1.text, 'value': multiple_choice_answer.value.text, 'options': ['Yes'], 'position': question_1.position } expected_text_answer = { 'question_label': question_2.label, 'type': 'text', 'text': question_2.text, 'value': text_answer.value, 'position': question_2.position } expected_numeric_answer = { 'question_label': question_3.label, 'type': 'numeric', 'text': question_3.text, 'value': numeric_answer.value, 'position': question_3.position } answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertIn(expected_multiple_choice_answer, answers) self.assertIn(expected_text_answer, answers) self.assertIn(expected_numeric_answer, answers)
def test_should_return_answers_for_delivery(self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory(label='deliveryReceived', flow=flow, text='Was Delivery Received?') question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?') question_3 = NumericQuestionFactory(text='How much was received?', label='amountReceived', flow=flow) option_yes = OptionFactory(text='Yes', question=question_1) run = RunFactory(runnable=delivery) multiple_choice_answer = MultipleChoiceAnswerFactory(run=run, question=question_1, value=option_yes) text_answer = TextAnswerFactory(run=run, question=question_2, value='2015-10-10') numeric_answer = NumericAnswerFactory(run=run, question=question_3, value=10) expected_multiple_choice_answer = { 'question_label': question_1.label, 'type': 'multipleChoice', 'text': question_1.text, 'value': multiple_choice_answer.value.text, 'options': ['Yes'], 'position': question_1.position } expected_text_answer = { 'question_label': question_2.label, 'type': 'text', 'text': question_2.text, 'value': text_answer.value, 'position': question_2.position } expected_numeric_answer = { 'question_label': question_3.label, 'type': 'numeric', 'text': question_3.text, 'value': numeric_answer.value, 'position': question_3.position } answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertIn(expected_multiple_choice_answer, answers) self.assertIn(expected_text_answer, answers) self.assertIn(expected_numeric_answer, answers)
def test_should_return_answers_for_delivery(self): delivery = DeliveryFactory() flow = FlowFactory(for_runnable_type="IMPLEMENTING_PARTNER") question_1 = MultipleChoiceQuestionFactory(label="deliveryReceived", flow=flow, text="Was Delivery Received?") question_2 = TextQuestionFactory(label="dateOfReceipt", flow=flow, text="When was Delivery Received?") question_3 = NumericQuestionFactory(text="How much was received?", label="amountReceived", flow=flow) option_yes = OptionFactory(text="Yes", question=question_1) run = RunFactory(runnable=delivery) multiple_choice_answer = MultipleChoiceAnswerFactory(run=run, question=question_1, value=option_yes) text_answer = TextAnswerFactory(run=run, question=question_2, value="2015-10-10") numeric_answer = NumericAnswerFactory(run=run, question=question_3, value=10) expected_multiple_choice_answer = { "question_label": question_1.label, "type": "multipleChoice", "text": question_1.text, "value": multiple_choice_answer.value.text, "options": ["Yes"], "position": question_1.position, } expected_text_answer = { "question_label": question_2.label, "type": "text", "text": question_2.text, "value": text_answer.value, "position": question_2.position, } expected_numeric_answer = { "question_label": question_3.label, "type": "numeric", "text": question_3.text, "value": numeric_answer.value, "position": question_3.position, } answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertIn(expected_multiple_choice_answer, answers) self.assertIn(expected_text_answer, answers) self.assertIn(expected_numeric_answer, answers)
def test_should_return_answers_for_delivery_in_the_same_order_as_flow(self): delivery = DeliveryFactory() flow = FlowFactory(label='IMPLEMENTING_PARTNER') question_1 = MultipleChoiceQuestionFactory(label='deliveryReceived', flow=flow, text='Was Delivery Received?', position=3) question_2 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?', position=1) question_3 = TextQuestionFactory(label='satisfiedWithProduct', flow=flow, text='Are you satisfied with product?', position=2) OptionFactory(text='No', question=question_1) OptionFactory(text='Yes', question=question_1) RunFactory(runnable=delivery) answers = delivery.answers() self.assertEqual(len(answers), 3) self.assertEqual(question_2.label, answers[0]['question_label']) self.assertEqual(question_3.label, answers[1]['question_label']) self.assertEqual(question_1.label, answers[2]['question_label'])