Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Add tests for recognize_whisper_api #738

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
run: |
python -m pip install 'pocketsphinx<5'
python -m pip install git+https://github.com/openai/whisper.git soundfile
python -m pip install openai
python -m pip install .
- name: Test with unittest
run: |
Expand Down
42 changes: 42 additions & 0 deletions tests/recognizers/test_whisper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from unittest import TestCase
from unittest.mock import MagicMock, patch

from speech_recognition import AudioData, Recognizer
from speech_recognition.recognizers import whisper


@patch("speech_recognition.recognizers.whisper.os.environ")
@patch("speech_recognition.recognizers.whisper.BytesIO")
@patch("openai.OpenAI")
class RecognizeWhisperApiTestCase(TestCase):
def test_recognize_default_arguments(self, OpenAI, BytesIO, environ):
client = OpenAI.return_value
transcript = client.audio.transcriptions.create.return_value

recognizer = MagicMock(spec=Recognizer)
audio_data = MagicMock(spec=AudioData)

actual = whisper.recognize_whisper_api(recognizer, audio_data)

self.assertEqual(actual, transcript.text)
audio_data.get_wav_data.assert_called_once_with()
BytesIO.assert_called_once_with(audio_data.get_wav_data.return_value)
OpenAI.assert_called_once_with(api_key=None)
client.audio.transcriptions.create.assert_called_once_with(
file=BytesIO.return_value, model="whisper-1"
)

def test_recognize_pass_arguments(self, OpenAI, BytesIO, environ):
client = OpenAI.return_value

recognizer = MagicMock(spec=Recognizer)
audio_data = MagicMock(spec=AudioData)

actual = whisper.recognize_whisper_api(
recognizer, audio_data, model="x-whisper", api_key="OPENAI_API_KEY"
)

OpenAI.assert_called_once_with(api_key="OPENAI_API_KEY")
client.audio.transcriptions.create.assert_called_once_with(
file=BytesIO.return_value, model="x-whisper"
)
Loading