def test_should_get_correct_track_status_when_no_delivery_or_delivery_is_not_tracked(self): release_order = ReleaseOrderFactory(order_number=2342) release_order_item = ReleaseOrderItemFactory(release_order=release_order) self.assertEqual(release_order.track(), None) delivery = DeliveryFactory(track=False) DeliveryNodeFactory(distribution_plan=delivery, item=release_order_item, track=False) self.assertEqual(release_order.track(), False)
def test_should_get_correct_delivery_if_exists(self): release_order = ReleaseOrderFactory(order_number=2342) release_order_item = ReleaseOrderItemFactory(release_order=release_order) delivery = DeliveryFactory() DeliveryNodeFactory(distribution_plan=delivery, item=release_order_item) self.assertEqual(release_order.delivery(), delivery.id)
def test_should_get_correct_track_status_when_delivery_is_tracked(self): release_order = ReleaseOrderFactory(order_number=2342) release_order_item = ReleaseOrderItemFactory( release_order=release_order) delivery = DeliveryFactory(track=True) DeliveryNodeFactory(distribution_plan=delivery, item=release_order_item, track=True) self.assertEqual(release_order.track(), True)
def test_should_get_correct_delivery_if_exists(self): release_order = ReleaseOrderFactory(order_number=2342) release_order_item = ReleaseOrderItemFactory( release_order=release_order) delivery = DeliveryFactory() DeliveryNodeFactory(distribution_plan=delivery, item=release_order_item) self.assertEqual(release_order.delivery(), delivery.id)
def test_should_get_orders__as_a_queryset__which_have_deliveries(self): order_one = ReleaseOrderFactory() order_two = ReleaseOrderFactory() order_three = ReleaseOrderFactory() order_item_one = ReleaseOrderItemFactory(release_order=order_one) order_item_two = ReleaseOrderItemFactory(release_order=order_two) DeliveryNodeFactory(item=order_item_one) DeliveryNodeFactory(item=order_item_two) delivered_orders = ReleaseOrder.objects.delivered().order_by('id') self.assertListEqual(list(delivered_orders), [order_one, order_two]) self.assertNotIn(order_three, delivered_orders)
def test_should_return_release_orders_filtered_by_from_or_to_date_separately( self): order = ReleaseOrderFactory(delivery_date=datetime.date(2014, 10, 5)) response = self.client.get('%s?%s' % (ENDPOINT_URL, 'fromDate=2014-10-04')) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['id'], order.id) response = self.client.get('%s?%s' % (ENDPOINT_URL, 'fromDate=2014-10-10')) self.assertEqual(len(response.data), 0) response = self.client.get('%s?%s' % (ENDPOINT_URL, 'toDate=2014-10-10')) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['id'], order.id) response = self.client.get('%s?%s' % (ENDPOINT_URL, 'toDate=2014-10-04')) self.assertEqual(len(response.data), 0)
def test_should_return_delivery_with_waybill_number(self): release_order = ReleaseOrderFactory(waybill=98765) release_order_item = ReleaseOrderItemFactory( release_order=release_order) node = DeliveryNodeFactory(item=release_order_item) self.assertEqual(node.number(), 98765)
def test_should_get_orders__as_a_queryset__whose_items_have_been_delivered_to_a_specific_consignee( self): consignee = ConsigneeFactory() order_one = ReleaseOrderFactory() order_two = ReleaseOrderFactory() order_three = ReleaseOrderFactory() order_item_one = ReleaseOrderItemFactory(release_order=order_one) order_item_two = ReleaseOrderItemFactory(release_order=order_two) DeliveryNodeFactory(item=order_item_one, consignee=consignee) DeliveryNodeFactory(item=order_item_two, consignee=consignee) consignee_orders = ReleaseOrder.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_returned_nodes_should_have_order_type_field(self): po_node = DeliveryNodeFactory(item=PurchaseOrderItemFactory(purchase_order=(PurchaseOrderFactory()))) ro_node = DeliveryNodeFactory(item=ReleaseOrderItemFactory(release_order=(ReleaseOrderFactory()))) response = self.client.get(ENDPOINT_URL) node_order_types = [node['order_type'] for node in response.data] self.assertItemsEqual([po_node.type(), ro_node.type()], node_order_types)
def test_is_release_order_need_sync_should_return_false(self): release_order = ReleaseOrderFactory() release_order_item = ReleaseOrderItemFactory( release_order=release_order) DeliveryNodeFactory(item=release_order_item) self.assertFalse( ReleaseOrderToDeliveryService.is_release_order_not_sync( release_order))
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 _create_node(self, delivery, is_purchase, order_number, track, tree_position, location='Madagascar'): if is_purchase: DeliveryNodeFactory( track=track, distribution_plan=delivery, tree_position=tree_position, location=location, item=PurchaseOrderItemFactory(purchase_order=PurchaseOrderFactory(order_number=order_number))) else: DeliveryNodeFactory( track=track, distribution_plan=delivery, tree_position=tree_position, location=location, item=ReleaseOrderItemFactory(release_order=ReleaseOrderFactory(waybill=order_number)))
def test_should_include_order_number_in_delivery_node_fields(self): purchase_order = PurchaseOrderFactory(order_number=200) DeliveryNodeFactory(item=PurchaseOrderItemFactory(purchase_order=purchase_order)) release_order = ReleaseOrderFactory(waybill=300) DeliveryNodeFactory(item=ReleaseOrderItemFactory(release_order=release_order)) response = self.client.get(ENDPOINT_URL) node_order_numbers = [node['order_number'] for node in response.data] self.assertItemsEqual([300, 200], node_order_numbers)
def test_should_know_its_order_number(self): purchase_order = PurchaseOrderFactory(order_number=200) po_node = DeliveryNodeFactory(item=PurchaseOrderItemFactory( purchase_order=purchase_order)) self.assertEqual(po_node.order_number(), 200) release_order = ReleaseOrderFactory(waybill=300) ro_node = DeliveryNodeFactory(item=ReleaseOrderItemFactory( release_order=release_order)) self.assertEqual(ro_node.order_number(), 300)
def test_should_provide_purchase_orders_that_have_deliveries_for_a_specific_consignee( self, mock_for_consignee): consignee_id = u'10' order = ReleaseOrderFactory() mock_for_consignee.return_value = ReleaseOrder.objects.all() response = self.client.get('%s?consignee=%s' % (ENDPOINT_URL, consignee_id)) mock_for_consignee.assert_called_with(consignee_id) self.assertDictContainsSubset({'id': order.id}, response.data[0])
def test_should_return_description_of_release_order_item_with_waybill_and_order_number( self): release_order = ReleaseOrderFactory(waybill=444555888) item = ReleaseOrderItemFactory(release_order=release_order) delivery = DeliveryFactory() DeliveryNodeFactory(distribution_plan=delivery, item=item) message = DeliveryRunMessage(delivery) self.assertRegexpMatches(message.description(), r'waybill(?i)') self.assertRegexpMatches(message.description(), r'444555888')
def create_release_orders(self): programme = ProgrammeFactory(name='YP104 MANAGEMENT RESULTS') sales_order = SalesOrderFactory(programme=programme) consignee = ConsigneeFactory() date = datetime.date(2014, 10, 05) purchase_order = PurchaseOrderFactory() ro_one = ReleaseOrderFactory(waybill=45143984) ro_two = ReleaseOrderFactory(waybill=234256, sales_order=sales_order, purchase_order=purchase_order, delivery_date=date) ro_item = ReleaseOrderItemFactory( release_order=ro_two, item=ItemFactory(description="HEK2013")) distribution_plan = DeliveryFactory() DeliveryNodeFactory(item=ro_item, distribution_plan=distribution_plan, location="Wakiso", consignee=consignee) return ro_one, ro_two, programme, consignee
def test_sync_release_order_to_delivery_should_execute(self): release_order = ReleaseOrderFactory() ReleaseOrderItemFactory(release_order=release_order) ReleaseOrderToDeliveryService.sync_release_order_to_delivery( release_order) self.assertTrue( len(DistributionPlan.objects.all()) is 1, 'Delivery not exist') self.assertTrue( len(DistributionPlanNode.objects.all()) is 1, 'DeliveryNode not exist') self.assertEqual(DistributionPlanNode.objects.all().first().item, ReleaseOrderItem.objects.all().first())
def test_should_add_node_related_to_changed_release_order_to_sync_data( self, mock_scan): release_order = ReleaseOrderFactory(waybill=123456) node = DeliveryNodeFactory(item=(ReleaseOrderItemFactory( release_order=release_order))) self.check_update_happens(node, release_order, { 'field': 'waybill', 'value': 654321 }, {'term': { 'order_item.order.id': [release_order.id] }}, mock_scan)
def test_list_should_not_display_deliveries_when_auto_track_off(self): first_consignee = ConsigneeFactory() ReleaseOrderItemFactory(release_order=ReleaseOrderFactory(consignee=first_consignee)) execute_sync_release_order_to_delivery() 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_list_should_display_deliveries_when_auto_track_on(self): first_consignee = ConsigneeFactory() system_settings = SystemSettings.objects.first() system_settings.auto_track = True system_settings.save() ReleaseOrderItemFactory(release_order=ReleaseOrderFactory(consignee=first_consignee)) execute_sync_release_order_to_delivery() 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), 1)
def test_should_serialise_node_sales_order_under_order(self): sales_order = SalesOrderFactory() release_order = ReleaseOrderFactory(sales_order=sales_order) node = DeliveryNodeFactory(item=(ReleaseOrderItemFactory( release_order=release_order))) expected = { "programme_id": sales_order.programme.id, "description": sales_order.description, "date": sales_order.date, "order_number": sales_order.order_number, "id": sales_order.id } serialised = serialise_nodes([node]) self.assertDictContainsSubset( expected, serialised[0]['order_item']['order']['sales_order'])
def test_should_get_export_list_for_warehouse(self, mock_build_contact): contact = { 'firstName': 'John', 'lastName': 'Ssenteza', 'phone': '+256 782 123456' } mock_build_contact.return_value = contact delivery = DeliveryFactory() consignee_name = 'the consignee' consignee = ConsigneeFactory(name=consignee_name) waybill = 5404939 mama_kit = 'Mama kit' material_code = 'Code 30' ro_item = ReleaseOrderItemFactory( item=ItemFactory(description=mama_kit, material_code=material_code), release_order=ReleaseOrderFactory(waybill=waybill)) delivery_date = '2015-09-06' luweero = 'Luweero' remark = 'manchester united beat liverpool' DeliveryNodeFactory(distribution_plan=delivery, delivery_date=delivery_date, consignee=consignee, item=ro_item, location=luweero, remark=remark) header = [ 'Waybill', 'Item Description', 'Material Code', 'Quantity Shipped', 'Shipment Date', 'Implementing Partner', 'Contact Person', 'Contact Number', 'District', 'Is End User', 'Is Tracked', 'Remarks' ] row_one = [ waybill, mama_kit, material_code, 10, delivery_date, consignee_name, '%s %s' % (contact['firstName'], contact['lastName']), contact['phone'], luweero, 'Yes', 'No', remark ] expected_data = [header, row_one] csv_exporter = DeliveryCSVExporter.create_delivery_exporter_by_type( 'Warehouse', self.HOSTNAME) self.assertEqual(csv_exporter.assemble_csv_data(), expected_data)
def test_should_serialise_node_purchase_order_under_release_order(self): purchase_order = PurchaseOrderFactory() release_order = ReleaseOrderFactory(purchase_order=purchase_order) node = DeliveryNodeFactory(item=(ReleaseOrderItemFactory( release_order=release_order))) expected = { "po_type": purchase_order.po_type, "order_number": purchase_order.order_number, "date": purchase_order.date, "sales_order_id": purchase_order.sales_order.id, "id": purchase_order.id, "is_single_ip": purchase_order.is_single_ip } serialised = serialise_nodes([node]) self.assertDictContainsSubset( expected, serialised[0]['order_item']['order']['purchase_order'])
def test_should_serialise_node_release_order(self): release_order = ReleaseOrderFactory() node = DeliveryNodeFactory(item=(ReleaseOrderItemFactory( release_order=release_order))) expected = { "consignee_id": release_order.consignee.id, "sales_order_id": release_order.sales_order.id, "purchase_order_id": release_order.purchase_order.id, "order_number": release_order.waybill, "order_type": "release_order", "release_order_number": release_order.order_number, "id": release_order.id, "delivery_date": release_order.delivery_date } serialised = serialise_nodes([node]) self.assertDictContainsSubset(expected, serialised[0]['order_item']['order'])
def test_should_return_none_if_release_order_does_not_have_delivery(self): release_order = ReleaseOrderFactory(order_number=2342) ReleaseOrderItemFactory(release_order=release_order) self.assertEqual(release_order.delivery(), None)
def test_is_release_order_need_sync_should_return_true(self): release_order = ReleaseOrderFactory() self.assertTrue( ReleaseOrderToDeliveryService.is_release_order_not_sync( release_order))
def test_should_return_none_if_release_order_does_not_have_delivery(self): release_order = ReleaseOrderFactory(order_number=2342) ReleaseOrderItemFactory(release_order=release_order) self.assertEqual(release_order.delivery(), None)
def test_no_two_release_orders_should_have_the_same_order_number(self): create_release_order = lambda: ReleaseOrderFactory(order_number=1234) create_release_order() self.assertRaises(IntegrityError, create_release_order)
def test_should_return_type(self): release_order = ReleaseOrderFactory() release_order_item = ReleaseOrderItemFactory( release_order=release_order) self.assertEqual(release_order_item.type(), "Waybill")
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