|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +from speech_recognition import AudioData, Recognizer |
| 4 | +from speech_recognition.recognizers import cohere_api |
| 5 | + |
| 6 | + |
| 7 | +@patch("cohere.ClientV2") |
| 8 | +def test_transcribe_default_model(mock_client_cls): |
| 9 | + mock_response = MagicMock() |
| 10 | + mock_response.text = "Transcription by Cohere" |
| 11 | + mock_client = MagicMock() |
| 12 | + mock_client.audio.transcriptions.create.return_value = mock_response |
| 13 | + mock_client_cls.return_value = mock_client |
| 14 | + |
| 15 | + audio_data = MagicMock(spec=AudioData) |
| 16 | + audio_data.get_wav_data.return_value = b"fake_wav" |
| 17 | + |
| 18 | + actual = cohere_api.recognize( |
| 19 | + MagicMock(spec=Recognizer), audio_data, language="en" |
| 20 | + ) |
| 21 | + |
| 22 | + assert actual == "Transcription by Cohere" |
| 23 | + audio_data.get_wav_data.assert_called_once() |
| 24 | + mock_client_cls.assert_called_once_with() |
| 25 | + mock_client.audio.transcriptions.create.assert_called_once() |
| 26 | + call_kw = mock_client.audio.transcriptions.create.call_args.kwargs |
| 27 | + assert call_kw["model"] == "cohere-transcribe-03-2026" |
| 28 | + assert call_kw["language"] == "en" |
| 29 | + assert "file" in call_kw |
| 30 | + |
| 31 | + |
| 32 | +@patch("cohere.ClientV2") |
| 33 | +def test_transcribe_with_language(mock_client_cls): |
| 34 | + mock_response = MagicMock() |
| 35 | + mock_response.text = "Japanese transcription" |
| 36 | + mock_client = MagicMock() |
| 37 | + mock_client.audio.transcriptions.create.return_value = mock_response |
| 38 | + mock_client_cls.return_value = mock_client |
| 39 | + |
| 40 | + audio_data = MagicMock(spec=AudioData) |
| 41 | + audio_data.get_wav_data.return_value = b"fake_wav" |
| 42 | + |
| 43 | + actual = cohere_api.recognize( |
| 44 | + MagicMock(spec=Recognizer), audio_data, language="ja" |
| 45 | + ) |
| 46 | + |
| 47 | + assert actual == "Japanese transcription" |
| 48 | + call_kw = mock_client.audio.transcriptions.create.call_args.kwargs |
| 49 | + assert call_kw["model"] == "cohere-transcribe-03-2026" |
| 50 | + assert call_kw["language"] == "ja" |
0 commit comments