Skip to content

Commit

Permalink
Merge 9b4fbc3 into aa0405e
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Lu committed Sep 23, 2016
2 parents aa0405e + 9b4fbc3 commit 03032c5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dist
PLANS.md
*.egg
*.egg-info
.eggs
venv
docs/_build
/.tox
Expand Down
9 changes: 9 additions & 0 deletions schematics/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def __eq__(self, other):
else:
return False

def __hash__(self):
if self.info:
return hash((self.summary, self._info_as_str()))
else:
return hash(self.summary)


class BaseError(Exception):
pass
Expand Down Expand Up @@ -91,6 +97,9 @@ def __eq__(self, other):
return other == self.messages
return False

def __hash__(self):
return hash(tuple(self.messages))

def __contains__(self, value):
return value in self.messages

Expand Down
4 changes: 4 additions & 0 deletions schematics/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ def _mock(self, context=None):
return random_string(get_value_in(self.min_length, self.max_length))

def to_native(self, value, context=None):
if value is None:
return None
if isinstance(value, str):
return value
if isinstance(value, self.allow_casts):
Expand All @@ -389,6 +391,8 @@ def to_native(self, value, context=None):
return str(value)
raise ConversionError(self.messages['convert'].format(value))

to_primitive = to_native

def validate_length(self, value, context=None):
length = len(value)
if self.max_length is not None and length > self.max_length:
Expand Down
13 changes: 11 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def test_string_regex():
StringType(regex='\d+').validate("a")


def test_string_to_native():
def test_string_to_native_and_to_primitive():
field = StringType()

with pytest.raises(ConversionError):
Expand All @@ -303,9 +303,16 @@ def test_string_to_native():
field.to_native(b'\xE0\xA0') # invalid UTF-8 sequence
with pytest.raises(ConversionError):
field.to_native(True)
with pytest.raises(ConversionError):
field.to_primitive(3.14)
with pytest.raises(ConversionError):
field.to_primitive(b'\xE0\xA0') # invalid UTF-8 sequence
with pytest.raises(ConversionError):
field.to_primitive(True)

if sys.version_info[0] == 2:
strings = [
(None, None),
(u'abcdefg', u'abcdefg'),
( 'abcdefg', u'abcdefg'),
(u'abc éíçßµ', u'abc éíçßµ'),
Expand All @@ -314,6 +321,7 @@ def test_string_to_native():
]
else:
strings = [
(None, None),
( 'abcdefg', 'abcdefg'),
(b'abcdefg', 'abcdefg'),
( 'abc éíçßµ', 'abc éíçßµ'),
Expand All @@ -324,7 +332,8 @@ def test_string_to_native():
for input, expected in strings:
res = field.to_native(input)
assert res == expected
assert type(res) is str
assert type(res) is str or res is None
field.to_primitive(input) == field.to_native(input)


def test_string_max_length_is_enforced():
Expand Down

0 comments on commit 03032c5

Please sign in to comment.