Beispiel #1
0
 def test_order_with_refund(self, data_flow_api_client, order_factory):
     """Test that endpoint returns refund data if it exists"""
     order = order_factory()
     ApprovedRefundFactory(
         order=order,
         requested_amount=order.total_cost / 5,
     )
     ApprovedRefundFactory(
         order=order,
         requested_amount=order.total_cost / 4,
     )
     ApprovedRefundFactory(
         order=order,
         requested_amount=order.total_cost / 3,
     )
     response = data_flow_api_client.get(self.view_url)
     assert response.status_code == status.HTTP_200_OK
     response_results = response.json()['results']
     assert len(response_results) == 1
     result = response_results[0]
     expected_result = get_expected_data_from_order(order)
     assert result == expected_result
    def test_export(
        self,
        es_with_collector,
        request_sortby,
        orm_ordering,
    ):
        """Test export of interaction search results."""
        factories = (
            OrderCancelledFactory,
            OrderCompleteFactory,
            OrderFactory,
            OrderPaidFactory,
            OrderSubscriberFactory,
            OrderWithAcceptedQuoteFactory,
            OrderWithCancelledQuoteFactory,
            OrderWithOpenQuoteFactory,
            OrderWithoutAssigneesFactory,
            OrderWithoutLeadAssigneeFactory,
            ApprovedRefundFactory,
            RequestedRefundFactory,
        )

        order_with_multiple_refunds = OrderPaidFactory()
        ApprovedRefundFactory(
            order=order_with_multiple_refunds,
            requested_amount=order_with_multiple_refunds.total_cost / 5,
        )
        ApprovedRefundFactory(
            order=order_with_multiple_refunds,
            requested_amount=order_with_multiple_refunds.total_cost / 4,
        )
        ApprovedRefundFactory(
            order=order_with_multiple_refunds,
            requested_amount=order_with_multiple_refunds.total_cost / 3,
        )

        for factory_ in factories:
            factory_()

        es_with_collector.flush_and_refresh()

        data = {}
        if request_sortby:
            data['sortby'] = request_sortby

        url = reverse('api-v3:search:order-export')

        with freeze_time('2018-01-01 11:12:13'):
            response = self.api_client.post(url, data=data)

        assert response.status_code == status.HTTP_200_OK
        assert parse_header(response.get('Content-Type')) == ('text/csv', {
            'charset':
            'utf-8'
        })
        assert parse_header(response.get('Content-Disposition')) == (
            'attachment',
            {
                'filename': 'Data Hub - Orders - 2018-01-01-11-12-13.csv'
            },
        )

        sorted_orders = Order.objects.order_by(orm_ordering, 'pk')
        reader = DictReader(StringIO(response.getvalue().decode('utf-8-sig')))

        assert reader.fieldnames == list(
            SearchOrderExportAPIView.field_titles.values())
        sorted_orders_and_refunds = ((order,
                                      order.refunds.filter(
                                          status=RefundStatus.approved))
                                     for order in sorted_orders)

        expected_row_data = [{
            'Order reference':
            order.reference,
            'Net price':
            Decimal(order.subtotal_cost) / 100,
            'Net refund':
            Decimal(sum(refund.net_amount
                        for refund in refunds), ) / 100 if refunds else None,
            'Status':
            order.get_status_display(),
            'Link':
            order.get_datahub_frontend_url(),
            'Sector':
            order.sector.name,
            'Market':
            order.primary_market.name,
            'UK region':
            order.uk_region.name,
            'Company':
            order.company.name,
            'Company country':
            order.company.address_country.name,
            'Company UK region':
            get_attr_or_none(order, 'company.uk_region.name'),
            'Company link':
            f'{settings.DATAHUB_FRONTEND_URL_PREFIXES["company"]}'
            f'/{order.company.pk}',
            'Contact':
            order.contact.name,
            'Contact job title':
            order.contact.job_title,
            'Contact link':
            f'{settings.DATAHUB_FRONTEND_URL_PREFIXES["contact"]}'
            f'/{order.contact.pk}',
            'Lead adviser':
            get_attr_or_none(order.get_lead_assignee(), 'adviser.name'),
            'Created by team':
            get_attr_or_none(order, 'created_by.dit_team.name'),
            'Date created':
            order.created_on,
            'Delivery date':
            order.delivery_date,
            'Date quote sent':
            get_attr_or_none(order, 'quote.created_on'),
            'Date quote accepted':
            get_attr_or_none(order, 'quote.accepted_on'),
            'Date payment received':
            order.paid_on,
            'Date completed':
            order.completed_on,
        } for order, refunds in sorted_orders_and_refunds]

        assert list(dict(row)
                    for row in reader) == format_csv_data(expected_row_data)