-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
109 lines (99 loc) · 4.3 KB
/
Copy pathtest_api.py
File metadata and controls
109 lines (99 loc) · 4.3 KB
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
import io
import json
import struct
import unittest
import api
class TestApi(unittest.TestCase):
def setUp(self):
api.app.config['TESTING'] = True
self.app = api.app.test_client()
def test_upload_audiofile(self):
response = self.app.post(
'/audiofiles',
content_type='multipart/form-data',
data={'file': (io.BytesIO(b'mockdata'), 'mockname.bin')}
)
response = json.loads(response.data.decode('utf-8'))
self.assertIn('self', response['_links'])
self.assertIn('audiofiles', response['_links'])
self.assertEqual('mockname.bin', response['name'])
self.assertEqual(8, response['length'])
def test_retrieve_audiofile(self):
response = self.app.post(
'/audiofiles',
content_type='multipart/form-data',
data={'file': (io.BytesIO(b'mockdata'), 'mockname.bin')}
)
response = json.loads(response.data.decode('utf-8'))
response = self.app.get(response['_links']['self']['href'])
response = json.loads(response.data.decode('utf-8'))
self.assertIn('self', response['_links'])
self.assertIn('audiofiles', response['_links'])
self.assertEqual('mockname.bin', response['name'])
self.assertEqual(8, response['length'])
def test_retrieve_audiofile_data(self):
response = self.app.post(
'/audiofiles',
content_type='multipart/form-data',
data={'file': (io.BytesIO(b'mockdata'), 'mockname.bin')}
)
response = json.loads(response.data.decode('utf-8'))
response = self.app.get(
response['_links']['self']['href'],
headers={'Accept': 'application/octet-stream'}
)
self.assertEqual(b'mockdata', response.data)
def test_list_audiofiles(self):
response = self.app.get('/audiofiles')
response = json.loads(response.data.decode('utf-8'))
response = self.app.post(
response['_links']['upload']['href'],
content_type='multipart/form-data',
data={'file': (io.BytesIO(b'mockdata1'), 'mockname1.bin')}
)
response = json.loads(response.data.decode('utf-8'))
response = self.app.get(response['_links']['audiofiles']['href'])
response = json.loads(response.data.decode('utf-8'))
response = self.app.post(
response['_links']['upload']['href'],
content_type='multipart/form-data',
data={'file': (io.BytesIO(b'mockdata2'), 'mockname2.bin')}
)
response = json.loads(response.data.decode('utf-8'))
response = self.app.get(response['_links']['audiofiles']['href'])
response = json.loads(response.data.decode('utf-8'))
self.assertIn('self', response['_links'])
self.assertIn('upload', response['_links'])
self.assertEqual(2, response['count'])
self.assertEqual(2, len(response['_embedded']['audiofile']))
audiofile1 = next(
audiofile for audiofile in response['_embedded']['audiofile']
if audiofile['name'] == 'mockname1.bin'
)
self.assertIn('self', audiofile1['_links'])
self.assertEqual(9, audiofile1['length'])
audiofile2 = next(
audiofile for audiofile in response['_embedded']['audiofile']
if audiofile['name'] == 'mockname2.bin'
)
self.assertIn('self', audiofile2['_links'])
self.assertEqual(9, audiofile2['length'])
def test_retrieve_audiodata(self):
audiofile = (
struct.pack('<4sI4s', b'RIFF', 40, b'WAVE') +
struct.pack('<4sIHHIIHH', b'fmt ', 16, 1, 1, 44100, 88200, 2, 16) +
struct.pack('<4sI4s', b'data', 4, b'\x45\x06\xcf\x73')
)
response = self.app.post(
'/audiofiles',
content_type='multipart/form-data',
data={'file': (io.BytesIO(audiofile), 'audio.wav')}
)
response = json.loads(response.data.decode('utf-8'))
response = self.app.get(response['_links']['audiodata']['href'])
response = json.loads(response.data.decode('utf-8'))
self.assertEqual(44100, response['samplerate'])
self.assertEqual(1, response['channels'])
self.assertEqual(16, response['bitsPerSample'])
if __name__ == '__main__':
unittest.main()