def test_save_and_fetch(self): dialog_state = self.repo.fetch_dialog_state(self.phone_number) self.assertEqual(self.phone_number, dialog_state.phone_number) event1 = CompletedPrompt( phone_number=self.phone_number, user_profile=UserProfile(validated=True), prompt=Prompt( slug="one", messages=[ PromptMessage(text="one"), PromptMessage(text="two") ], ), response="hi", drill_instance_id=uuid.uuid4(), ) event2 = AdvancedToNextPrompt( phone_number=self.phone_number, user_profile=UserProfile(validated=True), prompt=Prompt( slug="two", messages=[ PromptMessage(text="three"), PromptMessage(text="four") ], ), drill_instance_id=event1.drill_instance_id, ) dialog_state = DialogState( phone_number=self.phone_number, seq="0", user_profile=UserProfile(validated=True, language="de"), drill_instance_id=event1.drill_instance_id, ) batch = DialogEventBatch(phone_number=self.phone_number, events=[event1, event2], seq="216") self.repo.persist_dialog_state(batch, dialog_state) dialog_state2 = self.repo.fetch_dialog_state(self.phone_number) self.assertEqual(dialog_state.phone_number, dialog_state2.phone_number) self.assertEqual(dialog_state.user_profile.validated, dialog_state2.user_profile.validated) self.assertEqual(dialog_state.user_profile.language, dialog_state2.user_profile.language) batch_retrieved = self.repo.fetch_dialog_event_batch( self.phone_number, batch.batch_id) event1_retrieved = batch_retrieved.events[0] self.assertEqual(event1.response, event1_retrieved.response) # type: ignore event2_retrieved = batch_retrieved.events[1] self.assertEqual(event2.prompt.slug, event2_retrieved.prompt.slug) # type: ignore
def setUp(self) -> None: self.prompt = Prompt( slug="my-prompt", messages=[ PromptMessage(text="one", media_url="http://giphy.com/puppies/1"), PromptMessage(text="two"), ], ) self.drill = Drill(name="01 START", slug="01-start", prompts=[self.prompt])
def test_get_messages(self): dialog_event = CompletedPrompt( phone_number=self.phone, user_profile=self.validated_user_profile, prompt=self.drill.prompts[1], response="a", drill_instance_id=uuid.uuid4(), ) messages = [ PromptMessage(text="Hello Mario"), PromptMessage(text="You work for Tacombi"), ] output = get_messages(dialog_event=dialog_event, messages=messages) self.assertEqual(output[0].body, "Hello Mario") self.assertEqual(output[1].body, "You work for Tacombi")
def test_drill_with_one_prompt(self, *args): choose_language_drill = Drill( name="test-drill", slug="test-drill", prompts=[ Prompt( slug="ignore-response-1", messages=[PromptMessage(text="{{msg1}}")], response_user_profile_key="language", ), ], ) self.dialog_state.user_profile.validated = True self.dialog_state.current_drill = choose_language_drill self._set_current_prompt(0, drill=choose_language_drill) command = ProcessSMSMessage(self.phone_number, "es") batch = self._process_command(command) self._assert_event_types( batch, DialogEventType.COMPLETED_PROMPT, DialogEventType.DRILL_COMPLETED, ) self.assertEqual(batch.events[0].user_profile_updates, {"language": "es"}) self.assertEqual(batch.user_profile.language, "es")
def get_messages_for_event(event: DialogEvent): # noqa: C901 if isinstance(event, AdvancedToNextPrompt): return get_localized_messages(event, event.prompt.messages) elif isinstance(event, FailedPrompt): if not event.abandoned: return get_localized_messages(event, [PromptMessage(text=TRY_AGAIN)]) elif event.prompt.correct_response: return get_localized_messages( event, [PromptMessage(text="{{corrected_answer}}")], correct_answer=localize(event.prompt.correct_response, event.user_profile.language), ) elif isinstance(event, CompletedPrompt): if event.prompt.correct_response is not None: return get_localized_messages( event, [PromptMessage(text=CORRECT_ANSWER_COPY)]) elif isinstance(event, UserValidated): # User validated events will cause the scheduler to kick off a drill pass elif isinstance(event, UserValidationFailed): return get_localized_messages( event, [PromptMessage(text=USER_VALIDATION_FAILED_COPY)]) elif isinstance(event, DrillStarted): return get_localized_messages(event, event.first_prompt.messages) elif (isinstance(event, DrillCompleted) or isinstance(event, OptedOut) or isinstance(event, NextDrillRequested)): # Drills include a drill completed message pass elif isinstance(event, ReminderTriggered): return get_localized_messages(event, [PromptMessage(text=REMINDER)]) else: logging.info(f"Unknown event type: {event.event_type}") return []
def setUp(self): self.phone = "+15554238324" self.validated_user_profile = UserProfileSchema().load({ "validated": True, "language": "en", "name": "Mario", "is_demo": False }) self.non_validated_user_profile = UserProfileSchema().load({ "validated": False, "language": "en", "name": "Luigi", "is_demo": False }) self.drill = Drill( name="Test Drill", slug="test-drill", prompts=[ Prompt(slug="ignore-response-1", messages=[PromptMessage(text="Hello")]), Prompt( slug="graded-response-1", messages=[ PromptMessage(text="Intro!"), PromptMessage(text="Question 1") ], correct_response="a", ), Prompt( slug="graded-response-2", messages=[PromptMessage(text="Question 2")], correct_response="b", ), ], )
def setUp(self): self.phone = "+15554238324" self.validated_user_profile = UserProfile( validated=True, language="en", name="Mario", is_demo=False, account_info=AccountInfo( employer_id=1, employer_name="Tacombi", unit_id=1, unit_name="unit name", ), ) self.non_validated_user_profile = UserProfile(validated=False, language="en", name="Luigi", is_demo=False) self.drill = Drill( name="Test Drill", slug="test-drill", prompts=[ Prompt(slug="ignore-response-1", messages=[PromptMessage(text="Hello")]), Prompt( slug="graded-response-1", messages=[ PromptMessage(text="Intro!"), PromptMessage(text="Question 1"), ], correct_response="a) Philadelphia", ), Prompt( slug="graded-response-2", messages=[PromptMessage(text="Question 2")], correct_response="b", ), ], )
def setUp(self) -> None: logging.disable(logging.CRITICAL) self.drill_instance_id = uuid.UUID("11111111-1111-1111-1111-111111111111") self.phone_number = "123456789" self.dialog_state = DialogState( phone_number=self.phone_number, seq="0", drill_instance_id=self.drill_instance_id, ) self.drill = Drill( name="test-drill", slug="test-drill", prompts=[ Prompt(slug="ignore-response-1", messages=[PromptMessage(text="{{msg1}}")]), Prompt( slug="store-response", messages=[PromptMessage(text="{{msg1}}")], response_user_profile_key="self_rating_1", ), Prompt( slug="graded-response-1", messages=[PromptMessage(text="{{msg1}}")], correct_response="{{response1}}", ), Prompt( slug="graded-response-2", messages=[PromptMessage(text="{{msg1}}")], correct_response="{{response1}}", ), Prompt(slug="ignore-response-2", messages=[PromptMessage(text="{{msg1}}")]), ], ) self.repo = MagicMock() self.repo.fetch_dialog_state = MagicMock(return_value=self.dialog_state) self.repo.persist_dialog_state = MagicMock() self.next_seq = 1 self.now = datetime.now(UTC)
NextDrillRequested, DialogEvent, event_from_dict, ReminderTriggered, ) from stopcovid.dialog.models.state import UserProfile, DialogState, PromptState from stopcovid.dialog.registration import CodeValidationPayload from stopcovid.drills.drills import Prompt, PromptMessage, Drill DRILL = Drill( name="test-drill", slug="test-drill", prompts=[ Prompt(slug="ignore-response-1", messages=[PromptMessage(text="{{msg1}}")]), Prompt( slug="store-response", messages=[PromptMessage(text="{{msg1}}")], response_user_profile_key="self_rating_1", ), Prompt( slug="graded-response-1", messages=[PromptMessage(text="{{msg1}}")], correct_response="{{response1}}", ), Prompt( slug="graded-response-2", messages=[PromptMessage(text="{{msg1}}")], correct_response="{{response1}}", ),
def get_messages_for_event( event: DialogEvent) -> List[OutboundSMS]: # noqa: C901 language = event.user_profile.language if isinstance(event, AdvancedToNextPrompt): return get_messages(event, event.prompt.messages) elif isinstance(event, FailedPrompt): if not event.abandoned: return get_messages( event, [ PromptMessage(text=translate( language, SupportedTranslation.INCORRECT_ANSWER)) ], ) elif event.prompt.correct_response: return get_messages( event, [ PromptMessage(text=translate( language, SupportedTranslation.CORRECTED_ANSWER, correct_answer=event.prompt.correct_response, )) ], ) elif isinstance(event, CompletedPrompt): if event.prompt.correct_response is not None: return get_messages( event, [PromptMessage(text=correct_answer_response(language))], ) elif isinstance(event, UserValidated): # User validated events will cause the scheduler to kick off a drill pass elif isinstance(event, UserValidationFailed): return get_messages(event, [PromptMessage(text=USER_VALIDATION_FAILED_COPY)]) elif isinstance(event, DrillStarted): return get_messages(event, event.first_prompt.messages) elif isinstance(event, AdHocMessageSent): return get_messages(event, [ PromptMessage(text=event.sms.body, media_url=event.sms.media_url) ]) elif isinstance( event, ( DrillCompleted, OptedOut, NextDrillRequested, SchedulingDrillRequested, EnglishLessonDrillRequested, DrillRequested, ), ): pass else: logging.info(f"Unknown event type: {event.event_type}") return []