test_unit_polly.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2018, Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License").
6 # You may not use this file except in compliance with the License.
7 # A copy of the License is located at
8 #
9 # http://aws.amazon.com/apache2.0
10 #
11 # or in the "license" file accompanying this file. This file is distributed
12 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 # express or implied. See the License for the specific language governing
14 # permissions and limitations under the License.
15 
16 from __future__ import print_function
17 
18 
19 from mock import patch, MagicMock # python2 uses backport of unittest.mock(docs.python.org/3/library/unittest.mock.html)
20 import unittest
21 
22 
23 class TestPolly(unittest.TestCase):
24 
25  def setUp(self):
26  """important: import tts which is a relay package::
27 
28  devel/lib/python2.7/dist-packages/
29  +-- tts
30  | +-- __init__.py
31  +-- ...
32 
33  per http://docs.ros.org/api/catkin/html/user_guide/setup_dot_py.html:
34 
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.
39  """
40  import tts
41  self.assertIsNotNone(tts)
42 
43  @patch('tts.amazonpolly.Session')
44  def test_init(self, boto3_session_class_mock):
45  from tts.amazonpolly import AmazonPolly
46  AmazonPolly()
47 
48  self.assertGreater(boto3_session_class_mock.call_count, 0)
49  boto3_session_class_mock.return_value.client.assert_called_with('polly')
50 
51  @patch('tts.amazonpolly.Session')
52  def test_defaults(self, boto3_session_class_mock):
53  from tts.amazonpolly import AmazonPolly
54  polly = AmazonPolly()
55 
56  self.assertGreater(boto3_session_class_mock.call_count, 0)
57  boto3_session_class_mock.return_value.client.assert_called_with('polly')
58 
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)
64 
65  @patch('tts.amazonpolly.Session')
66  def test_good_synthesis_with_default_args(self, boto3_session_class_mock):
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'}
74 
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
79  d = {
80  'AudioStream': audio_stream_mock,
81  'ContentType': fake_audio_content_type,
82  'ResponseMetadata': fake_boto3_polly_response_metadata
83  }
84  boto3_polly_response_mock.__contains__.side_effect = d.__contains__
85  boto3_polly_response_mock.__getitem__.side_effect = d.__getitem__
86 
87  from tts.amazonpolly import AmazonPolly
88  polly_under_test = AmazonPolly()
89 
90  self.assertGreater(boto3_session_class_mock.call_count, 0)
91  boto3_session_obj_mock.client.assert_called_with('polly')
92 
93  res = polly_under_test.synthesize(text='hello')
94 
95  expected_synthesize_speech_kwargs = {
96  'LexiconNames': [],
97  'OutputFormat': 'ogg_vorbis',
98  'SampleRate': '22050',
99  'SpeechMarkTypes': [],
100  'Text': 'hello',
101  'TextType': 'text',
102  'VoiceId': 'Joanna',
103  }
104  boto3_polly_obj_mock.synthesize_speech.assert_called_with(**expected_synthesize_speech_kwargs)
105 
106  from tts.srv import PollyResponse
107  self.assertTrue(isinstance(res, PollyResponse))
108 
109  import json
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)
113 
114  self.assertEqual(fake_audio_content_type, j['Audio Type'])
115  self.assertEqual(str(fake_boto3_polly_response_metadata), j['Amazon Polly Response Metadata'])
116 
117  @patch('tts.amazonpolly.Session')
118  def test_polly_raises(self, boto3_session_class_mock):
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'}
126 
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
131  d = {
132  'AudioStream': audio_stream_mock,
133  'ContentType': fake_audio_content_type,
134  'ResponseMetadata': fake_boto3_polly_response_metadata
135  }
136  boto3_polly_response_mock.__contains__.side_effect = d.__contains__
137  boto3_polly_response_mock.__getitem__.side_effect = d.__getitem__
138 
139  from tts.amazonpolly import AmazonPolly
140  polly_under_test = AmazonPolly()
141 
142  self.assertGreater(boto3_session_class_mock.call_count, 0)
143  boto3_session_obj_mock.client.assert_called_with('polly')
144 
145  res = polly_under_test.synthesize(text='hello')
146 
147  expected_synthesize_speech_kwargs = {
148  'LexiconNames': [],
149  'OutputFormat': 'ogg_vorbis',
150  'SampleRate': '22050',
151  'SpeechMarkTypes': [],
152  'Text': 'hello',
153  'TextType': 'text',
154  'VoiceId': 'Joanna',
155  }
156  boto3_polly_obj_mock.synthesize_speech.assert_called_with(**expected_synthesize_speech_kwargs)
157 
158  from tts.srv import PollyResponse
159  self.assertTrue(isinstance(res, PollyResponse))
160 
161  import json
162  j = json.loads(res.result)
163  self.assertTrue('Exception' in j)
164  self.assertTrue('Traceback' in j)
165 
166  @patch('tts.amazonpolly.AmazonPolly')
167  def test_cli(self, amazon_polly_class_mock):
168  import sys
169  with patch.object(sys, 'argv', ['polly_node.py', '-n', 'polly-node']):
170  from tts import amazonpolly
171  amazonpolly.main()
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')
174 
175 
176 if __name__ == '__main__':
177  import rosunit
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)


tts
Author(s): AWS RoboMaker
autogenerated on Fri Mar 5 2021 03:06:38