Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Handle mandatory attributes
Browse files Browse the repository at this point in the history
Change-Id: I68019e8e7d4b0c7a4fae8f97dcc7475c9e519cdb
Fixes-Bug: #1227004
Fixes-Bug: #1227038
  • Loading branch information
jd committed Sep 18, 2013
1 parent 15c0526 commit 2af9917
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
5 changes: 4 additions & 1 deletion wsme/rest/json.py
Expand Up @@ -12,7 +12,7 @@
from wsme.types import Unset
import wsme.types
import wsme.utils
from wsme.exc import UnknownArgument
from wsme.exc import UnknownArgument, InvalidInput


try:
Expand Down Expand Up @@ -137,6 +137,9 @@ def myspecialtype_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(
Expand Down
7 changes: 5 additions & 2 deletions wsme/rest/xml.py
Expand Up @@ -9,7 +9,7 @@
from simplegeneric import generic

import wsme.types
from wsme.exc import UnknownArgument
from wsme.exc import UnknownArgument, InvalidInput

import re

Expand Down Expand Up @@ -98,10 +98,13 @@ def myspecialtype_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')
Expand Down
53 changes: 53 additions & 0 deletions wsme/tests/test_api.py
Expand Up @@ -222,6 +222,59 @@ def multiply(self, a, b):

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):

Expand Down

0 comments on commit 2af9917

Please sign in to comment.