Skip to content

Commit

Permalink
chore: remove now-unused base form class and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Fiedler <[email protected]>
  • Loading branch information
miketheman committed Nov 1, 2024
1 parent 67d4fb6 commit b87a3c5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 154 deletions.
114 changes: 2 additions & 112 deletions tests/unit/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import pytest

from webob.multidict import MultiDict
from wtforms.validators import StopValidation, ValidationError
from wtforms.validators import ValidationError

from warehouse.forms import Form, PasswordStrengthValidator, SetLocaleForm, URIValidator
from warehouse.forms import PasswordStrengthValidator, SetLocaleForm, URIValidator


class TestURIValidator:
Expand Down Expand Up @@ -83,116 +83,6 @@ def test_invalid_password(self, password, expected):
assert str(exc.value) == expected


def _raiser(exc):
raise exc


class TestForm:
def test_empty_form_no_errors(self):
form = Form()
assert form.errors == {}

def test_errors_is_not_cached(self):
# Changed in wtforms==2.3.0 (https://github.com/wtforms/wtforms/pull/568)
form = Form()
assert form.errors == {}
form._form_errors.append("An Error")
assert form.errors == {"__all__": ["An Error"]}

def test_form_level_validation_no_validators(self):
class TestForm(Form):
pass

form = TestForm()

assert form.validate()
assert form.errors == {}

def test_form_level_validation_full_validate(self):
class TestForm(Form):
@pretend.call_recorder
def full_validate(self):
pass

form = TestForm()

assert form.validate()
assert form.errors == {}
assert form.full_validate.calls == [pretend.call(form)]

def test_form_level_validation_full_validate_fails(self):
class TestForm(Form):
@pretend.call_recorder
def full_validate(self):
raise ValueError("A Value Error")

form = TestForm()

assert not form.validate()
assert form.errors == {"__all__": ["A Value Error"]}
assert form.full_validate.calls == [pretend.call(form)]

@pytest.mark.parametrize("validator_funcs", [[], [lambda f: None]])
def test_form_level_validation_meta_works(self, validator_funcs):
validator_funcs = [pretend.call_recorder(v) for v in validator_funcs]

class TestForm(Form):
class Meta:
validators = validator_funcs

form = TestForm()

assert form.validate()
assert form.errors == {}
for v in validator_funcs:
assert v.calls == [pretend.call(form)]

@pytest.mark.parametrize(
("validator_funcs", "errors", "stop_after"),
[
(
[
lambda f: _raiser(ValueError("An Error")),
lambda f: None,
lambda f: _raiser(ValueError("Another Error")),
lambda f: _raiser(StopValidation("Stop!")),
lambda f: _raiser(ValueError("This Won't Show.")),
],
["An Error", "Another Error", "Stop!"],
3,
),
(
[
lambda f: _raiser(ValueError("An Error")),
lambda f: None,
lambda f: _raiser(ValueError("Another Error")),
lambda f: _raiser(StopValidation),
lambda f: _raiser(ValueError("This Won't Show.")),
],
["An Error", "Another Error"],
3,
),
],
)
def test_form_level_validation_meta_fails(
self, validator_funcs, errors, stop_after
):
validator_funcs = [pretend.call_recorder(v) for v in validator_funcs]

class TestForm(Form):
class Meta:
validators = validator_funcs

form = TestForm()

assert not form.validate()
assert form.errors == {"__all__": errors}
for i, v in enumerate(validator_funcs):
assert v.calls == [pretend.call(form)]
if i >= stop_after:
break


class TestSetLocaleForm:
def test_validate(self):
form = SetLocaleForm(MultiDict({"locale_id": "es"}))
Expand Down
43 changes: 1 addition & 42 deletions warehouse/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.

from wtforms import Form as BaseForm, StringField
from wtforms.validators import InputRequired, StopValidation, ValidationError
from wtforms.validators import InputRequired, ValidationError
from zxcvbn import zxcvbn

from warehouse.i18n import KNOWN_LOCALES
Expand Down Expand Up @@ -75,47 +75,6 @@ def __call__(self, form, field):
raise ValidationError(msg)


# TODO: Remove this once `UploadForm` is updated to use `wtforms.Form` directly.
class Form(BaseForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._form_errors = []

def validate(self, *args, **kwargs):
success = super().validate(*args, **kwargs)

# Determine what form level validators we have to run.
form_validators = getattr(self.meta, "validators", [])
full_validate = getattr(self, "full_validate", None)
if full_validate is not None:
form_validators.append(full_validate.__func__)

# Attempt run any form level validators now.
self._form_errors = []
for validator in form_validators:
try:
validator(self)
except StopValidation as exc:
success = False
if exc.args and exc.args[0]:
self._form_errors.append(exc.args[0])
break
except ValueError as exc:
success = False
self._form_errors.append(exc.args[0])

return success

@property
def errors(self):
errors = super().errors

if self._form_errors:
errors["__all__"] = self._form_errors

return errors


class SetLocaleForm(BaseForm):
__params__ = ["locale_id"]

Expand Down

0 comments on commit b87a3c5

Please sign in to comment.