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 1 commit
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
3 changes: 2 additions & 1 deletion app/means_test/forms/savings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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, AllowedExceptions


class SavingsForm(BaseMeansTestForm):
Expand Down Expand Up @@ -44,6 +44,7 @@ class SavingsForm(BaseMeansTestForm):
validators=[
ValidateIfSession("has_valuables", True),
InputRequired(message=_("Enter the total of all valuable items over £500")),
AllowedExceptions(0),
NumberRange(
min=50000,
message=_("Enter 0 if you have no valuable items worth over £500 each"),
Expand Down
16 changes: 16 additions & 0 deletions app/means_test/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,19 @@ def validate_currency(value: str | None) -> Decimal | None:
raise ValueError("Enter a valid amount (maximum 2 decimal places)")

return decimal_value


class AllowedExceptions:
"""Allows for additional allowed values that are accepted but would fail subsequent validation.
This is typically used for when we want to allow £0 or a value greater than £x.
"""

def __init__(self, allowed_values: list[int | str] | int | str):
# Convert single value to list for consistency
self.allowed_values = (
[allowed_values] if not isinstance(allowed_values, list) else allowed_values
)

def __call__(self, form, field):
if field.data in self.allowed_values:
raise StopValidation()
41 changes: 41 additions & 0 deletions tests/unit_tests/means_test/test_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from wtforms import Form, IntegerField
from wtforms.validators import NumberRange
from app.means_test.validators import AllowedExceptions


def test_single_allowed_value():
class TestForm(Form):
amount = IntegerField(
"Amount", validators=[AllowedExceptions(0), NumberRange(min=501)]
)

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


def test_list_of_allowed_values():
class TestForm(Form):
amount = IntegerField(
"Amount", validators=[AllowedExceptions([0, 1, 2]), NumberRange(min=501)]
)

# Test each allowed value
for value in [0, 1, 2]:
form = TestForm(amount=value)
assert form.validate()


def test_non_allowed_value():
class TestForm(Form):
amount = IntegerField(
"Amount", validators=[AllowedExceptions([0, 1]), NumberRange(min=500)]
)

# Should fail because 3 is neither in allowed values nor >= 500
form = TestForm(amount=3)
assert form.validate() is False
assert "Number must be at least 500." in form.amount.errors

# Should pass because 500 meets the NumberRange requirement
form = TestForm(amount=500)
assert form.validate()
Loading