Skip to content

Commit 6903fa2

Browse files
committed
add unit tests
1 parent ef5b70b commit 6903fa2

7 files changed

Lines changed: 401 additions & 0 deletions

File tree

tests/assets/hw.wav

38.6 KB
Binary file not shown.

tests/test_api.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import json, pytest
2+
from auroraapi.api import *
3+
from auroraapi.globals import _config
4+
5+
class MockResponse(object):
6+
def __init__(self):
7+
self.status_code = 200
8+
self.headers = {}
9+
self.text = ""
10+
11+
def json(self):
12+
return json.loads(self.text)
13+
14+
class TestAPIException(object):
15+
def test_create(self):
16+
e = APIException("id","status","code","type","message")
17+
assert isinstance(e, APIException)
18+
assert e.id == "id"
19+
assert e.status == "status"
20+
assert e.code == "code"
21+
assert e.type == "type"
22+
assert e.message == "message"
23+
24+
def test_str(self):
25+
e = APIException("id","status","code","type","message")
26+
assert str(e) == "[{}] {}".format(e.code, e.message)
27+
28+
def test_str_no_code(self):
29+
e = APIException("id","status",None,"type","message")
30+
assert str(e) == "[{}] {}".format(e.status, e.message)
31+
32+
def test_repr(self):
33+
e = APIException("id","status","code","type","message")
34+
j = json.loads(repr(e))
35+
assert j["id"] == e.id
36+
assert j["status"] == e.status
37+
assert j["code"] == e.code
38+
assert j["type"] == e.type
39+
assert j["message"] == e.message
40+
41+
class TestAPIUtils(object):
42+
def setup(self):
43+
_config.app_id = "appid"
44+
_config.app_token = "apptoken"
45+
46+
def teardown(self):
47+
_config.app_id = None
48+
_config.app_token = None
49+
50+
def test_get_headers(self):
51+
h = get_headers()
52+
assert h["X-Application-ID"] == _config.app_id
53+
assert h["X-Application-Token"] == _config.app_token
54+
assert h["X-Device-ID"] == _config.device_id
55+
56+
def test_handle_error_no_error(self):
57+
handle_error(MockResponse())
58+
59+
def test_handle_error_json(self):
60+
r = MockResponse()
61+
r.status_code = 400
62+
r.headers = { "content-type": "application/json" }
63+
r.text = json.dumps({
64+
"id": "id",
65+
"code": "MissingApplicationIDHeader",
66+
"type": "BadRequest",
67+
"status": 400,
68+
"message": "message"
69+
})
70+
71+
with pytest.raises(APIException) as e:
72+
handle_error(r)
73+
assert e.id == "id"
74+
assert e.code == "MissingApplicationIDHeader"
75+
assert e.type == "BadRequest"
76+
assert e.status == 400
77+
assert e.message == "message"
78+
79+
def test_handle_error_413(self):
80+
r = MockResponse()
81+
r.status_code = 413
82+
r.headers["content-type"] = "text/html"
83+
r.text = "Request entity too large"
84+
85+
with pytest.raises(APIException) as e:
86+
handle_error(r)
87+
assert e.id == None
88+
assert e.status == 413
89+
assert e.type == "RequestEntityTooLarge"
90+
assert e.code == "RequestEntityTooLarge"
91+
assert e.message == "Request entity too large"
92+
93+
def test_handle_error_other(self):
94+
r = MockResponse()
95+
r.status_code = 503
96+
r.headers["content-type"] = "text/html"
97+
r.text = "Service unavailable"
98+
99+
100+
with pytest.raises(APIException) as e:
101+
handle_error(r)
102+
assert e.id == None
103+
assert e.status == 413
104+
assert e.type == None
105+
assert e.code == None
106+
assert e.message == r.text
107+
assert str(e) == "[{}] {}".format(r.status_code, r.text)

tests/test_audio.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import pytest, os, tempfile
2+
from auroraapi.audio import *
3+
4+
class TestAudioFile(object):
5+
def test_create(self):
6+
with open("tests/assets/hw.wav", "rb") as f:
7+
d = f.read()
8+
a = AudioFile(d)
9+
assert isinstance(a, AudioFile)
10+
assert len(a.audio.raw_data) + 44 == len(d)
11+
12+
def test_write_to_file(self):
13+
path = os.path.join(tempfile.gettempdir(), "out.wav")
14+
with open("tests/assets/hw.wav", "rb") as f:
15+
d1 = f.read()
16+
a1 = AudioFile(d1)
17+
a1.write_to_file(path)
18+
19+
with open(path, "rb") as f:
20+
d2 = f.read()
21+
a2 = AudioFile(d2)
22+
assert d2 == d1
23+
assert a1.audio == a2.audio
24+
25+
os.remove(path)
26+
27+
def test_get_wav(self):
28+
with open("tests/assets/hw.wav", "rb") as f:
29+
d = f.read()
30+
a = AudioFile(d)
31+
w = a.get_wav()
32+
assert d == w
33+
34+
def test_pad(self):
35+
with open("tests/assets/hw.wav", "rb") as f:
36+
d = f.read()
37+
a = AudioFile(d)
38+
# adds 1s of padding to each side (2 x (16000 Hz * 16 bits/sample * 1 byte/8bits))
39+
pa = a.pad(1)
40+
w = pa.get_wav()
41+
assert len(w) == len(d) + 64000
42+
assert w[-1] == 0
43+
assert w[44] == 0
44+
assert w[44 + 32000] == d[44]
45+
46+
def test_pad_left(self):
47+
with open("tests/assets/hw.wav", "rb") as f:
48+
d = f.read()
49+
a = AudioFile(d)
50+
# adds 1s of padding to left side (1 x (16000 Hz * 16 bits/sample * 1 byte/8bits))
51+
pa = a.pad_left(1)
52+
w = pa.get_wav()
53+
assert len(w) == len(d) + 32000
54+
assert w[-1] == d[-1]
55+
assert w[44] == 0
56+
assert w[44 + 32000] == d[44]
57+
58+
def test_pad_right(self):
59+
with open("tests/assets/hw.wav", "rb") as f:
60+
d = f.read()
61+
a = AudioFile(d)
62+
# adds 1s of padding to right side (1 x (16000 Hz * 16 bits/sample * 1 byte/8bits))
63+
pa = a.pad_right(1)
64+
w = pa.get_wav()
65+
assert len(w) == len(d) + 32000
66+
assert w[-1] == 0
67+
assert w[44] == d[44]
68+
assert w[-32000] == 0
69+
70+
def test_trim_silent(self):
71+
with open("tests/assets/hw.wav", "rb") as f:
72+
d = f.read()
73+
a = AudioFile(d)
74+
t = a.trim_silent()
75+
# TODO: actually add a test here. for now, it just checks for compilation
76+
77+
# def test_play(self):
78+
79+

tests/test_globals.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from auroraapi.globals import Config
2+
3+
class TestGlobals(object):
4+
def test_create_config(self):
5+
c = Config()
6+
assert isinstance(c, Config)
7+
assert c != None
8+
9+
def test_assign_config(self):
10+
c = Config()
11+
c.app_id = "test"
12+
c.app_token = "test123"
13+
14+
assert c.app_id == "test"
15+
assert c.app_token == "test123"
16+
assert c.device_id == None

tests/test_interpret.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
from auroraapi.interpret import Interpret
3+
4+
class TestInterpret(object):
5+
def test_create_no_arguments(self):
6+
with pytest.raises(TypeError):
7+
Interpret()
8+
9+
def test_create_wrong_type(self):
10+
with pytest.raises(TypeError):
11+
Interpret("test")
12+
13+
def test_create(self):
14+
d = { "intent": "test", "entities": {} }
15+
i = Interpret(d)
16+
assert isinstance(i, Interpret)
17+
assert i.intent == "test"
18+
assert len(i.entities) == 0
19+
20+
d = { "intent": "test", "entities": { "abc": "123" } }
21+
i = Interpret(d)
22+
assert isinstance(i, Interpret)
23+
assert i.intent == "test"
24+
assert len(i.entities) == 1
25+
assert i.entities["abc"] == "123"

tests/test_speech.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)