16 from __future__
import print_function
19 from mock
import patch, MagicMock
26 """important: import tts which is a relay package:: 28 devel/lib/python2.7/dist-packages/ 33 per http://docs.ros.org/api/catkin/html/user_guide/setup_dot_py.html: 35 A relay package is a folder with an __init__.py folder and nothing else. 36 Importing this folder in python will execute the contents of __init__.py, 37 which will in turn import the original python modules in the folder in 38 the sourcespace using the python exec() function. 41 self.assertIsNotNone(tts)
43 @patch(
'tts.amazonpolly.Session')
48 self.assertGreater(boto3_session_class_mock.call_count, 0)
49 boto3_session_class_mock.return_value.client.assert_called_with(
'polly')
51 @patch(
'tts.amazonpolly.Session')
56 self.assertGreater(boto3_session_class_mock.call_count, 0)
57 boto3_session_class_mock.return_value.client.assert_called_with(
'polly')
59 self.assertEqual(
'text', polly.default_text_type)
60 self.assertEqual(
'ogg_vorbis', polly.default_output_format)
61 self.assertEqual(
'Joanna', polly.default_voice_id)
62 self.assertEqual(
'.', polly.default_output_folder)
63 self.assertEqual(
'output', polly.default_output_file_basename)
65 @patch(
'tts.amazonpolly.Session')
67 boto3_session_obj_mock = MagicMock()
68 boto3_polly_obj_mock = MagicMock()
69 boto3_polly_response_mock = MagicMock()
70 audio_stream_mock = MagicMock()
71 fake_audio_stream_data =
'I am audio.' 72 fake_audio_content_type =
'super tts' 73 fake_boto3_polly_response_metadata = {
'foo':
'bar'}
75 boto3_session_class_mock.return_value = boto3_session_obj_mock
76 boto3_session_obj_mock.client.return_value = boto3_polly_obj_mock
77 boto3_polly_obj_mock.synthesize_speech.return_value = boto3_polly_response_mock
78 audio_stream_mock.read.return_value = fake_audio_stream_data
80 'AudioStream': audio_stream_mock,
81 'ContentType': fake_audio_content_type,
82 'ResponseMetadata': fake_boto3_polly_response_metadata
84 boto3_polly_response_mock.__contains__.side_effect = d.__contains__
85 boto3_polly_response_mock.__getitem__.side_effect = d.__getitem__
88 polly_under_test = AmazonPolly()
90 self.assertGreater(boto3_session_class_mock.call_count, 0)
91 boto3_session_obj_mock.client.assert_called_with(
'polly')
93 res = polly_under_test.synthesize(text=
'hello')
95 expected_synthesize_speech_kwargs = {
97 'OutputFormat':
'ogg_vorbis',
98 'SampleRate':
'22050',
99 'SpeechMarkTypes': [],
104 boto3_polly_obj_mock.synthesize_speech.assert_called_with(**expected_synthesize_speech_kwargs)
106 from tts.srv
import PollyResponse
107 self.assertTrue(isinstance(res, PollyResponse))
110 j = json.loads(res.result)
111 observed_audio_file_content = open(j[
'Audio File']).read()
112 self.assertEqual(fake_audio_stream_data, observed_audio_file_content)
114 self.assertEqual(fake_audio_content_type, j[
'Audio Type'])
115 self.assertEqual(str(fake_boto3_polly_response_metadata), j[
'Amazon Polly Response Metadata'])
117 @patch(
'tts.amazonpolly.Session')
119 boto3_session_obj_mock = MagicMock()
120 boto3_polly_obj_mock = MagicMock()
121 boto3_polly_response_mock = MagicMock()
122 audio_stream_mock = MagicMock()
123 fake_audio_stream_data =
'I am audio.' 124 fake_audio_content_type =
'super voice' 125 fake_boto3_polly_response_metadata = {
'foo':
'bar'}
127 boto3_session_class_mock.return_value = boto3_session_obj_mock
128 boto3_session_obj_mock.client.return_value = boto3_polly_obj_mock
129 boto3_polly_obj_mock.synthesize_speech.side_effect = RuntimeError(
'Amazon Polly Exception')
130 audio_stream_mock.read.return_value = fake_audio_stream_data
132 'AudioStream': audio_stream_mock,
133 'ContentType': fake_audio_content_type,
134 'ResponseMetadata': fake_boto3_polly_response_metadata
136 boto3_polly_response_mock.__contains__.side_effect = d.__contains__
137 boto3_polly_response_mock.__getitem__.side_effect = d.__getitem__
140 polly_under_test = AmazonPolly()
142 self.assertGreater(boto3_session_class_mock.call_count, 0)
143 boto3_session_obj_mock.client.assert_called_with(
'polly')
145 res = polly_under_test.synthesize(text=
'hello')
147 expected_synthesize_speech_kwargs = {
149 'OutputFormat':
'ogg_vorbis',
150 'SampleRate':
'22050',
151 'SpeechMarkTypes': [],
156 boto3_polly_obj_mock.synthesize_speech.assert_called_with(**expected_synthesize_speech_kwargs)
158 from tts.srv
import PollyResponse
159 self.assertTrue(isinstance(res, PollyResponse))
162 j = json.loads(res.result)
163 self.assertTrue(
'Exception' in j)
164 self.assertTrue(
'Traceback' in j)
166 @patch(
'tts.amazonpolly.AmazonPolly')
169 with patch.object(sys,
'argv', [
'polly_node.py',
'-n',
'polly-node']):
170 from tts
import amazonpolly
172 self.assertGreater(amazon_polly_class_mock.call_count, 0)
173 amazon_polly_class_mock.return_value.start.assert_called_with(node_name=
'polly-node', service_name=
'polly')
176 if __name__ ==
'__main__':
178 rosunit.unitrun(
'tts',
'unittest-polly', TestPolly)
def test_defaults(self, boto3_session_class_mock)
def test_init(self, boto3_session_class_mock)
def test_good_synthesis_with_default_args(self, boto3_session_class_mock)
def test_cli(self, amazon_polly_class_mock)
def test_polly_raises(self, boto3_session_class_mock)