-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_validator.py
208 lines (172 loc) · 5.84 KB
/
test_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import sys, json
import argparse
import base64
from fastapi.testclient import TestClient
from annotator import app
client = TestClient(app)
#Test 1
#Post audio file, validate that it IS an audio file
def test_is_audio_file():
params = {
"audioInput": "test_data/shakespeare_part1_par1.wav",
"audioInputType": "FILE"
}
response = client.post("/validate", json=params)
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
debug(json.dumps(response.json(), indent=4))
def test_is_audio_file_wrong_format():
params = {
"audioInput": "test_data/shakespeare_part1_par1.wav",
"audioInputType": "FILE",
"audioInputFormat": "MP3"
}
response = client.post("/validate", json=params)
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
debug(json.dumps(response.json(), indent=4))
def test_is_audio_file_undefined_format():
params = {
"audioInput": "test_data/shakespeare_part1_par1.wav",
"audioInputType": "FILE",
"audioInputFormat": "OGG"
}
response = client.post("/validate", json=params)
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
debug(json.dumps(response.json(), indent=4))
#Test 2
#Post encoded audio, validate that it IS audio
def test_is_audio_data():
audiofile = "test_data/shakespeare_part1_par1.wav"
with open(audiofile, "rb") as fh:
audiodata = base64.b64encode(fh.read())
params = {
"audioInput": audiodata
}
response = client.post("/validate", json=params)
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
debug(json.dumps(response.json(), indent=4))
#Test 3
#Test that audio contains speech
def test_audio_file_contains_speech():
params = {
"audioInput": "test_data/shakespeare_part1.wav",
"audioInputType": "FILE"
}
response = client.post("/validate", json=params)
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
debug(json.dumps(response.json(), indent=4))
def test_audio_file_does_not_contain_speech():
params = {
"audioInput": "test_data/silence15.wav",
"audioInputType": "FILE"
}
response = client.post("/validate", json=params)
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
expected_validation = {
"source": "checkAudioContainsSpeech",
"validation": 0,
"message": "Audio appears not to contain speech"
}
assert expected_validation in response.json()["validation"]
debug(json.dumps(response.json(), indent=4))
#Test 4
#Test that length of soundfile and text appears to match
def test_short_audio_short_text():
params = {
"audioInput": "test_data/shakespeare_sent1_phrase1.wav",
"audioInputType": "FILE",
"text": "test_data/shakespeare_sent1_phrase1.txt",
"textInputType": "FILE"
}
response = client.post("/validate", json=params)
debug(response.json())
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
assert int(response.json()["score"]) == 1
debug(json.dumps(response.json(), indent=4))
def test_short_audio_long_text():
params = {
"audioInput": "test_data/shakespeare_sent1_phrase1.wav",
"audioInputType": "FILE",
"text": "test_data/shakespeare_part1.txt",
"textInputType": "FILE"
}
response = client.post("/validate", json=params)
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
assert response.json()["score"] != 1
debug(json.dumps(response.json(), indent=4))
def test_long_audio_long_text():
params = {
"audioInput": "test_data/shakespeare_part1.wav",
"audioInputType": "FILE",
"text": "test_data/shakespeare_part1.txt",
"textInputType": "FILE"
}
response = client.post("/validate", json=params)
debug(response.json())
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
assert int(response.json()["score"]) == 1
debug(json.dumps(response.json(), indent=4))
def test_long_audio_short_text():
params = {
"audioInput": "test_data/shakespeare_part1.wav",
"audioInputType": "FILE",
"text": "test_data/shakespeare_sent1_phrase1.txt",
"textInputType": "FILE"
}
response = client.post("/validate", json=params)
debug(response.json())
if response.status_code != 200:
print(response)
print(response.json())
assert response.status_code == 200
assert int(response.json()["score"]) != 1
debug(json.dumps(response.json(), indent=4))
verbose = False
def debug(msg):
if verbose:
print(msg)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', help='Print verbose output.', action='store_true')
args = parser.parse_args()
verbose = args.verbose
#test_audio_file_does_not_contain_speech()
#sys.exit()
tests = [ m for m in dir() if m.startswith('test_')]
for test in tests:
debug(test)
try:
globals()[test]()
sys.stdout.write(".")
sys.stdout.flush()
debug(f"OK {test}")
except:
print(f"FAIL {test}")
sys.stdout.write("\n")
sys.exit()