From b13fbec234292c46af2af37be0387d98a15ba2bf Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 24 Sep 2014 19:39:16 +0200 Subject: Changed vocabcompiler test cases --- client/test.py | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index 6858700..9f9be8a 100644 --- a/client/test.py +++ b/client/test.py @@ -4,6 +4,8 @@ import os import sys import unittest import logging +import tempfile +import shutil import argparse from mock import patch, Mock @@ -32,31 +34,31 @@ class UnorderedList(list): class TestVocabCompiler(unittest.TestCase): - def testWordExtraction(self): - sentences = "temp_sentences.txt" - dictionary = "temp_dictionary.dic" - languagemodel = "temp_languagemodel.lm" - - words = [] + def testPhraseExtraction(self): + expected_phrases = ['MOCK'] mock_module = Mock() - mock_module.WORDS = [ - 'MOCK' - ] - - words.extend(mock_module.WORDS) - - with patch.object(g2p, 'translateWords') as translateWords: - with patch.object(vocabcompiler, 'text2lm') as text2lm: - with patch.object(brain.Brain, 'get_modules', - classmethod(lambda cls: [mock_module])): - vocabcompiler.compile(sentences, dictionary, languagemodel) - - translateWords.assert_called_once_with( - UnorderedList(words)) - self.assertTrue(text2lm.called) - os.remove(sentences) - os.remove(dictionary) + mock_module.WORDS = ['MOCK'] + + with patch.object(brain.Brain, 'get_modules', + classmethod(lambda cls: [mock_module])): + extracted_phrases = vocabcompiler.get_all_phrases() + self.assertEqual(expected_phrases, extracted_phrases) + + def testVocabulary(self): + phrases = ['THIS IS A TEST'] + + tempdir = tempfile.mkdtemp() + + vocab = vocabcompiler.DummyVocabulary(path=tempdir) + self.assertIsNone(vocab.compiled_revision) + self.assertFalse(vocab.is_compiled) + vocab.compile(phrases) + self.assertIsNotNone(vocab.compiled_revision) + self.assertTrue(vocab.is_compiled) + self.assertTrue(vocab.matches_phrases(phrases)) + + shutil.rmtree(tempdir) class TestMic(unittest.TestCase): -- cgit v1.3.1 From ae67577e9c66231b13d72822b407c5c699d0addc Mon Sep 17 00:00:00 2001 From: schneefux Date: Fri, 26 Sep 2014 12:29:33 +0200 Subject: Fix assert statements in vocabcompiler unittests --- client/test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index 9f9be8a..f91a06e 100644 --- a/client/test.py +++ b/client/test.py @@ -53,8 +53,9 @@ class TestVocabCompiler(unittest.TestCase): vocab = vocabcompiler.DummyVocabulary(path=tempdir) self.assertIsNone(vocab.compiled_revision) self.assertFalse(vocab.is_compiled) + self.assertFalse(vocab.matches_phrases(phrases)) vocab.compile(phrases) - self.assertIsNotNone(vocab.compiled_revision) + self.assertIsInstance(vocab.compiled_revision, str) self.assertTrue(vocab.is_compiled) self.assertTrue(vocab.matches_phrases(phrases)) -- cgit v1.3.1 From d282d1f7ea17feeb19cd311037d837e8f1c2a3d5 Mon Sep 17 00:00:00 2001 From: schneefux Date: Fri, 26 Sep 2014 12:30:39 +0200 Subject: Remove UnorderedList from unittests --- client/test.py | 6 ------ 1 file changed, 6 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index f91a06e..c08866f 100644 --- a/client/test.py +++ b/client/test.py @@ -26,12 +26,6 @@ DEFAULT_PROFILE = { } -class UnorderedList(list): - - def __eq__(self, other): - return sorted(self) == sorted(other) - - class TestVocabCompiler(unittest.TestCase): def testPhraseExtraction(self): -- cgit v1.3.1 From 30133ec0db993eee5bc9ada8b004f910d0a97c5a Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 1 Oct 2014 14:11:50 +0200 Subject: Fix G2P testcases, do not depend on fixed translation string --- client/test.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index c08866f..7f31f37 100644 --- a/client/test.py +++ b/client/test.py @@ -86,22 +86,17 @@ class TestMic(unittest.TestCase): class TestG2P(unittest.TestCase): def setUp(self): - self.translations = { - 'GOOD': 'G UH D', - 'BAD': 'B AE D', - 'UGLY': 'AH G L IY' - } + self.g2pconverter = g2p.PhonetisaurusG2P(**g2p.PhonetisaurusG2P.get_config()) + self.words = ['GOOD', 'BAD', 'UGLY'] def testTranslateWord(self): - for word in self.translations: - translation = self.translations[word] - self.assertEqual(g2p.translateWord(word), translation) + for word in self.words: + self.assertIn(word, self.g2pconverter.translate(word).keys()) def testTranslateWords(self): - words = self.translations.keys() - # preserve ordering - translations = [self.translations[w] for w in words] - self.assertEqual(g2p.translateWords(words), translations) + results = self.g2pconverter.translate(self.words).keys() + for word in self.words: + self.assertIn(word, results) class TestDiagnose(unittest.TestCase): -- cgit v1.3.1 From f9db756c18723abcacd54ad0667f69babca4c695 Mon Sep 17 00:00:00 2001 From: schneefux Date: Mon, 6 Oct 2014 18:11:32 +0200 Subject: PEP8 style fixes in test.py and vocabcompiler.py --- client/test.py | 3 ++- client/vocabcompiler.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index 7f31f37..0034974 100644 --- a/client/test.py +++ b/client/test.py @@ -86,7 +86,8 @@ class TestMic(unittest.TestCase): class TestG2P(unittest.TestCase): def setUp(self): - self.g2pconverter = g2p.PhonetisaurusG2P(**g2p.PhonetisaurusG2P.get_config()) + self.g2pconverter = g2p.PhonetisaurusG2P( + **g2p.PhonetisaurusG2P.get_config()) self.words = ['GOOD', 'BAD', 'UGLY'] def testTranslateWord(self): diff --git a/client/vocabcompiler.py b/client/vocabcompiler.py index 1cfe15e..d0f0124 100644 --- a/client/vocabcompiler.py +++ b/client/vocabcompiler.py @@ -323,7 +323,7 @@ class PocketsphinxVocabulary(AbstractVocabulary): line = "%s\t%s\n" % (word, pronounciation) else: line = "%s(%d)\t%s\n" % (word, i, pronounciation) - f.write(line) + f.write(line) def get_phrases_from_module(module): -- cgit v1.3.1 From 63aa78e9029de4059c0306926bfd1943cf07492e Mon Sep 17 00:00:00 2001 From: schneefux Date: Tue, 7 Oct 2014 14:24:19 +0200 Subject: Add unittests for G2P without phonetisaurus --- client/test.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index 0034974..079ba92 100644 --- a/client/test.py +++ b/client/test.py @@ -100,6 +100,43 @@ class TestG2P(unittest.TestCase): 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 patch.object(g2p.PhonetisaurusG2P, 'executable_found', + classmethod(lambda cls: True)): + with tempfile.NamedTemporaryFile() as f: + conf = g2p.PhonetisaurusG2P.get_config().items() + with patch.object(g2p.PhonetisaurusG2P, 'get_config', + classmethod(lambda cls: dict( + conf + [('fst_model', f.name)]))): + super(self.__class__, self).setUp() + + def testTranslateWord(self): + with patch('subprocess.Popen', + return_value=TestPatchedG2P.DummyProc()): + super(self.__class__, self).testTranslateWord() + + def testTranslateWords(self): + with patch('subprocess.Popen', + return_value=TestPatchedG2P.DummyProc()): + super(self.__class__, self).testTranslateWords() + + class TestDiagnose(unittest.TestCase): def testPythonImportCheck(self): # This a python stdlib module that definitely exists @@ -270,7 +307,9 @@ if __name__ == '__main__': test_cases = [TestBrain, TestModules, TestVocabCompiler, TestTTS, TestDiagnose] - if not args.light: + if args.light: + test_cases.append(TestPatchedG2P) + else: test_cases.append(TestG2P) test_cases.append(TestMic) -- cgit v1.3.1 From b81e89ead31c8442a9f71f0bd7eabe19f5985842 Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 8 Oct 2014 11:57:20 +0200 Subject: Add testcases for (patched) PocketsphinxVocabulary --- client/test.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 13 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index 079ba92..d4957b8 100644 --- a/client/test.py +++ b/client/test.py @@ -6,6 +6,7 @@ import unittest import logging import tempfile import shutil +import contextlib import argparse from mock import patch, Mock @@ -39,22 +40,60 @@ class TestVocabCompiler(unittest.TestCase): extracted_phrases = vocabcompiler.get_all_phrases() self.assertEqual(expected_phrases, extracted_phrases) - def testVocabulary(self): - phrases = ['THIS IS A TEST'] - tempdir = tempfile.mkdtemp() +class TestVocabulary(unittest.TestCase): - vocab = vocabcompiler.DummyVocabulary(path=tempdir) - self.assertIsNone(vocab.compiled_revision) - self.assertFalse(vocab.is_compiled) - self.assertFalse(vocab.matches_phrases(phrases)) - vocab.compile(phrases) - self.assertIsInstance(vocab.compiled_revision, str) - self.assertTrue(vocab.is_compiled) - self.assertTrue(vocab.matches_phrases(phrases)) + VOCABULARY = vocabcompiler.DummyVocabulary + @contextlib.contextmanager + def do_in_tempdir(self): + tempdir = tempfile.mkdtemp() + yield tempdir shutil.rmtree(tempdir) + def testVocabulary(self): + phrases = ['GOOD BAD UGLY'] + with self.do_in_tempdir() as tempdir: + vocab = self.VOCABULARY(path=tempdir) + self.assertIsNone(vocab.compiled_revision) + self.assertFalse(vocab.is_compiled) + self.assertFalse(vocab.matches_phrases(phrases)) + vocab.compile(phrases) + self.assertIsInstance(vocab.compiled_revision, str) + self.assertTrue(vocab.is_compiled) + self.assertTrue(vocab.matches_phrases(phrases)) + + +class TestPocketsphinxVocabulary(TestVocabulary): + + VOCABULARY = vocabcompiler.PocketsphinxVocabulary + + +class TestPatchedPocketsphinxVocabulary(TestPocketsphinxVocabulary): + + def testVocabulary(self): + + def write_test_vocab(text, output_file): + with open(output_file, "w") as f: + for word in text.split(' '): + f.write("%s\n" % word) + + def write_test_lm(text, output_file, **kwargs): + with open(output_file, "w") as f: + f.write("TEST") + + vocabcompiler.cmuclmtk = None + with patch('vocabcompiler.cmuclmtk') as mocked_cmuclmtk: + mocked_cmuclmtk.text2vocab = write_test_vocab + mocked_cmuclmtk.text2lm = write_test_lm + with patch.object(g2p.PhonetisaurusG2P, '__new__', + create=True) as mocked_g2p: + mocked_g2p.translate = (lambda *args, **kwargs: + {'GOOD': ['G UH D'], + 'BAD': ['B AE D'], + 'UGLY': ['AH G L IY']}) + super(self.__class__, self).testVocabulary() + class TestMic(unittest.TestCase): @@ -305,12 +344,14 @@ if __name__ == '__main__': # Change CWD to jasperpath.LIB_PATH os.chdir(jasperpath.LIB_PATH) - test_cases = [TestBrain, TestModules, TestVocabCompiler, TestTTS, - TestDiagnose] + test_cases = [TestBrain, TestModules, TestDiagnose, TestTTS, + TestVocabCompiler, TestVocabulary] if args.light: test_cases.append(TestPatchedG2P) + test_cases.append(TestPatchedPocketsphinxVocabulary) else: test_cases.append(TestG2P) + test_cases.append(TestPocketsphinxVocabulary) test_cases.append(TestMic) suite = unittest.TestSuite() -- cgit v1.3.1 From ffce99db9f6ade908a0b0f7c1fc847dad84b49bd Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 8 Oct 2014 14:47:06 +0200 Subject: improve vocabulary unittest coverage --- client/test.py | 67 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 16 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index d4957b8..f661b89 100644 --- a/client/test.py +++ b/client/test.py @@ -42,7 +42,6 @@ class TestVocabCompiler(unittest.TestCase): class TestVocabulary(unittest.TestCase): - VOCABULARY = vocabcompiler.DummyVocabulary @contextlib.contextmanager @@ -54,20 +53,56 @@ class TestVocabulary(unittest.TestCase): def testVocabulary(self): phrases = ['GOOD BAD UGLY'] with self.do_in_tempdir() as tempdir: - vocab = self.VOCABULARY(path=tempdir) - self.assertIsNone(vocab.compiled_revision) - self.assertFalse(vocab.is_compiled) - self.assertFalse(vocab.matches_phrases(phrases)) - vocab.compile(phrases) - self.assertIsInstance(vocab.compiled_revision, str) - self.assertTrue(vocab.is_compiled) - self.assertTrue(vocab.matches_phrases(phrases)) + self.vocab = self.VOCABULARY(path=tempdir) + self.assertIsNone(self.vocab.compiled_revision) + self.assertFalse(self.vocab.is_compiled) + self.assertFalse(self.vocab.matches_phrases(phrases)) + + # We're now testing error handling. To avoid flooding the + # output with error messages that are catched anyway, + # we'll temporarly disable logging. Otherwise, error log + # messages and traceback would be printed so that someone + # might think that tests failed even though they succeeded. + logging.disable(logging.ERROR) + with self.assertRaises(OSError): + with patch('os.makedirs', side_effect=OSError('test')): + self.vocab.compile(phrases) + with self.assertRaises(OSError): + with patch('%s.open' % vocabcompiler.__name__, + create=True, + side_effect=OSError('test')): + self.vocab.compile(phrases) + + class StrangeCompilationError(Exception): + pass + with self.assertRaises(StrangeCompilationError): + with patch.object(self.vocab, '_compile_vocabulary', + side_effect=StrangeCompilationError('test')): + self.vocab.compile(phrases) + with patch('os.remove', + side_effect=OSError('test')): + self.vocab.compile(phrases) + # Re-enable logging again + logging.disable(logging.NOTSET) + + self.vocab.compile(phrases) + self.assertIsInstance(self.vocab.compiled_revision, str) + self.assertTrue(self.vocab.is_compiled) + self.assertTrue(self.vocab.matches_phrases(phrases)) + self.vocab.compile(phrases) + self.vocab.compile(phrases, force=True) class TestPocketsphinxVocabulary(TestVocabulary): VOCABULARY = vocabcompiler.PocketsphinxVocabulary + def testVocabulary(self): + super(TestPocketsphinxVocabulary, self).testVocabulary() + self.assertIsInstance(self.vocab.decoder_kwargs, dict) + self.assertIn('lm', self.vocab.decoder_kwargs) + self.assertIn('dict', self.vocab.decoder_kwargs) + class TestPatchedPocketsphinxVocabulary(TestPocketsphinxVocabulary): @@ -82,17 +117,17 @@ class TestPatchedPocketsphinxVocabulary(TestPocketsphinxVocabulary): with open(output_file, "w") as f: f.write("TEST") - vocabcompiler.cmuclmtk = None - with patch('vocabcompiler.cmuclmtk') as mocked_cmuclmtk: + with patch('vocabcompiler.cmuclmtk', + create=True) as mocked_cmuclmtk: mocked_cmuclmtk.text2vocab = write_test_vocab mocked_cmuclmtk.text2lm = write_test_lm with patch.object(g2p.PhonetisaurusG2P, '__new__', create=True) as mocked_g2p: - mocked_g2p.translate = (lambda *args, **kwargs: - {'GOOD': ['G UH D'], - 'BAD': ['B AE D'], - 'UGLY': ['AH G L IY']}) - super(self.__class__, self).testVocabulary() + mocked_g2p.translate.return_value = (lambda *args, **kwargs: + {'GOOD': ['G UH D'], + 'BAD': ['B AE D'], + 'UGLY': ['AH G L IY']}) + super(TestPatchedPocketsphinxVocabulary, self).testVocabulary() class TestMic(unittest.TestCase): -- cgit v1.3.1 From f8ead5d8fbaba38364702332a50a87471175b65e Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 8 Oct 2014 16:41:20 +0200 Subject: Add test for keyword phrase extraction --- client/test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index f661b89..0269d18 100644 --- a/client/test.py +++ b/client/test.py @@ -40,6 +40,19 @@ class TestVocabCompiler(unittest.TestCase): extracted_phrases = vocabcompiler.get_all_phrases() self.assertEqual(expected_phrases, extracted_phrases) + def testKeywordPhraseExtraction(self): + expected_phrases = ['MOCK'] + + with tempfile.TemporaryFile() as f: + # We can't use mock_open here, because it doesn't seem to work + # with the 'for line in f' syntax + f.write("MOCK\n") + f.seek(0) + with patch('%s.open' % vocabcompiler.__name__, + return_value=f, create=True): + extracted_phrases = vocabcompiler.get_keyword_phrases() + self.assertEqual(expected_phrases, extracted_phrases) + class TestVocabulary(unittest.TestCase): VOCABULARY = vocabcompiler.DummyVocabulary -- cgit v1.3.1 From 6d4028cb6b76f759ac38bd091cad8d6bd662bb93 Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 8 Oct 2014 16:42:20 +0200 Subject: Update TestMic testcase to work with Vocabulary --- client/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index 0269d18..ac25b41 100644 --- a/client/test.py +++ b/client/test.py @@ -150,7 +150,7 @@ class TestMic(unittest.TestCase): self.time_clip = jasperpath.data('audio', 'time.wav') from stt import PocketSphinxSTT - self.stt = PocketSphinxSTT() + self.stt = PocketSphinxSTT(**PocketSphinxSTT.get_config()) def testTranscribeJasper(self): """ -- cgit v1.3.1 From baa4d68705def31614509bd4fdcb3438f175a985 Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 8 Oct 2014 16:50:56 +0200 Subject: Further improve unittest coverage --- client/test.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index ac25b41..7ab1761 100644 --- a/client/test.py +++ b/client/test.py @@ -88,10 +88,11 @@ class TestVocabulary(unittest.TestCase): class StrangeCompilationError(Exception): pass - with self.assertRaises(StrangeCompilationError): - with patch.object(self.vocab, '_compile_vocabulary', - side_effect=StrangeCompilationError('test')): + with patch.object(self.vocab, '_compile_vocabulary', + side_effect=StrangeCompilationError('test')): + with self.assertRaises(StrangeCompilationError): self.vocab.compile(phrases) + with self.assertRaises(StrangeCompilationError): with patch('os.remove', side_effect=OSError('test')): self.vocab.compile(phrases) @@ -130,17 +131,27 @@ class TestPatchedPocketsphinxVocabulary(TestPocketsphinxVocabulary): with open(output_file, "w") as f: f.write("TEST") + class DummyG2P(object): + def __init__(self, *args, **kwargs): + pass + + @classmethod + def get_config(self, *args, **kwargs): + return {} + + def translate(self, *args, **kwargs): + return {'GOOD': ['G UH D', + 'G UW D'], + 'BAD': ['B AE D'], + 'UGLY': ['AH G L IY']} + with patch('vocabcompiler.cmuclmtk', create=True) as mocked_cmuclmtk: mocked_cmuclmtk.text2vocab = write_test_vocab mocked_cmuclmtk.text2lm = write_test_lm - with patch.object(g2p.PhonetisaurusG2P, '__new__', - create=True) as mocked_g2p: - mocked_g2p.translate.return_value = (lambda *args, **kwargs: - {'GOOD': ['G UH D'], - 'BAD': ['B AE D'], - 'UGLY': ['AH G L IY']}) - super(TestPatchedPocketsphinxVocabulary, self).testVocabulary() + with patch('vocabcompiler.PhonetisaurusG2P', DummyG2P): + super(TestPatchedPocketsphinxVocabulary, + self).testVocabulary() class TestMic(unittest.TestCase): -- cgit v1.3.1 From ac0a6d731ad179d62bf2be6be28ee862a020bdab Mon Sep 17 00:00:00 2001 From: schneefux Date: Wed, 8 Oct 2014 20:11:58 +0200 Subject: Fix G2P testcase --- client/test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'client/test.py') diff --git a/client/test.py b/client/test.py index 7ab1761..0a414f4 100644 --- a/client/test.py +++ b/client/test.py @@ -215,8 +215,8 @@ class TestPatchedG2P(TestG2P): "UGLY\t18.9617\t AH G L AY \n", "") def setUp(self): - with patch.object(g2p.PhonetisaurusG2P, 'executable_found', - classmethod(lambda cls: True)): + with patch('g2p.diagnose.check_executable', + return_value=True): with tempfile.NamedTemporaryFile() as f: conf = g2p.PhonetisaurusG2P.get_config().items() with patch.object(g2p.PhonetisaurusG2P, 'get_config', -- cgit v1.3.1