test_unit_synthesizer.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 from mock import patch, MagicMock # python2 uses backport of unittest.mock(docs.python.org/3/library/unittest.mock.html)
19 import unittest
20 
21 
22 class TestSynthesizer(unittest.TestCase):
23 
24  def setUp(self):
25  """important: import tts which is a relay package::
26 
27  devel/lib/python2.7/dist-packages/
28  +-- tts
29  | +-- __init__.py
30  +-- ...
31 
32  per http://docs.ros.org/api/catkin/html/user_guide/setup_dot_py.html:
33 
34  A relay package is a folder with an __init__.py folder and nothing else.
35  Importing this folder in python will execute the contents of __init__.py,
36  which will in turn import the original python modules in the folder in
37  the sourcespace using the python exec() function.
38  """
39  import tts
40  self.assertIsNotNone(tts)
41 
42  def test_init(self):
43  from tts.synthesizer import SpeechSynthesizer
44  speech_synthesizer = SpeechSynthesizer()
45  self.assertEqual('text', speech_synthesizer.default_text_type)
46 
47  @patch('tts.amazonpolly.AmazonPolly')
49  polly_obj_mock = MagicMock()
50  polly_class_mock.return_value = polly_obj_mock
51 
52  test_text = 'hello'
53  test_metadata = '''
54  {
55  "output_path": "/tmp/test"
56  }
57  '''
58  expected_polly_synthesize_args = {
59  'output_format': 'ogg_vorbis',
60  'voice_id': 'Joanna',
61  'sample_rate': '22050',
62  'text_type': 'text',
63  'text': test_text,
64  'output_path': "/tmp/test"
65  }
66 
67  from tts.synthesizer import SpeechSynthesizer
68  from tts.srv import SynthesizerRequest
69  speech_synthesizer = SpeechSynthesizer(engine='POLLY_LIBRARY')
70  request = SynthesizerRequest(text=test_text, metadata=test_metadata)
71  response = speech_synthesizer._node_request_handler(request)
72 
73  self.assertGreater(polly_class_mock.call_count, 0)
74  polly_obj_mock.synthesize.assert_called_with(**expected_polly_synthesize_args)
75 
76  self.assertEqual(response.result, polly_obj_mock.synthesize.return_value.result)
77 
78  @patch('tts.amazonpolly.AmazonPolly')
80  polly_obj_mock = MagicMock()
81  polly_class_mock.return_value = polly_obj_mock
82 
83  test_text = 'hello'
84  test_metadata = '''I am no JSON'''
85 
86  from tts.synthesizer import SpeechSynthesizer
87  from tts.srv import SynthesizerRequest
88  speech_synthesizer = SpeechSynthesizer(engine='POLLY_LIBRARY')
89  request = SynthesizerRequest(text=test_text, metadata=test_metadata)
90  response = speech_synthesizer._node_request_handler(request)
91 
92  self.assertTrue(response.result.startswith('Exception: '))
93 
94  @patch('tts.amazonpolly.AmazonPolly')
95  def test_bad_engine(self, polly_class_mock):
96  polly_obj_mock = MagicMock()
97  polly_class_mock.return_value = polly_obj_mock
98 
99  ex = None
100 
101  from tts.synthesizer import SpeechSynthesizer
102  try:
103  SpeechSynthesizer(engine='NON-EXIST ENGINE')
104  except Exception as e:
105  ex = e
106 
107  self.assertTrue(isinstance(ex, SpeechSynthesizer.BadEngineError))
108 
110  import os
111  source_file_dir = os.path.dirname(os.path.abspath(__file__))
112  synthersizer_path = os.path.join(source_file_dir, '..', 'scripts', 'synthesizer_node.py')
113  import subprocess
114  o = subprocess.check_output(['python', synthersizer_path, '-h'])
115  self.assertTrue(str(o).startswith('Usage: '))
116 
117  @patch('tts.synthesizer.SpeechSynthesizer')
118  def test_cli_engine_dispatching_1(self, speech_synthesizer_class_mock):
119  import sys
120  with patch.object(sys, 'argv', ['synthesizer_node.py']):
121  import tts.synthesizer
123  speech_synthesizer_class_mock.assert_called_with(engine='POLLY_SERVICE', polly_service_name='polly')
124  speech_synthesizer_class_mock.return_value.start.assert_called_with(node_name='synthesizer_node',
125  service_name='synthesizer')
126 
127  @patch('tts.synthesizer.SpeechSynthesizer')
128  def test_cli_engine_dispatching_2(self, speech_synthesizer_class_mock):
129  import sys
130  with patch.object(sys, 'argv', ['synthesizer_node.py', '-e', 'POLLY_LIBRARY']):
131  from tts import synthesizer
132  synthesizer.main()
133  speech_synthesizer_class_mock.assert_called_with(engine='POLLY_LIBRARY')
134  self.assertGreater(speech_synthesizer_class_mock.return_value.start.call_count, 0)
135 
136  @patch('tts.synthesizer.SpeechSynthesizer')
137  def test_cli_engine_dispatching_3(self, speech_synthesizer_class_mock):
138  import sys
139  with patch.object(sys, 'argv', ['synthesizer_node.py', '-p', 'apolly']):
140  from tts import synthesizer
141  synthesizer.main()
142  speech_synthesizer_class_mock.assert_called_with(engine='POLLY_SERVICE', polly_service_name='apolly')
143  self.assertGreater(speech_synthesizer_class_mock.return_value.start.call_count, 0)
144 
145 
146 if __name__ == '__main__':
147  import rosunit
148  rosunit.unitrun('tts', 'unittest-synthesizer', TestSynthesizer)
def test_cli_engine_dispatching_1(self, speech_synthesizer_class_mock)
def test_good_synthesis_with_mostly_default_args_using_polly_lib(self, polly_class_mock)
def test_cli_engine_dispatching_3(self, speech_synthesizer_class_mock)
def test_bad_engine(self, polly_class_mock)
def test_synthesis_with_bad_metadata_using_polly_lib(self, polly_class_mock)
def test_cli_engine_dispatching_2(self, speech_synthesizer_class_mock)


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