Changeset 15

Show
Ignore:
Timestamp:
02/21/07 11:34:53 (2 years ago)
Author:
kevin
Message:

fix for #5 (booleans not forgiving enough)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tgwebservices/controllers.py

    r1 r15  
    108108    return entangle 
    109109 
     110def boolean_converter(value): 
     111    value = value.lower() 
     112    if value in ["", "false", "0", "no", "off"]: 
     113        return False 
     114    if value in ["true", "1", "yes", "on"]: 
     115        return True 
     116    raise validators.Invalid("%s is not a legal boolean value" % (value)) 
     117 
    110118def wsvalidate(*args, **kw): 
    111119    """Validates and converts incoming parameters. Also registers the  
     
    122130        # does. So, the parameters list is offset by one higher. 
    123131        for i in range(0, len(args)): 
    124             input_types[fi.params[i]] = args[i] 
     132            argtype = args[i] 
     133            if argtype is bool: 
     134                argtype = boolean_converter 
     135            input_types[fi.params[i]] = argtype 
    125136         
    126137        input_types.update(kw) 
  • trunk/tgwebservices/tests/test_json.py

    r1 r15  
    22from turbogears import testutil 
    33 
     4from tgwebservices.controllers import WebServicesRoot, wsexpose, wsvalidate 
    45from tgwebservices.tests.fixtures import * 
    56 
     
    3839    print output 
    3940    assert output == """{"faultcode": "Client", "faultstring": "Unexpected parameter in function call (somestrings() got an unexpected keyword argument 'foo')"}""" 
     41 
     42class BooleanInput(WebServicesRoot): 
     43    @wsexpose(bool) 
     44    @wsvalidate(bool) 
     45    def gotbool(self, truefalse): 
     46        return truefalse 
     47 
     48def test_boolean_input(): 
     49    def check(test, expected): 
     50        cherrypy.root = BooleanInput("http://foo.bar.baz/") 
     51        testutil.create_request("/gotbool?tg_format=json&truefalse=%s" % test) 
     52        output = cherrypy.response.body[0] 
     53        print output 
     54        assert output == """{"result": %s}""" % expected 
     55     
     56    tests = [ 
     57        ("False", "false"), 
     58        ("True", "true"), 
     59        ("1", "true"), 
     60        ("0", "false"), 
     61        ("false", "false"), 
     62        ("no", "false") 
     63    ] 
     64    for test in tests: 
     65        yield check, test[0], test[1] 
     66