def test_measure_forms_conditions_clears_unneeded_certificate(date_ranges): """Tests that MeasureConditionsForm 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() initial_data = {"measure_start_date": date_ranges.normal} data = { "required_certificate": certificate.pk, "action": action.pk, } form_expects_certificate = forms.MeasureConditionsForm( dict(data, **{"condition_code": code_with_certificate.pk}), prefix="", initial=initial_data, ) form_expects_certificate.is_valid() assert form_expects_certificate.cleaned_data[ "required_certificate"] == certificate form_expects_no_certificate = forms.MeasureConditionsForm( dict(data, **{"condition_code": code_without_certificate.pk}), prefix="", initial=initial_data, ) assert form_expects_no_certificate.is_valid() assert form_expects_no_certificate.cleaned_data[ "required_certificate"] is None
def test_measure_forms_conditions_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() start_date = date_ranges.normal.lower data = { "condition_code": condition_code.pk, "action": action.pk, "applicable_duty": applicable_duty, "start_date_0": start_date.day, "start_date_1": start_date.month, "start_date_2": start_date.year, } initial_data = {"measure_start_date": date_ranges.normal} form = forms.MeasureConditionsForm(data, prefix="", initial=initial_data) 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_form_invalid_data(): """Tests that MeasureConditionsForm raises a validation error when a required field is missing.""" action = factories.MeasureActionFactory.create() data = { "action": action.pk, } form = forms.MeasureConditionsForm(data, prefix="") assert not form.is_valid() assert form.errors["condition_code"][0] == "This field is required."
def test_measure_forms_conditions_form_valid_data(): """Tests that MeasureConditionsForm 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.MeasureConditionsForm(data, prefix="") assert form.is_valid()
def test_measure_forms_conditions_invalid_duty( reference_price, message, date_ranges, duty_sentence_parser, ): """Tests that, given an invalid or compound duty string, MeasureConditionsForm.clean raises a ValidationError with the appropriate error message.""" action = factories.MeasureActionFactory.create() condition_code = factories.MeasureConditionCodeFactory.create() start_date = date_ranges.normal.lower data = { "condition_code": condition_code.pk, "reference_price": reference_price, "action": action.pk, "start_date_0": start_date.day, "start_date_1": start_date.month, "start_date_2": start_date.year, } form = forms.MeasureConditionsForm(data, prefix="") assert not form.is_valid() assert message in form.errors["__all__"]
def test_measure_forms_conditions_valid_duty(date_ranges, duty_sentence_parser): """Tests that, given a valid, non-compound duty (e.g. '11 GBP / 100 kg' as opposed to '11 GBP / 100 kg + 12 %') MeasureConditionsForm.clean() returns cleaned_data updated with values taken from unsaved measure component objects, as generated by the DutySentenceParser.""" action = factories.MeasureActionFactory.create() condition_code = factories.MeasureConditionCodeFactory.create() start_date = date_ranges.normal.lower data = { "condition_code": condition_code.pk, "reference_price": "11 GBP / 100 kg", "action": action.pk, "start_date_0": start_date.day, "start_date_1": start_date.month, "start_date_2": start_date.year, } form = forms.MeasureConditionsForm(data, prefix="") 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")