Skip to content

Commit

Permalink
filter column attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Mudiwa Matanda authored and Mudiwa Matanda committed Feb 1, 2024
2 parents 6408da8 + 368a99d commit 70d41b9
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 8 deletions.
6 changes: 2 additions & 4 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 Expand Up @@ -402,12 +401,11 @@ def save(self, parent: Page) -> None:
try:
with contextlib.suppress(NodeAlreadySaved):
parent.add_child(instance=page)
page.save_revision().publish()
except ValidationError as err:
# FIXME: Find a better way to represent this.
raise ImportException(f"Validation error: {err}", self.row_num)

page.save_revision().publish()

def add_web_to_page(self, page: ContentPage) -> None:
page.enable_web = self.enable_web
page.title = self.title
Expand Down
1 change: 0 additions & 1 deletion home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ class WhatsAppTemplateCategory(models.TextChoices):
APIField("title"),
APIField("subtitle"),
APIField("body"),
APIField("whatsapp_template_example_values"),
APIField("tags"),
APIField("triggers"),
APIField("quick_replies"),
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,,,,,,
4 changes: 4 additions & 0 deletions home/tests/bad-whatsapp-template-vars.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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,,,,,,
Sub 1.13,1,1467,mnch_appointment_child_2,Appointment reminders,Appointment child 2,,,mnch_appointment_child_2,"Hi {{1}}
{{2}} {{4}}",mnch_appointment_child_2,MARKETING,Helen,,,,,,,,,,,0c9857a5-e381-49d7-b2fd-123348c4a373,,,,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,,,,,,
4 changes: 4 additions & 0 deletions home/tests/good-whatsapp-template-vars.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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,,,,,,
Sub 1.13,1,1467,mnch_appointment_child_2,Appointment reminders,Appointment child 2,,,mnch_appointment_child_2,"Hi {{1}}
{{2}} {{3}}",mnch_appointment_child_2,MARKETING,Helen,,,,,,,,,,,0c9857a5-e381-49d7-b2fd-123348c4a373,,,,English,,,,,,
2 changes: 0 additions & 2 deletions home/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ def test_detail_view_content(self, uclient):
"title": "self-help",
"subtitle": "",
"body": {"text": []},
"whatsapp_template_example_values": [],
"tags": ["self_help"],
"triggers": [],
"quick_replies": [],
Expand Down Expand Up @@ -313,7 +312,6 @@ def test_detail_view_with_children(self, uclient):
"title": "main menu first time user",
"subtitle": "",
"body": {"text": []},
"whatsapp_template_example_values": [],
"tags": ["menu"],
"triggers": ["Main menu"],
"quick_replies": ["Health Info", "Self-help", "Settings"],
Expand Down
73 changes: 72 additions & 1 deletion home/tests/test_content_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ def test_missing_related_pages(self, newcsv_impexp: ImportExport) -> None:
def test_invalid_wa_template_category(self, newcsv_impexp: ImportExport) -> None:
"""
Importing a WhatsApp template with an invalid category should raise an
error that results in an error message that gets sent back to the user
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-whatsapp-template-category.csv")
Expand All @@ -1019,6 +1019,77 @@ def test_import_required_fields(self, csv_impexp: ImportExport) -> None:
src, dst = csv_impexp.csvs2dicts(csv_bytes, content)
assert dst == src

def test_invalid_wa_template_vars(self, newcsv_impexp: ImportExport) -> None:
"""
Importing a WhatsApp template with invalid variables 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-whatsapp-template-vars.csv")

assert e.value.row_num == 3
# FIXME: Find a better way to represent this.
assert (
e.value.message
== "Validation error: {'whatsapp_body': ['Validation error in StreamBlock']}"
)

def test_invalid_wa_template_vars_update(self, newcsv_impexp: ImportExport) -> None:
"""
Updating a valid WhatsApp template with invalid variables 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-whatsapp-template-vars.csv")

# Update an existing page, which does the validation in
# `page.save_revision()` rather than `parent.add_child()`.
with pytest.raises(ImportException) as e:
newcsv_impexp.import_file("bad-whatsapp-template-vars.csv", purge=False)

assert e.value.row_num == 3
# FIXME: Find a better way to represent this.
assert (
e.value.message
== "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 70d41b9

Please sign in to comment.