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