Skip to content

Commit

Permalink
Add mocks to test microphone input
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybchris committed Apr 18, 2024
1 parent 0d7c1f5 commit 1697682
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions tests/voice/test_microphone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from typing import Any, Dict

import pytest
Expand All @@ -16,28 +17,54 @@
logger = logging.getLogger(__name__)


def mock_query_devices(device: int) -> Dict[str, Any]:
assert device == 0
return {
"name": "<mock-device>",
"index": 0,
"hostapi": 0,
"max_input_channels": 1,
"max_output_channels": 0,
"default_low_input_latency": 0.0326984126984127,
"default_low_output_latency": 0.01,
"default_high_input_latency": 0.04285714285714286,
"default_high_output_latency": 0.1,
"default_samplerate": 44100.0,
}


@pytest.mark.asyncio
@pytest.mark.voice
@pytest.mark.playback
class TestMicrophone:

async def test_create_microphone(self, monkeypatch: pytest.MonkeyPatch) -> None:
async def test_create_microphone_basic(self, monkeypatch: pytest.MonkeyPatch) -> None:
# pylint: disable=unused-argument
def mock_query_devices(device: int = -1) -> Dict[str, Any]:
return {
"name": "<mock-device>",
"index": 0,
"hostapi": 0,
"max_input_channels": 1,
"max_output_channels": 0,
"default_low_input_latency": 0.0326984126984127,
"default_low_output_latency": 0.01,
"default_high_input_latency": 0.04285714285714286,
"default_high_output_latency": 0.1,
"default_samplerate": 44100.0,
}

monkeypatch.setattr(sounddevice, "query_devices", mock_query_devices)
with Microphone.context(device=0):
pass

async def test_create_microphone_zero_channels(self, monkeypatch: pytest.MonkeyPatch) -> None:
match = (
"Selected input device does not have any input channels. \n"
"Please set MicrophoneInterface(device=<YOUR DEVICE ID>). \n"
"Devices:"
)

with pytest.raises(OSError, match=re.escape(match)):
# pylint: disable=unused-argument
def mock_query_devices(device: int = -1) -> Dict[str, Any]:
return {
"name": "<mock-device>",
"index": 0,
"hostapi": 0,
"max_input_channels": 0,
"max_output_channels": 0,
"default_low_input_latency": 0.0326984126984127,
"default_low_output_latency": 0.01,
"default_high_input_latency": 0.04285714285714286,
"default_high_output_latency": 0.1,
"default_samplerate": 44100.0,
}

monkeypatch.setattr(sounddevice, "query_devices", mock_query_devices)
with Microphone.context(device=0):
pass

0 comments on commit 1697682

Please sign in to comment.