Convert BoundedInt value from json into int

Wsme doesn't convert automaticaly the value of a UserType into
its basetype. This changeset do it

Change-Id: I7402e144e6a337bbed13789c1611a163bbf9ba61
This commit is contained in:
Mehdi Abaakouk 2013-09-20 10:02:49 +02:00
parent 42f02ab3c4
commit 4a05b599b3
3 changed files with 17 additions and 2 deletions

View File

@ -82,6 +82,10 @@ class BoundedInt(wtypes.UserType):
self.min = min
self.max = max
@staticmethod
def frombasetype(value):
return int(value) if value is not None else None
def validate(self, value):
if self.min is not None and value < self.min:
error = _('Value %(value)s is invalid (should be greater or equal '

View File

@ -285,8 +285,8 @@ class TestAlarms(FunctionalTest,
'comparison_operator': 'le',
'statistic': 'count',
'threshold': 50,
'evaluation_periods': 3,
'period': 180,
'evaluation_periods': '3',
'period': '180',
}
}
self.post_json('/alarms', params=json, status=201,
@ -296,6 +296,9 @@ class TestAlarms(FunctionalTest,
json['threshold_rule']['query'].append({
'field': 'project_id', 'op': 'eq',
'value': self.auth_headers['X-Project-Id']})
# to check to BoundedInt type convertion
json['threshold_rule']['evaluation_periods'] = 3
json['threshold_rule']['period'] = 180
if alarms[0].name == 'added_alarm':
for key in json:
if key.endswith('_rule'):

View File

@ -29,6 +29,14 @@ class TestWsmeCustomType(base.TestCase):
super(TestWsmeCustomType, self).setUp()
pecan.response = mock.MagicMock()
def test_bounded_int_convertion(self):
bi = v2.BoundedInt(1, 5)
self.assertEqual(bi.frombasetype("2"), 2)
def test_bounded_int_invalid_convertion(self):
bi = v2.BoundedInt(1, 5)
self.assertRaises(TypeError, bi.frombasetype, wsme)
def test_bounded_int_maxmin(self):
bi = v2.BoundedInt(1, 5)
self.assertRaises(wsme.exc.ClientSideError, bi.validate, -1)