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_not_return_deliveries_for_ip_if_received_is_false(self): first_consignee = ConsigneeFactory() date = datetime.date(2014, 07, 9) first_delivery = DeliveryFactory(consignee=first_consignee, track=True, delivery_date=date) second_delivery = DeliveryFactory(consignee=first_consignee, track=True) self.logout() self.log_consignee_in(consignee=first_consignee) response = self.client.get(ENDPOINT_URL) ids = map(lambda delivery: delivery['id'], response.data) self.assertEqual(response.status_code, 200) self.assertIn(first_delivery.id, ids) self.assertIn(second_delivery.id, ids) flow = FlowFactory(label=Flow.Label.IMPLEMENTING_PARTNER) delivery_received_qn = MultipleChoiceQuestionFactory(label='deliveryReceived', flow=flow) OptionFactory(question=delivery_received_qn, text='Yes') option_no = OptionFactory(question=delivery_received_qn, text='No') run = RunFactory(runnable=first_delivery) MultipleChoiceAnswerFactory(run=run, question=delivery_received_qn, value=option_no) response = self.client.get(ENDPOINT_URL) ids = map(lambda delivery: delivery['id'], response.data) self.assertEqual(response.status_code, 200) self.assertNotIn(first_delivery.id, ids) self.assertIn(second_delivery.id, ids)
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_delivery_with_waybill_number(self): release_order = ReleaseOrderFactory(waybill=98765) release_order_item = ReleaseOrderItemFactory(release_order=release_order) delivery = DeliveryFactory() DeliveryNodeFactory(distribution_plan=delivery, item=release_order_item) self.assertEqual(delivery.number(), 98765)
def test_should_create_alert(self, mock_contact): purchase_order = PurchaseOrderFactory(order_number=5678) purchase_order_item = PurchaseOrderItemFactory( purchase_order=purchase_order) consignee = ConsigneeFactory(name="Liverpool FC") contact_person_id = 'some_id' contact = { u'_id': contact_person_id, u'firstName': u'Chris', u'lastName': u'George', u'phone': u'+256781111111' } mock_contact.return_value = contact delivery = DeliveryFactory(consignee=consignee, contact_person_id=contact_person_id) DeliveryNodeFactory(item=purchase_order_item, distribution_plan=delivery) delivery.create_alert(Alert.ISSUE_TYPES.not_received) alerts = Alert.objects.filter(consignee_name="Liverpool FC", order_number=5678) self.assertEqual(alerts.count(), 1) alert = alerts.first() self.assertEqual(alert.order_type, PurchaseOrderItem.PURCHASE_ORDER) self.assertEqual(alert.order_number, 5678) self.assertEqual(alert.consignee_name, "Liverpool FC") self.assertEqual(alert.contact['contact_name'], "Chris George") self.assertEqual(alert.issue, Alert.ISSUE_TYPES.not_received) self.assertFalse(alert.is_resolved) self.assertIsNone(alert.remarks) self.assertEqual(alert.runnable.id, delivery.id) self.assertIsNone(alert.item_description)
def test_should_return_delivery_with_order_number(self): po = PurchaseOrderFactory(order_number=123456) po_item = PurchaseOrderItemFactory(purchase_order=po) delivery = DeliveryFactory() DeliveryNodeFactory(distribution_plan=delivery, item=po_item) self.assertEqual(delivery.number(), 123456)
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_node_should_get_its_parents_distribution_plan_when_for_single_parent( self): purchaser_order_item = PurchaseOrderItemFactory() delivery_one = DeliveryFactory() delivery_two = DeliveryFactory() root_one = DeliveryNodeFactory(distribution_plan=delivery_one, item=purchaser_order_item, quantity=50) root_two = DeliveryNodeFactory(distribution_plan=delivery_two, item=purchaser_order_item, quantity=60) child_one = DeliveryNodeFactory(parents=[(root_one, 10)], distribution_plan=None) child_two = DeliveryNodeFactory(parents=[(root_two, 20)], distribution_plan=None) child_three = DeliveryNodeFactory(parents=[(root_one, 10), (root_two, 10)], distribution_plan=None) self.assertEqual(child_one.distribution_plan, delivery_one) self.assertEqual(child_two.distribution_plan, delivery_two) self.assertIsNone(child_three.distribution_plan) grand_child_one = DeliveryNodeFactory(parents=[(child_one, 5)], distribution_plan=None) grand_child_two = DeliveryNodeFactory(parents=[(child_two, 20)], distribution_plan=None) grand_child_three = DeliveryNodeFactory(parents=[(child_three, 15)], distribution_plan=None) self.assertEqual(grand_child_one.distribution_plan, delivery_one) self.assertEqual(grand_child_two.distribution_plan, delivery_two) self.assertIsNone(grand_child_three.distribution_plan)
def test_should_create_alert(self, mock_contact): purchase_order = PurchaseOrderFactory(order_number=5678) purchase_order_item = PurchaseOrderItemFactory(purchase_order=purchase_order) consignee = ConsigneeFactory(name="Liverpool FC") contact_person_id = 'some_id' contact = {u'_id': contact_person_id, u'firstName': u'Chris', u'lastName': u'George', u'phone': u'+256781111111'} mock_contact.return_value = contact delivery = DeliveryFactory(consignee=consignee, contact_person_id=contact_person_id) DeliveryNodeFactory(item=purchase_order_item, distribution_plan=delivery) delivery.create_alert(Alert.ISSUE_TYPES.not_received) alerts = Alert.objects.filter(consignee_name="Liverpool FC", order_number=5678) self.assertEqual(alerts.count(), 1) alert = alerts.first() self.assertEqual(alert.order_type, PurchaseOrderItem.PURCHASE_ORDER) self.assertEqual(alert.order_number, 5678) self.assertEqual(alert.consignee_name, "Liverpool FC") self.assertEqual(alert.contact['contact_name'], "Chris George") self.assertEqual(alert.issue, Alert.ISSUE_TYPES.not_received) self.assertFalse(alert.is_resolved) self.assertIsNone(alert.remarks) self.assertEqual(alert.runnable.id, delivery.id) self.assertIsNone(alert.item_description)
def test_should_dequeue_next_run_in_the_queue(self): first_delivery_to_be_answered = DeliveryFactory(track=True) contact = {'name': 'Some name', 'phone': '098765433'} first_delivery_to_be_answered.build_contact = MagicMock(return_value=contact) self._schedule_run_for(first_delivery_to_be_answered) second_delivery_to_be_answered = DeliveryFactory(track=True) self._schedule_run_for(second_delivery_to_be_answered) data = { 'runnable': first_delivery_to_be_answered.id, 'answers': [ {'question_label': 'deliveryReceived', 'value': 'Yes'}] } next_run = RunQueue.objects.filter( Q(contact_person_id=second_delivery_to_be_answered.contact_person_id) & Q( status='not_started')).order_by( '-run_delay').first() self.client.post(ENDPOINT_URL, data=json.dumps(data), content_type='application/json') first_runs = Run.objects.filter(runnable=first_delivery_to_be_answered) next_run = RunQueue.objects.get(id=next_run.id) self.assertEqual(len(first_runs), 2) self.assertEqual(next_run.status, 'started') self.assertTrue(self.mock_distribution_alert_raise.delay.called)
def test_should_return_multiple_deliveries_along_with_their_corresponding_nodes( self): order = PurchaseOrderFactory() order_item_one = PurchaseOrderItemFactory(purchase_order=order) order_item_two = PurchaseOrderItemFactory(purchase_order=order) delivery_one = DeliveryFactory() delivery_two = DeliveryFactory() node_one = NodeFactory(item=order_item_one, distribution_plan=delivery_one) node_two = NodeFactory(item=order_item_two, distribution_plan=delivery_one) node_three = NodeFactory(item=order_item_one, distribution_plan=delivery_two) node_four = NodeFactory(item=order_item_two, distribution_plan=delivery_two) deliveries = order.deliveries() self.assertEqual(len(deliveries), 2) self.assertIn(delivery_one, list(deliveries)) self.assertIn(delivery_two, list(deliveries)) first_delivery_nodes = delivery_one.distributionplannode_set.all() second_delivery_nodes = delivery_two.distributionplannode_set.all() self.assertIn(node_one, first_delivery_nodes) self.assertIn(node_two, first_delivery_nodes) self.assertIn(node_three, second_delivery_nodes) self.assertIn(node_four, second_delivery_nodes)
def test_should_filter_deliveries_by_ip_and_number(self): first_consignee = ConsigneeFactory() second_consignee = ConsigneeFactory() purchase_order = PurchaseOrderFactory(order_number=123) po_item = PurchaseOrderItemFactory(purchase_order=purchase_order) first_delivery = DeliveryFactory(consignee=first_consignee, track=True) DeliveryNodeFactory(item=po_item, distribution_plan=first_delivery) second_delivery = DeliveryFactory(consignee=first_consignee, track=True) third_delivery = DeliveryFactory(consignee=second_consignee) self.logout() self.log_consignee_in(consignee=first_consignee) response = self.client.get(ENDPOINT_URL + '?query=123') ids = map(lambda delivery: delivery['id'], response.data) self.assertEqual(response.status_code, 200) self.assertIn(first_delivery.id, ids) self.assertNotIn(second_delivery.id, ids) self.assertNotIn(third_delivery.id, ids)
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_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_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_filter_based_on_outcome_and_ip(self): outcome_one = ProgrammeFactory(name='Outcome One') outcome_two = ProgrammeFactory(name='Outcome Two') consignee_one = ConsigneeFactory(name="Consignee One", type=Consignee.TYPES.implementing_partner) consignee_two = ConsigneeFactory(name="Consignee Two", type=Consignee.TYPES.implementing_partner) delivery_one = DeliveryFactory(programme=outcome_one, track=True) delivery_two = DeliveryFactory(programme=outcome_two, track=True) DeliveryNodeFactory( distribution_plan=delivery_one, consignee=consignee_one, tree_position=Flow.Label.IMPLEMENTING_PARTNER) DeliveryNodeFactory( distribution_plan=delivery_two, consignee=consignee_one, tree_position=Flow.Label.IMPLEMENTING_PARTNER) DeliveryNodeFactory( distribution_plan=delivery_two, consignee=consignee_two, tree_position=Flow.Label.IMPLEMENTING_PARTNER) endpoint_url = BACKEND_URL + 'stock-report?consignee=%d&outcome=%d' % (consignee_one.id, outcome_two.id) response = self.client.get(endpoint_url) results = response.data['results'] self.assertEqual(len(results), 1) self.assertEqual(results[0]['programme'], 'Outcome Two') self.assertEqual(len(results[0]['items']), 1) self.assertEqual(results[0]['items'][0]['consignee'], 'Consignee One')
def test_should_filter_admin_deliveries_by_multiple_queries(self): first_consignee = ConsigneeFactory() second_consignee = ConsigneeFactory() purchase_order = PurchaseOrderFactory(order_number=123) po_item = PurchaseOrderItemFactory(purchase_order=purchase_order) date = datetime.date(2014, 07, 9) first_delivery = DeliveryFactory(consignee=first_consignee, track=True, delivery_date=date) DeliveryNodeFactory(item=po_item, distribution_plan=first_delivery) second_delivery = DeliveryFactory(consignee=first_consignee, track=True, delivery_date=date) third_delivery = DeliveryFactory(consignee=second_consignee) response = self.client.get(ENDPOINT_URL + '?from=2014-07-6&to=2014-12-31&query=123') ids = map(lambda delivery: delivery['id'], response.data) self.assertEqual(response.status_code, 200) self.assertIn(first_delivery.id, ids) self.assertNotIn(second_delivery.id, ids) self.assertNotIn(third_delivery.id, ids)
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_know_if_it_is_fully_delivered_or_not_using_only_tracked_nodes( self): purchase_order = PurchaseOrderFactory() item_one = PurchaseOrderItemFactory(purchase_order=purchase_order, quantity=100) item_two = PurchaseOrderItemFactory(purchase_order=purchase_order, quantity=100) self.assertFalse(purchase_order.is_fully_delivered()) delivery = DeliveryFactory() node_one = NodeFactory( item=item_one, quantity=100, distribution_plan=delivery, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) self.assertFalse(purchase_order.is_fully_delivered()) node_two = NodeFactory( item=item_two, quantity=100, distribution_plan=delivery, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) self.assertFalse(purchase_order.is_fully_delivered()) delivery.track = True delivery.save() node_two.quantity = 50 node_two.save() self.assertFalse(purchase_order.is_fully_delivered()) node_two.quantity = 100 node_two.save() self.assertTrue(purchase_order.is_fully_delivered())
def _setup_nodes_with_answers(self): self._create_questions() programme_one = ProgrammeFactory(name='programme one') programme_two = ProgrammeFactory(name='programme two') programme_three = ProgrammeFactory(name='programme three') delivery_one = DeliveryFactory(track=True, programme=programme_one, delivery_date=date(2015, 3, 10), location='Wakiso') delivery_two = DeliveryFactory(track=True, programme=programme_two, delivery_date=date(2015, 6, 20), location='Kampala') delivery_three = DeliveryFactory(track=True, programme=programme_three, delivery_date=date(2015, 9, 30), location='Fort portal') DeliveryNodeFactory(distribution_plan=delivery_one, programme=programme_one, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) DeliveryNodeFactory(distribution_plan=delivery_two, programme=programme_two, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) DeliveryNodeFactory(distribution_plan=delivery_three, programme=programme_two, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) run_one = RunFactory(runnable=delivery_one) MultipleChoiceAnswerFactory(run=run_one, question=self.delivery_received_qtn, value=self.yes_one) MultipleChoiceAnswerFactory(run=run_one, question=self.delivery_in_good_order, value=self.no_two) MultipleChoiceAnswerFactory(run=run_one, question=self.satisfied_with_delivery, value=self.no_three) run_two = RunFactory(runnable=delivery_two) MultipleChoiceAnswerFactory(run=run_two, question=self.delivery_received_qtn, value=self.no_one) MultipleChoiceAnswerFactory(run=run_two, question=self.delivery_in_good_order, value=self.yes_two) MultipleChoiceAnswerFactory(run=run_two, question=self.satisfied_with_delivery, value=self.yes_three) run_three = RunFactory(runnable=delivery_three) MultipleChoiceAnswerFactory(run=run_three, question=self.delivery_received_qtn, value=self.yes_one) MultipleChoiceAnswerFactory(run=run_three, question=self.delivery_in_good_order, value=self.no_two) MultipleChoiceAnswerFactory(run=run_three, question=self.satisfied_with_delivery, value=self.yes_three)
def test_should_return_false_when_delivery_run_was_cancelled(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label='deliveryReceived') option = OptionFactory(text='Yes', question=question) run = RunFactory(runnable=delivery, status=Run.STATUS.cancelled) MultipleChoiceAnswerFactory(run=run, question=question, value=option) self.assertFalse(delivery.is_received())
def setup_distribution_plans(self): self.plan_one = DeliveryFactory(programme=self.programme_one, track=True) self.plan_two = DeliveryFactory(programme=self.programme_one, track=True) self.plan_three = DeliveryFactory(programme=self.programme_two, track=True) self.setup_nodes()
def setup_responses(self): DeliveryNode.objects.all().delete() MultipleChoiceQuestion.objects.all().delete() MultipleChoiceAnswer.objects.all().delete() from eums.fixtures.ip_questions import seed_ip_questions questions, options, _ = seed_ip_questions() self.programme = ProgrammeFactory(name='my-program') self.ip = ConsigneeFactory() self.today = FakeDate.today() po_item = PurchaseOrderItemFactory(quantity=100, value=1000) ip_delivery_one = DeliveryFactory(location='some location', track=True, programme=self.programme, consignee=self.ip, ip=self.ip, delivery_date=self.today + datetime.timedelta(days=3)) DeliveryNodeFactory(tree_position=DeliveryNode.IMPLEMENTING_PARTNER, quantity=10, item=po_item, distribution_plan=ip_delivery_one, consignee=self.ip) other_delivery = DeliveryFactory(location='Other location', delivery_date=self.today, track=True) DeliveryNodeFactory(tree_position=DeliveryNode.IMPLEMENTING_PARTNER, quantity=20, item=po_item, distribution_plan=other_delivery) MultipleChoiceAnswerFactory( run=RunFactory(runnable=ip_delivery_one, status=Run.STATUS.scheduled), question=questions['WAS_DELIVERY_RECEIVED'], value=options['DELIVERY_WAS_RECEIVED']) MultipleChoiceAnswerFactory( run=RunFactory(runnable=ip_delivery_one, status=Run.STATUS.scheduled), question=questions['IS_DELIVERY_IN_GOOD_ORDER'], value=options['IN_GOOD_CONDITION']) non_response_delivery_one = DeliveryFactory(delivery_date=self.today + datetime.timedelta(days=4), location='some location', track=True) DeliveryNodeFactory(tree_position=DeliveryNode.IMPLEMENTING_PARTNER, quantity=30, item=po_item, distribution_plan=non_response_delivery_one) RunFactory(runnable=non_response_delivery_one, status=Run.STATUS.scheduled)
def test_should_return_delivery_with_waybill_number(self): release_order = ReleaseOrderFactory(waybill=98765) release_order_item = ReleaseOrderItemFactory( release_order=release_order) delivery = DeliveryFactory() DeliveryNodeFactory(distribution_plan=delivery, item=release_order_item) self.assertEqual(delivery.number(), 98765)
def pair_tracked_date_on_updated(self, pre_purchase_order_tracked_date=None, delivery_tracked_date=None): purchase_order = PurchaseOrderFactory(tracked_date=pre_purchase_order_tracked_date) po_item = PurchaseOrderItemFactory(purchase_order=purchase_order) delivery = DeliveryFactory(track=True, tracked_date=delivery_tracked_date) DeliveryNodeFactory(distribution_plan=delivery, item=po_item) delivery.save() updated_tracked_date = PurchaseOrder.objects.get(pk=purchase_order.id).tracked_date return updated_tracked_date
def test_should_filter_deliveries_by_programme(self): programme = ProgrammeFactory(name='my programme') delivery = DeliveryFactory(programme=programme) DeliveryFactory() response = self.client.get('%s?programme=%s' % (ENDPOINT_URL, 'my')) self.assertEqual(Delivery.objects.count(), 2) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['id'], delivery.id)
def test_should_get_track_status_if_delivery_is_sent_and_tracked_fully(self): order = PurchaseOrderFactory() order_item = PurchaseOrderItemFactory(purchase_order=order, quantity=100) self.assertEqual(order.track(), PurchaseOrder.NOT_TRACKED) delivery_one = DeliveryFactory(track=True) delivery_two = DeliveryFactory(track=True) NodeFactory(item=order_item, distribution_plan=delivery_one, quantity=50, track=True) NodeFactory(item=order_item, distribution_plan=delivery_two, quantity=50, track=True) self.assertEqual(order.track(), PurchaseOrder.FULLY_TRACKED)
def test_should_return_false_when_delivery_is_not_received(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label="deliveryReceived") option = OptionFactory(text="No", question=question) run = RunFactory(runnable=delivery) self.assertFalse(delivery.is_received()) MultipleChoiceAnswerFactory(run=run, question=question, value=option) self.assertFalse(delivery.is_received())
def test_should_return_number_of_items_on_a_delivery(self): po = PurchaseOrderFactory(order_number=123456) po_item_one = PurchaseOrderItemFactory(purchase_order=po) po_item_two = PurchaseOrderItemFactory(purchase_order=po) delivery = DeliveryFactory() DeliveryNodeFactory(distribution_plan=delivery, item=po_item_one) self.assertEqual(delivery.number_of_items(), 1) DeliveryNodeFactory(distribution_plan=delivery, item=po_item_two) self.assertEqual(delivery.number_of_items(), 2)
def test_should_schedule_implementing_partner_flow_if_runnable_is_delivery(self): delivery = DeliveryFactory() NodeFactory(distribution_plan=delivery, item=PurchaseOrderItemFactory()) delivery.build_contact = MagicMock(return_value=self.contact) Runnable.objects.get = MagicMock(return_value=delivery) schedule_run_for(delivery) mock_start_delivery_run.assert_called_with(contact_person=self.contact, flow=self.IMPLEMENTING_PARTNER_FLOW_ID, item_description=ANY, sender=ANY)
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_true_when_delivery_is_partially_received(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label='deliveryReceived') option = OptionFactory(text='Yes', question=question) run = RunFactory(runnable=delivery) self.assertFalse(delivery.is_received()) MultipleChoiceAnswerFactory(run=run, question=question, value=option) self.assertTrue(delivery.is_partially_received())
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_schedule_implementing_partner_flow_if_runnable_is_delivery(self): delivery = DeliveryFactory() NodeFactory(distribution_plan=delivery, item=PurchaseOrderItemFactory()) delivery.build_contact = MagicMock(return_value=self.contact) Runnable.objects.get = MagicMock(return_value=delivery) self.flow_scheduler.schedule_run_for(delivery) self.mocked_create_run.assert_called_with(self.contact, self.ip_flow, ANY, ANY)
def test_should_filter_based_on_to_date(self): DeliveryNodeFactory(distribution_plan=DeliveryFactory(track=True), delivery_date=datetime.date(2015, 10, 1), tree_position=Flow.Label.IMPLEMENTING_PARTNER) DeliveryNodeFactory(distribution_plan=DeliveryFactory(track=True), delivery_date=datetime.date(2015, 11, 1), tree_position=Flow.Label.IMPLEMENTING_PARTNER) endpoint_url = BACKEND_URL + 'stock-report?toDate=2015-10-15' response = self.client.get(endpoint_url) results = response.data['results'] self.assertEqual(len(results), 1) self.assertEqual(results[0]['last_shipment_date'], '2015-10-01')
def test_should_schedule_implementing_partner_flow_if_runnable_is_delivery( self): delivery = DeliveryFactory() NodeFactory(distribution_plan=delivery, item=PurchaseOrderItemFactory()) delivery.build_contact = MagicMock(return_value=self.contact) Runnable.objects.get = MagicMock(return_value=delivery) self.flow_scheduler.schedule_run_for(delivery) self.mocked_create_run.assert_called_with(self.contact, self.ip_flow, ANY, ANY)
def test_should_return_false_when_delivery_is_received_but_no_answers_have_been_received_for_its_nodes(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label='deliveryReceived') option = OptionFactory(text='Yes', question=question) run = RunFactory(runnable=delivery) DeliveryNodeFactory(distribution_plan=delivery) DeliveryNodeFactory(distribution_plan=delivery) MultipleChoiceAnswerFactory(run=run, question=question, value=option) self.assertFalse(delivery.is_received())
def test_should_filter_deliveries_by_tracked_for_ip(self): first_consignee = ConsigneeFactory() DeliveryFactory(consignee=first_consignee) DeliveryFactory(consignee=first_consignee) DeliveryFactory(consignee=first_consignee) self.logout() self.log_consignee_in(consignee=first_consignee) response = self.client.get(ENDPOINT_URL) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 0)
def test_should_only_include_top_level_nodes_when_calculating_available_balance( self): purchase_order_item = PurchaseOrderItemFactory(quantity=500) root_one = NodeFactory(item=purchase_order_item, quantity=200, distribution_plan=DeliveryFactory(track=True)) self.assertEquals(purchase_order_item.available_balance(), 300) NodeFactory(item=purchase_order_item, parents=[(root_one, 120)], distribution_plan=DeliveryFactory(track=True)) self.assertEquals(purchase_order_item.available_balance(), 300)
def setUp(self): Alert.objects.all().delete() self.item_alert_1 = AlertFactory(order_number=81020737, runnable=DeliveryNodeFactory()) self.item_alert_2 = AlertFactory(order_number=81035556, runnable=DeliveryNodeFactory()) self.delivery_alert = AlertFactory(order_number=81025778, runnable=DeliveryFactory()) self.distribution_alert = AlertFactory( order_number=81034568, runnable=DeliveryFactory(), issue=Alert.ISSUE_TYPES.distribution_expired)
def test_should_return_true_when_shipment_is_received_regardless_of_confirmation_of_items(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label='deliveryReceived') option = OptionFactory(text='Yes', question=question) run = RunFactory(runnable=delivery) DeliveryNodeFactory(distribution_plan=delivery) DeliveryNodeFactory(distribution_plan=delivery) MultipleChoiceAnswerFactory(run=run, question=question, value=option) self.assertTrue(delivery.shipment_received())
def test_should_return_true_when_shipment_is_received_regardless_of_confirmation_of_items( self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label='deliveryReceived') option = OptionFactory(text='Yes', question=question) run = RunFactory(runnable=delivery) DeliveryNodeFactory(distribution_plan=delivery) DeliveryNodeFactory(distribution_plan=delivery) MultipleChoiceAnswerFactory(run=run, question=question, value=option) self.assertTrue(delivery.shipment_received())
def test_balance_should_decrease_when_tracked_nodes_exist(self): purchase_order_item = PurchaseOrderItemFactory(purchase_order=(PurchaseOrderFactory()), quantity=500) delivery = DeliveryFactory() node_one = NodeFactory(item=purchase_order_item, quantity=200, distribution_plan=delivery) self.assertEquals(purchase_order_item.available_balance(), 500) delivery.track = True delivery.save() self.assertEquals(purchase_order_item.available_balance(), 300) NodeFactory(item=purchase_order_item, quantity=120, distribution_plan=delivery) self.assertEquals(purchase_order_item.available_balance(), 180)
def test_should_return_false_when_delivery_is_received_and_there_are_no_answers_for_some_nodes(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label='deliveryReceived') option = OptionFactory(text='Yes', question=question) run = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(run=run, question=question, value=option) node_one = DeliveryNodeFactory(distribution_plan=delivery) DeliveryNodeFactory(distribution_plan=delivery) item_question = MultipleChoiceQuestionFactory(label='itemReceived') yes_node_option = OptionFactory(text='Yes', question=item_question) MultipleChoiceAnswerFactory(run=RunFactory(runnable=node_one), question=item_question, value=yes_node_option) self.assertFalse(delivery.is_received())
def test_should_decrease_balance_when_saving_tracked_or_not_tracked_nodes(self): purchase_order_item = PurchaseOrderItemFactory(quantity=500) delivery = DeliveryFactory() DeliveryNodeFactory(item=purchase_order_item, quantity=200, distribution_plan=delivery) self.assertEquals(purchase_order_item.available_balance(), 300) self.assertEquals(purchase_order_item.quantity_shipped(), 200) delivery.track = True delivery.save() self.assertEquals(purchase_order_item.available_balance(), 300) self.assertEquals(purchase_order_item.quantity_shipped(), 200) tracked_delivery = DeliveryFactory(track=True) DeliveryNodeFactory(item=purchase_order_item, quantity=120, distribution_plan=tracked_delivery) self.assertEquals(purchase_order_item.available_balance(), 180) self.assertEquals(purchase_order_item.quantity_shipped(), 320)
def test_should_return_true_when_delivery_is_received_and_all_node_answers_are_received(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label='deliveryReceived') option = OptionFactory(text='Yes', question=question) run = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(run=run, question=question, value=option) node_one = DeliveryNodeFactory(distribution_plan=delivery) node_two = DeliveryNodeFactory(distribution_plan=delivery) item_question = MultipleChoiceQuestionFactory(label='itemReceived') option_two = OptionFactory(text='Yes', question=item_question) MultipleChoiceAnswerFactory(run=RunFactory(runnable=node_one), question=item_question, value=option_two) MultipleChoiceAnswerFactory(run=RunFactory(runnable=node_two), question=item_question, value=option_two) self.assertTrue(delivery.is_received())
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_false_when_delivery_is_received_and_some_nodes_are_not_received(self): delivery = DeliveryFactory() question = MultipleChoiceQuestionFactory(label="deliveryReceived") option = OptionFactory(text="Yes", question=question) run = RunFactory(runnable=delivery) MultipleChoiceAnswerFactory(run=run, question=question, value=option) node_one = DeliveryNodeFactory(distribution_plan=delivery) node_two = DeliveryNodeFactory(distribution_plan=delivery) item_question = MultipleChoiceQuestionFactory(label="itemReceived") yes_node_option = OptionFactory(text="Yes", question=item_question) no_node_option = OptionFactory(text="No", question=item_question) MultipleChoiceAnswerFactory(run=RunFactory(runnable=node_one), question=item_question, value=yes_node_option) MultipleChoiceAnswerFactory(run=RunFactory(runnable=node_two), question=item_question, value=no_node_option) self.assertFalse(delivery.is_received())
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(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_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_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_know_if_it_is_fully_delivered_or_not_using_only_tracked_nodes(self): purchase_order = PurchaseOrderFactory() item_one = PurchaseOrderItemFactory(purchase_order=purchase_order, quantity=100) item_two = PurchaseOrderItemFactory(purchase_order=purchase_order, quantity=100) self.assertFalse(purchase_order.is_fully_delivered()) delivery = DeliveryFactory() node_one = NodeFactory(item=item_one, quantity=100, distribution_plan=delivery) self.assertFalse(purchase_order.is_fully_delivered()) node_two = NodeFactory(item=item_two, quantity=100, distribution_plan=delivery) self.assertFalse(purchase_order.is_fully_delivered()) delivery.track = True delivery.save() node_two.quantity = 50 node_two.save() self.assertFalse(purchase_order.is_fully_delivered()) node_two.quantity = 100 node_two.save() self.assertTrue(purchase_order.is_fully_delivered())
def test_should_mirror_delivery_tracked_status_on_all_nodes_when_tracked_status_changes_on_delivery(self): Delivery.objects.all().delete() delivery = DeliveryFactory(track=False) root_node = DeliveryNodeFactory(distribution_plan=delivery) child_node = DeliveryNodeFactory(distribution_plan=delivery, parents=[(root_node, 5)]) self.assertFalse(root_node.track) self.assertFalse(child_node.track) delivery.track = True delivery.save() self.assertTrue(DeliveryNode.objects.get(pk=root_node.id).track) self.assertTrue(DeliveryNode.objects.get(pk=child_node.id).track) delivery.track = False delivery.save() self.assertFalse(DeliveryNode.objects.get(pk=root_node.id).track) self.assertFalse(DeliveryNode.objects.get(pk=child_node.id).track)
def test_should_confirm_delivery_when_all_nodes_are_answered(self): delivery = DeliveryFactory() node_one = DeliveryNodeFactory(distribution_plan=delivery) node_two = DeliveryNodeFactory(distribution_plan=delivery) item_question = MultipleChoiceQuestionFactory(label='itemReceived') option_one = OptionFactory(text='Yes', question=item_question) option_two = OptionFactory(text='No', question=item_question) delivery.confirm() self.assertFalse(delivery.confirmed) MultipleChoiceAnswerFactory(run=RunFactory(runnable=node_one), question=item_question, value=option_one) delivery.confirm() self.assertFalse(delivery.confirmed) MultipleChoiceAnswerFactory(run=RunFactory(runnable=node_two), question=item_question, value=option_two) delivery.confirm() self.assertTrue(delivery.confirmed)