summaryrefslogtreecommitdiff
path: root/tests/google_assistant_test.py
blob: 857c1e4d332875dfd294ab397308e4b6e690b941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3

import unittest
from unittest.mock import MagicMock, patch

import google_assistant
import unittest_utils  # Needed for --unittests_ignore_perf flag


class TestGoogleAssistant(unittest.TestCase):
    def test_failure_case(self):
        with patch('requests.post') as mock:
            response = MagicMock()
            response.status_code = 404
            mock.return_value = response
            ret = google_assistant.ask_google('What happens with a 404 response?')
            self.assertFalse(ret.success)
            self.assertTrue('failed; code 404' in ret.response)
            self.assertEqual('', ret.audio_transcription)
            self.assertEqual('', ret.audio_url)

    def test_success_case(self):
        with patch('requests.post') as mock:
            response = MagicMock()
            response.status_code = 200
            json = {'response': 'LGTM', 'audio': '', 'success': True}
            response.json = MagicMock(return_value=json)
            mock.return_value = response
            ret = google_assistant.ask_google('Is this thing working?', recognize_speech=False)
            self.assertTrue(ret.success)


if __name__ == '__main__':
    unittest.main()