From 7d80c7d7b6c983cd631986b3c74672389c8916d9 Mon Sep 17 00:00:00 2001 From: schneefux Date: Tue, 30 Dec 2014 19:09:49 +0100 Subject: Split unittests into separate files and move them to tests/ --- tests/test_g2p.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/test_g2p.py (limited to 'tests/test_g2p.py') diff --git a/tests/test_g2p.py b/tests/test_g2p.py new file mode 100644 index 0000000..e18ebff --- /dev/null +++ b/tests/test_g2p.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8-*- +import unittest +import tempfile +import mock +from client import g2p + + +def phonetisaurus_installed(): + try: + g2p.PhonetisaurusG2P(**g2p.PhonetisaurusG2P.get_config()) + except OSError: + return False + else: + return True + + +@unittest.skipUnless(phonetisaurus_installed, + "Phonetisaurus or fst_model not present") +class TestG2P(unittest.TestCase): + + def setUp(self): + self.g2pconverter = g2p.PhonetisaurusG2P( + **g2p.PhonetisaurusG2P.get_config()) + self.words = ['GOOD', 'BAD', 'UGLY'] + + def testTranslateWord(self): + for word in self.words: + self.assertIn(word, self.g2pconverter.translate(word).keys()) + + def testTranslateWords(self): + results = self.g2pconverter.translate(self.words).keys() + for word in self.words: + self.assertIn(word, results) + + +class TestPatchedG2P(TestG2P): + class DummyProc(object): + def __init__(self, *args, **kwargs): + self.returncode = 0 + + def communicate(self): + return ("GOOD\t9.20477\t G UH D \n" + + "GOOD\t14.4036\t G UW D \n" + + "GOOD\t16.0258\t G UH D IY \n" + + "BAD\t0.7416\t B AE D \n" + + "BAD\t12.5495\t B AA D \n" + + "BAD\t13.6745\t B AH D \n" + + "UGLY\t12.572\t AH G L IY \n" + + "UGLY\t17.9278\t Y UW G L IY \n" + + "UGLY\t18.9617\t AH G L AY \n", "") + + def setUp(self): + with mock.patch('client.g2p.diagnose.check_executable', + return_value=True): + with tempfile.NamedTemporaryFile() as f: + conf = g2p.PhonetisaurusG2P.get_config().items() + with mock.patch.object(g2p.PhonetisaurusG2P, 'get_config', + classmethod(lambda cls: dict(conf + + [('fst_model', f.name)]))): + super(self.__class__, self).setUp() + + def testTranslateWord(self): + with mock.patch('subprocess.Popen', + return_value=TestPatchedG2P.DummyProc()): + super(self.__class__, self).testTranslateWord() + + def testTranslateWords(self): + with mock.patch('subprocess.Popen', + return_value=TestPatchedG2P.DummyProc()): + super(self.__class__, self).testTranslateWords() -- cgit v1.3.1 From 87262c75c3e645358e6587b64c8bd2adca9477b2 Mon Sep 17 00:00:00 2001 From: schneefux Date: Tue, 30 Dec 2014 19:37:24 +0100 Subject: Add skip conditions to unittests for dependency checking --- tests/test_g2p.py | 2 +- tests/test_stt.py | 21 +++++++++++++++++++++ tests/test_vocabcompiler.py | 4 ++-- 3 files changed, 24 insertions(+), 3 deletions(-) (limited to 'tests/test_g2p.py') diff --git a/tests/test_g2p.py b/tests/test_g2p.py index e18ebff..7904cbf 100644 --- a/tests/test_g2p.py +++ b/tests/test_g2p.py @@ -15,7 +15,7 @@ def phonetisaurus_installed(): return True -@unittest.skipUnless(phonetisaurus_installed, +@unittest.skipUnless(phonetisaurus_installed(), "Phonetisaurus or fst_model not present") class TestG2P(unittest.TestCase): diff --git a/tests/test_stt.py b/tests/test_stt.py index 267c1c2..cb33ff6 100644 --- a/tests/test_stt.py +++ b/tests/test_stt.py @@ -1,9 +1,30 @@ #!/usr/bin/env python2 # -*- coding: utf-8-*- import unittest +import imp from client import stt, jasperpath +def cmuclmtk_installed(): + try: + imp.find_module('cmuclmtk') + except ImportError: + return False + else: + return True + + +def pocketsphinx_installed(): + try: + imp.find_module('pocketsphinx') + except ImportError: + return False + else: + return True + + +@unittest.skipUnless(cmuclmtk_installed(), "CMUCLMTK not present") +@unittest.skipUnless(pocketsphinx_installed(), "Pocketsphinx not present") class TestSTT(unittest.TestCase): def setUp(self): diff --git a/tests/test_vocabcompiler.py b/tests/test_vocabcompiler.py index 561440a..f9e427f 100644 --- a/tests/test_vocabcompiler.py +++ b/tests/test_vocabcompiler.py @@ -90,8 +90,8 @@ class TestVocabulary(unittest.TestCase): self.vocab.compile(phrases, force=True) -@unittest.skipIf(vocabcompiler.cmuclmtk is None, - "CMUCLMTK not present") +@unittest.skipUnless(hasattr(vocabcompiler, 'cmuclmtk'), + "CMUCLMTK not present") class TestPocketsphinxVocabulary(TestVocabulary): VOCABULARY = vocabcompiler.PocketsphinxVocabulary -- cgit v1.3.1