Skip to content

Commit

Permalink
Merge pull request #717 from praekeltfoundation/fix-group-membership-…
Browse files Browse the repository at this point in the history
…count-mock

Fix states and simplify tests
  • Loading branch information
erikh360 authored Nov 22, 2023
2 parents f7d0f63 + c3b10b6 commit 6758978
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 81 deletions.
29 changes: 14 additions & 15 deletions yal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,22 @@ async def process_message(self, message):
group_name="Endline Survey Completed"
)
if error:
return await self.go_to_state("state_error")

if group_count >= int(endline_study_max_participant_count):
return await self.go_to_state(
self.state_name = self.ERROR_STATE
elif group_count >= int(endline_study_max_participant_count):
self.state_name = (
EndlineTermsApplication.ENDLINE_LIMIT_REACHED_STATE
)

self.state_name = EndlineTermsApplication.START_STATE

data = {
"endline_survey_started": "True",
}
error = await rapidpro.update_profile(
whatsapp_id, data, self.user.metadata
)
if error:
return await self.go_to_state("state_error")
else:
self.state_name = EndlineTermsApplication.START_STATE

data = {
"endline_survey_started": "True",
}
error = await rapidpro.update_profile(
whatsapp_id, data, self.user.metadata
)
if error:
self.state_name = self.ERROR_STATE

# Fields that RapidPro sets after a feedback push message
elif feedback_state:
Expand Down
89 changes: 23 additions & 66 deletions yal/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ async def test_survey_invite_remind_me_tomorrow(

@pytest.mark.asyncio
@mock.patch("yal.rapidpro.get_group_membership_count")
async def test_state_endline_limit_reached1(
async def test_state_endline_limit_reached(
get_group_membership_count, tester: AppTester, rapidpro_mock
):
get_group_membership_count.return_value = False, 250
Expand All @@ -1603,83 +1603,40 @@ async def test_state_endline_limit_reached1(
await tester.user_input("Yes, I want to answer")
tester.assert_state("state_endline_limit_reached")


@pytest.mark.asyncio
@mock.patch("yal.rapidpro.get_group_membership_count")
async def test_state_endline_limit_reached_menu(
get_group_membership_count, tester: AppTester, rapidpro_mock
):
get_group_membership_count.return_value = False, 250

tester.user.metadata["baseline_survey_completed"] = True
tester.user.metadata["endline_survey_started"] = "Pending"
tester.user.metadata["terms_accepted"] = True
tester.user.metadata["onboarding_completed"] = True

await tester.user_input(
"test",
transport_metadata={
"message": {"button": {"payload": "state_endline_limit_reached"}}
},
tester.assert_message(
"\n".join(
[
"Eish! It looks like you just missed the cut off for our survey. "
"No worries, we get it, life happens!",
"",
"Stay tuned for more survey opportunities. We appreciate your "
"enthusiasm and hope you can catch the next one.",
"",
"Go ahead and browse the menu or ask us a question.",
"",
"1. Go to the menu",
"2. Ask a question",
]
)
)

message = "\n".join(
[
"Eish! It looks like you just missed the cut off for our survey. "
"No worries, we get it, life happens!",
"",
"Stay tuned for more survey opportunities. We appreciate your "
"enthusiasm and hope you can catch the next one.",
"",
"Go ahead and browse the menu or ask us a question.",
"",
"1. Go to the menu",
"2. Ask a question",
]
)
tester.assert_message(message)

await tester.user_input("menu")
@pytest.mark.asyncio
async def test_state_endline_limit_reached_menu(tester: AppTester, rapidpro_mock):
tester.setup_state("state_endline_limit_reached")

await tester.user_input("1")

tester.assert_state("state_mainmenu")


@pytest.mark.asyncio
@mock.patch("yal.askaquestion.config")
@mock.patch("yal.rapidpro.get_group_membership_count")
async def test_state_endline_limit_reached_aaq(
mock_config, get_group_membership_count, tester: AppTester, rapidpro_mock
mock_config, tester: AppTester, rapidpro_mock
):
mock_config.AAQ_URL = "http://aaq-test.com"
get_group_membership_count.return_value = False, 250

tester.user.metadata["baseline_survey_completed"] = True
tester.user.metadata["endline_survey_started"] = "Pending"
tester.user.metadata["terms_accepted"] = True
tester.user.metadata["onboarding_completed"] = True

await tester.user_input(
"test",
transport_metadata={
"message": {"button": {"payload": "state_endline_limit_reached"}}
},
)

message = "\n".join(
[
"Eish! It looks like you just missed the cut off for our survey. "
"No worries, we get it, life happens!",
"",
"Stay tuned for more survey opportunities. We appreciate your "
"enthusiasm and hope you can catch the next one.",
"",
"Go ahead and browse the menu or ask us a question.",
"",
"1. Go to the menu",
"2. Ask a question",
]
)
tester.assert_message(message)
tester.setup_state("state_endline_limit_reached")

await tester.user_input("Ask a question")

Expand Down

0 comments on commit 6758978

Please sign in to comment.