3 # © Copyright 2021-2022, Scott Gasch
5 """google_assistant unittest."""
8 from unittest.mock import MagicMock, patch
10 import google_assistant
11 import unittest_utils # Needed for --unittests_ignore_perf flag
14 class TestGoogleAssistant(unittest.TestCase):
15 def test_failure_case(self):
16 with patch('requests.post') as mock:
17 response = MagicMock()
18 response.status_code = 404
19 mock.return_value = response
20 ret = google_assistant.ask_google('What happens with a 404 response?')
21 self.assertFalse(ret.success)
22 self.assertTrue('failed; code 404' in ret.response)
23 self.assertEqual('', ret.audio_transcription)
24 self.assertEqual('', ret.audio_url)
26 def test_success_case(self):
27 with patch('requests.post') as mock:
28 response = MagicMock()
29 response.status_code = 200
30 json = {'response': 'LGTM', 'audio': '', 'success': True}
31 response.json = MagicMock(return_value=json)
32 mock.return_value = response
33 ret = google_assistant.ask_google('Is this thing working?', recognize_speech=False)
34 self.assertTrue(ret.success)
37 if __name__ == '__main__':