Skip to content

Commit 63c8039

Browse files
author
Benjamin Moody
committed
Add a test case for reading FLAC files over HTTP.
We want to be sure that the soundfile library is compatible with wfdb.io._url.NetFile, since wfdb.io._signal._rd_compressed_file relies on this. This isn't a complete test of WFDB/FLAC functionality, in part because the sample files are very small so we can't really exercise the seeking functionality. However, this should be sufficient to verify that soundfile doesn't make silly assumptions (like assuming the input file corresponds to an OS file descriptor.)
1 parent f71f2a5 commit 63c8039

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/test_url.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,62 @@ def _test_binary(self, url, content, buffering):
196196
self.assertEqual(bf.tell(), len(content))
197197

198198

199+
class TestRemoteFLACFiles(unittest.TestCase):
200+
"""
201+
Test reading FLAC files over HTTP.
202+
"""
203+
204+
def test_whole_file(self):
205+
"""
206+
Test reading a complete FLAC file using local and HTTP APIs.
207+
208+
This tests that we can read the file 'sample-data/flacformats.d2'
209+
(a 24-bit FLAC stream) using the soundfile library, first by
210+
reading the file from the local filesystem, and then using
211+
wfdb.io._url.openurl() to access it through a simulated web server.
212+
213+
This is meant to verify that the soundfile library works using only
214+
the standard Python file object API (as implemented by
215+
wfdb.io._url.NetFile), and doesn't require the input file to be an
216+
actual io.FileIO object.
217+
218+
Parameters
219+
----------
220+
N/A
221+
222+
Returns
223+
-------
224+
N/A
225+
226+
"""
227+
import soundfile
228+
import numpy as np
229+
230+
data_file_path = "sample-data/flacformats.d2"
231+
expected_format = "FLAC"
232+
expected_subtype = "PCM_24"
233+
234+
# Read the file using standard file I/O
235+
sf1 = soundfile.SoundFile(data_file_path)
236+
self.assertEqual(sf1.format, expected_format)
237+
self.assertEqual(sf1.subtype, expected_subtype)
238+
data1 = sf1.read()
239+
240+
# Read the file using HTTP
241+
with open(data_file_path, "rb") as f:
242+
file_content = {"/foo.dat": f.read()}
243+
with DummyHTTPServer(file_content) as server:
244+
url = server.url("/foo.dat")
245+
file2 = wfdb.io._url.openurl(url, "rb")
246+
sf2 = soundfile.SoundFile(file2)
247+
self.assertEqual(sf2.format, expected_format)
248+
self.assertEqual(sf2.subtype, expected_subtype)
249+
data2 = sf2.read()
250+
251+
# Check that results are equal
252+
np.testing.assert_array_equal(data1, data2)
253+
254+
199255
class DummyHTTPServer(http.server.HTTPServer):
200256
"""
201257
HTTPServer used to simulate a web server for testing.

0 commit comments

Comments
 (0)