Skip to content
Snippets Groups Projects
Commit 755cac78 authored by Martin Burchell's avatar Martin Burchell
Browse files

Small refactor for date conversion handling

This also fixes error handling for malformed dates
parent b324238b
No related branches found
No related tags found
No related merge requests found
......@@ -175,10 +175,8 @@ class CellConverter(object):
return object_dict
def convert_value(self):
if self.type == 'date':
return self.convert_date()
converters = {
'date': lambda x: self.convert_date(),
'text': lambda x: x,
'integer': lambda x: int(x),
'number': lambda x: Decimal(x)
......@@ -188,10 +186,9 @@ class CellConverter(object):
_(u"Unknown data type '%s' ") % (self.type))
try:
return converters[self.type](self.value)
except:
raise SheetImportException(
_(u"Can not process value '%s' of type '%s' ") %
(self.value, self.type))
except Exception as e:
message = _("%s\nCan not process value '%s' of type '%s' ") % (e.message, self.value, self.type)
raise SheetImportException(message), None, sys.exc_info()[2]
def convert_date(self):
if isinstance(self.value, basestring):
......
......@@ -148,7 +148,7 @@ def test_successful_runs_of_parse_date(importer):
def test_exception_raised_on_faulty_dates(importer):
bad_date = '05x01-2015'
with pytest.raises(ValueError):
with pytest.raises(SheetImportException):
converter = CellConverter(bad_date,
{'type': 'date',
'field': '',
......@@ -218,7 +218,9 @@ def test_convert_value_raises_on_malformed_value(importer):
with pytest.raises(SheetImportException) as excinfo:
converter.convert_value()
assert excinfo.value.message == _(u"Can not process value 'not_integer' of type 'integer' ")
messages = excinfo.value.message.split('\n')
assert _(u"Can not process value 'not_integer' of type 'integer' ") in messages
def test_convert_value_raises_on_date_without_format(importer):
......@@ -230,7 +232,9 @@ def test_convert_value_raises_on_date_without_format(importer):
with pytest.raises(SheetImportException) as excinfo:
converter.convert_value()
assert excinfo.value.message == _(u"Date format not specified for 'created' ")
messages = excinfo.value.message.split('\n')
assert _(u"Date format not specified for 'created' ") in messages
def test_normalize_row_differences(importer):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment