This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
test辨識介面單元試驗.py
77 lines (65 loc) · 2.48 KB
/
test辨識介面單元試驗.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
from base64 import b64encode
import io
import json
from os.path import abspath, dirname, join
from django.test.testcases import TestCase
from django.urls.base import resolve
from 臺灣言語服務.Kaldi介面 import Kaldi辨識
from 臺灣言語工具.語音辨識.聲音檔 import 聲音檔
from 臺灣言語服務.KaldiModels import Kaldi辨識結果
class 辨識介面單元試驗(TestCase):
def setUp(self):
self.音檔 = 聲音檔.對參數轉(2, 16000, 1, b'sui2')
self.blob = b64encode(
json.dumps(list(self.音檔.wav格式資料()))[1:-1].encode('utf-8')
).decode('utf-8')
def test_有對應函式(self):
對應 = resolve('/辨識音檔')
self.assertEqual(對應.func, Kaldi辨識)
def test_成功(self):
回應 = self.client.post('/辨識音檔', {
'語言': '台語',
'blob': self.blob,
})
self.assertEqual(回應.status_code, 200)
def test_blob有音檔(self):
self.client.post('/辨識音檔', {
'語言': '台語',
'blob': self.blob,
})
self.assertEqual(
Kaldi辨識結果.objects.get().聲音檔().wav格式資料(),
self.音檔.wav格式資料()
)
def test_file有音檔(self):
with io.BytesIO(self.音檔.wav格式資料()) as 音檔:
self.client.post('/辨識音檔', {
'語言': '台語',
'音檔': 音檔,
})
self.assertEqual(
Kaldi辨識結果.objects.get().聲音檔().wav格式資料(),
self.音檔.wav格式資料()
)
def test_短檔毋是wav(self):
with io.BytesIO(b'sui2') as 音檔:
回應 = self.client.post('/辨識音檔', {
'語言': '台語',
'音檔': 音檔,
})
self.assertEqual(回應.status_code, 400)
def test_長檔mp3(self):
with open(
join(dirname(abspath(__file__)), 'imtong', 'sui2.mp3'), 'rb'
) as mp3_tong:
with io.BytesIO(mp3_tong.read()) as 音檔:
self.client.post('/辨識音檔', {
'語言': '台語',
'音檔': 音檔,
})
Kaldi辨識結果.objects.get().聲音檔().wav格式資料()
def test_音檔佮blob攏無傳(self):
回應 = self.client.post('/辨識音檔', {
'語言': '台語',
})
self.assertEqual(回應.status_code, 400)