Go to the documentation of this file.00001
00002
00003
00004 import os
00005 import logging
00006 import unittest
00007 import ConfigParser
00008
00009 from nose.tools import raises
00010 from rospeex_core.sr.google import Client
00011 from rospeex_core import exceptions as ext
00012
00013
00014
00015 FORMAT = '[%(levelname)s] %(message)s @%(filename)s:%(funcName)s:%(lineno)d'
00016 logging.basicConfig(format=FORMAT, level=logging.DEBUG)
00017 logger = logging.getLogger(__name__)
00018
00019
00020 class TestSpeechRecognitionClient_Google(unittest.TestCase):
00021 def setUp(self):
00022 base_dir = os.path.dirname(__file__)
00023 filename = os.path.join(base_dir, 'config.cfg')
00024 settings = ConfigParser.ConfigParser()
00025 settings.read(filename)
00026 self.flac_file = os.path.join(
00027 base_dir,
00028 settings.get('SpeechRecognition', 'flac_file')
00029 )
00030 self.broken_wav_file = os.path.join(
00031 base_dir,
00032 settings.get('SpeechRecognition', 'broken_wav_file')
00033 )
00034 self.wav_file = os.path.join(
00035 base_dir,
00036 settings.get('SpeechRecognition', 'wav_file')
00037 )
00038 self.access_key = settings.get(
00039 'SpeechRecognition',
00040 'google_access_key'
00041 )
00042
00043 @raises(ext.ParameterException)
00044 def test_request_invalid_audio_format_flac(self):
00045 request_data = open(self.flac_file, 'rb').read()
00046 client = Client()
00047 client._key = self.access_key
00048 client.request(request_data)
00049
00050 @raises(ext.ParameterException)
00051 def test_request_invalid_audio_format_broken_wav(self):
00052 language = 'ja'
00053 request_data = open(self.broken_wav_file, 'rb').read()
00054 client = Client()
00055 client._key = self.access_key
00056 client.request(data=request_data, language=language)
00057
00058 @raises(ext.ParameterException)
00059 def test_request_invalid_language(self):
00060 language = 'hoge'
00061 request_data = open(self.wav_file, 'rb').read()
00062 client = Client()
00063 client._key = self.access_key
00064 client.request(data=request_data, language=language)
00065
00066 @raises(ext.ParameterException)
00067 def test_request_invalid_access_key(self):
00068 language = 'ja'
00069 request_data = open(self.wav_file, 'rb').read()
00070 client = Client()
00071 client._key = None
00072 client.request(data=request_data, language=language)
00073
00074 @raises(ext.RequestTimeoutException)
00075 def test_request_invalid_request_timeout(self):
00076 language = 'ja'
00077 request_data = open(self.wav_file, 'rb').read()
00078 client = Client()
00079 client._key = self.access_key
00080 client.request(data=request_data, language=language, timeout=1)
00081
00082 def test_request_valid_language(self):
00083 for language in Client.LANGUAGES:
00084 request_data = open(self.wav_file, 'rb').read()
00085 client = Client()
00086 client._key = self.access_key
00087 msg = client.request(data=request_data, language=language)
00088 logger.info(msg)
00089
00090
00091 if __name__ == '__main__':
00092 import rosunit
00093 test_class = TestSpeechRecognitionClient_Google
00094 rosunit.unitrun(
00095 'rospeex_core',
00096 'speech_recognition_client_google',
00097 test_class,
00098 None,
00099 coverage_packages=['rospeex_core.sr']
00100 )