Skip to content

Commit

Permalink
move test case to test_audio.py
Browse files Browse the repository at this point in the history
  • Loading branch information
amjadnattouf committed Nov 5, 2024
1 parent b039f77 commit 999cd4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
19 changes: 18 additions & 1 deletion tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
20 changes: 0 additions & 20 deletions tests/test_special_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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())
Expand Down

0 comments on commit 999cd4c

Please sign in to comment.