test_validators.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # import python libraries
00005 import os
00006 import logging
00007 import unittest
00008 import ConfigParser
00009 
00010 # import locasl libraries
00011 import rospeex_core.validators as validators
00012 from rospeex_core import exceptions as ext
00013 
00014 # setup logging
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 TestValidators(unittest.TestCase):
00021     """ rospeex.validators unittest class """
00022 
00023     AUDIO_LENGTH = 8000
00024     SUPPORTED_LANGUAGES = ['ja', 'en', 'zhe', '*']
00025     FRAMERATE = 16000
00026     CHANNELS = 1
00027     SAMPWIDTH = 2
00028 
00029     def setUp(self):
00030         """ setup class """
00031         base_dir = os.path.dirname(__file__)
00032         settings = ConfigParser.ConfigParser()
00033         filename = os.path.join(base_dir, 'config.cfg')
00034         settings.read(filename)
00035         self.flac_file = os.path.join(
00036             base_dir,
00037             settings.get('Validators', 'flac_file')
00038         )
00039         self.wav_file = os.path.join(
00040             base_dir,
00041             settings.get('Validators', 'wav_file')
00042         )
00043         self.big_wav_file = os.path.join(
00044             base_dir,
00045             settings.get('Validators', 'big_wav_file')
00046         )
00047         self.broken_wav_file = os.path.join(
00048             base_dir,
00049             settings.get('Validators', 'broken_wav_file')
00050         )
00051 
00052     def test_supported_languages(self):
00053         failed = False
00054         err_msg = ''
00055 
00056         # test case 01
00057         try:
00058             validators.check_language('hoge', self.SUPPORTED_LANGUAGES)
00059             failed = True
00060 
00061         except ext.UnsupportedLanguageException as err:
00062             err_msg = str(err)
00063             logger.info(err_msg)
00064 
00065         self.failIf(failed, 'fail to validate')
00066 
00067         # test case 02
00068         try:
00069             validators.check_language('ja', self.SUPPORTED_LANGUAGES)
00070 
00071         except ext.UnsupportedLanguageException as err:
00072             err_msg = str(err)
00073             failed = True
00074             logger.info(err_msg)
00075 
00076         self.failIf(failed, 'fail to validate. Exception:%s' % err_msg)
00077 
00078         # test case 03
00079         try:
00080             validators.check_language('*', self.SUPPORTED_LANGUAGES)
00081 
00082         except ext.UnsupportedLanguageException as err:
00083             err_msg = str(err)
00084             failed = True
00085             logger.info(err_msg)
00086 
00087         self.failIf(failed, 'fail to validate. Exception:%s' % err_msg)
00088 
00089     def test_wave_file(self):
00090         failed = False
00091         err_msg = ''
00092 
00093         # broken wave
00094         try:
00095             data = open(self.broken_wav_file, 'rb').read()
00096             validators.check_wave_data(
00097                 data,
00098                 self.FRAMERATE,
00099                 self.CHANNELS,
00100                 self.SAMPWIDTH,
00101                 self.AUDIO_LENGTH
00102             )
00103             failed = True
00104 
00105         except ext.InvalidAudioDataException as err:
00106             err_msg = str(err)
00107             logger.info(err_msg)
00108 
00109         self.failIf(failed, 'broken wave data')
00110 
00111         # non wave file
00112         try:
00113             data = open(self.flac_file, 'rb').read()
00114             validators.check_wave_data(
00115                 data,
00116                 self.FRAMERATE,
00117                 self.CHANNELS,
00118                 self.SAMPWIDTH,
00119                 self.AUDIO_LENGTH
00120             )
00121             failed = True
00122 
00123         except ext.InvalidAudioDataException as err:
00124             err_msg = str(err)
00125             logger.info(err_msg)
00126 
00127         self.failIf(failed, 'non wave data')
00128 
00129         # invalid format (nchannel)
00130         try:
00131             data = open(self.wav_file, 'rb').read()
00132             validators.check_wave_data(
00133                 data,
00134                 self.FRAMERATE,
00135                 2,
00136                 self.SAMPWIDTH,
00137                 self.AUDIO_LENGTH
00138             )
00139             failed = True
00140 
00141         except ext.InvalidAudioDataException as err:
00142             err_msg = str(err)
00143             logger.info(err_msg)
00144 
00145         except Exception as err:
00146             failed = True
00147             err_msg = str(err)
00148             logger.info(err_msg)
00149 
00150         self.failIf(failed, 'invalid format nchannel %s' % err_msg)
00151 
00152         # invalid format (framerate)
00153         try:
00154             data = open(self.wav_file, 'rb').read()
00155             validators.check_wave_data(
00156                 data,
00157                 self.FRAMERATE+1000,
00158                 self.CHANNELS,
00159                 self.SAMPWIDTH,
00160                 self.AUDIO_LENGTH
00161             )
00162             failed = True
00163 
00164         except ext.InvalidAudioDataException as err:
00165             err_msg = str(err)
00166             logger.info(err_msg)
00167 
00168         except Exception as err:
00169             failed = True
00170             err_msg = str(err)
00171             logger.info(err_msg)
00172 
00173         self.failIf(failed, 'invalid format framerate %s' % err_msg)
00174 
00175         # invalid format (sampwidth)
00176         try:
00177             data = open(self.wav_file, 'rb').read()
00178             validators.check_wave_data(
00179                 data,
00180                 self.FRAMERATE,
00181                 self.CHANNELS,
00182                 self.SAMPWIDTH+2,
00183                 self.AUDIO_LENGTH
00184             )
00185             failed = True
00186 
00187         except ext.InvalidAudioDataException as err:
00188             err_msg = str(err)
00189             logger.info(err_msg)
00190 
00191         except Exception as err:
00192             failed = True
00193             err_msg = str(err)
00194             logger.info(err_msg)
00195         self.failIf(failed, 'invalid format sampwidth %s' % err_msg)
00196 
00197         # too big wave
00198         try:
00199             data = open(self.big_wav_file, 'rb').read()
00200             validators.check_wave_data(
00201                 data,
00202                 self.FRAMERATE,
00203                 self.CHANNELS,
00204                 self.SAMPWIDTH,
00205                 self.AUDIO_LENGTH
00206             )
00207             failed = True
00208 
00209         except ext.InvalidAudioDataException as err:
00210             err_msg = str(err)
00211             logger.info(err_msg)
00212 
00213         except Exception as err:
00214             failed = True
00215             err_msg = str(err)
00216             logger.info(err_msg)
00217 
00218         self.failIf(failed, 'too big data size sampwidth %s' % err_msg)
00219 
00220         # valid format
00221         try:
00222             data = open(self.wav_file, 'rb').read()
00223             validators.check_wave_data(
00224                 data,
00225                 self.FRAMERATE,
00226                 self.CHANNELS,
00227                 self.SAMPWIDTH,
00228                 self.AUDIO_LENGTH
00229             )
00230 
00231         except ext.InvalidAudioDataException as err:
00232             failed = True
00233             err_msg = str(err)
00234             logger.info(err_msg)
00235 
00236         self.failIf(failed, 'invalid validate %s' % err_msg)
00237 
00238 
00239 if __name__ == '__main__':
00240     import rosunit
00241     rosunit.unitrun(
00242         'rospeex_core',
00243         'validator',
00244         TestValidators,
00245         None,
00246         coverage_packages=['rospeex_core.validators']
00247     )


rospeex_core
Author(s): Komei Sugiura
autogenerated on Thu Apr 20 2017 03:08:53