Skip to content

Commit

Permalink
Catch validation errors during CPI save_revision() as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Jan 29, 2024
1 parent bf03020 commit 76a8f71
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
3 changes: 1 addition & 2 deletions home/import_content_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,11 @@ def create_content_page_index_from_row(self, row: "ContentRow") -> None:
try:
with contextlib.suppress(NodeAlreadySaved):
self.home_page(locale).add_child(instance=index)
index.save_revision().publish()
except ValidationError as err:
# FIXME: Find a better way to represent this.
raise ImportException(f"Validation error: {err}")

index.save_revision().publish()

def create_shadow_content_page_from_row(
self, row: "ContentRow", row_num: int
) -> None:
Expand Down
2 changes: 2 additions & 0 deletions home/tests/bad-cpi-translation-key.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,whatsapp_template_name,whatsapp_template_category,example_values,variation_title,variation_body,sms_title,sms_body,ussd_title,ussd_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,locale,next_prompt,buttons,image_link,doc_link,media_link,related_pages
Menu 1,0,1303,appointment-reminders,,Appointment reminders,,,,,,,,,,,,,,,,,,BADUUID,,,,English,,,,,,
2 changes: 2 additions & 0 deletions home/tests/good-cpi-translation-key.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
structure,message,page_id,slug,parent,web_title,web_subtitle,web_body,whatsapp_title,whatsapp_body,whatsapp_template_name,whatsapp_template_category,example_values,variation_title,variation_body,sms_title,sms_body,ussd_title,ussd_body,messenger_title,messenger_body,viber_title,viber_body,translation_tag,tags,quick_replies,triggers,locale,next_prompt,buttons,image_link,doc_link,media_link,related_pages
Menu 1,0,1303,appointment-reminders,,Appointment reminders,,,,,,,,,,,,,,,,,,8cff04aa-cf08-4120-88b4-e2269b7d5d80,,,,English,,,,,,
35 changes: 35 additions & 0 deletions home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,41 @@ def test_invalid_wa_template_vars_update(self, newcsv_impexp: ImportExport) -> N
== "Validation error: {'whatsapp_body': ['Validation error in StreamBlock']}"
)

def test_cpi_validation_failure(self, newcsv_impexp: ImportExport) -> None:
"""
Importing a ContentPageIndex with an invalid translation key should
raise an error that results in an error message that gets sent back to
the user.
"""
with pytest.raises(ImportException) as e:
newcsv_impexp.import_file("bad-cpi-translation-key.csv")

assert e.value.row_num == 2
# FIXME: Find a better way to represent this.
assert (
e.value.message
== "Validation error: {'translation_key': ['“BADUUID” is not a valid UUID.']}"
)

def test_cpi_validation_failure_update(self, newcsv_impexp: ImportExport) -> None:
"""
Updating a valid ContentPageIndex with an invalid translation key
should raise an error that results in an error message that gets sent
back to the user. The update validation happens in a different code
path from the initial import.
"""
newcsv_impexp.import_file("good-cpi-translation-key.csv")

with pytest.raises(ImportException) as e:
newcsv_impexp.import_file("bad-cpi-translation-key.csv", purge=False)

assert e.value.row_num == 2
# FIXME: Find a better way to represent this.
assert (
e.value.message
== "Validation error: {'translation_key': ['“BADUUID” is not a valid UUID.']}"
)


# "old-xlsx" has at least three bugs, so we don't bother testing it.
@pytest.fixture(params=["old-csv", "new-csv", "new-xlsx"])
Expand Down

0 comments on commit 76a8f71

Please sign in to comment.