Permalink
Browse files

Fixes #169: When py_check() returns False, Pony should truncate too l…

…ong values included in ValueError message
  • Loading branch information...
kozlovsky committed Apr 1, 2016
1 parent 02bc2b7 commit 225e3c51e82f790bf4933b199239ee107a25398e
Showing with 26 additions and 5 deletions.
  1. +3 −5 pony/orm/core.py
  2. +19 −0 pony/orm/tests/test_diagram_attribute.py
  3. +4 −0 pony/utils.py
View
@@ -26,7 +26,7 @@
OperationalError, IntegrityError, InternalError, ProgrammingError, NotSupportedError
)
from pony import utils
from pony.utils import localbase, decorator, cut_traceback, throw, reraise, get_lambda_args, \
from pony.utils import localbase, decorator, cut_traceback, throw, reraise, truncate_repr, get_lambda_args, \
deprecated, import_module, parse_expr, is_ident, tostring, strjoin, concat
__all__ = '''
@@ -1867,10 +1867,8 @@ def validate(attr, val, obj=None, entity=None, from_db=False):
if from_db: return converter.sql2py(val)
val = converter.validate(val)
except UnicodeDecodeError as e:
vrepr = repr(val)
if len(vrepr) > 100: vrepr = vrepr[:97] + '...'
throw(ValueError, 'Value for attribute %s cannot be converted to %s: %s'
% (attr, unicode.__name__, vrepr))
% (attr, unicode.__name__, truncate_repr(val)))
else:
rentity = reverse.entity
if not isinstance(val, rentity):
@@ -1887,7 +1885,7 @@ def validate(attr, val, obj=None, entity=None, from_db=False):
if cache is not val._session_cache_:
throw(TransactionError, 'An attempt to mix objects belonging to different transactions')
if attr.py_check is not None and not attr.py_check(val):
throw(ValueError, 'Check for attribute %s failed. Value: %r' % (attr, val))
throw(ValueError, 'Check for attribute %s failed. Value: %s' % (attr, truncate_repr(val)))
return val
def parse_value(attr, row, offsets):
assert len(attr.columns) == len(offsets)
@@ -525,6 +525,25 @@ class Entity2(db.Entity):
a = Set('Entity1', py_check=lambda val: True)
db.generate_mapping(create_tables=True)
@raises_exception(ValueError, "Check for attribute Entity1.a failed. Value: "
"u'12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345...")
def test_py_check_truncate(self):
db = Database('sqlite', ':memory:')
class Entity1(db.Entity):
a = Required(str, py_check=lambda val: False)
db.generate_mapping(create_tables=True)
with db_session:
obj = Entity1(a='1234567890' * 1000)
@raises_exception(ValueError, 'Value for attribute Entity1.a is too long. Max length is 10, value length is 10000')
def test_str_max_len(self):
db = Database('sqlite', ':memory:')
class Entity1(db.Entity):
a = Required(str, 10)
db.generate_mapping(create_tables=True)
with db_session:
obj = Entity1(a='1234567890' * 1000)
def test_foreign_key_sql_type_1(self):
db = Database('sqlite', ':memory:')
class Foo(db.Entity):
View
@@ -128,6 +128,10 @@ def throw(exc_type, *args, **kwargs):
raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback
finally: del exc
def truncate_repr(s, max_len=100):
s = repr(s)
return s if len(s) <= max_len else s[:max_len-3] + '...'
lambda_args_cache = {}
def get_lambda_args(func):

0 comments on commit 225e3c5

Please sign in to comment.