Skip to content

Commit 14f9984

Browse files
Add tests for Python websocket server
1 parent 477e18f commit 14f9984

6 files changed

Lines changed: 52 additions & 2 deletions

File tree

python_websocket_server/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,11 @@ DeepSpeech service. The websocket timeout on the ingress is set to 1 hour.
7070
## Contributing
7171

7272
Bug reports and merge requests are welcome.
73+
74+
### Running tests
75+
76+
Navigate to the [`python_websocket_server`](python_websocket_server) directory. Then, run tests by executing:
77+
78+
```
79+
python -m pytest tests/test_app.py
80+
```

python_websocket_server/application.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ server {
1111
threadpool {
1212
count = 5
1313
}
14+
15+
stt_endpoint = "/api/v1/stt"
1416
}

python_websocket_server/deepspeech_server/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async def healthcheck(_):
2727
return response.text("Welcome to DeepSpeech Server!")
2828

2929

30-
@app.websocket("/api/v1/stt")
30+
@app.websocket(conf['server.stt_endpoint'])
3131
async def stt(request, ws):
3232
logger.debug(f"Received {request.method} request at {request.path}")
3333
try:

python_websocket_server/requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ black>=20.8b1
44
ffmpeg-python==0.2.0
55
sanic==20.3.0
66
pyhocon==0.3.54
7-
numpy~=1.17.0
7+
numpy~=1.19.3
8+
pytest-sanic==1.6.2
9+
click~=7.1.2
10+
pytest~=6.1.2
61.8 KB
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import ast
2+
3+
import pytest
4+
from pyhocon import ConfigFactory
5+
from sanic.websocket import WebSocketProtocol
6+
7+
from deepspeech_server.app import app
8+
9+
# Load app configs and initialize DeepSpeech model
10+
conf = ConfigFactory.parse_file("application.conf")
11+
12+
13+
@pytest.fixture
14+
def sanic_server(loop, sanic_client):
15+
return loop.run_until_complete(sanic_client(app, protocol=WebSocketProtocol))
16+
17+
18+
def test_index_returns_200():
19+
request, response = app.test_client.get("/")
20+
assert response.status == 200
21+
22+
23+
async def test_valid_audio(sanic_server):
24+
ws_conn = await sanic_server.ws_connect(conf['server.stt_endpoint'])
25+
with open("tests/experience_proves_this.wav", mode="rb") as file:
26+
audio = file.read()
27+
await ws_conn.send_bytes(audio)
28+
result = await ws_conn.receive()
29+
assert ast.literal_eval(result.data)["text"] == "experience proves this"
30+
31+
32+
async def test_invalid_audio(sanic_server):
33+
ws_conn = await sanic_server.ws_connect(conf['server.stt_endpoint'])
34+
audio = b"this ain't audio"
35+
await ws_conn.send_bytes(audio)
36+
result = await ws_conn.receive()
37+
assert ast.literal_eval(result.data)["message"] == "Something went wrong"

0 commit comments

Comments
 (0)