Merge "Use StringType from WSME"

This commit is contained in:
Jenkins 2014-02-17 01:37:24 +00:00 committed by Gerrit Code Review
commit f843cdaaaa
2 changed files with 2 additions and 82 deletions

View File

@ -16,9 +16,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import re
import six
import wsme
from wsme import types as wtypes
@ -69,57 +66,11 @@ macaddress = MacAddressType()
uuid = UuidType()
# TODO(lucasagomes): WSME already has this StringType implementation on trunk,
# so remove it on the next WSME release (> 0.5b6)
class StringType(wtypes.UserType):
"""A simple string type. Can validate a length and a pattern.
:param min_length: Possible minimum length
:param max_length: Possible maximum length
:param pattern: Possible string pattern
Example::
Name = StringType(min_length=1, pattern='^[a-zA-Z ]*$')
"""
basetype = six.string_types
name = "string"
def __init__(self, min_length=None, max_length=None, pattern=None):
self.min_length = min_length
self.max_length = max_length
if isinstance(pattern, six.string_types):
self.pattern = re.compile(pattern)
else:
self.pattern = pattern
def validate(self, value):
if not isinstance(value, self.basetype):
error = 'Value should be string'
raise ValueError(error)
if self.min_length is not None and len(value) < self.min_length:
error = 'Value should have a minimum character requirement of %s' \
% self.min_length
raise ValueError(error)
if self.max_length is not None and len(value) > self.max_length:
error = 'Value should have a maximum character requirement of %s' \
% self.max_length
raise ValueError(error)
if self.pattern is not None and not self.pattern.match(value):
error = 'Value should match the pattern %s' % self.pattern.pattern
raise ValueError(error)
return value
class JsonPatchType(wtypes.Base):
"""A complex type that represents a single json-patch operation."""
path = wtypes.wsattr(StringType(pattern='^(/[\w-]+)+$'), mandatory=True)
path = wtypes.wsattr(wtypes.StringType(pattern='^(/[\w-]+)+$'),
mandatory=True)
op = wtypes.wsattr(wtypes.Enum(str, 'add', 'replace', 'remove'),
mandatory=True)
value = wtypes.text

View File

@ -16,8 +16,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import re
import mock
import six
import webtest
@ -55,35 +53,6 @@ class TestUuidType(base.FunctionalTest):
types.UuidType.validate, 'invalid-uuid')
# TODO(lucasagomes): The tests for the StringType class were ported from
# WSME trunk remove it on the next WSME release (> 0.5b6)
class TestStringType(base.FunctionalTest):
def test_validate_string_type(self):
v = types.StringType(min_length=1, max_length=10,
pattern='^[a-zA-Z0-9]*$')
v.validate('1')
v.validate('12345')
v.validate('1234567890')
self.assertRaises(ValueError, v.validate, '')
self.assertRaises(ValueError, v.validate, '12345678901')
# Test a pattern validation
v.validate('a')
v.validate('A')
self.assertRaises(ValueError, v.validate, '_')
def test_validate_string_type_precompile(self):
precompile = re.compile('^[a-zA-Z0-9]*$')
v = types.StringType(min_length=1, max_length=10,
pattern=precompile)
# Test a pattern validation
v.validate('a')
v.validate('A')
self.assertRaises(ValueError, v.validate, '_')
class MyPatchType(types.JsonPatchType):
"""Helper class for TestJsonPatchType tests."""