Skip to content

Commit

Permalink
more powerful pluggable validation and formencode support extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Mar 6, 2024
1 parent dcbe153 commit a02d363
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 268 deletions.
9 changes: 0 additions & 9 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ def test_sanitize_language_code_numeric(self):
def test_sanitize_language_code_numeric_variant(self):
assert i18n.sanitize_language_code('de-CH-1996') == 'de_CH'

def test_formencode_gettext_nulltranslation():
prev_gettext = i18n.ugettext
def nop_gettext(v):
return v

i18n.ugettext = nop_gettext
assert i18n._formencode_gettext('something') == 'something'
i18n.ugettext = prev_gettext


def test_get_unaccessible_translator():
def _fake_find(*args, **kwargs):
Expand Down
17 changes: 6 additions & 11 deletions tests/test_stack/dispatch/controllers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import tg
from tg.controllers import TGController
from tg.decorators import expose, validate, https, variable_decode, with_trailing_slash, \
from tg.decorators import expose, validate, https, with_trailing_slash, \
without_trailing_slash, with_engine
from tg import expose, redirect, config
from tg.controllers import TGController
from tg import dispatched_controller
from tests.test_validation import validators
from tests.test_validation import IntValidator
from tg._compat import unicode_text, u_
from tg.support.converters import asbool

Expand All @@ -29,7 +29,7 @@ def _lookup(self, *args):

class SubController(TGController):
nested = NestedSubController()

@expose()
def foo(self,):
return 'sub_foo'
Expand Down Expand Up @@ -64,7 +64,7 @@ def hitme(self):

class LookupController(TGController):
nested = NestedSubController()

@expose()
def findme(self, *args, **kw):
return 'got to lookup'
Expand Down Expand Up @@ -172,13 +172,13 @@ def flash_no_redirect(self):
return tg.flash.message

@expose('json')
@validate(validators={"some_int": validators.Int()})
@validate(validators={"some_int": IntValidator})
def validated_int(self, some_int):
assert isinstance(some_int, int)
return dict(response=some_int)

@expose('json')
@validate(validators={"a":validators.Int()})
@validate(validators={"a":IntValidator})
def validated_and_unvalidated(self, a, b):
assert isinstance(a, int)
assert isinstance(b, unicode_text)
Expand Down Expand Up @@ -224,11 +224,6 @@ def test_url_sop(self):
def test_https(self, **kw):
return ''

@expose('json')
@variable_decode
def test_vardec(self, **kw):
return kw

@expose('mako:echo.mak')
def echo(self):
return dict()
Expand Down
23 changes: 0 additions & 23 deletions tests/test_stack/dispatch/test_decorated_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,6 @@ def setup_method(self):

self.app = app_from_config(base_config)

def test_variabledecode_fail(self):
resp = self.app.get('/test_vardec', params={'test-1': '1',
'test-2': 2,
'test--repetitions': 'hi'})
assert resp.json['test-1'] == '1', resp.json
assert resp.json['test--repetitions'] == 'hi', resp.json
assert 'test' not in resp.json, resp.json

def test_variabledecode_partial_fail(self):
resp = self.app.get('/test_vardec', params={'test-1': '1',
'test-2': 2,
'test-': 4})
assert resp.json['test-1'] == '1'
assert resp.json['test-'] == '4'
assert len(resp.json['test']) == 2

def test_variable_decode(self):
from formencode.variabledecode import variable_encode
obj = dict(a=['1','2','3'], b=dict(c=[dict(d='1')]))
params = variable_encode(dict(obj=obj), add_repetitions=False)
resp = self.app.get('/test_vardec', params=params)
assert resp.json['obj'] == obj, (resp.json['obj'], obj)

def test_without_trailing_slash(self):
resp = self.app.get('/without_tslash/', status=301)
assert resp.headers['Location'].endswith('/without_tslash')
Expand Down
18 changes: 9 additions & 9 deletions tests/test_tg_controller_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from wsgiref.validate import validator

import pytest
from tests.test_validation import validators
from tests.test_validation import IntValidator, BoolValidator

from webob import Response, Request
from tg._compat import unicode_text, u_
Expand Down Expand Up @@ -289,8 +289,8 @@ def failure(self, *args, **kw):
return "failure"

@expose()
@validate(dict(a=validators.Int(), b=validators.StringBool()),
error_handler=failure)
@validate(dict(a=IntValidator),
error_handler=failure)
def _default(self, a, b=None, **kw):
return "default with args and validators %s %s"%(a, b)

Expand Down Expand Up @@ -436,13 +436,13 @@ def flash_no_redirect(self):
return tg.get_flash()

@expose('json')
@validate(validators=dict(some_int=validators.Int()))
@validate(validators=dict(some_int=IntValidator))
def validated_int(self, some_int):
assert isinstance(some_int, int)
return dict(response=some_int)

@expose('json')
@validate(validators=dict(a=validators.Int()))
@validate(validators=dict(a=IntValidator))
def validated_and_unvalidated(self, a, b):
assert isinstance(a, int)
assert isinstance(b, unicode_text)
Expand All @@ -453,15 +453,15 @@ def error_handler(self, **kw):
return 'validation error handler'

@expose('json')
@validate(validators=dict(a=validators.Int()),
@validate(validators=dict(a=IntValidator),
error_handler=error_handler)
def validated_with_error_handler(self, a, b):
assert isinstance(a, int)
assert isinstance(b, unicode_text)
return dict(int=a,str=b)

@expose('json')
@validate(validators=dict(a=validators.Int()),
@validate(validators=dict(a=IntValidator),
error_handler=error_controller.errors_here)
def validated_with_remote_error_handler(self, a, b):
assert isinstance(a, int)
Expand Down Expand Up @@ -523,7 +523,7 @@ class TestNotFoundController(TestWSGIController):
def setup_method(self):
self.app = make_app(NotFoundController)
TestWSGIController.setup_method(self)

def test_not_found(self):
r = self.app.get('/something', status=404)
assert '404 Not Found' in r, r
Expand Down Expand Up @@ -782,7 +782,7 @@ def test_default_with_validator_pass2(self):
assert "default with args and validators 66 None" in r.body.decode('utf-8'), r

def test_default_with_validator_fail2(self):
r =self.app.get('/sub5/default_with_args/True/more')
r =self.app.get('/sub5/default_with_args/notanint/more')
assert "failure" in r.body.decode('utf-8'), r

def test_custom_content_type_in_controller(self):
Expand Down

0 comments on commit a02d363

Please sign in to comment.