tests: add tests on PercentagePerYearFormField (#85549)

This commit is contained in:
Benjamin Dauvergne 2024-01-11 13:25:05 +01:00
parent aa4992942b
commit 5f1e40e2e4
1 changed files with 26 additions and 0 deletions

26
tests/test_fields.py Normal file
View File

@ -0,0 +1,26 @@
from decimal import Decimal
import pytest
from django.core.exceptions import ValidationError
from eo_gestion.eo_facture.fields import PercentagePerYearFormField
def test_percentage_per_year():
field = PercentagePerYearFormField()
assert field.clean('2020:50,2021:50') == [(2020, Decimal('0.5')), (2021, Decimal('0.5'))]
with pytest.raises(ValidationError, match=r'percentage does not sum to 100'):
field.clean('2020:50,2021:60')
with pytest.raises(ValidationError, match=r'percentage does not sum to 100'):
field.clean('2020:50,2021:10')
with pytest.raises(ValidationError, match=r'years are not ordered'):
field.clean('2021:50,2020:50')
with pytest.raises(ValidationError, match=r'years are not unique'):
field.clean('2020:50,2020:50')
with pytest.raises(ValidationError, match=r'years are not consecutive'):
field.clean('2020:50,2022:50')