857c1e4d332875dfd294ab397308e4b6e690b941
[python_utils.git] / tests / google_assistant_test.py
1 #!/usr/bin/env python3
2
3 import unittest
4 from unittest.mock import MagicMock, patch
5
6 import google_assistant
7 import unittest_utils  # Needed for --unittests_ignore_perf flag
8
9
10 class TestGoogleAssistant(unittest.TestCase):
11     def test_failure_case(self):
12         with patch('requests.post') as mock:
13             response = MagicMock()
14             response.status_code = 404
15             mock.return_value = response
16             ret = google_assistant.ask_google('What happens with a 404 response?')
17             self.assertFalse(ret.success)
18             self.assertTrue('failed; code 404' in ret.response)
19             self.assertEqual('', ret.audio_transcription)
20             self.assertEqual('', ret.audio_url)
21
22     def test_success_case(self):
23         with patch('requests.post') as mock:
24             response = MagicMock()
25             response.status_code = 200
26             json = {'response': 'LGTM', 'audio': '', 'success': True}
27             response.json = MagicMock(return_value=json)
28             mock.return_value = response
29             ret = google_assistant.ask_google('Is this thing working?', recognize_speech=False)
30             self.assertTrue(ret.success)
31
32
33 if __name__ == '__main__':
34     unittest.main()