Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 0 for valuables input #152

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/means_test/forms/savings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from flask import session
from wtforms.validators import InputRequired, NumberRange
from wtforms.validators import InputRequired
from app.means_test.fields import MoneyField
from app.means_test.forms import BaseMeansTestForm
from app.means_test.widgets import MoneyInput
from flask_babel import lazy_gettext as _
from app.means_test.validators import ValidateIfSession
from app.means_test.validators import ValidateIfSession, NumberRangeAllowZero


class SavingsForm(BaseMeansTestForm):
Expand Down Expand Up @@ -44,7 +44,7 @@ class SavingsForm(BaseMeansTestForm):
validators=[
ValidateIfSession("has_valuables", True),
InputRequired(message=_("Enter the total of all valuable items over £500")),
NumberRange(
NumberRangeAllowZero(
min=50000,
message=_("Enter 0 if you have no valuable items worth over £500 each"),
), # This value is in pence
Expand Down
12 changes: 11 additions & 1 deletion app/means_test/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from decimal import Decimal, InvalidOperation
from enum import Enum
from flask import session
from wtforms.validators import StopValidation
from wtforms.validators import StopValidation, NumberRange


class ValidateIfType(Enum):
Expand Down Expand Up @@ -126,3 +126,13 @@ def validate_currency(value: str | None) -> Decimal | None:
raise ValueError("Enter a valid amount (maximum 2 decimal places)")

return decimal_value


class NumberRangeAllowZero(NumberRange):
"""Number range validator that allows 0 as a valid input."""

def __call__(self, form, field):
if field.data == 0:
return

super().__call__(form, field)
58 changes: 58 additions & 0 deletions tests/unit_tests/means_test/test_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from wtforms import Form, IntegerField
from app.means_test.validators import NumberRangeAllowZero


def test_allows_zero():
class TestForm(Form):
amount = IntegerField("Amount", validators=[NumberRangeAllowZero(min=500)])

form = TestForm(amount=0)
assert form.validate()


def test_respects_minimum():
class TestForm(Form):
amount = IntegerField("Amount", validators=[NumberRangeAllowZero(min=500)])

form = TestForm(amount=499)
assert form.validate() is False
assert "Number must be at least 500." in form.amount.errors

form = TestForm(amount=501)
assert form.validate()


def test_respects_maximum():
class TestForm(Form):
amount = IntegerField(
"Amount", validators=[NumberRangeAllowZero(min=500, max=1000)]
)

# Test zero (should pass)
form = TestForm(amount=0)
assert form.validate()

# Test value above maximum
form = TestForm(amount=1001)
assert form.validate() is False
assert "Number must be between 500 and 1000." in form.amount.errors

# Test value at maximum
form = TestForm(amount=1000)
assert form.validate()


def test_non_numeric_value():
class TestForm(Form):
amount = IntegerField("Amount", validators=[NumberRangeAllowZero(min=500)])

form = TestForm(amount="not_a_number")
assert form.validate() is False


def test_none_value():
class TestForm(Form):
amount = IntegerField("Amount", validators=[NumberRangeAllowZero(min=500)])

form = TestForm(amount=None)
assert form.validate() is False
Loading