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

Fix error handling for missing taxonomy

parent c103fc48
No related branches found
No related tags found
2 merge requests!166Staging,!157Django 2.2 and Python 3 upgrade
......@@ -159,12 +159,34 @@ class Importer(object):
return num_saved
def _get_spreadsheet_error_message(self, row, exc_inst):
# TODO: A TransportException during transport.items.add_terms()
# (eg "Term matching query does not exist") will not
# be handled correctly here as 'item' won't exist
# The exception will have different formats:
# Example of failure during transport.items.create():
# {
# 'enumerator': [ErrorDetail(string='Ensure this field has no more than 190 characters.', code='max_length')],
# 'status_code': 400,
# 'item': {
# 'timestamp': datetime.datetime(2018, 8, 9, 12, 14, 26, 766000, tzinfo=tzoffset(None, 21600)),
# 'body': 'the community members want more food.',
# 'gender': 'female',
# 'location': 'Camp 4',
# 'enumerator': '<a very long string>',
# 'external_id': '97f61035-6feb-40a1-9e7e-15c0f65cfdb5'
# }
# }
# Example of failure during transport.items.add_terms():
# {
# 'detail': 'Term matching query does not exist.',
# 'status_code': 400,
# 'terms': {'taxonomy': 'age-ranges', 'name': ''},
# 'item_id': 4
# }
exc_inst.message.pop('status_code', None)
exc_inst.message.pop('item_id', None)
item = exc_inst.message.pop('item', {})
detail = exc_inst.message.pop('detail', None)
terms = exc_inst.message.pop('terms', None)
messages = []
......@@ -182,6 +204,14 @@ class Importer(object):
)
)
if detail and terms:
taxonomy = terms['taxonomy']
name = terms['name']
messages.append(
_(f"Error: {detail}\nTaxonomy: {taxonomy}\nName: {name}\n")
)
return _("There was a problem with row {0} of the spreadsheet:\n{1}").format(
row, '\n'.join(messages)
)
......
......@@ -409,7 +409,7 @@ def test_save_rows_creates_item_with_term(importer):
@pytest.mark.django_db
def test_save_rows_handles_exception(importer):
def test_save_rows_handles_invalid_enumerator(importer):
invalid_enumerator = "Yakub=Aara smart card no point in Kialla hoi lay smart card hoday yan gor Sara Thor Sara ,hetalli bolli aara loi bolla nosir ,zodi aara Thor Sara oi tum aara smart card loi tum .Aara tum Thor asi day yan bishi manshe zani ar bishi goba asi ,Bormar shorkari aarari zeyan hor yan oilday hetarar bolor hota .kinto hetarar aarari forok gorid day ,zodi Burmar shor karotum soyi ensaf takito aarari Thor Sara nohoito"
objects = [
......@@ -448,6 +448,42 @@ def test_save_rows_handles_exception(importer):
)
@pytest.mark.django_db
def test_save_rows_handles_missing_taxonomy(importer):
objects = [
{
'body': 'Text',
'timestamp': datetime.datetime(2014, 7, 21),
'terms': [
{
'name': '18-25',
'taxonomy': 'age-ranges',
}
],
'_row_number': 29,
}
]
importer.profile['columns'] = [
{
'name': 'Age',
'type': 'taxonomy',
'field': 'terms',
'taxonomy': 'age-ranges',
},
]
with pytest.raises(SheetImportException) as excinfo:
importer.save_rows(objects)
assert str(excinfo.value) == (
"There was a problem with row 29 of the spreadsheet:\n"
"Error: Taxonomy matching query does not exist.\n"
"Taxonomy: age-ranges\n"
"Name: 18-25\n"
)
@pytest.mark.django_db
def test_duplicate_records_not_imported(importer):
objects = [
......
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