Exemplo n.º 1
0
    def test_without_committing(self):
        """Test that a quote can be generated without saving its changes."""
        order = OrderFactory()
        order.generate_quote(by=AdviserFactory(), commit=False)

        assert order.quote.reference
        assert order.quote.content
        assert order.status == OrderStatus.QUOTE_AWAITING_ACCEPTANCE

        order.refresh_from_db()
        assert not order.quote
        assert not Quote.objects.count()
        assert order.status == OrderStatus.DRAFT
    def test_without_committing(self):
        """Test that a quote can be generated without saving its changes."""
        order = OrderFactory()
        order.generate_quote(by=AdviserFactory(), commit=False)

        assert order.quote.reference
        assert order.quote.content
        assert order.status == OrderStatus.quote_awaiting_acceptance

        order.refresh_from_db()
        assert not order.quote
        assert not Quote.objects.count()
        assert order.status == OrderStatus.draft
    def test_pricing_updated_on_assignee_deleted(self):
        """Test that if an assignee is deleted, the pricing on the order changes."""
        order = OrderFactory(discount_value=0)
        assert order.total_cost > 0
        pre_update_total_cost = order.total_cost

        assignee = order.assignees.first()
        assignee.delete()

        order.refresh_from_db()
        post_update_total_cost = order.total_cost

        assert pre_update_total_cost != post_update_total_cost
    def test_pricing_update_on_assignee_created(self):
        """Test that if a new assignee is added, the pricing on the order changes."""
        order = OrderFactory(discount_value=0)
        assert order.total_cost > 0
        pre_update_total_cost = order.total_cost

        OrderAssigneeFactory(order=order)

        order.refresh_from_db()
        assert order.total_cost > 0
        post_update_total_cost = order.total_cost

        assert pre_update_total_cost != post_update_total_cost
    def test_pricing_updated_on_order_save(self):
        """
        Test that if an order is saved, the related pricing is recalculated and
        the order updated.
        """
        order = OrderFactory(vat_status=VATStatus.uk, discount_value=0)
        assert order.vat_cost > 0

        order.vat_status = VATStatus.outside_eu
        order.save()

        order.refresh_from_db()
        assert order.vat_cost == 0
    def test_pricing_unchanged_if_update_unrelated(self):
        """
        Test that if an unrelated field gets updated, the pricing stays the same.
        """
        order = OrderFactory()
        pre_update_pricing = get_pricing_from_order(order)

        order.description = 'updated description'
        order.save()

        order.refresh_from_db()
        post_update_pricing = get_pricing_from_order(order)

        assert pre_update_pricing == post_update_pricing
Exemplo n.º 7
0
    def test_create(self, order_status):
        """Test a successful call to create a list of payments."""
        order = OrderFactory(status=order_status)

        url = reverse('api-v3:omis:payment:collection',
                      kwargs={'order_pk': order.pk})
        response = self.api_client.post(
            url,
            [
                {
                    'transaction_reference': 'some ref1',
                    'amount': 1,
                    'received_on': '2017-04-20',
                },
                {
                    'transaction_reference': 'some ref2',
                    'amount': order.total_cost - 1,
                    'method': PaymentMethod.manual,
                    'received_on': '2017-04-21',
                },
            ],
        )
        assert response.status_code == status.HTTP_201_CREATED
        response_items = sorted(response.json(),
                                key=itemgetter('transaction_reference'))
        assert response_items == [
            {
                'created_on': '2017-04-25T13:00:00Z',
                'reference': '201704250001',
                'transaction_reference': 'some ref1',
                'additional_reference': '',
                'amount': 1,
                'method': PaymentMethod.bacs,  # bacs is the default one
                'received_on': '2017-04-20',
            },
            {
                'created_on': '2017-04-25T13:00:00Z',
                'reference': '201704250002',
                'transaction_reference': 'some ref2',
                'additional_reference': '',
                'amount': order.total_cost - 1,
                'method': PaymentMethod.manual,
                'received_on': '2017-04-21',
            },
        ]
        order.refresh_from_db()
        assert order.status == OrderStatus.paid
        assert order.paid_on == dateutil_parse('2017-04-21T00:00:00Z')
    def test_ok_if_order_in_allowed_status(self, allowed_status, force):
        """
        Test that the order can be cancelled if it's in one of the allowed statuses.
        """
        reason = CancellationReason.objects.order_by('?').first()
        order = OrderFactory(status=allowed_status)
        adviser = AdviserFactory()

        with freeze_time('2018-07-12 13:00'):
            order.cancel(by=adviser, reason=reason, force=force)

        order.refresh_from_db()
        assert order.status == OrderStatus.cancelled
        assert order.cancelled_on == dateutil_parse('2018-07-12T13:00Z')
        assert order.cancellation_reason == reason
        assert order.cancelled_by == adviser
Exemplo n.º 9
0
    def test_with_commit(self):
        """
        Test that if udpate_order_pricing is called with commit = True,
        the order model is changed and the db as well.
        """
        order = OrderFactory(vat_status=VATStatus.uk)
        orig_total_cost = order.total_cost

        # change order and recalculate pricing without saving
        order.vat_status = VATStatus.outside_eu
        update_order_pricing(order, commit=True)

        assert order.total_cost != orig_total_cost

        # refresh and check that it changed
        order.refresh_from_db()
        assert order.total_cost != orig_total_cost
Exemplo n.º 10
0
    def test_without_committing(self):
        """
        Test that if udpate_order_pricing is called without committing,
        the order model is changed but not the db.
        """
        order = OrderFactory(vat_status=VATStatus.UK)
        orig_total_cost = order.total_cost

        # change order and recalculate pricing without saving
        order.vat_status = VATStatus.OUTSIDE_EU
        update_order_pricing(order, commit=False)

        assert order.total_cost != orig_total_cost

        # refresh and check that it hasn't changed
        order.refresh_from_db()
        assert order.total_cost == orig_total_cost
Exemplo n.º 11
0
    def test_atomicity(self):
        """
        Test that if there's a problem with saving the order, nothing gets saved.
        """
        reason = CancellationReason.objects.order_by('?').first()
        order = OrderFactory(status=OrderStatus.draft)

        with mock.patch.object(order, 'save') as mocked_save:
            mocked_save.side_effect = Exception()

            with pytest.raises(Exception):
                order.cancel(by=None, reason=reason)

            order.refresh_from_db()
            assert order.status == OrderStatus.draft
            assert not order.cancellation_reason
            assert not order.cancelled_on
            assert not order.cancelled_by
    def test_create_as_atomic_operation(self):
        """
        Test that if there's a problem when saving the order, the quote is not saved
        either so that we keep db integrity.
        """
        order = OrderFactory()

        url = reverse('api-v3:omis:quote:detail', kwargs={'order_pk': order.pk})

        with mock.patch.object(Order, 'save') as mocked_save:
            mocked_save.side_effect = Exception()

            with pytest.raises(Exception):
                self.api_client.post(url)

        order.refresh_from_db()
        assert not order.quote
        assert not Quote.objects.count()
    def test_pricing_unchanged_if_update_unrelated(self):
        """
        Test that if an assignee is changed in an unrelated way,
        the pricing on the order doesn't change.
        """
        order = OrderFactory(discount_value=0)
        assert order.total_cost > 0
        pre_update_total_cost = order.total_cost

        assignee = order.assignees.first()
        assignee.is_lead = not assignee.is_lead
        assignee.save()

        order.refresh_from_db()
        assert order.total_cost > 0
        post_update_total_cost = order.total_cost

        assert pre_update_total_cost == post_update_total_cost