|
| 1 | +import os, json, array, pytest, mock |
| 2 | +import auroraapi |
| 3 | +from auroraapi.globals import _config |
| 4 | +from auroraapi.api import APIException |
| 5 | +from auroraapi.audio import * |
| 6 | +from auroraapi.text import Text |
| 7 | +from auroraapi.speech import * |
| 8 | + |
| 9 | +def mock_pyaudio_record(a, b): |
| 10 | + with open("tests/assets/hw.wav", "rb") as f: |
| 11 | + yield array.array('h', AudioFile(f.read()).audio.raw_data) |
| 12 | + |
| 13 | +class TestSpeech(object): |
| 14 | + def setup(self): |
| 15 | + try: |
| 16 | + _config.app_id = os.environ["APP_ID"] |
| 17 | + _config.app_token = os.environ["APP_TOKEN"] |
| 18 | + _config.device_id = os.environ["DEVICE_ID"] |
| 19 | + except: |
| 20 | + pass |
| 21 | + |
| 22 | + def teardown(self): |
| 23 | + _config.app_id = None |
| 24 | + _config.app_token = None |
| 25 | + _config.device_id = None |
| 26 | + |
| 27 | + def test_create_no_argument(self): |
| 28 | + with pytest.raises(TypeError): |
| 29 | + Speech() |
| 30 | + |
| 31 | + def test_create_none(self): |
| 32 | + with pytest.raises(TypeError): |
| 33 | + Speech(None) |
| 34 | + |
| 35 | + def test_create_wrong_type(self): |
| 36 | + with pytest.raises(TypeError): |
| 37 | + Speech("string") |
| 38 | + |
| 39 | + def test_create(self): |
| 40 | + with open("tests/assets/hw.wav", "rb") as f: |
| 41 | + Speech(AudioFile(f.read())) |
| 42 | + |
| 43 | + def test_text(self): |
| 44 | + with open("tests/assets/hw.wav", "rb") as f: |
| 45 | + s = Speech(AudioFile(f.read())) |
| 46 | + t = s.text() |
| 47 | + |
| 48 | + assert isinstance(t, Text) |
| 49 | + assert t.text.lower().strip() == "hello world" |
| 50 | + |
| 51 | +class TestSpeechNoCreds(object): |
| 52 | + def test_text(self): |
| 53 | + with pytest.raises(APIException): |
| 54 | + with open("tests/assets/hw.wav", "rb") as f: |
| 55 | + Speech(AudioFile(f.read())).text() |
| 56 | + |
| 57 | +class TestListen(object): |
| 58 | + def setup(self): |
| 59 | + with open("tests/assets/hw.wav", "rb") as f: |
| 60 | + self.audio_file = AudioFile(f.read()) |
| 61 | + try: |
| 62 | + _config.app_id = os.environ["APP_ID"] |
| 63 | + _config.app_token = os.environ["APP_TOKEN"] |
| 64 | + _config.device_id = os.environ["DEVICE_ID"] |
| 65 | + except: |
| 66 | + pass |
| 67 | + |
| 68 | + def teardown(self): |
| 69 | + _config.app_id = None |
| 70 | + _config.app_token = None |
| 71 | + _config.device_id = None |
| 72 | + |
| 73 | + def test_listen(self): |
| 74 | + with mock.patch('auroraapi.audio._pyaudio_record', new=mock_pyaudio_record): |
| 75 | + s = listen() |
| 76 | + assert isinstance(s, Speech) |
| 77 | + assert isinstance(s.audio, AudioFile) |
| 78 | + assert len(self.audio_file.audio) == len(s.audio.audio) |
| 79 | + |
| 80 | + def test_continuously_listen(self): |
| 81 | + with mock.patch('auroraapi.audio._pyaudio_record', new=mock_pyaudio_record): |
| 82 | + for s in continuously_listen(): |
| 83 | + assert isinstance(s, Speech) |
| 84 | + assert isinstance(s.audio, AudioFile) |
| 85 | + assert len(self.audio_file.audio) == len(s.audio.audio) |
| 86 | + break |
| 87 | + |
| 88 | + def test_listen_and_transcribe(self): |
| 89 | + with mock.patch('auroraapi.audio._pyaudio_record', new=mock_pyaudio_record): |
| 90 | + t = listen_and_transcribe() |
| 91 | + assert isinstance(t, Text) |
| 92 | + assert t.text.lower().strip() == "hello world" |
| 93 | + |
| 94 | + def test_continuously_listen_and_transcribe(self): |
| 95 | + with mock.patch('auroraapi.audio._pyaudio_record', new=mock_pyaudio_record): |
| 96 | + for t in continuously_listen_and_transcribe(): |
| 97 | + assert isinstance(t, Text) |
| 98 | + assert t.text.lower().strip() == "hello world" |
| 99 | + break |
0 commit comments