Merge "Handle mandatory attributes"
This commit is contained in:
commit
17adf7d4c6
wsme
@ -12,7 +12,7 @@ from simplegeneric import generic
|
||||
from wsme.types import Unset
|
||||
import wsme.types
|
||||
import wsme.utils
|
||||
from wsme.exc import UnknownArgument
|
||||
from wsme.exc import UnknownArgument, InvalidInput
|
||||
|
||||
|
||||
try:
|
||||
@ -137,6 +137,9 @@ def fromjson(datatype, value):
|
||||
if attrdef.name in value:
|
||||
setattr(obj, attrdef.key,
|
||||
fromjson(attrdef.datatype, value[attrdef.name]))
|
||||
elif attrdef.mandatory:
|
||||
raise InvalidInput(attrdef.name, None,
|
||||
"Mandatory field missing.")
|
||||
return obj
|
||||
elif wsme.types.isusertype(datatype):
|
||||
value = datatype.frombasetype(
|
||||
|
@ -9,7 +9,7 @@ import xml.etree.ElementTree as et
|
||||
from simplegeneric import generic
|
||||
|
||||
import wsme.types
|
||||
from wsme.exc import UnknownArgument
|
||||
from wsme.exc import UnknownArgument, InvalidInput
|
||||
|
||||
import re
|
||||
|
||||
@ -98,10 +98,13 @@ def fromxml(datatype, element):
|
||||
return datatype.frombasetype(fromxml(datatype.basetype, element))
|
||||
if wsme.types.iscomplex(datatype):
|
||||
obj = datatype()
|
||||
for attrdef in datatype._wsme_attributes:
|
||||
for attrdef in wsme.types.list_attributes(datatype):
|
||||
sub = element.find(attrdef.name)
|
||||
if sub is not None:
|
||||
setattr(obj, attrdef.key, fromxml(attrdef.datatype, sub))
|
||||
elif attrdef.mandatory:
|
||||
raise InvalidInput(attrdef.name, None,
|
||||
"Mandatory field missing.")
|
||||
return obj
|
||||
if datatype is wsme.types.bytes:
|
||||
return element.text.encode('ascii')
|
||||
|
@ -220,6 +220,59 @@ class TestController(unittest.TestCase):
|
||||
|
||||
self.assertEquals(res.body, b('"hellohello"'))
|
||||
|
||||
def test_wsattr_mandatory(self):
|
||||
class ComplexType(object):
|
||||
attr = wsme.types.wsattr(int, mandatory=True)
|
||||
|
||||
class MyRoot(WSRoot):
|
||||
@expose(int, body=ComplexType)
|
||||
@validate(ComplexType)
|
||||
def clx(self, a):
|
||||
return a.attr
|
||||
|
||||
r = MyRoot(['restjson'])
|
||||
app = webtest.TestApp(r.wsgiapp())
|
||||
res = app.post_json('/clx', params={}, expect_errors=True,
|
||||
headers={'Accept': 'application/json'})
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_wsproperty_mandatory(self):
|
||||
class ComplexType(object):
|
||||
def foo(self):
|
||||
pass
|
||||
|
||||
attr = wsme.types.wsproperty(int, foo, foo, mandatory=True)
|
||||
|
||||
class MyRoot(WSRoot):
|
||||
@expose(int, body=ComplexType)
|
||||
@validate(ComplexType)
|
||||
def clx(self, a):
|
||||
return a.attr
|
||||
|
||||
r = MyRoot(['restjson'])
|
||||
app = webtest.TestApp(r.wsgiapp())
|
||||
res = app.post_json('/clx', params={}, expect_errors=True,
|
||||
headers={'Accept': 'application/json'})
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_validate_enum_mandatory(self):
|
||||
class Version(object):
|
||||
number = wsme.types.wsattr(wsme.types.Enum(str, 'v1', 'v2'),
|
||||
mandatory=True)
|
||||
|
||||
class MyWS(WSRoot):
|
||||
@expose(str)
|
||||
@validate(Version)
|
||||
def setcplx(self, version):
|
||||
pass
|
||||
|
||||
r = MyWS(['restjson'])
|
||||
app = webtest.TestApp(r.wsgiapp())
|
||||
res = app.post_json('/setcplx', params={'version': {}},
|
||||
expect_errors=True,
|
||||
headers={'Accept': 'application/json'})
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
|
||||
class TestFunctionDefinition(unittest.TestCase):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user