Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
    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))
Ejemplo n.º 5
0
    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))
Ejemplo n.º 6
0
 def test_welcome_creates_user(self):
     QuestionFactory()
     self.assertEqual(User.objects.count(), 0)
     index(MockRequest())
     self.assertEqual(User.objects.count(), 1)
Ejemplo n.º 7
0
 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))