Esempio n. 1
0
    def test_only_status_eu_specified_fails(self, values_as_data):
        """
        Test that if only vat_status = eu is specified, it raises a ValidationError
        as vat_verified (true or false) has to be specified as well.

        Test both scenarios:
        - with fields on the instance (values_as_data=False)
        - with fields as values in the data param (values_as_data=True)
        """
        order_fields = {
            'vat_status': VATStatus.EU,
            'vat_number': '',
            'vat_verified': None,
        }

        order = Order(**(order_fields if not values_as_data else {}))
        data = order_fields if values_as_data else {}

        validator = VATSubValidator()

        with pytest.raises(ValidationError) as exc:
            validator(data=data, order=order)
        assert exc.value.detail == {
            'vat_verified': ['This field is required.']
        }
Esempio n. 2
0
    def test_only_status_eu_verified_false_specified_succeeds(
            self, values_as_data):
        """
        Test that if vat_status = eu, vat_verified = False and vat_number is not specified,
        the validation passes and vat_number is not required when vat_verified is False.

        Test both scenarios:
        - with fields on the instance (values_as_data=False)
        - with fields as values in the data param (values_as_data=True)
        """
        order_fields = {
            'vat_status': VATStatus.EU,
            'vat_number': '',
            'vat_verified': False,
        }

        order = Order(**(order_fields if not values_as_data else {}))
        data = order_fields if values_as_data else {}

        validator = VATSubValidator()

        try:
            validator(data=data, order=order)
        except Exception:
            pytest.fail('Should not raise a validator error.')
    def test_only_status_non_eu_succeeds(self, values_as_data, vat_status):
        """
        Test that if vat_status != eu, the validation passes even if the other
        fields are empty.

        Test both scenarios:
        - with fields on the instance (values_as_data=False)
        - with fields as values in the data param (values_as_data=True)
        """
        order_fields = {
            'vat_status': vat_status,
            'vat_number': '',
            'vat_verified': None,
        }

        order = Order(**(order_fields if not values_as_data else {}))
        data = order_fields if values_as_data else {}

        validator = VATValidator()
        validator.set_instance(order)

        try:
            validator(data)
        except Exception:
            pytest.fail('Should not raise a validator error.')
    def test_complete_verified_eu_vat_succeeds(self, values_as_data):
        """
        Test that if vat_status = eu, vat_verified = True and vat_number is specified,
        the validation passes.

        Test both scenarios:
        - with fields on the instance (values_as_data=False)
        - with fields as values in the data param (values_as_data=True)
        """
        order_fields = {
            'vat_status': VATStatus.eu,
            'vat_number': '0123456789',
            'vat_verified': True,
        }

        order = Order(**(order_fields if not values_as_data else {}))
        data = order_fields if values_as_data else {}

        validator = VATValidator()
        validator.set_instance(order)

        try:
            validator(data)
        except Exception:
            pytest.fail('Should not raise a validator error.')
    def test_only_status_eu_verified_true_specified_fails(
            self, values_as_data):
        """
        Test that if vat_status = eu and vat_verified = True but vat_number is not specified,
        it raises a ValidationError.

        Test both scenarios:
        - with fields on the instance (values_as_data=False)
        - with fields as values in the data param (values_as_data=True)
        """
        order_fields = {
            'vat_status': VATStatus.eu,
            'vat_number': '',
            'vat_verified': True,
        }

        order = Order(**(order_fields if not values_as_data else {}))
        data = order_fields if values_as_data else {}

        validator = VATValidator()
        validator.set_instance(order)

        with pytest.raises(ValidationError) as exc:
            validator(data)
        assert exc.value.detail == {'vat_number': ['This field is required.']}
    def test_zero_if_order_incomplete(self):
        """
        Test that if an order doesn't have all the VAT fields, the pricing is zero.
        """
        order = Order(vat_status=None)
        pricing = calculate_order_pricing(order)

        assert pricing == ZERO_PRICING
    def test_incomplete_raises_exception(self):
        """
        Test that if the order doesn't have the right VAT fields populated,
        it raises a ValidationError.
        """
        order = Order(vat_status=None)

        with pytest.raises(ValidationError):
            should_vat_be_applied(order)
    def test_set_instance_via_serializer_context(self):
        """
        Test that seriaizer.set_context gets the order from serializer.context['order'].
        """
        order = Order()
        serializer = mock.Mock(context={'order': order})

        validator = OrderInStatusValidator(allowed_statuses=())
        validator.set_context(serializer)
        assert validator.instance == order
    def test_zero_with_no_estimated_time(self):
        """
        Test that if an order doesn't have any assignees, the pricing is zero.
        """
        order = OrderFactory(assignees=[])
        assert not order.assignees.count()

        order = Order(vat_status=None)
        pricing = calculate_order_pricing(order)

        assert pricing == ZERO_PRICING
    def test_validation(self, order_status, force, should_pass):
        """Test the validator with different order status and force values."""
        order = Order(status=order_status)

        validator = CancellableOrderSubValidator(force=force)

        if should_pass:
            validator(order=order)
        else:
            with pytest.raises(APIConflictException):
                validator(order=order)
    def test_incomplete_order(self, values_as_data):
        """
        Test that an incomplete order doesn't pass the validation.

        Test both scenarios:
        - with fields on the instance (values_as_data=False)
        - with fields as values in the data param (values_as_data=True)
        """
        order_fields = {
            'primary_market': None,
            'description': '',
            'delivery_date': None,
            'vat_status': '',
        }
        order_m2m_fields = {
            'service_types': [],
        }

        if values_as_data:
            order = Order()
            data = {**order_fields, **order_m2m_fields}
        else:
            order = Order(**order_fields)
            for k, v in order_m2m_fields.items():
                getattr(order, k).set(v)
            data = {}

        validator = OrderDetailsFilledInValidator()
        validator.set_instance(order)

        with pytest.raises(ValidationError) as exc:
            validator(data)

        all_fields = list(order_fields) + list(order_m2m_fields)
        assert exc.value.detail == {
            **{field: ['This field is required.']
               for field in all_fields},
            'assignees': ['You need to add at least one assignee.'],
        }
    def test_validation_with_order(self, order_status, mapping, data, should_pass):
        """Test the validator with different order status, mapping and data."""
        order = Order(
            status=order_status,
            description='original description',
        )
        serializer = mock.Mock(instance=order)

        validator = OrderEditableFieldsValidator(mapping)

        if should_pass:
            validator(data, serializer)
        else:
            with pytest.raises(ValidationError):
                validator(data, serializer)
Esempio n. 13
0
 def test_in_pound(self):
     """
     Test converting the pricing of an order to an OrderPricing obj with
     values defined in pound.
     """
     order = Order(
         net_cost=1101,
         subtotal_cost=1001,
         vat_cost=302,
         total_cost=1303,
     )
     pricing = get_pricing_from_order(order, in_pence=False)
     assert pricing.net_cost == 11.01
     assert pricing.subtotal_cost == 10.01
     assert pricing.vat_cost == 3.02
     assert pricing.total_cost == 13.03
Esempio n. 14
0
 def test_cannot_with_incomplete_vat_data(self, fields):
     """
     Test that it returns False if the VAT fields are incomplete.
     """
     order = Order(**fields)
     assert not can_pricing_be_calculated(order)
Esempio n. 15
0
 def test_should(self, fields):
     """Test the cases where the VAT should be applied."""
     order = Order(**fields)
     assert should_vat_be_applied(order)
Esempio n. 16
0
 def test_can(self, fields):
     """Test that it returns True if the VAT fields are filled in."""
     order = Order(**fields)
     assert can_pricing_be_calculated(order)