From 999cd4c17b4ea444e3daea1a9c737ce063f2ad6a Mon Sep 17 00:00:00 2001 From: Amjad Natouf Date: Tue, 5 Nov 2024 20:46:57 +0100 Subject: [PATCH] move test case to test_audio.py --- tests/test_audio.py | 19 ++++++++++++++++++- tests/test_special_features.py | 20 -------------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/tests/test_audio.py b/tests/test_audio.py index 0e195157..f20aa31a 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -140,7 +140,24 @@ def test_flac_stereo_24_bit(self): self.assertSimilar(audio.get_raw_data()[:32], b"\x00\x00\x00\x00\xfe\xff\x00\x02\x00\x00\xfe\xff\x00\x00\x00\xff\x01\x00\x02\xfc\xff\xfe\x01\x00\x02\xfc\xff\xfe\x07\x00\x01\xf6") else: self.assertSimilar(audio.get_raw_data()[:32], b"\x00\x00\x00\x00\x00\x00\xfe\xff\x00\x00\x02\x00\x00\x00\xfe\xff\x00\x00\x00\x00\x00\xff\x01\x00\x00\x02\xfc\xff\x00\xfe\x01\x00") - + # Added test for MP3 + # Test that attempting to recognize speech from an incompatible audio file raises a ValueError. + def test_incompatible_audio_file_error(self): + # Define paths to audio files + self.AUDIO_FILE_EN_MP3 = path.join(path.dirname(path.realpath(__file__)), "english.mp3") + # Create a Recognizer instance + r = sr.Recognizer() + # Verify that a ValueError is raised when an incompatible audio file is used + with self.assertRaises(ValueError) as context: + # Load the audio file for recognition + # Attempt to record audio from the MP3 file + with sr.AudioFile(self.AUDIO_FILE_EN_MP3) as source:r.record(source) + + # Check that the raised ValueError contains the expected message + self.assertEqual( + str(context.exception), + "Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format" + ) if __name__ == "__main__": unittest.main() diff --git a/tests/test_special_features.py b/tests/test_special_features.py index f95506f0..9dd2574e 100644 --- a/tests/test_special_features.py +++ b/tests/test_special_features.py @@ -10,9 +10,7 @@ class TestSpecialFeatures(unittest.TestCase): def setUp(self): - # Define paths to audio files self.AUDIO_FILE_EN = os.path.join(os.path.dirname(os.path.realpath(__file__)), "english.wav") - self.AUDIO_FILE_EN_MP3 = os.path.join(os.path.dirname(os.path.realpath(__file__)), "english.mp3") self.addTypeEqualityFunc(str, self.assertSameWords) @unittest.skipIf(sys.platform.startswith("win"), "skip on Windows") @@ -23,24 +21,6 @@ def test_sphinx_keywords(self): self.assertEqual(r.recognize_sphinx(audio, keyword_entries=[("wan", 0.95), ("too", 1.0), ("tree", 1.0)]), "tree too wan") self.assertEqual(r.recognize_sphinx(audio, keyword_entries=[("un", 0.95), ("to", 1.0), ("tee", 1.0)]), "tee to un") - # Added test for MP3 - # Test that attempting to recognize speech from an incompatible audio file raises a ValueError. - def test_incompatible_audio_file_error(self): - - # Create a Recognizer instance - r = sr.Recognizer() - # Verify that a ValueError is raised when an incompatible audio file is used - with self.assertRaises(ValueError) as context: - # Load the audio file for recognition - # Attempt to record audio from the MP3 file - with sr.AudioFile(self.AUDIO_FILE_EN_MP3) as source:r.record(source) - - # Check that the raised ValueError contains the expected message - self.assertEqual( - str(context.exception), - "Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format" - ) - def assertSameWords(self, tested, reference, msg=None): set_tested = set(tested.split()) set_reference = set(reference.split())