Skip to content

Commit

Permalink
Merge pull request #79 from ssssam/sam/testsuite
Browse files Browse the repository at this point in the history
Add a simple testsuite
  • Loading branch information
sampsyo authored Jan 3, 2019
2 parents d3ca86e + 9afd929 commit 92f73b3
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ install:
- python setup.py install

script:
- python -c "import audioread"
- pytest
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[aliases]
test=pytest
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# included in all copies or substantial portions of the Software.

import os
from distutils.core import setup
from setuptools import setup
import imp

version = imp.load_source('audioread.version', 'audioread/version.py')
Expand All @@ -36,6 +36,14 @@ def _read(fn):

packages=['audioread'],

setup_requires=[
'pytest-runner'
],

tests_require=[
'pytest'
],

classifiers=[
'Topic :: Multimedia :: Sound/Audio :: Conversion',
'Intended Audience :: Developers',
Expand Down
51 changes: 51 additions & 0 deletions test/conftest.py
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)
6 changes: 6 additions & 0 deletions test/data/test-1.json
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 added test/data/test-1.mp3
Binary file not shown.
6 changes: 6 additions & 0 deletions test/data/test-2.json
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 added test/data/test-2.mp3
Binary file not shown.
51 changes: 51 additions & 0 deletions test/test_audioread.py
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
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tox]
envlist = py27,py36

[testenv]
deps = pytest
commands = pytest {posargs}

0 comments on commit 92f73b3

Please sign in to comment.