4 from unittest.mock import MagicMock, patch
6 import google_assistant
7 import unittest_utils # Needed for --unittests_ignore_perf flag
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)
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)
33 if __name__ == '__main__':