Skip to content

Commit

Permalink
Got existing tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzbrand committed Jan 16, 2024
2 parents 9db6171 + 42dac3c commit 9069f6e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 56 deletions.
1 change: 1 addition & 0 deletions home/tests/content2.csv
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Reply with a number to take part in a *free* self-help program created by WHO.
_Stop smoking with the help of a guided, 42-day program._
2. Manage your stress 🧘🏽‍♀️
_Learn how to cope with stress and improve your wellbeing._",,,3e5d77f7-4d34-430d-aad7-d9ca01f79732,self_help,,,English,,[],,,,,[]

4 changes: 2 additions & 2 deletions home/tests/invalid-locale-name.csv
Original file line number Diff line number Diff line change
@@ -1,2 +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,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,659,ma_import-export,,MA_import export,,,,,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,NotEnglish,,,,,,
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,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,659,ma_import-export,,MA_import export,,,,,,,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,NotEnglish,,,,,,
4 changes: 2 additions & 2 deletions home/tests/missing-gotopage.csv
Original file line number Diff line number Diff line change
@@ -1,2 +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,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,659,ma_import-export,,MA_import export,,,Missing,GoToPage,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,English,,"[{""type"":""go_to_page"",""title"":""Missing"",""slug"":""missing""}]",,,,
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,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,659,ma_import-export,,MA_import export,,,Missing,GoToPage,,,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,English,,"[{""type"":""go_to_page"",""title"":""Missing"",""slug"":""missing""}]",,,,
4 changes: 2 additions & 2 deletions home/tests/missing-parent.csv
Original file line number Diff line number Diff line change
@@ -1,2 +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,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,659,ma_import-export,missing-parent,MA_import export,,,,,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,English,,,,,,
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,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,659,ma_import-export,missing-parent,MA_import export,,,,,,,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,English,,,,,,
4 changes: 2 additions & 2 deletions home/tests/missing-related-page.csv
Original file line number Diff line number Diff line change
@@ -1,2 +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,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,659,ma_import-export,,MA_import export,,Test body,,,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,English,,,,,,missing related
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,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,659,ma_import-export,,MA_import export,,Test body,,,,,,,,,,,,,,38a22433-e474-4f5a-b06b-7367d1a7f664,,,,English,,,,,,missing related
9 changes: 9 additions & 0 deletions home/tests/page_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ContentTrigger,
MessengerBlock,
ViberBlock,
SMSBlock,
WhatsappBlock,
SMSBlock,
)
Expand Down Expand Up @@ -118,6 +119,14 @@ def to_dict(self) -> dict[str, Any]:
return super().to_dict() | {"variation_messages": varmsgs, "buttons": buttons}


@dataclass
class SBlk(ContentBlock):
BLOCK_TYPE_STR = "SMS_Message"
BLOCK_TYPE = SMSBlock

# TODO: More body things.


@dataclass
class MBlk(ContentBlock):
BLOCK_TYPE_STR = "messenger_block"
Expand Down
123 changes: 75 additions & 48 deletions home/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
VariationBlock,
)

from .page_builder import MBlk, MBody, PageBuilder, WABlk, WABody
from .page_builder import MBlk, MBody, SBlk, SBody, PageBuilder, WABlk, WABody
from .utils import create_page


Expand Down Expand Up @@ -46,6 +46,7 @@ def create_test_data(self):
MBody(
"main menu first time user", [MBlk("*Welcome to HealthAlert* 🌍")]
),
SBody("main menu first time user", [SBlk("*Welcome to HealthAlert*")]),
],
tags=["menu"],
quick_replies=["Self-help", "Settings", "Health Info"],
Expand All @@ -58,6 +59,7 @@ def create_test_data(self):
bodies=[
WABody("health info", [WABlk("*Health information* 🏥")]),
MBody("health info", [MBlk("*Health information* 🏥")]),
SBody("health info", [SBlk("*Health information* 🏥")]),
],
tags=["health_info"],
)
Expand All @@ -68,6 +70,7 @@ def create_test_data(self):
bodies=[
WABody("self-help", [WABlk("*Self-help programs* 🌬️")]),
MBody("self-help", [MBlk("*Self-help programs* 🌬️")]),
SBody("self-help", [SBlk("*Self-help programs* 🌬️")]),
],
tags=["self_help"],
)
Expand Down Expand Up @@ -110,54 +113,7 @@ def test_tag_filtering(self, uclient):
content = json.loads(response.content)
assert content["count"] == 2

def test_platform_filtering(self, uclient):
"""
If a platform filter is provided, only pages with content for that
platform are returned.
"""
page1 = ContentPage.objects.first()
page2 = ContentPage.objects.last()
# web page
page1.enable_messenger = False
page1.enable_whatsapp = False
page1.enable_viber = False
# This page has web_title, but not web_body. It's unclear what the
# importer should do in that case, so enable web explicitly.
page1.enable_web = True
page1.save_revision().publish()
# whatsapp page
page2.enable_messenger = False
page2.enable_web = False
page2.enable_viber = False
page2.save_revision().publish()
# messenger page
[page3] = ContentPage.objects.exclude(pk__in=[page1, page2])[:1]
page3.enable_web = False
page3.enable_whatsapp = False
page3.enable_viber = False
page3.save_revision().publish()

# it should return only web pages if filtered
response = uclient.get("/api/v2/pages/?web=true")
content = json.loads(response.content)
assert content["count"] == 1
# it should return only whatsapp pages if filtered
response = uclient.get("/api/v2/pages/?whatsapp=true")
content = json.loads(response.content)
assert content["count"] == 1
# it should return only messenger pages if filtered
response = uclient.get("/api/v2/pages/?messenger=true")
content = json.loads(response.content)
assert content["count"] == 1
# it should return only viber pages if filtered
response = uclient.get("/api/v2/pages/?viber=true")
content = json.loads(response.content)
assert content["count"] == 0
# it should return all pages for no filter
response = uclient.get("/api/v2/pages/")
content = json.loads(response.content)
# exclude home pages and index pages
assert content["count"] == 3

def test_whatsapp_draft(self, uclient):
"""
Expand Down Expand Up @@ -644,3 +600,74 @@ def test_orderedcontent_detail_endpoint_tags_flag(self, uclient):
"value": "female",
}
assert content["pages"][0]["tags"] == [t.name for t in self.page1.tags.all()]

@pytest.mark.django_db
class TestContentPageAPI2:
"""
Tests contentpage API without test data fixtures
"""
def test_platform_filtering(self, uclient):
"""
If a platform filter is provided, only pages with content for that
platform are returned.
"""
home_page = HomePage.objects.first()
main_menu = PageBuilder.build_cpi(home_page, "main-menu", "Main Menu")
page1 = PageBuilder.build_cp(
parent=main_menu,
slug="main-menu-first-time-user",
title="main menu first time user",
bodies=[],
web_body=["Colour"],
)
page2 = PageBuilder.build_cp(
parent=main_menu,
slug="health-info",
title="health info",
bodies=[
WABody("health info", [WABlk("*Health information* 🏥")]),
],

)
page3 = PageBuilder.build_cp(
parent=main_menu,
slug="self-help",
title="self-help",
bodies=[
MBody("self-help", [MBlk("*Self-help programs* 🌬️")]),
],
)
page4 = PageBuilder.build_cp(
parent=main_menu,
slug="self-help-sms",
title="self-help-sms",
bodies=[
SBody("self-help-sms", [SBlk("*Self-help programs*SMS")]),
],
)

# it should return only web pages if filtered
response = uclient.get("/api/v2/pages/?web=true")
content = json.loads(response.content)
assert content["count"] == 1
# it should return only whatsapp pages if filtered
response = uclient.get("/api/v2/pages/?whatsapp=true")
content = json.loads(response.content)
assert content["count"] == 1
# it should return only sms pages if filtered
response = uclient.get("/api/v2/pages/?sms=true")
content = json.loads(response.content)
assert content["count"] == 1
# it should return only messenger pages if filtered
response = uclient.get("/api/v2/pages/?messenger=true")
content = json.loads(response.content)
assert content["count"] == 1
# it should return only viber pages if filtered
response = uclient.get("/api/v2/pages/?viber=true")
content = json.loads(response.content)
assert content["count"] == 0
# it should return all pages for no filter
response = uclient.get("/api/v2/pages/")
content = json.loads(response.content)
# exclude home pages and index pages
assert content["count"] == 4

0 comments on commit 9069f6e

Please sign in to comment.