mirror of
https://github.com/Infinidat/infi.clickhouse_orm.git
synced 2025-02-15 08:10:32 +03:00
show field name in error when invalid value assigned
This commit is contained in:
parent
a89fd63f4c
commit
50e63fb1f8
|
@ -1,7 +1,8 @@
|
|||
from __future__ import unicode_literals
|
||||
import sys
|
||||
from logging import getLogger
|
||||
|
||||
from six import with_metaclass
|
||||
from six import with_metaclass, reraise
|
||||
import pytz
|
||||
|
||||
from .fields import Field, StringField
|
||||
|
@ -124,8 +125,13 @@ class Model(with_metaclass(ModelBase)):
|
|||
'''
|
||||
field = self.get_field(name)
|
||||
if field:
|
||||
value = field.to_python(value, pytz.utc)
|
||||
field.validate(value)
|
||||
try:
|
||||
value = field.to_python(value, pytz.utc)
|
||||
field.validate(value)
|
||||
except ValueError:
|
||||
tp, v, tb = sys.exc_info()
|
||||
new_msg = "{} (field '{}')".format(v, name)
|
||||
reraise(tp, tp(new_msg), tb)
|
||||
super(Model, self).__setattr__(name, value)
|
||||
|
||||
def set_database(self, db):
|
||||
|
|
|
@ -79,6 +79,27 @@ class ModelTestCase(unittest.TestCase):
|
|||
"datetime_field": datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=pytz.utc)
|
||||
})
|
||||
|
||||
def test_field_name_in_error_message_for_invalid_value_in_constructor(self):
|
||||
bad_value = 1
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
SimpleModel(str_field=bad_value)
|
||||
|
||||
self.assertEqual(
|
||||
"Invalid value for StringField: {} (field 'str_field')".format(repr(bad_value)),
|
||||
text_type(cm.exception)
|
||||
)
|
||||
|
||||
def test_field_name_in_error_message_for_invalid_value_in_assignment(self):
|
||||
instance = SimpleModel()
|
||||
bad_value = 'foo'
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
instance.float_field = bad_value
|
||||
|
||||
self.assertEqual(
|
||||
"Invalid value for Float32Field - {} (field 'float_field')".format(repr(bad_value)),
|
||||
text_type(cm.exception)
|
||||
)
|
||||
|
||||
|
||||
class SimpleModel(Model):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user