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 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_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_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 test_should_get_orders__as_a_queryset__whose_items_have_been_delivered_to_a_specific_consignee(self): consignee = ConsigneeFactory() order_one = PurchaseOrderFactory() order_two = PurchaseOrderFactory() order_three = PurchaseOrderFactory() order_item_one = PurchaseOrderItemFactory(purchase_order=order_one) order_item_two = PurchaseOrderItemFactory(purchase_order=order_two) NodeFactory(item=order_item_one, consignee=consignee) NodeFactory(item=order_item_two, consignee=consignee) consignee_orders = PurchaseOrder.objects.for_consignee(consignee.id).order_by('id') self.assertListEqual(list(consignee_orders), [order_one, order_two]) self.assertNotIn(order_three, consignee_orders)
def test_should_schedule_flow_with_sender_as_parent_node_consignee_name_if_node_has_parent( self): sender_org_name = "Dwelling Places" sender_org = ConsigneeFactory(name=sender_org_name) parent_node = NodeFactory(consignee=sender_org) node = NodeFactory(consignee=sender_org, parents=[(parent_node, 10)]) node.build_contact = MagicMock(return_value=self.contact) Runnable.objects.get = MagicMock(return_value=node) node.consignee.build_contact = MagicMock(return_value=self.contact) self.flow_scheduler.schedule_run_for(node) self.mocked_create_run.assert_called_with(self.contact, ANY, node.item.item.description, sender_org_name)
def test_should_get_track_status_if_delivery_is_just_saved(self): order = PurchaseOrderFactory() order_item = PurchaseOrderItemFactory(purchase_order=order, quantity=100) delivery_one = DeliveryFactory(track=False) NodeFactory(item=order_item, distribution_plan=delivery_one, quantity=100, track=False) self.assertEqual(order.track(), PurchaseOrder.NOT_TRACKED)
def test_should_know_if_it_has_a_distribution_plan_or_not(self): purchase_order = PurchaseOrderFactory() purchase_order_item = PurchaseOrderItemFactory( purchase_order=purchase_order) self.assertFalse(purchase_order.has_plan()) NodeFactory(item=purchase_order_item) self.assertTrue(purchase_order.has_plan())
def test_should_queue_run_for_a_contact_if_contact_has_current_run_for_a_different_runnable( self, mock_run_queue_enqueue): RunFactory(runnable=self.node) node_two = NodeFactory(contact_person_id=self.node.contact_person_id) node_two.build_contact = MagicMock(return_value=self.contact) mock_run_queue_enqueue.return_value = None self.flow_scheduler.schedule_run_for(node_two) mock_run_queue_enqueue.assert_called_with(node_two, ANY)
def test_should_schedule_flow_to_start_after_buffer_when_calculated_send_time_is_in_past( self): some_date = FakeDate.today() - datetime.timedelta(days=10) node = NodeFactory(delivery_date=some_date) node.build_contact = MagicMock(return_value=self.contact) self.flow_scheduler.schedule_run_for(node) self.assertEqual(self.mock_celery.invoked_after, 10.0)
def test_balance_should_decrease_when_tracked_nodes_exist(self): purchase_order_item = PurchaseOrderItemFactory(quantity=500) delivery = DeliveryFactory() 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_schedule_end_user_flow_if_node_tree_position_is_end_user( self): node = NodeFactory(tree_position=Node.END_USER) node.build_contact = MagicMock(return_value=self.contact) Runnable.objects.get = MagicMock(return_value=node) self.flow_scheduler.schedule_run_for(node) self.mocked_create_run.assert_called_with(self.contact, self.end_user_flow, ANY, ANY)
def test_should_build_contact_with_details_from_contacts_service(self, mock_get): contact_id = '54335c56b3ae9d92f038abb0' fake_contact_json = {'firstName': "test", 'lastName': "user1", 'phone': "+256 782 443439", '_id': contact_id} fake_response = FakeResponse(fake_contact_json, 200) node = NodeFactory(contact_person_id=contact_id) mock_get.return_value = fake_response contact = node.build_contact() self.assertEqual(contact, fake_contact_json) mock_get.assert_called_with("%s%s/" % (settings.CONTACTS_SERVICE_URL, contact_id))
def test_should_schedule_middleman_flow_if_node_tree_position_is_middleman( self): node = NodeFactory(tree_position=Node.MIDDLE_MAN) node.build_contact = MagicMock(return_value=self.contact) Runnable.objects.get = MagicMock(return_value=node) self.flow_scheduler.schedule_run_for(node) self.mocked_create_run.assert_called_with(self.contact, self.middle_man_flow, ANY, ANY)
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_schedule_queued_runs_for_a_consignee_after_expiring_over_due_runs( self, mock_get_overdue_runs, mock_schedule_run_for, mock_deque): overdue_run = RunFactory(runnable=self.node) node = NodeFactory() run_queue_item = RunQueueFactory( runnable=node, contact_person_id=self.node.contact_person_id) mock_get_overdue_runs.return_value = [overdue_run] mock_schedule_run_for.return_value = None mock_deque.return_value = run_queue_item self.flow_scheduler.expire_overdue_runs() mock_deque.assert_called_with(self.node.contact_person_id) mock_schedule_run_for.assert_called_with(run_queue_item.runnable) self.assertEqual(run_queue_item.status, RunQueue.STATUS.started)
def setUp(self): self.__backup_original_methods() self.__mock_ready() self.flow_scheduler.settings.RAPIDPRO_LIVE = True self.contact = { 'first_name': 'Test', 'last_name': 'User', 'phone': '+256 772 123456' } self.node = NodeFactory() local_celery.app.control.revoke = MagicMock(return_value=None) self.node.build_contact = MagicMock(return_value=self.contact) Node.objects.get = MagicMock(return_value=self.node) Runnable.objects.get = MagicMock(return_value=self.node) self.ip_flow = FlowFactory(label=Flow.Label.IMPLEMENTING_PARTNER) self.end_user_flow = FlowFactory(label=Flow.Label.END_USER) self.middle_man_flow = FlowFactory(label=Flow.Label.MIDDLE_MAN) self.MIDDLEMAN_FLOW_ID = 1 self.END_USER_FLOW_ID = 2 self.IMPLEMENTING_PARTNER_FLOW_ID = 3