Exemplo n.º 1
0
    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 _create_questions(self):
        flow = FlowFactory(label='IMPLEMENTING_PARTNER')

        self.delivery_received_qtn = MultipleChoiceQuestionFactory(text='Was delivery received?', flow=flow,
                                                                   position=1,
                                                                   label=Question.LABEL.deliveryReceived)
        self.yes_one = OptionFactory(text='Yes', question=self.delivery_received_qtn)
        self.no_one = OptionFactory(text='No', question=self.delivery_received_qtn)

        self.date_received_qtn = TextQuestionFactory(text='When was delivery received?', flow=flow, position=2,
                                                     label='dateOfReceipt')

        self.delivery_in_good_order = MultipleChoiceQuestionFactory(text='Was delivery in good condition?',
                                                                    flow=flow, position=3,
                                                                    label=Question.LABEL.isDeliveryInGoodOrder)
        self.yes_two = OptionFactory(text='Yes', question=self.delivery_in_good_order)
        self.no_two = OptionFactory(text='No', question=self.delivery_in_good_order)

        self.satisfied_with_delivery = MultipleChoiceQuestionFactory(text="Are you satisfied with the delivery?",
                                                                     flow=flow, position=4,
                                                                     label="satisfiedWithDelivery")
        self.yes_three = OptionFactory(text="Yes", question=self.satisfied_with_delivery)
        self.no_three = OptionFactory(text="No", question=self.satisfied_with_delivery)

        self.additional_comments = TextQuestionFactory(text='Additional Remarks', flow=flow, position=5,
                                                       label='additionalDeliveryComments')
Exemplo n.º 3
0
    def test_should_get_questions_sorted_by_text(self):
        text_question_one = TextQuestionFactory(text='B')
        text_question_two = TextQuestionFactory(text='A')
        expected_data_one = self.expected_response_data(text_question_one)
        expected_data_two = self.expected_response_data(text_question_two)

        get_response = self.client.get(ENDPOINT_URL)

        self.assertEqual(get_response.status_code, 200)
        self.assertDictContainsSubset(expected_data_two, get_response.data[0])
        self.assertDictContainsSubset(expected_data_one, get_response.data[1])
Exemplo n.º 4
0
    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')
Exemplo n.º 5
0
    def test_should_search_for_question_by_label(self):
        text_question_one = TextQuestionFactory(label='productReceived')
        text_question_two = TextQuestionFactory(label='satisfied')
        expected_data_one = self.expected_response_data(text_question_one)
        expected_data_two = self.expected_response_data(text_question_two)

        get_response = self.client.get(ENDPOINT_URL +
                                       '?search=productReceived')

        self.assertEqual(get_response.status_code, 200)
        self.assertDictContainsSubset(expected_data_one, get_response.data[0])
        self.assertNotIn(expected_data_two, get_response.data)
Exemplo n.º 6
0
 def setup_flow_with_questions(self, flow_type):
     flow = FlowFactory(label=flow_type)
     delivery_received_qn = MultipleChoiceQuestionFactory(label='deliveryReceived', flow=flow)
     OptionFactory(question=delivery_received_qn, text='Yes')
     OptionFactory(question=delivery_received_qn, text='No')
     TextQuestionFactory(label='dateOfReceipt', flow=flow)
     good_order_qn = MultipleChoiceQuestionFactory(label='isDeliveryInGoodOrder', flow=flow)
     OptionFactory(question=good_order_qn, text='Yes')
     OptionFactory(question=good_order_qn, text='No')
     OptionFactory(question=good_order_qn, text='Incomplete')
     satisfied_qn = MultipleChoiceQuestionFactory(label='areYouSatisfied', flow=flow)
     OptionFactory(question=satisfied_qn, text='Yes')
     OptionFactory(question=satisfied_qn, text='No')
     TextQuestionFactory(label='additionalDeliveryComments', flow=flow)
Exemplo n.º 7
0
    def setup_multiple_nodes_with_answers(self, number_of_nodes):
        consignee_one = ConsigneeFactory(name='consignee one')
        programme_one = ProgrammeFactory(name='my first programme')
        po_item = PurchaseOrderItemFactory(item=ItemFactory(description='Mama kit'),
                                           purchase_order=PurchaseOrderFactory(order_number=329293))

        flow = FlowFactory(label='WEB')
        question_1 = MultipleChoiceQuestionFactory(text='Was the item received?', label='itemReceived', flow=flow,
                                                   position=1)
        option_1 = OptionFactory(text='Yes', question=question_1)
        question_2 = NumericQuestionFactory(text='How much was received?', label='amountReceived', flow=flow)
        question_3 = MultipleChoiceQuestionFactory(text='What is the quality of the product?', label='qualityOfProduct',
                                                   flow=flow, position=3)
        option_3 = OptionFactory(text='Damaged', question=question_3)
        question_4 = MultipleChoiceQuestionFactory(text='Are you satisfied with the product?',
                                                   label='satisfiedWithProduct', flow=flow, position=4)
        option_4 = OptionFactory(text='Yes', question=question_4)
        question_5 = TextQuestionFactory(label='dateOfReceipt', flow=flow, text='When was Delivery Received?')
        nodes = []

        for index in range(number_of_nodes):
            node = DeliveryNodeFactory(consignee=consignee_one, item=po_item, programme=programme_one,
                                       distribution_plan=DeliveryFactory(track=True),
                                       track=True)

            run_one = RunFactory(runnable=node)
            MultipleChoiceAnswerFactory(question=question_1, run=run_one, value=option_1)
            NumericAnswerFactory(question=question_2, run=run_one, value=5)
            MultipleChoiceAnswerFactory(question=question_3, run=run_one, value=option_3)
            MultipleChoiceAnswerFactory(question=question_4, run=run_one, value=option_4)
            TextAnswerFactory(run=run_one, question=question_5, value='2014-10-10')
            nodes.append(node)

        return nodes
Exemplo n.º 8
0
    def test_should_save_text_answer(self):
        text = 'Some text'
        params = {'text': text}
        self.text_question.create_answer(params, self.run)
        answers = self.text_question.textanswer_set.all()

        self.assertEqual(answers.count(), 1)
        self.assertEqual(answers.first().value, text)

        self.text_question = TextQuestionFactory(text='When did you receive items', label="dateOfReceipt")
        params = {'text': self.__get_current_date('%d/%m/%Y')}

        self.text_question.create_answer(params, self.run)
        answers = self.text_question.textanswer_set.all()

        self.assertEqual(answers.first().value, self.__get_current_date('%Y-%m-%d'))
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    def test_should_get_question(self):
        text_question = TextQuestionFactory()
        expected_data = self.expected_response_data(text_question)

        get_response = self.client.get(ENDPOINT_URL)

        self.assertEqual(get_response.status_code, 200)
        self.assertDictContainsSubset(expected_data, get_response.data[0])
Exemplo n.º 11
0
    def test_questions_should_have_type_with_default_values(self):
        text_question = TextQuestionFactory()
        self.assertEqual(text_question.type, 'text')

        numeric_question = NumericQuestionFactory()
        self.assertEqual(numeric_question.type, 'numeric')

        multiple_choice_question = MultipleChoiceQuestionFactory()
        self.assertEqual(multiple_choice_question.type, 'multipleChoice')
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
class TextQuestionTest(QuestionTest):
    def test_should_save_text_answer(self):
        text = 'Some text'
        params = {'text': text}
        self.text_question.create_answer(params, self.run)
        answers = self.text_question.textanswer_set.all()

        self.assertEqual(answers.count(), 1)
        self.assertEqual(answers.first().value, text)

        self.text_question = TextQuestionFactory(text='When did you receive items', label="dateOfReceipt")
        params = {'text': self.__get_current_date('%d/%m/%Y')}

        self.text_question.create_answer(params, self.run)
        answers = self.text_question.textanswer_set.all()

        self.assertEqual(answers.first().value, self.__get_current_date('%Y-%m-%d'))

    def __get_current_date(self, pattern):
        return datetime.datetime.strftime(datetime.datetime.now().date(), pattern)
Exemplo n.º 14
0
    def test_should_update_text_answers(self):
        text_question = TextQuestionFactory(label='dateOfReceipt')
        text_answer = TextAnswerFactory(value="6/10/2014",
                                        question=text_question)
        updated_text_answer_details = {"value": "5/10/2014"}
        response = self.client.patch(ENDPOINT_URL + str(text_answer.id) + "/",
                                     updated_text_answer_details,
                                     format='json')

        self.assertEqual(response.status_code, 200)
        self.assertDictContainsSubset(updated_text_answer_details,
                                      response.data)
Exemplo n.º 15
0
    def test_should_get_date_answers(self):
        date_question = TextQuestionFactory(label='dateOfReceipt')
        date_answer = TextAnswerFactory(value="6/10/2014",
                                        question=date_question)
        date_answer_details = {
            "value": date_answer.value,
            "question": date_answer.question_id
        }
        response = self.client.get(ENDPOINT_URL)

        self.assertEqual(response.status_code, 200)
        self.assertDictContainsSubset(date_answer_details, response.data[0])
Exemplo n.º 16
0
    def test_should_serialise_node_with_delivery_delay(self):
        expected_delivery_date = timezone.datetime(2015, 1, 1).date()
        date_of_receipt = timezone.datetime(2015, 1, 10).date()
        node = DeliveryNodeFactory(delivery_date=expected_delivery_date)
        date_received_question = TextQuestionFactory(label='dateOfReceipt')
        run = RunFactory(runnable=node)
        TextAnswerFactory(question=date_received_question,
                          run=run,
                          value=date_of_receipt)

        serialised = serialise_nodes([node])
        self.assertDictContainsSubset({'delivery_delay': 9}, serialised[0])
Exemplo n.º 17
0
    def test_should_add_node_related_to_changed_question_to_sync_data(
            self, mock_scan):
        question = TextQuestionFactory(text='original')
        node = DeliveryNodeFactory()
        TextAnswerFactory(question=question, run=(RunFactory(runnable=node)))

        self.check_update_happens(node, question, {
            'field': 'text',
            'value': 'changed'
        }, {'term': {
            'responses.question.id': [question.id]
        }}, mock_scan)
Exemplo n.º 18
0
    def test_should_add_node_related_to_changed_text_answer_to_sync_data(
            self, mock_scan):
        node = DeliveryNodeFactory()
        answer = TextAnswerFactory(value='original',
                                   question=TextQuestionFactory(),
                                   run=RunFactory(runnable=node))

        self.check_update_happens(node, answer, {
            'field': 'value',
            'value': 'changed'
        }, {'term': {
            'responses.id': [answer.id]
        }}, mock_scan)
Exemplo n.º 19
0
 def test_should_add_node_related_to_deleted_text_answer_to_sync_data(
         self, mock_scan):
     node = DeliveryNodeFactory()
     answer = TextAnswerFactory(run=RunFactory(runnable=node),
                                question=TextQuestionFactory())
     self.check_update_happens(
         node,
         answer,
         delete=True,
         mock_scan=mock_scan,
         expected_match_clause={'term': {
             'id': [node.id]
         }})
Exemplo n.º 20
0
    def test_should_create_text_answers(self):
        text_question = TextQuestionFactory(label='dateOfReceipt')
        run = RunFactory()

        text_answer_details = {
            "value": "1",
            "question": text_question.id,
            "run": run.id
        }
        response = self.client.post(ENDPOINT_URL,
                                    text_answer_details,
                                    format='json')

        self.assertEqual(response.status_code, 201)
        self.assertDictContainsSubset(text_answer_details, response.data)
Exemplo n.º 21
0
    def test_should_return_a_deliveries_answers(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?')

        option_yes = OptionFactory(text='Yes', question=question_1)

        run = RunFactory(runnable=delivery)

        MultipleChoiceAnswerFactory(run=run, question=question_1, value=option_yes)
        TextAnswerFactory(run=run, question=question_2, value='2015-10-10')

        response = self.client.get('%s%d/%s/' % (ENDPOINT_URL, delivery.id, 'answers'))

        self.assertEqual(len(response.data), 2)
Exemplo n.º 22
0
    def test_should_record_an_answer_of_type_text_for_a_node_from_request_data(
            self, *_):
        uuid = 'b3fad71f-ca0a-4212-b7f9-892dd3dc4c4b'

        TextQuestionFactory(text='What date was it received?',
                            label='dateOfReceipt',
                            flow=self.flow)

        run = RunFactory(phone=('%s' % self.PHONE))
        url_params = self._create_rapid_pro_url_params(self.PHONE, uuid,
                                                       '21/12/2015', None,
                                                       'dateOfReceipt')

        response = self.client.post(HOOK_URL, url_params)

        answers = TextAnswer.objects.filter(question__label='dateOfReceipt',
                                            run=run)
        created_answer = answers.first()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(created_answer.value, '2015-12-21')
Exemplo n.º 23
0
    def test_should_return_answers_to_top_level_nodes_of_a_delivery(self):
        delivery = DeliveryFactory()
        node_one = DeliveryNodeFactory(distribution_plan=delivery)
        node_two = DeliveryNodeFactory(distribution_plan=delivery)

        flow = FlowFactory(label='WEB')

        question_1 = MultipleChoiceQuestionFactory(text='Was the item received?', label='itemReceived', flow=flow,
                                                   position=1)
        option_1 = OptionFactory(text='Yes', question=question_1)

        question_2 = NumericQuestionFactory(text='How much was received?', label='amountReceived', flow=flow)

        question_3 = MultipleChoiceQuestionFactory(text='What is the quality of the product?', label='qualityOfProduct',
                                                   flow=flow, position=3)
        option_3 = OptionFactory(text='Damaged', question=question_3)

        question_4 = MultipleChoiceQuestionFactory(text='Are you satisfied with the product?',
                                                   label='satisfiedWithProduct', flow=flow, position=4)
        option_4 = OptionFactory(text='Yes', question=question_4)

        question_5 = TextQuestionFactory(text='Remarks', label='additionalDeliveryComments',
                                         flow=flow, position=5)
        run_one = RunFactory(runnable=node_one)
        MultipleChoiceAnswerFactory(question=question_1, run=run_one, value=option_1)
        NumericAnswerFactory(question=question_2, run=run_one, value=5)
        MultipleChoiceAnswerFactory(question=question_3, run=run_one, value=option_3)
        MultipleChoiceAnswerFactory(question=question_4, run=run_one, value=option_4)
        TextAnswerFactory(question=question_5, run=run_one, value='Answer1')

        run_two = RunFactory(runnable=node_two)
        MultipleChoiceAnswerFactory(question=question_1, run=run_two, value=option_1)
        NumericAnswerFactory(question=question_2, run=run_two, value=3)
        MultipleChoiceAnswerFactory(question=question_3, run=run_two, value=option_3)
        MultipleChoiceAnswerFactory(question=question_4, run=run_two, value=option_4)
        TextAnswerFactory(question=question_5, run=run_two, value='Answer2')

        response = self.client.get('%s%d/%s/' % (ENDPOINT_URL, delivery.id, 'node_answers'))

        self.assertEqual(len(response.data), 2)
Exemplo n.º 24
0
 def __prepare_for_is_distribution_expired_test(self, delivery):
     flow = Flow.objects.get(label=Flow.Label.IMPLEMENTING_PARTNER)
     question_is_received = MultipleChoiceQuestionFactory(
         label=Question.LABEL.deliveryReceived, flow=flow)
     question_received_date = TextQuestionFactory(
         label=Question.LABEL.dateOfReceipt, flow=flow)
     option_is_received = OptionFactory(text='Yes',
                                        question=question_is_received)
     run = RunFactory(runnable=delivery)
     DeliveryNodeFactory(distribution_plan=delivery,
                         tree_position=Flow.Label.IMPLEMENTING_PARTNER)
     DeliveryNodeFactory(distribution_plan=delivery,
                         tree_position=Flow.Label.IMPLEMENTING_PARTNER)
     MultipleChoiceAnswerFactory(run=run,
                                 question=question_is_received,
                                 value=option_is_received)
     datetime_span = (
         datetime.datetime.today() -
         datetime.timedelta(days=8)).strftime('%Y-%m-%dT%H:%M:%S')
     TextAnswerFactory(run=run,
                       question=question_received_date,
                       value=datetime_span)
Exemplo n.º 25
0
    def test_should_get_all_questions_and_responses(self):
        run = RunFactory()

        item_received_question = MultipleChoiceQuestionFactory(
            label='product_received')
        yes = OptionFactory(question=item_received_question, text='Yes')
        item_received_question.answers.create(value=yes, run=run)

        date_received_question = TextQuestionFactory(label='date_received')
        date_received_question.textanswer_set.create(value='2014-01-01',
                                                     run=run)

        quantity_received_question = NumericQuestionFactory(
            label='quantity_received')
        quantity_received_question.numericanswer_set.create(value=12, run=run)

        expected_data = {
            'product_received': 'Yes',
            'quantity_received': 12,
            'date_received': '2014-01-01'
        }
        self.assertDictEqual(run.questions_and_responses(), expected_data)
Exemplo n.º 26
0
    def test_should_filter_alerts_by_runnable_type_when_distribution(self):
        delivery = DeliveryFactory(time_limitation_on_distribution=3)
        question_received_date = TextQuestionFactory(
            label=Question.LABEL.dateOfReceipt, flow=self.flow_ip)
        run = RunFactory(runnable=delivery)
        DeliveryNodeFactory(distribution_plan=delivery,
                            tree_position=Flow.Label.IMPLEMENTING_PARTNER)
        received_date = '2015-11-20T16:00:00'
        TextAnswerFactory(run=run,
                          question=question_received_date,
                          value=received_date)

        distribution_alert = AlertFactory(
            runnable=delivery, issue=Alert.ISSUE_TYPES.distribution_expired)
        distribution_alert_ids, distribution_alerts = self.__respond_by_alert_type(
            'distribution')

        self.assertEqual(Alert.objects.count(), 1)
        self.assertTrue(distribution_alerts[0]['date_received'])
        self.assertEqual(distribution_alerts[0]['date_received'],
                         received_date)
        self.assertEqual(len(distribution_alerts), 1)
        self.assertIn(distribution_alert.id, distribution_alert_ids)
Exemplo n.º 27
0
 def test_should_not_call_post_create_answer_hook_for_questions_that_have_none(self, mock_run):
     question = TextQuestionFactory()
     TextAnswerFactory(question=question)
     self.assertEqual(mock_run.call_count, 0)
Exemplo n.º 28
0
 def setup_questions(self):
     TextQuestionFactory(label='dateOfReceipt', flow=self.ip_flow)
     NumericQuestionFactory(label='amountReceived', flow=self.ip_flow)
Exemplo n.º 29
0
 def test_should_call_post_create_answer_hook_on_save_if_question_specifies_a_hook(self, mock_constructor, mock_run):
     mock_constructor.return_value = None
     question = TextQuestionFactory(when_answered='update_consignee_stock_level')
     answer = TextAnswerFactory(question=question)
     mock_constructor.assert_called_with(answer)
     mock_run.assert_called()
Exemplo n.º 30
0
    def test_should_return_answers_for_all_first_level_nodes_in_a_delivery(
            self):
        delivery = DeliveryFactory()
        node_one = DeliveryNodeFactory(distribution_plan=delivery)
        node_two = DeliveryNodeFactory(distribution_plan=delivery)

        flow = FlowFactory(label='WEB')

        question_1 = MultipleChoiceQuestionFactory(
            text='Was the item received?',
            label='itemReceived',
            flow=flow,
            position=1)
        option_1 = OptionFactory(text='Yes', question=question_1)

        question_2 = NumericQuestionFactory(text='How much was received?',
                                            label='amountReceived',
                                            flow=flow)

        question_3 = MultipleChoiceQuestionFactory(
            text='What is the quality of the product?',
            label='qualityOfProduct',
            flow=flow,
            position=3)
        option_3 = OptionFactory(text='Damaged', question=question_3)

        question_4 = MultipleChoiceQuestionFactory(
            text='Are you satisfied with the product?',
            label='satisfiedWithProduct',
            flow=flow,
            position=4)
        option_4 = OptionFactory(text='Yes', question=question_4)

        question_5 = TextQuestionFactory(text='Remarks',
                                         label='additionalDeliveryComments',
                                         flow=flow,
                                         position=5)
        run_one = RunFactory(runnable=node_one)
        MultipleChoiceAnswerFactory(question=question_1,
                                    run=run_one,
                                    value=option_1)
        NumericAnswerFactory(question=question_2, run=run_one, value=5)
        MultipleChoiceAnswerFactory(question=question_3,
                                    run=run_one,
                                    value=option_3)
        MultipleChoiceAnswerFactory(question=question_4,
                                    run=run_one,
                                    value=option_4)
        TextAnswerFactory(question=question_5, run=run_one, value="Answer")

        run_two = RunFactory(runnable=node_two)
        MultipleChoiceAnswerFactory(question=question_1,
                                    run=run_two,
                                    value=option_1)
        NumericAnswerFactory(question=question_2, run=run_two, value=3)
        MultipleChoiceAnswerFactory(question=question_3,
                                    run=run_two,
                                    value=option_3)
        MultipleChoiceAnswerFactory(question=question_4,
                                    run=run_two,
                                    value=option_4)
        TextAnswerFactory(question=question_5, run=run_two, value="Answer")

        node_answers = delivery.node_answers()

        expected_multiple_choice_answer = {
            'question_label': question_1.label,
            'type': 'multipleChoice',
            'text': question_1.text,
            'value': 'Yes',
            'options': ['Yes'],
            'position': question_1.position
        }
        expected_text_answer = {
            'question_label': question_5.label,
            'type': 'text',
            'text': question_5.text,
            'value': 'Answer',
            'position': question_5.position
        }

        self.assertEqual(len(node_answers), 2)

        self.assertIn(node_answers[0]['id'], [node_one.id, node_two.id])
        self.assertEqual(len(node_answers[0]['answers']), 5)
        self.assertIn(expected_multiple_choice_answer,
                      node_answers[0]['answers'])

        self.assertIn(node_answers[1]['id'], [node_one.id, node_two.id])
        self.assertEqual(len(node_answers[1]['answers']), 5)
        self.assertIn(expected_text_answer, node_answers[1]['answers'])
Exemplo n.º 31
0
    def setup_nodes_with_answers(self):
        DistributionPlanNode.append_positive_answers = MagicMock(return_value=None)
        ip = ConsigneeFactory(name='ip one')
        middle_man = ConsigneeFactory(name='middle man one', type=Consignee.TYPES.middle_man)
        end_user_one = ConsigneeFactory(name='consignee one', type=Consignee.TYPES.end_user)
        end_user_two = ConsigneeFactory(name='consignee two', type=Consignee.TYPES.end_user)
        programme_one = ProgrammeFactory(name='my first programme')
        programme_two = ProgrammeFactory(name='my second programme')
        programme_three = ProgrammeFactory(name='my third programme')
        purchase_order_item = PurchaseOrderItemFactory(item=ItemFactory(description='Mama kit'),
                                                       purchase_order=PurchaseOrderFactory(order_number=329293))
        release_order_item = ReleaseOrderItemFactory(item=ItemFactory(description='Baba bla bla'),
                                                     release_order=ReleaseOrderFactory(waybill=5540322))

        ip_node_one = DeliveryNodeFactory(consignee=ip, item=purchase_order_item, quantity=1500,
                                          programme=programme_one, track=True,
                                          distribution_plan=DeliveryFactory(track=True),
                                          location='Fort portal', tree_position=Flow.Label.IMPLEMENTING_PARTNER)
        ip_node_two = DeliveryNodeFactory(consignee=ip, item=release_order_item, quantity=100,
                                          distribution_plan=DeliveryFactory(track=True), programme=programme_two,
                                          location='Kampala', tree_position=Flow.Label.IMPLEMENTING_PARTNER, track=True)
        ip_node_three = DeliveryNodeFactory(consignee=ip, item=release_order_item, quantity=100,
                                            distribution_plan=DeliveryFactory(track=True), programme=programme_two,
                                            location='Gulu', tree_position=Flow.Label.IMPLEMENTING_PARTNER, track=True)
        ip_node_four = DeliveryNodeFactory(consignee=ip, item=purchase_order_item, quantity=100,
                                           distribution_plan=DeliveryFactory(track=True), programme=programme_three,
                                           location='Gulu', tree_position=Flow.Label.IMPLEMENTING_PARTNER, track=True)

        middle_man_node = DeliveryNodeFactory(consignee=middle_man, item=purchase_order_item,
                                              programme=programme_one, location='Wakiso', track=True,
                                              tree_position=Flow.Label.MIDDLE_MAN, parents=[(ip_node_one, 1500)],
                                              distribution_plan=None)
        end_user_node_one = DeliveryNodeFactory(consignee=end_user_one,
                                                item=purchase_order_item, parents=[(middle_man_node, 1000)],
                                                programme=programme_one, location='Amuru', track=True,
                                                distribution_plan=None)
        end_user_node_two = DeliveryNodeFactory(consignee=end_user_two, item=purchase_order_item, track=True,
                                                parents=[(middle_man_node, 500)], programme=programme_one,
                                                distribution_plan=None)
        assign_to_self_node = DeliveryNodeFactory(consignee=ip, item=purchase_order_item,
                                                  tree_position=Flow.Label.END_USER, parents=[(ip_node_four, 93)],
                                                  programme=programme_three, distribution_plan=None,
                                                  is_assigned_to_self=True)

        # IP_ITEM Flow and Questions
        ip_item_flow = FlowFactory(label=Flow.Label.IMPLEMENTING_PARTNER)
        ip_item_question_1 = MultipleChoiceQuestionFactory(text='Was the item received?', label='itemReceived',
                                                           when_answered='update_consignee_inventory',
                                                           flow=ip_item_flow, position=1)
        ip_item_option_1 = OptionFactory(text='Yes', question=ip_item_question_1)
        ip_item_option_no = OptionFactory(text='No', question=ip_item_question_1)
        ip_item_question_2 = NumericQuestionFactory(text='How much was received?', label='amountReceived',
                                                    when_answered='update_consignee_stock_level', flow=ip_item_flow,
                                                    position=3)
        ip_item_question_3 = MultipleChoiceQuestionFactory(text='What is the quality of the product?',
                                                           label='qualityOfProduct',
                                                           flow=ip_item_flow, position=3)
        ip_item_option_2 = OptionFactory(text='Good', question=ip_item_question_3)
        ip_item_option_3 = OptionFactory(text='Damaged', question=ip_item_question_3)
        ip_item_question_4 = MultipleChoiceQuestionFactory(text='Are you satisfied with the product?',
                                                           label='satisfiedWithProduct', flow=ip_item_flow, position=4)
        ip_item_option_4 = OptionFactory(text='Yes', question=ip_item_question_4)
        ip_item_option_5 = OptionFactory(text='No', question=ip_item_question_4)

        # MIDDLE_MAN Flow and Questions
        middle_man_flow = FlowFactory(label=Flow.Label.MIDDLE_MAN)
        mm_question_1 = MultipleChoiceQuestionFactory(text='Was product received?', label='productReceived',
                                                      flow=middle_man_flow, position=1)
        mm_option_1 = OptionFactory(text='Yes', question=mm_question_1)
        mm_option_2 = OptionFactory(text='No', question=mm_question_1)
        mm_question_2 = TextQuestionFactory(label='dateOfReceipt', flow=middle_man_flow,
                                            text='When was item received?', position=2)
        mm_question_3 = NumericQuestionFactory(text='What is the amount received?', label='amountReceived',
                                               flow=middle_man_flow, position=3)

        end_user_flow = FlowFactory(label=Flow.Label.END_USER)
        eu_question_1 = MultipleChoiceQuestionFactory(text='Was the item received?', label='itemReceived',
                                                      flow=end_user_flow, position=1)
        eu_option_1 = OptionFactory(text='Yes', question=eu_question_1)
        eu_question_2 = NumericQuestionFactory(text='How much was received?', label='amountReceived',
                                               flow=end_user_flow)
        eu_question_3 = MultipleChoiceQuestionFactory(text='What is the quality of the product?',
                                                      label='qualityOfProduct', flow=end_user_flow, position=3)
        eu_option_3 = OptionFactory(text='Damaged', question=eu_question_3)
        eu_option_3_1 = OptionFactory(text='Good', question=eu_question_3)
        eu_question_4 = MultipleChoiceQuestionFactory(text='Are you satisfied with the product?',
                                                      label='satisfiedWithProduct', flow=end_user_flow, position=4)
        eu_option_4 = OptionFactory(text='Yes', question=eu_question_4)
        eu_question_5 = TextQuestionFactory(label='dateOfReceipt', flow=end_user_flow,
                                            text='When was Delivery Received?')

        ip_run_one = RunFactory(runnable=ip_node_one)
        MultipleChoiceAnswerFactory(question=ip_item_question_1, run=ip_run_one, value=ip_item_option_1)
        NumericAnswerFactory(question=ip_item_question_2, run=ip_run_one, value=1500)
        MultipleChoiceAnswerFactory(question=ip_item_question_3, run=ip_run_one, value=ip_item_option_2)
        MultipleChoiceAnswerFactory(question=ip_item_question_4, run=ip_run_one, value=ip_item_option_4)

        ip_run_three = RunFactory(runnable=ip_node_three)
        MultipleChoiceAnswerFactory(question=ip_item_question_1, run=ip_run_three, value=ip_item_option_no)

        ip_run_two = RunFactory(runnable=ip_node_two)
        MultipleChoiceAnswerFactory(question=ip_item_question_1, run=ip_run_two, value=ip_item_option_1)
        NumericAnswerFactory(question=ip_item_question_2, run=ip_run_two, value=50)
        MultipleChoiceAnswerFactory(question=ip_item_question_3, run=ip_run_two, value=ip_item_option_3)
        MultipleChoiceAnswerFactory(question=ip_item_question_4, run=ip_run_two, value=ip_item_option_5)

        middle_man_node_run = RunFactory(runnable=middle_man_node)
        MultipleChoiceAnswerFactory(question=mm_question_1, run=middle_man_node_run, value=mm_option_1)
        TextAnswerFactory(question=mm_question_2, run=middle_man_node_run, value='2014-9-25')
        NumericAnswerFactory(question=mm_question_3, run=middle_man_node_run, value=1501, remark='Some remark 2')

        end_user_run_one = RunFactory(runnable=end_user_node_one)
        MultipleChoiceAnswerFactory(question=eu_question_1, run=end_user_run_one, value=eu_option_1)
        NumericAnswerFactory(question=eu_question_2, run=end_user_run_one, value=5)
        MultipleChoiceAnswerFactory(question=eu_question_3, run=end_user_run_one, value=eu_option_3)
        MultipleChoiceAnswerFactory(question=eu_question_4, run=end_user_run_one, value=eu_option_4)
        TextAnswerFactory(run=end_user_run_one, question=eu_question_5, value='2014-10-10')

        end_user_run_two = RunFactory(runnable=end_user_node_two)
        MultipleChoiceAnswerFactory(question=eu_question_1, run=end_user_run_two, value=eu_option_1)
        NumericAnswerFactory(question=eu_question_2, run=end_user_run_two, value=500)
        MultipleChoiceAnswerFactory(question=eu_question_3, run=end_user_run_two, value=eu_option_3)
        MultipleChoiceAnswerFactory(question=eu_question_4, run=end_user_run_two, value=eu_option_4)
        TextAnswerFactory(question=eu_question_5, run=end_user_run_two, value='2013-12-12')

        assign_to_self_run = RunFactory(runnable=assign_to_self_node)
        MultipleChoiceAnswerFactory(question=eu_question_1, run=assign_to_self_run, value=eu_option_1)
        NumericAnswerFactory(question=eu_question_2, run=assign_to_self_run, value=500)
        TextAnswerFactory(question=eu_question_5, run=assign_to_self_run, value='2013-12-14')
        MultipleChoiceAnswerFactory(question=eu_question_3, run=assign_to_self_run, value=eu_option_3_1)
        MultipleChoiceAnswerFactory(question=eu_question_4, run=assign_to_self_run, value=eu_option_4)

        return end_user_node_one, purchase_order_item, release_order_item, end_user_node_two, ip_node_two, ip, assign_to_self_node