def test_measure_forms_conditions_wizard_clears_unneeded_certificate( date_ranges): """Tests that MeasureConditionsWizardStepForm removes certificates that are not expected by the measure condition code.""" certificate = factories.CertificateFactory.create() code_with_certificate = factories.MeasureConditionCodeFactory( accepts_certificate=True, ) code_without_certificate = factories.MeasureConditionCodeFactory( accepts_certificate=False, ) action = factories.MeasureActionFactory.create() data = { "required_certificate": certificate.pk, "action": action.pk, } form_expects_certificate = forms.MeasureConditionsWizardStepForm( dict(data, **{"condition_code": code_with_certificate.pk}), prefix="", measure_start_date=date_ranges.normal, ) form_expects_certificate.is_valid() assert form_expects_certificate.cleaned_data[ "required_certificate"] == certificate form_expects_no_certificate = forms.MeasureConditionsWizardStepForm( dict(data, **{"condition_code": code_without_certificate.pk}), prefix="", measure_start_date=date_ranges.normal, ) assert form_expects_no_certificate.is_valid() assert form_expects_no_certificate.cleaned_data[ "required_certificate"] is None
def test_measure_forms_conditions_wizard_applicable_duty( applicable_duty, is_valid, date_ranges, duty_sentence_parser, ): """Tests that applicable_duty form field handles simple and complex duty sentence strings, raising an error, if an invalid string is passed.""" action = factories.MeasureActionFactory.create() condition_code = factories.MeasureConditionCodeFactory.create() data = { "condition_code": condition_code.pk, "action": action.pk, "applicable_duty": applicable_duty, } form = forms.MeasureConditionsWizardStepForm( data, prefix="", measure_start_date=date_ranges.normal, ) assert form.is_valid() == is_valid if not is_valid: assert "Enter a valid duty sentence." in form.errors["applicable_duty"]
def test_measure_forms_conditions_wizard_invalid_duty( reference_price, message, date_ranges, duty_sentence_parser, ): """Tests that, given an invalid or compound duty string, MeasureConditionsWizardStepForm.clean raises a ValidationError with the appropriate error message.""" action = factories.MeasureActionFactory.create() condition_code = factories.MeasureConditionCodeFactory.create() data = { "condition_code": condition_code.pk, "reference_price": reference_price, "action": action.pk, } initial_data = {"measure_start_date": date_ranges.normal} form = forms.MeasureConditionsWizardStepForm( data, prefix="", measure_start_date=date_ranges.normal, initial=initial_data, ) assert not form.is_valid() assert message in form.errors["__all__"]
def test_measure_forms_conditions_wizard_form_invalid_data(date_ranges): """Tests that MeasureConditionsWizardStepForm raises a validation error when a required field is missing.""" action = factories.MeasureActionFactory.create() data = { "action": action.pk, } form = forms.MeasureConditionsWizardStepForm( data, prefix="", measure_start_date=date_ranges.normal, ) assert not form.is_valid() assert form.errors["condition_code"][0] == "This field is required."
def test_measure_forms_conditions_wizard_form_valid_data(date_ranges): """Tests that MeasureConditionsWizardStepForm is valid when initialised with minimal required fields.""" condition_code = factories.MeasureConditionCodeFactory.create() action = factories.MeasureActionFactory.create() data = { "condition_code": condition_code.pk, "action": action.pk, } # MeasureConditionsForm.__init__ expects prefix kwarg for instantiating crispy forms `Layout` object form = forms.MeasureConditionsWizardStepForm( data, prefix="", measure_start_date=date_ranges.normal, ) assert form.is_valid()
def test_measure_forms_conditions_wizard_valid_duty(date_ranges, duty_sentence_parser): action = factories.MeasureActionFactory.create() condition_code = factories.MeasureConditionCodeFactory.create() data = { "condition_code": condition_code.pk, "reference_price": "11 GBP / 100 kg", "action": action.pk, } form = forms.MeasureConditionsWizardStepForm( data, prefix="", measure_start_date=date_ranges.normal, ) form.is_valid() assert form.cleaned_data["duty_amount"] == 11 assert form.cleaned_data["monetary_unit"].code == "GBP" assert (form.cleaned_data["condition_measurement"].measurement_unit. abbreviation == "100 kg")