style: format test_voice.py with black and sort imports

This commit is contained in:
Paul Gauthier (aider) 2024-11-30 15:41:19 -08:00
parent caeceb58a5
commit 97daff4a10

View file

@ -1,45 +1,53 @@
import pytest
from unittest.mock import Mock, patch
import numpy as np
import os
import queue
import tempfile
import os
from unittest.mock import Mock, patch
import numpy as np
import pytest
from aider.voice import SoundDeviceError, Voice
from aider.voice import Voice, SoundDeviceError
@pytest.fixture
def mock_sounddevice():
with patch('sounddevice.query_devices') as mock_query:
with patch("sounddevice.query_devices") as mock_query:
mock_query.return_value = [
{'name': 'test_device', 'max_input_channels': 2},
{'name': 'another_device', 'max_input_channels': 1}
{"name": "test_device", "max_input_channels": 2},
{"name": "another_device", "max_input_channels": 1},
]
yield mock_query
@pytest.fixture
def mock_soundfile():
with patch('soundfile.SoundFile') as mock_sf:
with patch("soundfile.SoundFile") as mock_sf:
yield mock_sf
def test_voice_init_default_device(mock_sounddevice):
voice = Voice()
assert voice.device_id is None
assert voice.audio_format == 'wav'
assert voice.audio_format == "wav"
def test_voice_init_specific_device(mock_sounddevice):
voice = Voice(device_name='test_device')
voice = Voice(device_name="test_device")
assert voice.device_id == 0
def test_voice_init_invalid_device(mock_sounddevice):
with pytest.raises(ValueError) as exc:
Voice(device_name='nonexistent_device')
assert 'Device' in str(exc.value)
assert 'not found' in str(exc.value)
Voice(device_name="nonexistent_device")
assert "Device" in str(exc.value)
assert "not found" in str(exc.value)
def test_voice_init_invalid_format():
with pytest.raises(ValueError) as exc:
Voice(audio_format='invalid')
assert 'Unsupported audio format' in str(exc.value)
Voice(audio_format="invalid")
assert "Unsupported audio format" in str(exc.value)
def test_callback_processing():
voice = Voice()
@ -58,13 +66,14 @@ def test_callback_processing():
# Verify data is queued
assert not voice.q.empty()
@patch('aider.voice.litellm')
@patch("aider.voice.litellm")
def test_record_and_transcribe(mock_litellm, mock_soundfile):
voice = Voice()
# Mock the recording process
with patch('prompt_toolkit.shortcuts.prompt'):
with patch('sounddevice.InputStream'):
with patch("prompt_toolkit.shortcuts.prompt"):
with patch("sounddevice.InputStream"):
# Mock the transcription response
mock_litellm.transcription.return_value = Mock(text="Hello, world!")
@ -73,6 +82,7 @@ def test_record_and_transcribe(mock_litellm, mock_soundfile):
assert result == "Hello, world!"
mock_litellm.transcription.assert_called_once()
def test_get_prompt():
voice = Voice()
voice.start_time = voice.start_time = os.times().elapsed
@ -84,7 +94,8 @@ def test_get_prompt():
assert "" in prompt # Should contain some filled blocks
assert "" in prompt # Should contain some empty blocks
@patch('sounddevice.InputStream')
@patch("sounddevice.InputStream")
def test_record_and_transcribe_keyboard_interrupt(mock_stream):
voice = Voice()
mock_stream.side_effect = KeyboardInterrupt()
@ -92,7 +103,8 @@ def test_record_and_transcribe_keyboard_interrupt(mock_stream):
result = voice.record_and_transcribe()
assert result is None
@patch('sounddevice.InputStream')
@patch("sounddevice.InputStream")
def test_record_and_transcribe_device_error(mock_stream):
voice = Voice()
mock_stream.side_effect = SoundDeviceError("Test error")