def test_should_reduce_quantity_received_when_answer_to_amount_received_is_deleted( self): answer = NumericAnswerFactory(question=self.amount_received, value=100, run=self.run) answer.delete() self.assertEqual(self._get_consignee_item().amount_received, 0)
def create_runs_and_answers(self): self.run_one = RunFactory(runnable=self.middle_man_node, status='completed') self.run_one_multiple_answer_one = MultipleChoiceAnswerFactory( run=self.run_one, question=self.multiple_choice_question, value=self.yes_option) self.run_one_multiple_answer_two = MultipleChoiceAnswerFactory( run=self.run_one, question=self.multiple_choice_question_two, value=self.yes_option) self.run_one_numeric_answer_one = NumericAnswerFactory( run=self.run_one, value=80, question=self.numeric_question) self.run_two = RunFactory(runnable=self.end_user_node, status='completed') self.run_two_multiple_answer_one = MultipleChoiceAnswerFactory( run=self.run_two, question=self.multiple_choice_question, value=self.yes_option) self.run_two_multiple_answer_two = MultipleChoiceAnswerFactory( run=self.run_two, question=self.multiple_choice_question_two, value=self.yes_option) self.run_two_numeric_answer_one = NumericAnswerFactory( run=self.run_two, value=80, question=self.numeric_question)
def test_gets_all_response_for_node_consignee(self): multichoice_question = MultipleChoiceQuestionFactory(label='productReceived') yes_option = OptionFactory(text='Yes', question=multichoice_question) no_option = OptionFactory(text='No', question=multichoice_question) sugar = ItemFactory(description='Sugar') salt = ItemFactory(description='Salt') numeric_question = NumericQuestionFactory(label='AmountReceived') item = SalesOrderItemFactory(item=salt, description='10 bags of salt') salt_node = DeliveryNodeFactory(quantity=100, item=PurchaseOrderItemFactory(sales_order_item=item)) run = RunFactory(runnable=salt_node, status='completed') sugar_item = SalesOrderItemFactory(item=sugar, description='10 bags of sugar') sugar_node = DeliveryNodeFactory(quantity=100, item=PurchaseOrderItemFactory(sales_order_item=sugar_item)) sugar_run = RunFactory(runnable=sugar_node, status='completed') multiple_answer_one = MultipleChoiceAnswerFactory(run=run, question=multichoice_question, value=yes_option) numeric_answer_one = NumericAnswerFactory(run=run, value=80, question=numeric_question) multiple_answer_two = MultipleChoiceAnswerFactory(run=sugar_run, question=multichoice_question, value=no_option) numeric_answer_two = NumericAnswerFactory(run=sugar_run, value=80, question=numeric_question) salt_node_responses = salt_node.responses() sugar_node_responses = sugar_node.responses() self.assertIn(multiple_answer_one, salt_node_responses[run]) self.assertIn(numeric_answer_one, salt_node_responses[run]) self.assertIn(multiple_answer_two, sugar_node_responses[sugar_run]) self.assertIn(numeric_answer_two, sugar_node_responses[sugar_run])
def add_a_node_with_response(self, number=1): delivery = DeliveryFactory(track=True) programme = ProgrammeFactory(name='special_program') sales_order = SalesOrderFactory(programme=programme) quantity_received_qn = NumericQuestion.objects.get(label='amountReceived') while number > 0: purchase_order = PurchaseOrderFactory(order_number=4748278 + number, sales_order=sales_order) po_item = PurchaseOrderItemFactory(purchase_order=purchase_order, item=ItemFactory(material_code='Code 23' + str(number), description='Jerrycans' + str(number))) self.extra_ip_node = DeliveryNodeFactory(programme=delivery.programme, distribution_plan=delivery, item=po_item, quantity=40, acknowledged=40, balance=40, location='Amudat', tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) run = RunFactory(runnable=self.extra_ip_node) NumericAnswerFactory(question=quantity_received_qn, value=40, run=run) number -= 1 end_node_one = DeliveryNodeFactory(programme=delivery.programme, consignee=self.end_user, parents=[(self.extra_ip_node, 30)], tree_position=DistributionPlanNode.END_USER, item=po_item) run_end_node_one = RunFactory(runnable=end_node_one) NumericAnswerFactory(question=quantity_received_qn, value=43, run=run_end_node_one)
def test_gets_correct_last_shipment_date_value_for_stock_report(self): programme = ProgrammeFactory(name='special_program') delivery = DeliveryFactory(track=True, programme=programme) sales_order = SalesOrderFactory(programme=programme) purchase_order = PurchaseOrderFactory(order_number=4748278, sales_order=sales_order) po_item = PurchaseOrderItemFactory(purchase_order=purchase_order, item=ItemFactory(material_code='Code 23', description='Jerrycans')) ip_node_one = DeliveryNodeFactory(distribution_plan=delivery, item=po_item, quantity=40, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER, delivery_date=FakeDate.build(2015, 03, 19)) run_one = RunFactory(runnable=ip_node_one) quantity_received_qn = NumericQuestion.objects.get(label='amountReceived') NumericAnswerFactory(question=quantity_received_qn, value=39, run=run_one) last_shipment_date = FakeDate.build(2015, 10, 07) ip_node_two = DeliveryNodeFactory(distribution_plan=delivery, item=po_item, quantity=30, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER, delivery_date=last_shipment_date) run_two = RunFactory(runnable=ip_node_two) NumericAnswerFactory(question=quantity_received_qn, value=40, run=run_two) expected_data = [ {'document_number': purchase_order.order_number, 'programme': programme.name, 'last_shipment_date': str(last_shipment_date), 'total_value_received': Decimal('79'), 'total_value_dispensed': Decimal('0'), 'total_value_lost': Decimal('0'), 'balance': Decimal('79'), 'items': [{'code': unicode(po_item.item.material_code), 'description': unicode(po_item.item.description), 'consignee': self.ip.name, 'location': ip_node_one.location, 'quantity_delivered': 3, 'date_delivered': str(self.ip_node_two.delivery_date), 'quantity_confirmed': 2, 'date_confirmed': '2014-01-02', 'quantity_dispatched': 2, 'quantity_lost': 0, 'balance': 0 }, {'code': unicode(po_item.item.material_code), 'description': unicode(po_item.item.description), 'consignee': self.ip.name, 'location': ip_node_two.location, 'quantity_delivered': 5, 'date_delivered': str(self.ip_node_one.delivery_date), 'quantity_confirmed': 4, 'date_confirmed': '2014-01-01', 'quantity_dispatched': 2, 'quantity_lost': 0, 'balance': 2 }] }] response = self.client.get(ENDPOINT_URL) self.assert_api_response_with_correct_last_shipment_date(response, expected_data)
def test_should_update_quantity_received_when_amount_received_answer_value_is_updated( self): answer = NumericAnswerFactory(question=self.amount_received, value=100, run=self.run) answer.value = 75 answer.save() self.assertEqual(self._get_consignee_item().amount_received, 75)
def test_should_add_quantity_received_to_stock_if_amount_received_question_has_not_been_answered_yet( self): self.assertEqual(self._get_consignee_item().amount_received, 0) NumericAnswerFactory(question=self.amount_received, value=100, run=self.run) self.assertEqual(self._get_consignee_item().amount_received, 100)
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
def test_should_get_all_answers(self): run = RunFactory() multiple_answer_one = MultipleChoiceAnswerFactory(run=run) numeric_answer_one = NumericAnswerFactory(run=run) run_answers = run.answers() self.assertIn(multiple_answer_one, run_answers) self.assertIn(numeric_answer_one, run_answers)
def test_should_update_node_balance_with_quantity_acknowledged(self): initial_balance = self.node.balance NumericAnswerFactory(question=self.amount_received, value=103, run=self.run) node = DistributionPlanNode.objects.get(pk=self.node.id) self.assertNotEquals(node.balance, initial_balance) self.assertEquals(node.balance, 103)
def __login_and_update_existing_numeric_answer(self, login_func, updated_numeric_answer): login_func() numeric_question = NumericQuestionFactory(label='amountReceived') numeric_answer = NumericAnswerFactory(value=1, question=numeric_question) return self.client.patch(ENDPOINT_URL + str(numeric_answer.id) + "/", updated_numeric_answer, format='json')
def test_should_serialise_node_with_value_lost(self): po_item = PurchaseOrderItemFactory(quantity=108, value=33) node = DeliveryNodeFactory(quantity=100, item=po_item) amount_received_qn = NumericQuestionFactory(label='amountReceived') run = RunFactory(runnable=node) NumericAnswerFactory(question=amount_received_qn, run=run, value=53) serialised = serialise_nodes([node]) self.assertDictContainsSubset({'value_lost': 14.36}, serialised[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)
def create_runs_and_answers(self): self.run_one = RunFactory(runnable=self.middle_man_node, status='completed') self.run_one_multiple_answer_one = MultipleChoiceAnswerFactory(run=self.run_one, question=self.multiple_choice_question, value=self.yes_option) self.run_one_multiple_answer_two = MultipleChoiceAnswerFactory(run=self.run_one, question=self.multiple_choice_question_two, value=self.yes_option) self.run_one_numeric_answer_one = NumericAnswerFactory(run=self.run_one, value=80, question=self.numeric_question) self.run_two = RunFactory(runnable=self.end_user_node, status='completed') self.run_two_multiple_answer_one = MultipleChoiceAnswerFactory(run=self.run_two, question=self.multiple_choice_question, value=self.yes_option) self.run_two_multiple_answer_two = MultipleChoiceAnswerFactory(run=self.run_two, question=self.multiple_choice_question_two, value=self.yes_option) self.run_two_numeric_answer_one = NumericAnswerFactory(run=self.run_two, value=80, question=self.numeric_question)
def create_line_item_runs_and_answers(self): self.line_item_run_one = NodeLineItemRunFactory(node_line_item=self.node_line_item_one, status='completed') self.run_one_multiple_answer_one = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_one, question=self.multiple_choice_question, value=self.yes_option) self.run_one_multiple_answer_two = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_one, question=self.multiple_choice_question_two, value=self.yes_option) self.run_one_numeric_answer_one = NumericAnswerFactory(line_item_run=self.line_item_run_one, value=80, question=self.numeric_question) self.line_item_run_two = NodeLineItemRunFactory(node_line_item=self.node_line_item_two, status='completed') self.run_two_multiple_answer_one = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_two, question=self.multiple_choice_question, value=self.yes_option) self.run_two_multiple_answer_two = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_two, question=self.multiple_choice_question_two, value=self.yes_option) self.run_two_numeric_answer_one = NumericAnswerFactory(line_item_run=self.line_item_run_two, value=80, question=self.numeric_question)
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)
def test_should_update_numeric_answers(self): numeric_question = NumericQuestionFactory(label='amountReceived') numeric_answer = NumericAnswerFactory(value=1, question=numeric_question) updated_numeric_answer_details = {"value": 2} response = self.client.patch(ENDPOINT_URL + str(numeric_answer.id) + "/", updated_numeric_answer_details, format='json') self.assertEqual(response.status_code, 200) self.assertDictContainsSubset(updated_numeric_answer_details, response.data)
def test_should_add_node_related_to_deleted_numeric_answer_to_sync_data( self, mock_scan): node = DeliveryNodeFactory() answer = NumericAnswerFactory(run=RunFactory(runnable=node), question=NumericQuestionFactory()) self.check_update_happens( node, answer, delete=True, mock_scan=mock_scan, expected_match_clause={'term': { 'id': [node.id] }})
def test_should_add_node_related_to_changed_numeric_answer_to_sync_data( self, mock_scan): node = DeliveryNodeFactory() answer = NumericAnswerFactory(value=50, question=NumericQuestionFactory(), run=RunFactory(runnable=node)) self.check_update_happens(node, answer, { 'field': 'value', 'value': 100 }, {'term': { 'responses.id': [answer.id] }}, mock_scan)
def test_should_get_Numeric_answers(self): numeric_question = NumericQuestionFactory(label='amountReceived') numeric_answer = NumericAnswerFactory(value=1, question=numeric_question) numeric_answer_details = { "id": numeric_answer.id, "value": numeric_answer.value, "question": numeric_answer.question_id, "run": numeric_answer.run_id } response = self.client.get(ENDPOINT_URL) self.assertEqual(response.status_code, 200) self.assertDictContainsSubset(numeric_answer_details, response.data[0])
def test_should_get_latest_response(self): multichoice_question = MultipleChoiceQuestionFactory(label='productReceived') no_option = OptionFactory(text='No', question=multichoice_question) sugar = ItemFactory(description='Sugar') numeric_question = NumericQuestionFactory(label='AmountReceived') node = DeliveryNodeFactory(quantity=100, item=PurchaseOrderItemFactory(item=sugar)) run = RunFactory(runnable=node, status='completed') self.assertIsNone(node.latest_response()) multiple_answer = MultipleChoiceAnswerFactory(run=run, question=multichoice_question, value=no_option) self.assertEqual(node.latest_response(), multiple_answer) numeric_answer = NumericAnswerFactory(run=run, value=80, question=numeric_question) self.assertEqual(node.latest_response(), numeric_answer)
def test_gets_all_response_for_node_consignee(self): multichoice_question = MultipleChoiceQuestionFactory( label='productReceived') yes = multichoice_question.option_set.first() numeric_question = NumericQuestionFactory(label='AmountReceived') salt = ItemFactory(description='Salt') sales_order = SalesOrderFactory() sales_order_item = SalesOrderItemFactory(item=salt, description='10 bags of salt', sales_order=sales_order) item = PurchaseOrderItemFactory(sales_order_item=sales_order_item) node = DeliveryNodeFactory(quantity=100, item=item) child_node_one = DeliveryNodeFactory(parents=[(node, 50)], item=item) child_node_two = DeliveryNodeFactory(parents=[(node, 50)], item=item) child_node_three = DeliveryNodeFactory(parents=[(child_node_one, 50)], item=item) run = RunFactory(runnable=node, status='completed') run_one = RunFactory(runnable=child_node_one, status='completed') run_two = RunFactory(runnable=child_node_two, status='completed') run_three = RunFactory(runnable=child_node_three, status='completed') NumericAnswerFactory(run=run, value=80, question=numeric_question) MultipleChoiceAnswerFactory(run=run_one, question=multichoice_question, value=yes) MultipleChoiceAnswerFactory(run=run_two, question=multichoice_question, value=yes) NumericAnswerFactory(run=run_three, value=80, question=numeric_question) self.maxDiff = None expected_data = { 'node': node.consignee.name, 'children': [{ 'node': child_node_one.consignee.name, 'children': [{ 'node': child_node_three.consignee.name, 'children': [], 'answers': { u'AmountReceived': u'80' } }], 'answers': { u'productReceived': u'UNCATEGORISED' } }, { 'node': child_node_two.consignee.name, 'children': [], 'answers': { u'productReceived': u'UNCATEGORISED' } }], 'answers': { u'AmountReceived': u'80' } } response_details_route = 'distribution-plan-responses/%s/sales_order_item/%s/' endpoint_url = BACKEND_URL + response_details_route % ( node.consignee.id, item.id) response = self.client.get(endpoint_url) self.assertDictContainsSubset({'node': expected_data['node']}, response.data) self.assertDictContainsSubset({'answers': expected_data['answers']}, response.data) self.assertItemsEqual(expected_data['children'], response.data['children'])
def test_should_update_quantity_received_when_amount_received_answer_value_is_updated(self): answer = NumericAnswerFactory(question=self.amount_received, value=100, run=self.run) answer.value = 75 answer.save() self.assertEqual(self._get_consignee_item().amount_received, 75)
def test_should_reduce_quantity_received_when_answer_to_amount_received_is_deleted(self): answer = NumericAnswerFactory(question=self.amount_received, value=100, run=self.run) answer.delete() self.assertEqual(self._get_consignee_item().amount_received, 0)
def test_should_tell_if_numeric_question_ends_the_flow(self): answer = NumericAnswerFactory() flow = Flow(final_end_nodes=[[answer.question.id, Flow.NO_OPTION]]) self.assertTrue(flow.is_final_ended(answer))
distribution_plan=plan) DeliveryNodeFactory(parents=[(wakiso_node_1, 40)], tree_position="MIDDLE_MAN", item=po_item_1, distribution_plan=plan) DeliveryNodeFactory(parents=[(wakiso_node_2, 58)], tree_position="END_USER", item=po_item_2, distribution_plan=plan) # web answers MultipleChoiceAnswerFactory(question=web_questions.web_question_1, value=web_questions.yes_1, run=run_77) NumericAnswerFactory(question=web_questions.web_question_2, value=50, run=run_77) MultipleChoiceAnswerFactory(question=web_questions.web_question_3, value=web_questions.expired, run=run_77) MultipleChoiceAnswerFactory(question=web_questions.web_question_4, value=web_questions.yes_2, run=run_77) TextAnswerFactory(question=web_questions.web_question_5, value='nothing much', run=run_77) MultipleChoiceAnswerFactory(question=web_questions.web_question_1, value=web_questions.yes_1, run=run_78) NumericAnswerFactory(question=web_questions.web_question_2,
class ResponsesEndPointTest(AuthenticatedAPITestCase): def create_questions_and_items(self): self.multiple_choice_question_two = MultipleChoiceQuestionFactory( label='satisfiedWithProduct') self.multiple_choice_question = MultipleChoiceQuestionFactory( label='productReceived') self.yes_option = OptionFactory(text='Yes', question=self.multiple_choice_question) OptionFactory(text='No', question=self.multiple_choice_question) self.numeric_question = NumericQuestionFactory(label='amountReceived') salt = ItemFactory(description='Salt') self.sales_order_item = SalesOrderItemFactory( item=salt, description='10 bags of salt') self.item = PurchaseOrderItemFactory(item=salt, value=1000) def create_nodes(self): self.ip_node = DeliveryNodeFactory(quantity=100) self.middle_man_node = DeliveryNodeFactory( parents=[(self.ip_node, 100)], tree_position=DistributionPlanNode.MIDDLE_MAN, distribution_plan=self.ip_node.distribution_plan, item=self.item) self.end_user_node = DeliveryNodeFactory( parents=[(self.middle_man_node, 100)], tree_position=DistributionPlanNode.END_USER, distribution_plan=self.ip_node.distribution_plan, item=self.item) def create_runs_and_answers(self): self.run_one = RunFactory(runnable=self.middle_man_node, status='completed') self.run_one_multiple_answer_one = MultipleChoiceAnswerFactory( run=self.run_one, question=self.multiple_choice_question, value=self.yes_option) self.run_one_multiple_answer_two = MultipleChoiceAnswerFactory( run=self.run_one, question=self.multiple_choice_question_two, value=self.yes_option) self.run_one_numeric_answer_one = NumericAnswerFactory( run=self.run_one, value=80, question=self.numeric_question) self.run_two = RunFactory(runnable=self.end_user_node, status='completed') self.run_two_multiple_answer_one = MultipleChoiceAnswerFactory( run=self.run_two, question=self.multiple_choice_question, value=self.yes_option) self.run_two_multiple_answer_two = MultipleChoiceAnswerFactory( run=self.run_two, question=self.multiple_choice_question_two, value=self.yes_option) self.run_two_numeric_answer_one = NumericAnswerFactory( run=self.run_two, value=80, question=self.numeric_question) def expected_response_data(self, node, consignee, programme, type): expected_data = { u'item': u'Salt', u'amountSent': 100, u'value': 1000, u'latestResponseDate': node.latest_response().date_created, u'node': node.id, u'consignee': { u'id': consignee.id, u'name': consignee.name, u'type': type }, u'ip': { 'id': node.ip.id, 'location': node.ip.location }, u'%s' % self.numeric_question.label: u'%s' % self.run_one_numeric_answer_one.format(), u'%s' % self.multiple_choice_question.label: u'%s' % self.run_one_multiple_answer_one.format(), u'%s' % self.multiple_choice_question_two.label: u'%s' % self.run_one_multiple_answer_two.format(), u'programme': { u'id': programme.id, u'name': programme.name }, u'location': node.location } return expected_data def test_should_provide_summary_data_from_node_response(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() url = "%s%d/" % (ENDPOINT_URL, self.middle_man_node.consignee.id) response = self.client.get(url, format='json') consignee = self.middle_man_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data = self.expected_response_data( self.middle_man_node, consignee, programme, DistributionPlanNode.MIDDLE_MAN) self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data[0]) self.assertEquals(len(response.data), 1) self.assertDictContainsSubset(expected_data, response.data[0]) def test_should_provide_summary_data_from_all_node_responses(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() response = self.client.get(ENDPOINT_URL, format='json') consignee_one = self.middle_man_node.consignee consignee_two = self.end_user_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data_node_one = self.expected_response_data( self.middle_man_node, consignee_one, programme, DistributionPlanNode.MIDDLE_MAN) expected_data_node_two = self.expected_response_data( self.end_user_node, consignee_two, programme, DistributionPlanNode.END_USER) self.assertEqual(response.status_code, 200) self.assertEquals(len(response.data), 2) self.assertIn(expected_data_node_one, response.data) self.assertIn(expected_data_node_two, response.data) def test_should_provide_summary_data_from_all_end_user_node_responses( self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() response = self.client.get(END_USER_ENDPOINT_URL, format='json') consignee = self.end_user_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data_node = self.expected_response_data( self.end_user_node, consignee, programme, DistributionPlanNode.END_USER) self.assertEqual(response.status_code, 200) self.assertEquals(len(response.data), 1) self.assertEquals(expected_data_node, response.data[0]) self.assertDictContainsSubset(expected_data_node, response.data[0]) def test_should_provide_summary_data_from_node_response_for_end_user(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() url = "%s%d/" % (NODE_ENDPOINT_URL, self.end_user_node.id) response = self.client.get(url, format='json') expected_data = { "node": { "plan_id": self.end_user_node.distribution_plan.id, "contact_person_id": self.end_user_node.contact_person_id, "consignee": self.end_user_node.consignee.id, "id": self.end_user_node.id, "location": self.end_user_node.location }, "run_id": self.run_two.id, "responses": { self.numeric_question.label: { "id": self.run_two_numeric_answer_one.id, "value": self.run_two_numeric_answer_one.value, "formatted_value": self.run_two_numeric_answer_one.format() }, self.multiple_choice_question.label: { "id": self.run_two_multiple_answer_one.id, "value": self.yes_option.id, "formatted_value": self.run_two_multiple_answer_one.format() }, self.multiple_choice_question_two.label: { "id": self.run_two_multiple_answer_two.id, "value": self.yes_option.id, "formatted_value": self.run_two_multiple_answer_two.format() } } } self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data) self.assertDictContainsSubset(expected_data, response.data) def test_should_not_provide_summary_data_from_node_response_for_middleman_user( self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() url = "%s%d/" % (NODE_ENDPOINT_URL, self.run_one.id) response = self.client.get(url, format='json') expected_data = {} self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data) self.assertEquals(len(response.data), 0) def test_should_only_return_implementing_partner_responses(self): question = MultipleChoiceQuestionFactory(label='productReceived') yes_option = OptionFactory(text='Yes', question=question) item = ItemFactory(description='Salt') order_item = PurchaseOrderItemFactory(item=item, value=1000) node_ip_one = DeliveryNodeFactory( tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER, item=order_item) node_end_user = DeliveryNodeFactory( tree_position=DistributionPlanNode.END_USER, item=order_item) run_one = RunFactory(runnable=node_ip_one, status='completed') run_two = RunFactory(runnable=node_end_user, status='completed') answer_one = MultipleChoiceAnswerFactory(run=run_one, question=question, value=yes_option) MultipleChoiceAnswerFactory(run=run_two, question=question, value=yes_option) response = self.client.get(IP_ENDPOINT_URL, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) node_item = response.data[0] self.assertEqual(node_item['item'], 'Salt') self.assertEqual( node_item['programme'], { 'id': node_ip_one.distribution_plan.programme.id, 'name': node_ip_one.distribution_plan.programme.name }) self.assertEqual(node_item['ip'], { 'id': node_ip_one.ip.id, 'location': unicode(node_ip_one.ip.location) }) self.assertEqual(node_item[question.label], answer_one.format())
class ResponsesEndPointTest(AuthenticatedAPITestCase): def create_questions_and_items(self): self.multiple_choice_question_two = MultipleChoiceQuestionFactory(label='satisfiedWithProduct') self.multiple_choice_question = MultipleChoiceQuestionFactory(label='productReceived') self.yes_option = OptionFactory(text='Yes', question=self.multiple_choice_question) OptionFactory(text='No', question=self.multiple_choice_question) self.numeric_question = NumericQuestionFactory(label='amountReceived') salt = ItemFactory(description='Salt') self.item = SalesOrderItemFactory(item=salt, description='10 bags of salt') def create_nodes_and_line_item(self): self.ip_node = DistributionPlanNodeFactory() self.middle_man_node = DistributionPlanNodeFactory(parent=self.ip_node, tree_position=DistributionPlanNode.MIDDLE_MAN, distribution_plan=self.ip_node.distribution_plan) self.node_line_item_one = DistributionPlanLineItemFactory(distribution_plan_node=self.middle_man_node, targeted_quantity=100, item=self.item) self.end_user_node = DistributionPlanNodeFactory(parent=self.middle_man_node, tree_position=DistributionPlanNode.END_USER, distribution_plan=self.ip_node.distribution_plan) self.node_line_item_two = DistributionPlanLineItemFactory(distribution_plan_node=self.end_user_node, targeted_quantity=100, item=self.item) def create_line_item_runs_and_answers(self): self.line_item_run_one = NodeLineItemRunFactory(node_line_item=self.node_line_item_one, status='completed') self.run_one_multiple_answer_one = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_one, question=self.multiple_choice_question, value=self.yes_option) self.run_one_multiple_answer_two = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_one, question=self.multiple_choice_question_two, value=self.yes_option) self.run_one_numeric_answer_one = NumericAnswerFactory(line_item_run=self.line_item_run_one, value=80, question=self.numeric_question) self.line_item_run_two = NodeLineItemRunFactory(node_line_item=self.node_line_item_two, status='completed') self.run_two_multiple_answer_one = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_two, question=self.multiple_choice_question, value=self.yes_option) self.run_two_multiple_answer_two = MultipleChoiceAnswerFactory(line_item_run=self.line_item_run_two, question=self.multiple_choice_question_two, value=self.yes_option) self.run_two_numeric_answer_one = NumericAnswerFactory(line_item_run=self.line_item_run_two, value=80, question=self.numeric_question) def expected_response_data(self, node, consignee, programme, type): expected_data = {u'item': u'10 bags of salt', u'amountSent': 100, u'node': node.id, u'consignee': {u'id': consignee.id, u'name': consignee.name, u'type': type}, u'ip': node.get_ip(), u'%s' % self.numeric_question.label: u'%s' % self.run_one_numeric_answer_one.format(), u'%s' % self.multiple_choice_question.label: u'%s' % self.run_one_multiple_answer_one.format(), u'%s' % self.multiple_choice_question_two.label: u'%s' % self.run_one_multiple_answer_two.format(), u'programme': {u'id': programme.id, u'name': programme.name}, u'location': node.location} return expected_data def test_should_provide_summary_data_from_node_response(self): self.create_questions_and_items() self.create_nodes_and_line_item() self.create_line_item_runs_and_answers() url = "%s%d/" % (ENDPOINT_URL, self.middle_man_node.consignee.id) response = self.client.get(url, format='json') consignee = self.middle_man_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data = self.expected_response_data(self.middle_man_node, consignee, programme, DistributionPlanNode.MIDDLE_MAN) self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data[0]) self.assertEquals(len(response.data), 1) self.assertDictContainsSubset(expected_data, response.data[0]) def test_should_provide_summary_data_from_all_node_responses(self): self.create_questions_and_items() self.create_nodes_and_line_item() self.create_line_item_runs_and_answers() response = self.client.get(ENDPOINT_URL, format='json') consignee_one = self.middle_man_node.consignee consignee_two = self.end_user_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data_node_one = self.expected_response_data(self.middle_man_node, consignee_one, programme, DistributionPlanNode.MIDDLE_MAN) expected_data_node_two = self.expected_response_data(self.end_user_node, consignee_two, programme, DistributionPlanNode.END_USER) self.assertEqual(response.status_code, 200) self.assertEquals(len(response.data), 2) self.assertEquals(expected_data_node_one, response.data[0]) self.assertEquals(expected_data_node_two, response.data[1]) self.assertDictContainsSubset(expected_data_node_one, response.data[0]) self.assertDictContainsSubset(expected_data_node_two, response.data[1]) def test_should_provide_summary_data_from_all_end_user_node_responses(self): self.create_questions_and_items() self.create_nodes_and_line_item() self.create_line_item_runs_and_answers() response = self.client.get(END_USER_ENDPOINT_URL, format='json') consignee = self.end_user_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data_node = self.expected_response_data(self.end_user_node, consignee, programme, DistributionPlanNode.END_USER) self.assertEqual(response.status_code, 200) self.assertEquals(len(response.data), 1) self.assertEquals(expected_data_node, response.data[0]) self.assertDictContainsSubset(expected_data_node, response.data[0]) def test_should_provide_summary_data_from_plan_item_response_for_end_user(self): self.create_questions_and_items() self.create_nodes_and_line_item() self.create_line_item_runs_and_answers() url = "%s%d/" % (PLAN_ITEM_ENDPOINT_URL, self.node_line_item_two.id) response = self.client.get(url, format='json') expected_data = { "node": { "plan_id": self.end_user_node.distribution_plan.id, "contact_person_id": self.end_user_node.contact_person_id, "consignee": self.end_user_node.consignee.id, "id": self.end_user_node.id, "location": self.end_user_node.location }, "line_item_run_id": self.line_item_run_two.id, "responses": { self.numeric_question.label: { "id": self.run_two_numeric_answer_one.id, "value": self.run_two_numeric_answer_one.value, "formatted_value": self.run_two_numeric_answer_one.format() }, self.multiple_choice_question.label: { "id": self.run_two_multiple_answer_one.id, "value": self.yes_option.id, "formatted_value": self.run_two_multiple_answer_one.format() }, self.multiple_choice_question_two.label: { "id": self.run_two_multiple_answer_two.id, "value": self.yes_option.id, "formatted_value": self.run_two_multiple_answer_two.format() } } } self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data) self.assertDictContainsSubset(expected_data, response.data) def test_should_not_provide_summary_data_from_plan_item_response_for_middleman_user(self): self.create_questions_and_items() self.create_nodes_and_line_item() self.create_line_item_runs_and_answers() url = "%s%d/" % (PLAN_ITEM_ENDPOINT_URL, self.node_line_item_one.id) response = self.client.get(url, format='json') expected_data = {} self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data) self.assertEquals(len(response.data), 0)
def setup_responses(self): DeliveryNode.objects.all().delete() MultipleChoiceQuestion.objects.all().delete() from eums.fixtures.end_user_questions import seed_questions questions, options = seed_questions() self.programme = ProgrammeFactory(name='my-program') distribution_plan = DeliveryFactory(programme=self.programme, track=True, location='someLocation') item_one = ItemFactory(description='desc_one', material_code='code_one') item_two = ItemFactory(description='desc_two', material_code='code_two') item_three = ItemFactory(description='desc_three', material_code='code_three') po_item_one = PurchaseOrderItemFactory(quantity=100, value=1000, item=item_one) po_item_two = PurchaseOrderItemFactory(quantity=100, value=1000, item=item_two) po_item_three = PurchaseOrderItemFactory(quantity=100, value=1000, item=item_three) end_user_node_one = DeliveryNodeFactory( tree_position=DeliveryNode.END_USER, quantity=6, location='someLocation', distribution_plan=distribution_plan, item=po_item_one) MultipleChoiceAnswerFactory(run=RunFactory( runnable=end_user_node_one, status=Run.STATUS.scheduled), question=questions['WAS_PRODUCT_RECEIVED'], value=options['PRODUCT_WAS_RECEIVED']) MultipleChoiceAnswerFactory(run=RunFactory( runnable=end_user_node_one, status=Run.STATUS.scheduled), question=questions['QUALITY_OF_PRODUCT'], value=options['DAMAGED']) NumericAnswerFactory(run=RunFactory(runnable=end_user_node_one, status=Run.STATUS.scheduled), question=questions['AMOUNT_RECEIVED'], value=4) self.ip = ConsigneeFactory() self.today = FakeDate.today() self.end_user_node_two = DeliveryNodeFactory( tree_position=DeliveryNode.END_USER, quantity=9, location='someLocation', item=po_item_two, distribution_plan=distribution_plan) MultipleChoiceAnswerFactory(run=RunFactory( runnable=self.end_user_node_two, status=Run.STATUS.scheduled), question=questions['WAS_PRODUCT_RECEIVED'], value=options['PRODUCT_WAS_NOT_RECEIVED']) end_user_node_three = DeliveryNodeFactory( tree_position=DeliveryNode.END_USER, quantity=10, item=po_item_three, location='someLocation', distribution_plan=distribution_plan, ip=self.ip, consignee=self.ip, delivery_date=self.today + datetime.timedelta(days=3)) MultipleChoiceAnswerFactory(run=RunFactory( runnable=end_user_node_three, status=Run.STATUS.scheduled), question=questions['WAS_PRODUCT_RECEIVED'], value=options['PRODUCT_WAS_NOT_RECEIVED']) end_user_node_four = DeliveryNodeFactory( tree_position=DeliveryNode.END_USER, quantity=40, item=po_item_one) MultipleChoiceAnswerFactory(run=RunFactory( runnable=end_user_node_four, status=Run.STATUS.scheduled), question=questions['WAS_PRODUCT_RECEIVED'], value=options['PRODUCT_WAS_NOT_RECEIVED']) end_user_node_five = DeliveryNodeFactory( tree_position=DeliveryNode.END_USER, quantity=50, item=po_item_one) MultipleChoiceAnswerFactory(run=RunFactory( runnable=end_user_node_five, status=Run.STATUS.scheduled), question=questions['WAS_PRODUCT_RECEIVED'], value=options['PRODUCT_WAS_NOT_RECEIVED']) non_response_node = DeliveryNodeFactory( tree_position=DeliveryNode.END_USER, track=True, quantity=60, item=po_item_one) RunFactory(runnable=non_response_node, status=Run.STATUS.scheduled)
class ResponsesEndPointTest(AuthenticatedAPITestCase): def create_questions_and_items(self): self.multiple_choice_question_two = MultipleChoiceQuestionFactory(label='satisfiedWithProduct') self.multiple_choice_question = MultipleChoiceQuestionFactory(label='productReceived') self.yes_option = OptionFactory(text='Yes', question=self.multiple_choice_question) OptionFactory(text='No', question=self.multiple_choice_question) self.numeric_question = NumericQuestionFactory(label='amountReceived') salt = ItemFactory(description='Salt') self.sales_order_item = SalesOrderItemFactory(item=salt, description='10 bags of salt') self.item = PurchaseOrderItemFactory(item=salt, value=1000) def create_nodes(self): self.ip_node = DeliveryNodeFactory(quantity=100) self.middle_man_node = DeliveryNodeFactory(parents=[(self.ip_node, 100)], tree_position=DistributionPlanNode.MIDDLE_MAN, distribution_plan=self.ip_node.distribution_plan, item=self.item) self.end_user_node = DeliveryNodeFactory(parents=[(self.middle_man_node, 100)], tree_position=DistributionPlanNode.END_USER, distribution_plan=self.ip_node.distribution_plan, item=self.item) def create_runs_and_answers(self): self.run_one = RunFactory(runnable=self.middle_man_node, status='completed') self.run_one_multiple_answer_one = MultipleChoiceAnswerFactory(run=self.run_one, question=self.multiple_choice_question, value=self.yes_option) self.run_one_multiple_answer_two = MultipleChoiceAnswerFactory(run=self.run_one, question=self.multiple_choice_question_two, value=self.yes_option) self.run_one_numeric_answer_one = NumericAnswerFactory(run=self.run_one, value=80, question=self.numeric_question) self.run_two = RunFactory(runnable=self.end_user_node, status='completed') self.run_two_multiple_answer_one = MultipleChoiceAnswerFactory(run=self.run_two, question=self.multiple_choice_question, value=self.yes_option) self.run_two_multiple_answer_two = MultipleChoiceAnswerFactory(run=self.run_two, question=self.multiple_choice_question_two, value=self.yes_option) self.run_two_numeric_answer_one = NumericAnswerFactory(run=self.run_two, value=80, question=self.numeric_question) def expected_response_data(self, node, consignee, programme, type): expected_data = {u'item': u'Salt', u'amountSent': 100, u'value': 1000, u'latestResponseDate': node.latest_response().date_created, u'node': node.id, u'consignee': {u'id': consignee.id, u'name': consignee.name, u'type': type}, u'ip': {'id': node.ip.id, 'location': node.ip.location}, u'%s' % self.numeric_question.label: u'%s' % self.run_one_numeric_answer_one.format(), u'%s' % self.multiple_choice_question.label: u'%s' % self.run_one_multiple_answer_one.format(), u'%s' % self.multiple_choice_question_two.label: u'%s' % self.run_one_multiple_answer_two.format(), u'programme': {u'id': programme.id, u'name': programme.name}, u'location': node.location} return expected_data def test_should_provide_summary_data_from_node_response(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() url = "%s%d/" % (ENDPOINT_URL, self.middle_man_node.consignee.id) response = self.client.get(url, format='json') consignee = self.middle_man_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data = self.expected_response_data(self.middle_man_node, consignee, programme, DistributionPlanNode.MIDDLE_MAN) self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data[0]) self.assertEquals(len(response.data), 1) self.assertDictContainsSubset(expected_data, response.data[0]) def test_should_provide_summary_data_from_all_node_responses(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() response = self.client.get(ENDPOINT_URL, format='json') consignee_one = self.middle_man_node.consignee consignee_two = self.end_user_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data_node_one = self.expected_response_data(self.middle_man_node, consignee_one, programme, DistributionPlanNode.MIDDLE_MAN) expected_data_node_two = self.expected_response_data(self.end_user_node, consignee_two, programme, DistributionPlanNode.END_USER) self.assertEqual(response.status_code, 200) self.assertEquals(len(response.data), 2) self.assertIn(expected_data_node_one, response.data) self.assertIn(expected_data_node_two, response.data) def test_should_provide_summary_data_from_all_end_user_node_responses(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() response = self.client.get(END_USER_ENDPOINT_URL, format='json') consignee = self.end_user_node.consignee programme = self.middle_man_node.distribution_plan.programme expected_data_node = self.expected_response_data(self.end_user_node, consignee, programme, DistributionPlanNode.END_USER) self.assertEqual(response.status_code, 200) self.assertEquals(len(response.data), 1) self.assertEquals(expected_data_node, response.data[0]) self.assertDictContainsSubset(expected_data_node, response.data[0]) def test_should_provide_summary_data_from_node_response_for_end_user(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() url = "%s%d/" % (NODE_ENDPOINT_URL, self.end_user_node.id) response = self.client.get(url, format='json') expected_data = { "node": { "plan_id": self.end_user_node.distribution_plan.id, "contact_person_id": self.end_user_node.contact_person_id, "consignee": self.end_user_node.consignee.id, "id": self.end_user_node.id, "location": self.end_user_node.location }, "run_id": self.run_two.id, "responses": { self.numeric_question.label: { "id": self.run_two_numeric_answer_one.id, "value": self.run_two_numeric_answer_one.value, "formatted_value": self.run_two_numeric_answer_one.format() }, self.multiple_choice_question.label: { "id": self.run_two_multiple_answer_one.id, "value": self.yes_option.id, "formatted_value": self.run_two_multiple_answer_one.format() }, self.multiple_choice_question_two.label: { "id": self.run_two_multiple_answer_two.id, "value": self.yes_option.id, "formatted_value": self.run_two_multiple_answer_two.format() } } } self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data) self.assertDictContainsSubset(expected_data, response.data) def test_should_not_provide_summary_data_from_node_response_for_middleman_user(self): self.create_questions_and_items() self.create_nodes() self.create_runs_and_answers() url = "%s%d/" % (NODE_ENDPOINT_URL, self.run_one.id) response = self.client.get(url, format='json') expected_data = {} self.assertEqual(response.status_code, 200) self.assertEquals(expected_data, response.data) self.assertEquals(len(response.data), 0) def test_should_only_return_implementing_partner_responses(self): question = MultipleChoiceQuestionFactory(label='productReceived') yes_option = OptionFactory(text='Yes', question=question) item = ItemFactory(description='Salt') order_item = PurchaseOrderItemFactory(item=item, value=1000) node_ip_one = DeliveryNodeFactory(tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER, item=order_item) node_end_user = DeliveryNodeFactory(tree_position=DistributionPlanNode.END_USER, item=order_item) run_one = RunFactory(runnable=node_ip_one, status='completed') run_two = RunFactory(runnable=node_end_user, status='completed') answer_one = MultipleChoiceAnswerFactory(run=run_one, question=question, value=yes_option) MultipleChoiceAnswerFactory(run=run_two, question=question, value=yes_option) response = self.client.get(IP_ENDPOINT_URL, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) node_item = response.data[0] self.assertEqual(node_item['item'], 'Salt') self.assertEqual(node_item['programme'], {'id': node_ip_one.distribution_plan.programme.id, 'name': node_ip_one.distribution_plan.programme.name}) self.assertEqual(node_item['ip'], {'id': node_ip_one.ip.id, 'location': unicode(node_ip_one.ip.location)}) self.assertEqual(node_item[question.label], answer_one.format())
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'])
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
def test_returns_correct_balance(self): ip = ConsigneeFactory(type=Consignee.TYPES.implementing_partner) delivery = DeliveryFactory(consignee=ip, track=True) purchase_order = PurchaseOrderFactory() po_item = PurchaseOrderItemFactory(purchase_order=purchase_order) ip_node_one = DeliveryNodeFactory( programme=delivery.programme, consignee=ip, distribution_plan=delivery, item=po_item, quantity=40, acknowledged=40, balance=40, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) ip_node_two = DeliveryNodeFactory( programme=delivery.programme, consignee=ip, distribution_plan=delivery, item=po_item, quantity=30, acknowledged=30, balance=30, tree_position=DistributionPlanNode.IMPLEMENTING_PARTNER) run_one = RunFactory(runnable=ip_node_one) run_two = RunFactory(runnable=ip_node_two) quantity_received_qn = NumericQuestion.objects.get( label='amountReceived') NumericAnswerFactory(question=quantity_received_qn, value=40, run=run_one) NumericAnswerFactory(question=quantity_received_qn, value=30, run=run_two) end_node_one = DeliveryNodeFactory( programme=delivery.programme, consignee=self.end_user, parents=[(ip_node_one, 30), (ip_node_two, 15)], tree_position=DistributionPlanNode.END_USER, item=po_item) end_node_two = DeliveryNodeFactory( programme=delivery.programme, consignee=self.end_user, parents=[(ip_node_one, 5), (ip_node_two, 5)], tree_position=DistributionPlanNode.END_USER, item=po_item) run_end_node_one = RunFactory(runnable=end_node_one) run_end_node_two = RunFactory(runnable=end_node_two) NumericAnswerFactory(question=quantity_received_qn, value=43, run=run_end_node_one) NumericAnswerFactory(question=quantity_received_qn, value=10, run=run_end_node_two) endpoint_url = BACKEND_URL + 'stock-report/%s/' % ip.id response = self.client.get(endpoint_url) expected_data = [{ 'document_number': purchase_order.order_number, 'total_value_received': Decimal('700'), 'total_value_dispensed': Decimal('550'), 'balance': Decimal('150'), 'items': [{ 'code': unicode(po_item.item.material_code), 'description': unicode(po_item.item.description), 'consignee': ip.name, 'location': end_node_one.location, 'quantity_delivered': 30, 'date_delivered': str(end_node_one.delivery_date), 'quantity_confirmed': 30, 'date_confirmed': '', 'quantity_dispatched': 20, 'balance': 10 }, { 'code': unicode(po_item.item.material_code), 'description': unicode(po_item.item.description), 'consignee': ip.name, 'location': end_node_one.location, 'quantity_delivered': 40, 'date_delivered': str(end_node_one.delivery_date), 'quantity_confirmed': 40, 'date_confirmed': '', 'quantity_dispatched': 35, 'balance': 5 }] }] self.assert_api_response(response, expected_data)
def test_should_throw_an_error_when_amount_received_question_is_answered_before_the_item_received_question( self): create_answer = lambda: NumericAnswerFactory( question=self.amount_received, value=100, run=self.run) self.assertRaises(AssertionError, create_answer)