Skip to content

Commit

Permalink
[unit tests] switched to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
janscience committed Apr 15, 2024
1 parent d091784 commit 7292765
Show file tree
Hide file tree
Showing 12 changed files with 569 additions and 539 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/runchecks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ jobs:
pip install pyaudio
pip install sounddevice
pip install soundcard
pip install nose nose-cov
pip install codecov
pip install pytest pytest-cov
pip install pdoc3
pip install mkdocs
pip install -r requirements.txt
Expand All @@ -36,7 +35,7 @@ jobs:
shell: bash

- name: Run tests
run: nosetests -vv --with-coverage --cover-package=audioio
run: pytest -v --cov-report html:cover --cov-report term --cov audioio tests/
shell: bash

- name: Build documentation
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/uploaddocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ jobs:
pip install pyaudio
pip install sounddevice
pip install soundcard
pip install nose nose-cov
pip install codecov
pip install pytest pytest-cov
pip install pdoc3
pip install mkdocs
pip install genbadge[coverage]
pip install -r requirements.txt
- name: Run tests
run: nosetests -vv --with-coverage --cover-html --cover-xml --cover-package=audioio
run: pytest -v --cov-report html:cover --cov-report term --cov audioio tests/
shell: bash

- name: Build documentation
Expand Down
2 changes: 1 addition & 1 deletion build-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PACKAGEROOT="$(dirname "$(realpath "$0")")"
BUILDROOT="$PACKAGEROOT/site"

# check for code coverage report:
# need to call nosetest with --with-coverage --cover-html --cover-xml
# need to call pytest --cov-report html:cover --cov-report term --cov audioio tests/
HAS_COVER=false
test -d cover && HAS_COVER=true

Expand Down
52 changes: 33 additions & 19 deletions tests/test_audioconverter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nose.tools import assert_equal, assert_greater, assert_greater_equal, assert_less, assert_raises
import pytest
import os
import shutil
import numpy as np
Expand Down Expand Up @@ -26,20 +26,31 @@ def test_main():
destpath = 'test3'
os.mkdir(destpath)
write_audio_file(filename)
assert_raises(SystemExit, ac.main, '-h')
assert_raises(SystemExit, ac.main, '--help')
assert_raises(SystemExit, ac.main, '--version')
with pytest.raises(SystemExit):
ac.main()
with pytest.raises(SystemExit):
ac.main('-h')
with pytest.raises(SystemExit):
ac.main('--help')
with pytest.raises(SystemExit):
ac.main('--version')
ac.main('-l')
ac.main('-f', 'wav', '-l')
ac.main('-f', 'wav', '-o', destfile, filename)
assert_raises(SystemExit, ac.main)
assert_raises(SystemExit, ac.main, '')
assert_raises(SystemExit, ac.main, '-f', 'xxx', '-l')
assert_raises(SystemExit, ac.main, '-f', 'xxx', '-o', destfile, filename)
assert_raises(SystemExit, ac.main, '-o', 'test.xxx', filename)
assert_raises(SystemExit, ac.main, '-f', 'xyz123', filename)
assert_raises(SystemExit, ac.main, filename)
assert_raises(SystemExit, ac.main, '-o', filename, filename)
with pytest.raises(SystemExit):
ac.main('')
with pytest.raises(SystemExit):
ac.main('-f', 'xxx', '-l')
with pytest.raises(SystemExit):
ac.main('-f', 'xxx', '-o', destfile, filename)
with pytest.raises(SystemExit):
ac.main('-o', 'test.xxx', filename)
with pytest.raises(SystemExit):
ac.main('-f', 'xyz123', filename)
with pytest.raises(SystemExit):
ac.main(filename)
with pytest.raises(SystemExit):
ac.main('-o', filename, filename)
ac.main('-o', destfile + '.wav', filename)
ac.main('-f', 'wav', '-o', destfile, filename)
ac.main('-u', '-f', 'wav', '-o', destfile, filename)
Expand All @@ -64,11 +75,14 @@ def test_main():
ac.main('-c', '1', '-o', destfile + '.wav', filename1)
ac.main('-c', '0-2', '-o', destfile + '.wav', filename1)
ac.main('-c', '0-1,3', '-o', destfile + '.wav', filename1)
assert_raises(SystemExit, ac.main, '-o', destfile + '.wav', filename, filename1)
with pytest.raises(SystemExit):
ac.main('-o', destfile + '.wav', filename, filename1)
write_audio_file(filename1, 2, 20000)
assert_raises(SystemExit, ac.main, '-o', destfile + '.wav', filename, filename1)
with pytest.raises(SystemExit):
ac.main('-o', destfile + '.wav', filename, filename1)
write_audio_file(filename1)
assert_raises(SystemExit, ac.main, '-n', '1', '-o', destfile, filename, filename1)
with pytest.raises(SystemExit):
ac.main('-n', '1', '-o', destfile, filename, filename1)
ac.main('-n', '1', '-f', 'wav', '-o', destfile, filename, filename1)
shutil.rmtree(destfile)
ac.main('-vv', '-o', destfile + '.wav', filename, filename1)
Expand All @@ -77,17 +91,17 @@ def test_main():
xdata, xrate = al.load_audio(filename1)
n += len(xdata)
xdata, xrate = al.load_audio(destfile + '.wav')
assert_equal(len(xdata), n, 'len of merged files')
assert len(xdata) == n, 'len of merged files'
md1 = al.metadata(filename)
md1['CodingHistory'] = 'A=PCM,F=44100,W=16,M=stereo,T=test.wav\nA=PCM,F=44100,W=16,M=stereo,T=test2.wav'
md2 = al.metadata(destfile + '.wav')
md2['CodingHistory'] = md2['CodingHistory'].replace('\r\n', '\n')
assert_equal(md1, md2, 'metadata of merged files')
assert md1 == md2, 'metadata of merged files'
ac.main('-d', '4', '-o', destfile + '.wav', filename)
xdata, xrate = al.load_audio(filename)
ydata, yrate = al.load_audio(destfile + '.wav')
assert_equal(len(ydata), len(xdata)//4, 'decimation data')
assert_equal(yrate*4, xrate, 'decimation rate')
assert len(ydata) == len(xdata)//4, 'decimation data'
assert yrate*4 == xrate, 'decimation rate'
ac.main('-o', 'test{Num}.wav', filename)
os.remove('test42.wav')
os.remove(filename)
Expand Down
114 changes: 61 additions & 53 deletions tests/test_audioloader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nose.tools import assert_true, assert_false, assert_equal, assert_raises
import pytest
import os
import numpy as np
import audioio.audiowriter as aw
Expand Down Expand Up @@ -32,18 +32,17 @@ def test_single_frame():
am.select_module(lib)
full_data, rate = al.load_audio(filename, verbose=4)
with al.AudioLoader(filename, 5.0, 2.0, verbose=4) as data:
assert_false(np.any(np.abs(full_data[0] - data[0]) > tolerance), 'first frame access failed with %s module' % lib)
assert_false(np.any(np.abs(full_data[-1] - data[-1]) > tolerance), 'last frame access failed with %s module' % lib)
def access_end(n):
x = data[len(data)+n]
assert not np.any(np.abs(full_data[0] - data[0]) > tolerance), 'first frame access failed with %s module' % lib
assert not np.any(np.abs(full_data[-1] - data[-1]) > tolerance), 'last frame access failed with %s module' % lib
for n in range(10):
assert_raises(IndexError, access_end, n)
with pytest.raises(IndexError):
x = data[len(data)+n]
failed = -1
for inx in np.random.randint(-len(data), len(data), ntests):
if np.any(np.abs(full_data[inx] - data[inx]) > tolerance):
failed = inx
break
assert_true(failed < 0, 'single random frame access failed at index %d with %s module' % (failed, lib))
assert failed < 0, 'single random frame access failed at index %d with %s module' % (failed, lib)
os.remove(filename)
am.enable_module()

Expand All @@ -63,17 +62,17 @@ def test_slice():
full_data, rate = al.load_audio(filename, verbose=4)
with al.AudioLoader(filename, 5.0, 2.0, verbose=4) as data:
for n in range(5):
assert_false(np.any(np.abs(data[:n]-full_data[:n]) > tolerance), 'zero slice up to %d does not match' % n)
assert not np.any(np.abs(data[:n]-full_data[:n]) > tolerance), 'zero slice up to %d does not match' % n
for n in range(1, 5):
assert_false(np.any(np.abs(data[:50:n]-full_data[:50:n]) > tolerance), 'step slice with step=%d does not match' % n)
assert not np.any(np.abs(data[:50:n]-full_data[:50:n]) > tolerance), 'step slice with step=%d does not match' % n
for time in [0.1, 1.5, 2.0, 5.5, 8.0]:
nframes = int(time*data.samplerate)
failed = -1
for inx in np.random.randint(0, len(data)-nframes, ntests):
if np.any(np.abs(full_data[inx:inx+nframes] - data[inx:inx+nframes]) > tolerance):
failed = inx
break
assert_true(failed < 0, 'random frame slice access failed at index %d with nframes=%d and %s module' % (failed, nframes, lib))
assert failed < 0, 'random frame slice access failed at index %d with nframes=%d and %s module' % (failed, nframes, lib)
os.remove(filename)
am.enable_module()

Expand All @@ -100,7 +99,7 @@ def test_forward():
if np.any(np.abs(full_data[inx:inx+nframes] - data[inx:inx+nframes]) > tolerance):
failed = inx
break
assert_true(failed < 0, 'frame slice access forward failed at index %d with nframes=%d and %s module' % (failed, nframes, lib))
assert failed < 0, 'frame slice access forward failed at index %d with nframes=%d and %s module' % (failed, nframes, lib)
os.remove(filename)
am.enable_module()

Expand Down Expand Up @@ -128,7 +127,7 @@ def test_backward():
if np.any(np.abs(full_data[inx:inx+nframes] - data[inx:inx+nframes]) > tolerance):
failed = inx
break
assert_true(failed < 0, 'frame slice access backward failed at index %d with nframes=%d and %s module' % (failed, nframes, lib))
assert failed < 0, 'frame slice access backward failed at index %d with nframes=%d and %s module' % (failed, nframes, lib)
os.remove(filename)
am.enable_module()

Expand All @@ -155,7 +154,7 @@ def test_negative():
if np.any(np.abs(full_data[-inx:-inx+nframes] - data[-inx:-inx+nframes]) > tolerance):
failed = -inx
break
assert_true(failed < 0, 'frame slice access backward with negative indices failed at index %d with nframes=%d and %s module' % (failed, nframes, lib))
assert failed < 0, 'frame slice access backward with negative indices failed at index %d with nframes=%d and %s module' % (failed, nframes, lib)
os.remove(filename)
am.enable_module()

Expand Down Expand Up @@ -188,7 +187,7 @@ def test_multiple():
if np.any(np.abs(full_data[inx] - data[inx]) > tolerance):
failed = 1
break
assert_equal(failed, -1, ('multiple random frame access failed with %s module at indices ' % lib) + str(inx))
assert failed == -1, ('multiple random frame access failed with %s module at indices ' % lib) + str(inx)
os.remove(filename)
am.enable_module()

Expand All @@ -200,7 +199,8 @@ def test_modules():
for lib, load_file in al.audio_loader_funcs:
print(lib)
am.disable_module(lib)
assert_raises(ImportError, load_file, filename)
with pytest.raises(ImportError):
load_file(filename)
data = al.AudioLoader(verbose=4)
load_funcs = {
'soundfile': data.open_soundfile,
Expand All @@ -211,7 +211,8 @@ def test_modules():
}
if lib not in load_funcs:
continue
assert_raises(ImportError, load_funcs[lib], filename, 10.0, 2.0)
with pytest.raises(ImportError):
load_funcs[lib](filename, 10.0, 2.0)
if am.select_module(lib):
# check double opening:
load_funcs[lib](filename)
Expand All @@ -223,20 +224,28 @@ def test_modules():

def test_audio_files():
am.enable_module()
assert_raises(ValueError, al.load_audio, '')
assert_raises(FileNotFoundError, al.load_audio, 'xxx.wav')
assert_raises(ValueError, al.AudioLoader, '')
assert_raises(FileNotFoundError, al.AudioLoader, 'xxx.wav')
with pytest.raises(ValueError):
al.load_audio('')
with pytest.raises(ValueError):
al.AudioLoader('')
with pytest.raises(FileNotFoundError):
al.load_audio('xxx.wav')
with pytest.raises(FileNotFoundError):
al.AudioLoader('xxx.wav')
filename = 'test.wav'
df = open(filename, 'w')
df.close()
assert_raises(EOFError, al.load_audio, filename)
assert_raises(EOFError, al.AudioLoader, filename)
with pytest.raises(EOFError):
al.load_audio(filename)
with pytest.raises(EOFError):
al.AudioLoader(filename)
os.remove(filename)
write_audio_file(filename)
am.disable_module()
assert_raises(IOError, al.load_audio, filename)
assert_raises(IOError, al.AudioLoader, filename)
with pytest.raises(IOError):
al.load_audio(filename)
with pytest.raises(IOError):
al.AudioLoader(filename)
os.remove(filename)
am.enable_module()

Expand All @@ -249,7 +258,7 @@ def test_iter():
tolerance = 2.0**(-15)
with al.AudioLoader(filename, 0.2) as data:
for k, x in enumerate(data):
assert_false(np.any(np.abs(x-full_data[k]) > tolerance), 'iteration %d does not match' % k)
assert not np.any(np.abs(x-full_data[k]) > tolerance), 'iteration %d does not match' % k


def test_blocks():
Expand All @@ -264,22 +273,21 @@ def test_blocks():
for x in al.blocks(data, n, 10):
read_data.append(x[:-10].copy())
read_data = np.vstack(read_data)
assert_equal(full_data.shape[0]-10, read_data.shape[0], 'len of blocked data differ from input data')
assert_equal(full_data.shape[1], read_data.shape[1], 'columns of blocked data differ from input data')
assert_false(np.any(np.abs(full_data[:-10] - read_data) > tolerance), 'blocks() failed')
assert full_data.shape[0]-10 == read_data.shape[0], 'len of blocked data differ from input data'
assert full_data.shape[1] == read_data.shape[1], 'columns of blocked data differ from input data'
assert not np.any(np.abs(full_data[:-10] - read_data) > tolerance), 'blocks() failed'
read_data = []
with al.AudioLoader(filename) as data:
for x in data.blocks(n, 10):
read_data.append(x[:-10].copy())
read_data = np.vstack(read_data)
assert_equal(full_data.shape[0]-10, read_data.shape[0], 'len of blocked data differ from input data')
assert_equal(full_data.shape[1], read_data.shape[1], 'columns of blocked data differ from input data')
assert_false(np.any(np.abs(full_data[:-10] - read_data) > tolerance), 'blocks() failed')
assert full_data.shape[0]-10 == read_data.shape[0], 'len of blocked data differ from input data'
assert full_data.shape[1] == read_data.shape[1], 'columns of blocked data differ from input data'
assert not np.any(np.abs(full_data[:-10] - read_data) > tolerance), 'blocks() failed'

def wrong_blocks(data):
for x in al.blocks(data, 10, 20):
with pytest.raises(ValueError):
for x in al.blocks(full_data, 10, 20):
pass
assert_raises(ValueError, wrong_blocks, full_data)


def test_unwrap():
Expand All @@ -298,35 +306,35 @@ def test_unwrap():
sf.set_unwrap(1.5, down_scale=False)
sdata = sf[:,:]
md = sf.metadata()['INFO']
assert_equal(md['UnwrapThreshold'], '1.50', 'AudioLoader with unwrap adds metadata')
assert_equal(md['Gain'], '20mV', 'AudioLoader with unwrap modifies gain')
assert_equal(len(sdata), len(t), 'AudioLoader with unwrap keeps frames')
assert_equal(sdata.ndim, 2, 'AudioLoader with unwrap keeps two dimensions')
assert_true(np.max(sdata) > 1.4, 'AudioLoader with unwrap expands beyond +1')
assert_true(np.min(sdata) < -1.4, 'AudioLoader with unwrap expands below -1')
assert md['UnwrapThreshold'] == '1.50', 'AudioLoader with unwrap adds metadata'
assert md['Gain'] == '20mV', 'AudioLoader with unwrap modifies gain'
assert len(sdata) == len(t), 'AudioLoader with unwrap keeps frames'
assert sdata.ndim == 2, 'AudioLoader with unwrap keeps two dimensions'
assert np.max(sdata) > 1.4, 'AudioLoader with unwrap expands beyond +1'
assert np.min(sdata) < -1.4, 'AudioLoader with unwrap expands below -1'

with al.AudioLoader(filename) as sf:
sf.set_unwrap(1.5)
sdata = sf[:,:]
md = sf.metadata()['INFO']
assert_equal(md['UnwrapThreshold'], '0.75', 'AudioLoader with unwrap adds metadata')
assert_equal(md['Gain'], '40.0mV', 'AudioLoader with unwrap modifies gain')
assert_equal(len(sdata), len(t), 'AudioLoader with unwrap keeps frames')
assert_equal(sdata.ndim, 2, 'AudioLoader with unwrap keeps two dimensions')
assert_true(np.max(sdata) <= 1.0, 'AudioLoader with unwrap downscales below +1')
assert_true(np.min(sdata) >= -1.0, 'AudioLoader with unwrap downscales above -1')
assert md['UnwrapThreshold'] == '0.75', 'AudioLoader with unwrap adds metadata'
assert md['Gain'] == '40.0mV', 'AudioLoader with unwrap modifies gain'
assert len(sdata) == len(t), 'AudioLoader with unwrap keeps frames'
assert sdata.ndim == 2, 'AudioLoader with unwrap keeps two dimensions'
assert np.max(sdata) <= 1.0, 'AudioLoader with unwrap downscales below +1'
assert np.min(sdata) >= -1.0, 'AudioLoader with unwrap downscales above -1'

with al.AudioLoader(filename) as sf:
sf.set_unwrap(1.5, clips=True)
sdata = sf[:,:]
md = sf.metadata()['INFO']
assert_equal(md['UnwrapThreshold'], '1.50', 'AudioLoader with unwrap adds metadata')
assert_equal(md['UnwrapClippedAmplitude'], '1.00', 'AudioLoader with unwrap adds metadata')
assert_equal(md['Gain'], '20mV', 'AudioLoader with unwrap modifies gain')
assert_equal(len(sdata), len(t), 'AudioLoader with unwrap keeps frames')
assert_equal(sdata.ndim, 2, 'AudioLoader with unwrap keeps two dimensions')
assert_true(np.max(sdata) <= 1.0, 'AudioLoader with unwrap clips at +1')
assert_true(np.min(sdata) >= -1.0, 'AudioLoader with unwrap clips at -1')
assert md['UnwrapThreshold'] == '1.50', 'AudioLoader with unwrap adds metadata'
assert md['UnwrapClippedAmplitude'] == '1.00', 'AudioLoader with unwrap adds metadata'
assert md['Gain'] == '20mV', 'AudioLoader with unwrap modifies gain'
assert len(sdata) == len(t), 'AudioLoader with unwrap keeps frames'
assert sdata.ndim == 2, 'AudioLoader with unwrap keeps two dimensions'
assert np.max(sdata) <= 1.0, 'AudioLoader with unwrap clips at +1'
assert np.min(sdata) >= -1.0, 'AudioLoader with unwrap clips at -1'

os.remove(filename)

Expand Down

0 comments on commit 7292765

Please sign in to comment.