def test_validation_passes_on_creation(self): """Test that the validation passes if we are creating the order instead of editing it.""" serializer = mock.Mock(instance=None) validator = OrderEditableFieldsValidator( {OrderStatus.paid: {'contact'}}) validator.set_context(serializer) validator({'description': 'lorem ipsum'})
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) validator.set_context(serializer) if should_pass: validator(data) else: with pytest.raises(ValidationError): validator(data)
class Meta: model = Order fields = ( 'id', 'reference', 'status', 'created_on', 'created_by', 'modified_on', 'modified_by', 'company', 'contact', 'primary_market', 'sector', 'uk_region', 'service_types', 'description', 'contacts_not_to_approach', 'contact_email', 'contact_phone', 'product_info', 'further_info', 'existing_agents', 'permission_to_approach_contacts', 'delivery_date', 'po_number', 'discount_value', 'vat_status', 'vat_number', 'vat_verified', 'net_cost', 'subtotal_cost', 'vat_cost', 'total_cost', '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', 'archived_documents_url_path', 'paid_on', 'completed_by', 'completed_on', 'cancelled_by', 'cancelled_on', 'cancellation_reason', ) read_only_fields = ( 'id', 'reference', 'status', 'created_on', 'created_by', 'modified_on', 'modified_by', 'contact_email', 'contact_phone', 'product_info', 'permission_to_approach_contacts', 'discount_value', 'net_cost', 'subtotal_cost', 'vat_cost', 'total_cost', 'archived_documents_url_path', 'paid_on', 'completed_by', 'completed_on', 'cancelled_by', 'cancelled_on', 'cancellation_reason', 'billing_company_name', 'billing_contact_name', 'billing_email', 'billing_phone', ) validators = ( # only some of the fields can be changed depending of the status OrderEditableFieldsValidator( { OrderStatus.draft: { *ORDER_FIELDS_INVOICE_RELATED, 'description', 'service_types', 'sector', 'uk_region', 'contacts_not_to_approach', 'contact', 'existing_agents', 'further_info', 'delivery_date', }, OrderStatus.quote_awaiting_acceptance: { *ORDER_FIELDS_INVOICE_RELATED, 'contact', }, OrderStatus.quote_accepted: { *ORDER_FIELDS_INVOICE_RELATED, 'contact', }, OrderStatus.paid: {'contact'}, OrderStatus.complete: {}, # nothing can be changed OrderStatus.cancelled: {}, # nothing can be changed }, ), # contact has to work at company ContactWorksAtCompanyValidator(), # validate billing address if edited AddressValidator( lazy=True, fields_mapping={ 'billing_address_1': { 'required': True }, 'billing_address_2': { 'required': False }, 'billing_address_town': { 'required': True }, 'billing_address_county': { 'required': False }, 'billing_address_postcode': { 'required': False }, 'billing_address_country': { 'required': True }, }, ), )