Skip to content

Commit

Permalink
Add test for RequireValue
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Mar 23, 2024
1 parent cea3e93 commit 0ae3579
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 15 additions & 1 deletion tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
make_app, setup_session_dir, teardown_session_dir)

from tg._compat import PY3, unicode_text, u_, default_im_func
from tg.validation import TGValidationError, _ValidationStatus, Convert
from tg.validation import TGValidationError, _ValidationStatus, Convert, RequireValue
from tg.i18n import lazy_ugettext as l_


Expand Down Expand Up @@ -318,6 +318,13 @@ def chain_validation_2(self, val):
def chain_validation_begin(self, val):
return '>3'

@expose(content_type='text/plain')
@validate({
'val': RequireValue(msg=l_("Value is required"))
}, error_handler=validation_errors_response)
def require_value(self, val=None):
return val


class TestTGController(TestWSGIController):
def setup_method(self):
Expand Down Expand Up @@ -505,6 +512,13 @@ def test_validation_errors_lazy_unicode(self):
resp = self.app.post('/lazy_unicode_error_pow', {'num': 'NOT_A_NUMBER'}, status=412)
assert resp.json['errors']['num'] == u_('àèìòù'), resp.json

def test_requirevalue_validation(self):
resp = self.app.post('/require_value', {"val": "hello"})
assert resp.text == 'hello', resp

resp = self.app.post('/require_value', {}, status=412)
assert resp.json["errors"]["val"] == 'Value is required', resp


class TestChainValidation(TestWSGIController):
def setup_method(self):
Expand Down
3 changes: 2 additions & 1 deletion tg/validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings

from tg.configuration.utils import TGConfigError
from tg._compat import unicode_text

from .i18n import lazy_ugettext
Expand Down Expand Up @@ -89,7 +90,7 @@ def check(self, config, method, params):
validation_function = validation_validators[supported_class]

if validation_function is None:
raise TGConfigError(f"No validation validator function found for: {exception_class}")
raise TGConfigError(f"No validation validator function found for: {schema_class}")

validated_params = validation_function(validators, params)

Expand Down

0 comments on commit 0ae3579

Please sign in to comment.