-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from ssssam/sam/testsuite
Add a simple testsuite
- Loading branch information
Showing
10 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ install: | |
- python setup.py install | ||
|
||
script: | ||
- python -c "import audioread" | ||
- pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[aliases] | ||
test=pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# This file is part of audioread. | ||
# Copyright 2018, Sam Thursfield | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
|
||
|
||
import json | ||
import os | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def audiofile(request): | ||
"""Fixture that provides an AudiofileSpec instance.""" | ||
spec_path = os.path.join(DATADIR, request.param + '.json') | ||
with open(spec_path, 'r') as f: | ||
spec = json.load(f) | ||
result = AudiofileSpec(**spec) | ||
return result | ||
|
||
|
||
# The audiofiles used for testing live in the data/ directory. They are defined | ||
# by .json files and each one must be listed here by name. | ||
TEST_AUDIOFILES = ['test-1', 'test-2'] | ||
|
||
DATADIR = os.path.join(os.path.dirname(__file__), 'data') | ||
|
||
|
||
class AudiofileSpec(): | ||
"""Defines the expected attributes for a test audiofile.""" | ||
def __init__(self, filename, duration, channels, samplerate): | ||
self.path = os.path.join(DATADIR, filename) | ||
self.duration = duration | ||
self.channels = channels | ||
self.samplerate = samplerate | ||
|
||
|
||
def pytest_generate_tests(metafunc): | ||
"""Parametrize the audiofile() fixture using TEST_AUDIOFILES.""" | ||
if 'audiofile' in metafunc.fixturenames: | ||
metafunc.parametrize("audiofile", TEST_AUDIOFILES, indirect=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"filename": "test-1.mp3", | ||
"duration": 15.0, | ||
"channels": 1, | ||
"samplerate": 22050 | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"filename": "test-2.mp3", | ||
"duration": 2, | ||
"channels": 2, | ||
"samplerate": 44100 | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# This file is part of audioread. | ||
# Copyright 2018, Sam Thursfield | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
|
||
|
||
import json | ||
import os | ||
import sys | ||
|
||
import pytest | ||
|
||
import audioread | ||
|
||
|
||
# The 'audiofile' fixture is defined in conftest.py. | ||
|
||
|
||
def test_audioread_early_exit(audiofile): | ||
"""Abort the read before it is completed. | ||
This test guards against regressions such as | ||
https://github.com/beetbox/audioread/pull/78 | ||
""" | ||
with audioread.audio_open(audiofile.path) as a: | ||
assert int(a.duration) == int(audiofile.duration) | ||
assert a.channels == audiofile.channels | ||
assert a.samplerate == audiofile.samplerate | ||
# Now we exit the context manager without reading any data. | ||
|
||
|
||
def test_audioread_full(audiofile): | ||
"""Read the audio data from the file.""" | ||
with audioread.audio_open(audiofile.path) as a: | ||
assert int(a.duration) == int(audiofile.duration) | ||
assert a.channels == audiofile.channels | ||
assert a.samplerate == audiofile.samplerate | ||
|
||
# Now read all the data and assert that it's the correct type. | ||
for block in a: | ||
assert type(block) == bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[tox] | ||
envlist = py27,py36 | ||
|
||
[testenv] | ||
deps = pytest | ||
commands = pytest {posargs} |