def test_basic_full_conversation(self): exercise = ExerciseFactory() QuestionFactory(exercise=exercise, question='question one', answer='one') QuestionFactory(exercise=exercise, question='question two', answer='two') # New user, starting the exercise and getting the first question response = _make_request_and_return_text(user_id='user') self.assertIn('Welcome', response) if 'question one' in response: self.assertNotIn('question two', response) first_question = 1 else: self.assertIn('question two', response) first_question = 2 # Test getting the answer wrong response = _make_request_and_return_text(user_id='user', text='WRONG') self.assertIn('incorrect', response) # Get the answer right, will get the next question answer = 'one' if first_question == 1 else 'two' response = _make_request_and_return_text(user_id='user', text=answer) expected_question = 'question two' if first_question == 1 else 'question one' self.assertIn(expected_question, response) # Get the answer right, asks if we want to try another exercise answer = 'one' if first_question == 2 else 'two' raw_response = index(MockRequest(user_id='user', text=answer)) response_text = GoogleTestUtils.get_text_from_google_response( raw_response) self.assertIn('Would you like to try another exercise', response_text) conversation_token = ( GoogleTestUtils.get_conversation_token_from_google_response( raw_response)) self.assertIsNotNone(conversation_token) # We don't want to try another, goodebye raw_response = index( MockRequest(user_id='user', text='no', conversation_token=conversation_token)) response_text = GoogleTestUtils.get_text_from_google_response( raw_response) self.assertIn('Goodbye', response_text) self.assertTrue(GoogleTestUtils.google_response_is_tell(raw_response))
def _make_request_and_return_text(text=None, user_id=None, conversation_token=None): response = index( MockRequest(text=text, user_id=user_id, conversation_token=conversation_token)) return GoogleTestUtils.get_text_from_google_response(response)
def test_not_another_exercise(self): UserFactory(user_id='user') response = index( MockRequest( text='no', user_id='user', conversation_token=TOKEN_DO_ANOTHER_EXERCISE, )) response_text = GoogleTestUtils.get_text_from_google_response(response) self.assertIn("Goodbye", response_text) self.assertTrue(GoogleTestUtils.google_response_is_tell(response))
def test_do_another_exercise(self): UserFactory(user_id='user') QuestionFactory(question="What's a pea?") response = index( MockRequest( text='yes', user_id='user', conversation_token=TOKEN_DO_ANOTHER_EXERCISE, )) response_text = GoogleTestUtils.get_text_from_google_response(response) self.assertIn("let's go", response_text) self.assertIn(" What's a pea?", response_text) self.assertTrue(GoogleTestUtils.google_response_is_ask(response))
def test_exercise_completion(self): exercise = ExerciseFactory() ExerciseStateFactory( user=UserFactory(user_id='user'), exercise=exercise, current_question=QuestionFactory(exercise=exercise, answer='right'), completed=False, ) response = index(MockRequest(text='right', user_id='user')) response_text = GoogleTestUtils.get_text_from_google_response(response) self.assertIn("finished", response_text) self.assertTrue(ExerciseState.objects.get().completed) self.assertEqual( TOKEN_DO_ANOTHER_EXERCISE, GoogleTestUtils.get_conversation_token_from_google_response( response))
def test_welcome_creates_user(self): QuestionFactory() self.assertEqual(User.objects.count(), 0) index(MockRequest()) self.assertEqual(User.objects.count(), 1)
def test_hello_world_if_not_json(self): result = index(MockRequest(body='NOT JSON')) self.assertIsInstance(result, HttpResponse) self.assertIn("Hello world", str(result.content))
def test_health_check_ends_conversation(self): response = index(MockRequest(health_check=True)) assert GoogleTestUtils.google_response_is_tell(response)
def test_talk_to_ends_conversation(self): response = index(MockRequest(text="talk to ultimate quiz")) assert GoogleTestUtils.google_response_is_tell(response) response_text = GoogleTestUtils.get_text_from_google_response(response) assert 'True Sight' in response_text assert 'Goodbye' in response_text
def test_pause(self): with patch('apps.video_player.views.Messenger') as messenger: result = index(MockRequest(text='play')) self.assertEqual(messenger.play_pause_video.call_count, 1) self.assertEqual("On it!", GoogleUtils.get_text_from_google_response(result))
def test_tell_response_for_blue_planet(self): with patch('apps.video_player.views.Messenger') as messenger: result = index(MockRequest(text='Blue Planet')) self.assertEqual(messenger.open_website.call_count, 1) self.assertIn("Opening Blue Planet", GoogleUtils.get_text_from_google_response(result))
def test_says_i_dont_know_how(self): result = index(MockRequest(text='dance')) self.assertIs(type(result), JsonResponse) self.assertIn("I don't know how to dance", GoogleUtils.get_text_from_google_response(result))
def test_asks_what_would_you_like_to_play(self): result = index(MockRequest(text='')) self.assertIs(type(result), JsonResponse) self.assertEqual("What would you like to play?", GoogleUtils.get_text_from_google_response(result))