Since this thing is on the innerwebs I suppose it should have a
[python_utils.git] / tests / google_assistant_test.py
1 #!/usr/bin/env python3
2
3 # © Copyright 2021-2022, Scott Gasch
4
5 """google_assistant unittest."""
6
7 import unittest
8 from unittest.mock import MagicMock, patch
9
10 import google_assistant
11 import unittest_utils  # Needed for --unittests_ignore_perf flag
12
13
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)
25
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)
35
36
37 if __name__ == '__main__':
38     unittest.main()