From 49f13f4834af54dc3c2d983f3a6dc13374e376a4 Mon Sep 17 00:00:00 2001 From: ganshihao21 <1410815216@qq.com> Date: Wed, 3 Jul 2024 23:39:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E4=BB=A5=E6=A3=80=E6=B5=8B=E8=AF=AD=E9=9F=B3=E5=8A=A9?= =?UTF-8?q?=E6=89=8B=E5=94=A4=E9=86=92=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text/test_snowboydecoder.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/voice_assistant/text/test_snowboydecoder.py diff --git a/src/voice_assistant/text/test_snowboydecoder.py b/src/voice_assistant/text/test_snowboydecoder.py new file mode 100644 index 0000000..445a3f9 --- /dev/null +++ b/src/voice_assistant/text/test_snowboydecoder.py @@ -0,0 +1,56 @@ +import unittest +from unittest.mock import Mock, patch +from snowboydecoder import HotwordDetector + +# 假设 RESOURCE_FILE 是在 snowboydecoder.py 中定义的 +RESOURCE_FILE = 'path/to/resource.res' + +class TestHotwordDetector(unittest.TestCase): + + @patch('snowboydecoder.snowboydetect.SnowboyDetect') + @patch('snowboydecoder.pyaudio.PyAudio') + def test_init(self, mock_pyaudio, mock_snowboydetect): + # Mock SnowboyDetect + mock_detector = mock_snowboydetect.return_value + mock_detector.NumChannels.return_value = 1 + mock_detector.SampleRate.return_value = 16000 + mock_detector.BitsPerSample.return_value = 16 + mock_detector.NumHotwords.return_value = 1 + + # Mock PyAudio + mock_audio = mock_pyaudio.return_value + + # Mock audio stream + mock_stream = Mock() + mock_audio.open.return_value = mock_stream + + # Initialize HotwordDetector + decoder_model = 'model_id' + sensitivity = 0.5 + audio_gain = 1.0 + detector = HotwordDetector(decoder_model, RESOURCE_FILE, sensitivity, audio_gain) + + # Verify interactions + mock_snowboydetect.assert_called_once_with( + resource_filename=RESOURCE_FILE.encode(), + model_str='model_id'.encode() + ) + mock_detector.SetAudioGain.assert_called_once_with(audio_gain) + mock_detector.SetSensitivity.assert_called_once_with('0.5'.encode()) + + # Verify PyAudio and Stream configurations + mock_pyaudio.assert_called_once() + mock_audio.open.assert_called_once_with( + input=True, output=False, + format=mock_audio.get_format_from_width(2).return_value, + channels=1, + rate=16000, + frames_per_buffer=2048, + stream_callback=detector.audio_callback + ) + + # Verify RingBuffer size + self.assertEqual(len(detector.ring_buffer), 0) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file