Пример #1
0
    def test_with_empty_order(self):
        """
        Test that an order without any of the billing fields filled in is populated
        with the company/contact details.
        """
        contact = ContactFactory()
        company = CompanyWithRegAddressFactory()
        order = OrderWithoutBillingDataFactory(
            company=company,
            contact=contact,
        )

        populate_billing_data(order)

        assert not order.billing_contact_name
        assert not order.billing_email
        assert not order.billing_phone

        assert order.billing_company_name == company.name
        assert order.billing_address_1 == company.registered_address_1
        assert order.billing_address_2 == company.registered_address_2
        assert order.billing_address_town == company.registered_address_town
        assert order.billing_address_county == company.registered_address_county
        assert order.billing_address_postcode == company.registered_address_postcode
        assert order.billing_address_country == company.registered_address_country
Пример #2
0
    def test_with_null_values(self):
        """
        Test that if a company has address fields with null values, they are translated into
        empty strings when copied over.

        This is because the Company model contains a bug that allows null values for CharFields.
        """
        company = CompanyFactory(
            registered_address_2=None,
            registered_address_county=None,
            registered_address_postcode=None,
            registered_address_country_id=None,
        )
        order = OrderFactory(
            company=company,
            billing_address_2='',
            billing_address_county='',
            billing_address_postcode='',
            billing_address_country=None,
        )

        populate_billing_data(order)

        assert order.billing_address_2 == ''
        assert order.billing_address_county == ''
        assert order.billing_address_postcode == ''
        assert order.billing_address_country is None
Пример #3
0
    def test_with_already_populated_billing_address(self, billing_address):
        """
        Test that if the order has some billing address fields already populated,
        none of the address fields get overridden.
        """
        company = CompanyFactory.build(
            address_1='1',
            address_2='Hello st.',
            address_town='Muckamore',
            address_county='Antrim',
            address_postcode='BT41 4QE',
            address_country_id=Country.ireland.value.id,
        )
        order = OrderFactory.build(
            company=company,
            **billing_address,
        )

        populate_billing_data(order)

        # check that the fields didn't get overridden
        actual_billing_address = {
            field_name: getattr(order, field_name)
            for field_name in billing_address
        }
        assert actual_billing_address == billing_address
Пример #4
0
    def test_with_empty_order(self, initial_model_values,
                              expected_billing_address_fields):
        """
        Test that an order without any of the billing fields filled in is populated
        with the company/contact details.
        """
        company = CompanyFactory.build(**initial_model_values)
        order = OrderFactory.build(
            billing_company_name='',
            billing_contact_name='',
            billing_email='',
            billing_phone='',
            billing_address_1='',
            billing_address_2='',
            billing_address_town='',
            billing_address_county='',
            billing_address_postcode='',
            billing_address_country=None,
            company=company,
            contact=ContactFactory.build(),
        )

        populate_billing_data(order)

        assert not order.billing_contact_name
        assert not order.billing_email
        assert not order.billing_phone
        assert order.billing_company_name == company.name

        actual_billing_address = {
            field_name: getattr(order, field_name)
            for field_name in expected_billing_address_fields
        }
        assert actual_billing_address == expected_billing_address_fields
Пример #5
0
    def test_with_already_populated_billing_company_name(self):
        """
        Test that if the billing company name for an order is already set,
        it does not get overridden.
        """
        billing_company_name = 'My Corp'

        order = OrderFactory.build(
            contact=ContactFactory.build(),
            billing_company_name=billing_company_name,
        )

        populate_billing_data(order)

        assert order.billing_company_name == billing_company_name
Пример #6
0
    def test_with_already_populated_billing_detail(self):
        """
        Test that if the order has an order details field already populated,
        it does not get overridden.
        """
        billing_company_name = 'My Corp'

        contact = ContactFactory()
        order = OrderFactory(
            contact=contact,
            billing_company_name=billing_company_name,
        )

        populate_billing_data(order)

        assert order.billing_company_name == billing_company_name
Пример #7
0
    def test_with_already_populated_billing_address(self, billing_address):
        """
        Test that if the order has some billing address fields already populated,
        none of the address fields get overridden.
        """
        company = CompanyWithRegAddressFactory()
        order = OrderFactory(
            company=company,
            **billing_address,
        )

        populate_billing_data(order)

        # check that the fields didn't get overridden
        for field in billing_address:
            assert str(getattr(order, field)) == str(billing_address[field])
Пример #8
0
    def generate_quote(self, by, commit=True):
        """
        Generate a new quote and assign it to the current order.
        The status of the order changes to "Quote awaiting acceptance".

        :returns: a quote for this order

        :param by: who made the action
        :param commit: if False, the changes will not be saved. Useful for previewing a quote

        :raises rest_framework.exceptions.ValidationError: in case of validation error
        :raises datahub.omis.core.exceptions.Conflict: in case of errors with the state of the
            current order
        :raises RuntimeError: after trying max_retries times without being able to generate a
            valid value for the quote reference
        """
        for validator in [
                validators.OrderDetailsFilledInValidator(),
                validators.NoOtherActiveQuoteExistsValidator(),
                validators.OrderInStatusValidator(
                    allowed_statuses=(OrderStatus.draft, ), ),
        ]:
            validator.set_instance(self)
            validator()

        self.quote = Quote.objects.create_from_order(order=self,
                                                     by=by,
                                                     commit=commit)
        self.status = OrderStatus.quote_awaiting_acceptance
        populate_billing_data(self)

        if commit:
            self.save()

            # send signal
            quote_generated.send(sender=self.__class__, order=self)

        return self.quote