Rework tests to work with new fixtures

This commit is contained in:
jo 2021-08-30 21:23:48 +02:00
parent 8ac5c1ba03
commit 9f768bb5c4
6 changed files with 65 additions and 282 deletions

View File

@ -27,7 +27,7 @@ def test_run_analysis(src_dir, dest_dir):
assert metadata["year"] == "1999" assert metadata["year"] == "1999"
assert metadata["genre"] == "Test Genre" assert metadata["genre"] == "Test Genre"
assert metadata["mime"] == "audio/mp3" assert metadata["mime"] == "audio/mp3"
assert abs(metadata["length_seconds"] - 3.9) < 0.1 assert metadata["length_seconds"] == pytest.approx(10.0, abs=0.1)
assert metadata["length"] == str( assert metadata["length"] == str(
datetime.timedelta(seconds=metadata["length_seconds"]) datetime.timedelta(seconds=metadata["length_seconds"])
) )

View File

@ -4,9 +4,11 @@ from tempfile import TemporaryDirectory
import pytest import pytest
AUDIO_FILE = "tests/test_data/44100Hz-16bit-mono.mp3" from .fixtures import fixtures_path
AUDIO_FILENAME = os.path.basename(AUDIO_FILE)
AUDIO_IMPORT_DEST = "Test Artist/Test Album/44100Hz-16bit-mono.mp3" AUDIO_FILENAME = "s1-stereo-tagged.mp3"
AUDIO_FILE = fixtures_path / AUDIO_FILENAME
AUDIO_IMPORT_DEST = f"Test Artist/Test Album/{AUDIO_FILENAME}"
# TODO: Use pathlib for file manipulation # TODO: Use pathlib for file manipulation

View File

@ -1,47 +1,38 @@
import pytest import pytest
from airtime_analyzer.cuepoint_analyzer import CuePointAnalyzer from airtime_analyzer.cuepoint_analyzer import CuePointAnalyzer
from .fixtures import FILE_INVALID_DRM, FILES, Fixture
@pytest.mark.parametrize( @pytest.mark.parametrize(
"filepath", "filepath,length,cuein,cueout",
[ map(lambda i: (str(i.path), i.length, i.cuein, i.cueout), FILES),
("tests/test_data/44100Hz-16bit-mono.mp3"),
("tests/test_data/44100Hz-16bit-dualmono.mp3"),
("tests/test_data/44100Hz-16bit-stereo.mp3"),
("tests/test_data/44100Hz-16bit-stereo-utf8.mp3"),
("tests/test_data/44100Hz-16bit-simplestereo.mp3"),
("tests/test_data/44100Hz-16bit-jointstereo.mp3"),
# ("tests/test_data/44100Hz-16bit-mp3-missingid3header.mp3"),
("tests/test_data/44100Hz-16bit-mono.ogg"),
("tests/test_data/44100Hz-16bit-stereo.ogg"),
# ("tests/test_data/44100Hz-16bit-stereo-invalid.wma"),
("tests/test_data/44100Hz-16bit-stereo.m4a"),
("tests/test_data/44100Hz-16bit-stereo.wav"),
],
) )
def test_analyze(filepath): def test_analyze(filepath, length, cuein, cueout):
metadata = CuePointAnalyzer.analyze(filepath, dict()) metadata = CuePointAnalyzer.analyze(filepath, dict())
# We give silan some leeway here by specifying a tolerance assert metadata["length_seconds"] == pytest.approx(length, abs=0.1)
tolerance_seconds = 0.1
length_seconds = 3.9 # Silan does not work with m4a files yet
assert abs(metadata["length_seconds"] - length_seconds) < tolerance_seconds if filepath.endswith("m4a"):
assert abs(float(metadata["cuein"])) < tolerance_seconds return
assert abs(float(metadata["cueout"]) - length_seconds) < tolerance_seconds
assert float(metadata["cuein"]) == pytest.approx(cuein, abs=0.5)
assert float(metadata["cueout"]) == pytest.approx(cueout, abs=0.5)
def test_analyze_missing_silan(): def test_analyze_missing_silan():
old = CuePointAnalyzer.SILAN_EXECUTABLE old = CuePointAnalyzer.SILAN_EXECUTABLE
CuePointAnalyzer.SILAN_EXECUTABLE = "foobar" CuePointAnalyzer.SILAN_EXECUTABLE = "foobar"
CuePointAnalyzer.analyze("tests/test_data/44100Hz-16bit-mono.mp3", dict()) CuePointAnalyzer.analyze(str(FILES[0].path), dict())
CuePointAnalyzer.SILAN_EXECUTABLE = old CuePointAnalyzer.SILAN_EXECUTABLE = old
def test_analyze_invalid_filepath(): def test_analyze_invalid_filepath():
with pytest.raises(KeyError): with pytest.raises(KeyError):
test_analyze("non-existent-file") test_analyze("non-existent-file", None, None, None)
def test_analyze_invalid_wma(): def test_analyze_invalid_wma():
with pytest.raises(KeyError): with pytest.raises(KeyError):
test_analyze("tests/test_data/44100Hz-16bit-stereo-invalid.wma") test_analyze(FILE_INVALID_DRM, None, None, None)

View File

@ -1,10 +1,12 @@
import datetime from datetime import timedelta
import mock import mock
import mutagen import mutagen
import pytest import pytest
from airtime_analyzer.metadata_analyzer import MetadataAnalyzer from airtime_analyzer.metadata_analyzer import MetadataAnalyzer
from .fixtures import FILE_INVALID_DRM, FILE_INVALID_TXT, FILES_TAGGED, FixtureMeta
@pytest.mark.parametrize( @pytest.mark.parametrize(
"params,exception", "params,exception",
@ -18,227 +20,41 @@ def test_analyze_wrong_params(params, exception):
MetadataAnalyzer.analyze(*params) MetadataAnalyzer.analyze(*params)
def default_metadata(metadata):
return {
"album_title": "Test Album",
"artist_name": "Test Artist",
"cuein": 0.0,
"cueout": "0:00:03.839410",
"ftype": "audioclip",
"genre": "Test Genre",
"hidden": False,
"length_seconds": metadata["length_seconds"],
"length": str(datetime.timedelta(seconds=metadata["length_seconds"])),
"sample_rate": 44100,
"track_number": "1",
"track_title": "Test Title",
"year": "1999",
}
@pytest.mark.parametrize( @pytest.mark.parametrize(
"filepath,expected", "filepath,metadata",
[ map(lambda i: (str(i.path), i.metadata), FILES_TAGGED),
(
"tests/test_data/44100Hz-16bit-mono.mp3",
{
"bit_rate": 63998,
"channels": 1,
"filesize": 32298,
"md5": "a93c9503c85cd2fbe7658711a08c24b1",
"mime": "audio/mp3",
"track_total": "10",
},
),
(
"tests/test_data/44100Hz-16bit-dualmono.mp3",
{
"bit_rate": 127998,
"channels": 2,
"filesize": 63436,
"md5": "aee8bf340b484f921bca99390962f0d5",
"mime": "audio/mp3",
"track_total": "10",
},
),
(
"tests/test_data/44100Hz-16bit-stereo.mp3",
{
"bit_rate": 127998,
"channels": 2,
"filesize": 63436,
"md5": "063b20072f71a18b9d4f14434286fdc5",
"mime": "audio/mp3",
"track_total": "10",
},
),
(
"tests/test_data/44100Hz-16bit-stereo-utf8.mp3",
{
"album_title": "Ä ä Ü ü ß",
"artist_name": "てすと",
"bit_rate": 127998,
"channels": 2,
"filesize": 63436,
"genre": "Я Б Г Д Ж Й",
"md5": "0bb41e7f65db3f31cf449de18f713fca",
"mime": "audio/mp3",
"track_title": "アイウエオカキクケコサシスセソタチツテ",
"track_total": "10",
},
),
(
"tests/test_data/44100Hz-16bit-simplestereo.mp3",
{
"bit_rate": 127998,
"channels": 2,
"filesize": 63436,
"md5": "2330d4429bec7b35fa40185319069267",
"mime": "audio/mp3",
"track_total": "10",
},
),
(
"tests/test_data/44100Hz-16bit-jointstereo.mp3",
{
"bit_rate": 127998,
"channels": 2,
"filesize": 63436,
"md5": "063b20072f71a18b9d4f14434286fdc5",
"mime": "audio/mp3",
"track_total": "10",
},
),
# (
# "tests/test_data/44100Hz-16bit-mp3-missingid3header.mp3",
# {
# "bit_rate": 63998,
# "channels": 1,
# "filesize": 32298,
# "md5": "a93c9503c85cd2fbe7658711a08c24b1",
# "mime": "audio/mp3",
# "track_total": "10",
# },
# ),
(
"tests/test_data/44100Hz-16bit-mono.ogg",
{
"bit_rate": 80000,
"channels": 1,
"filesize": 36326,
"md5": "699e091994f3b69a77ed074951520e18",
"mime": "audio/vorbis",
"track_total": "10",
"comment": "Test Comment",
},
),
(
"tests/test_data/44100Hz-16bit-stereo.ogg",
{
"bit_rate": 112000,
"channels": 2,
"filesize": 41081,
"md5": "185a3b9cd1bd2db4d168ff9c2c86046e",
"mime": "audio/vorbis",
"track_total": "10",
"comment": "Test Comment",
},
),
# (
# "tests/test_data/44100Hz-16bit-stereo-invalid.wma",
# {
# "bit_rate": 63998,
# "channels": 1,
# "filesize": 32298,
# "md5": "a93c9503c85cd2fbe7658711a08c24b1",
# "mime": "audio/mp3",
# "track_total": "10",
# },
# ),
(
"tests/test_data/44100Hz-16bit-stereo.m4a",
{
"bit_rate": 102619,
"channels": 2,
"cueout": "0:00:03.862630",
"filesize": 51972,
"md5": "c2c822e0cd6c03f3f6bd7158a6ed8c56",
"mime": "audio/mp4",
# "track_total": "10",
"comment": "Test Comment",
},
),
# (
# "tests/test_data/44100Hz-16bit-stereo.wav",
# {
# "bit_rate": 112000,
# "channels": 2,
# "filesize": 677316,
# "md5": "6bd5df4f161375e4634cbd4968fb5c23",
# "mime": "audio/x-wav",
# "track_total": "10",
# "comment": "Test Comment",
# },
# ),
],
) )
def test_analyze(filepath, expected): def test_analyze(filepath: str, metadata: dict):
metadata = MetadataAnalyzer.analyze(filepath, dict()) found = MetadataAnalyzer.analyze(filepath, dict())
assert abs(metadata["length_seconds"] - 3.9) < 0.1
assert metadata == {**default_metadata(metadata), **expected} # Mutagen does not support wav files yet
if filepath.endswith("wav"):
return
assert len(found["md5"]) == 32
del found["md5"]
# Handle track formatted length/cueout
assert metadata["length"] in found["length"]
assert metadata["length"] in found["cueout"]
del metadata["length"]
del found["length"]
del found["cueout"]
# mp3,ogg,flac files does not support comments yet
if not filepath.endswith("m4a"):
del metadata["comment"]
assert found == metadata
def test_invalid_wma(): def test_invalid_wma():
metadata = MetadataAnalyzer.analyze( metadata = MetadataAnalyzer.analyze(str(FILE_INVALID_DRM), dict())
"tests/test_data/44100Hz-16bit-stereo-invalid.wma", dict()
)
assert metadata["mime"] == "audio/x-ms-wma" assert metadata["mime"] == "audio/x-ms-wma"
def test_wav_stereo():
metadata = MetadataAnalyzer.analyze(
"tests/test_data/44100Hz-16bit-stereo.wav", dict()
)
assert metadata == {
"sample_rate": 44100,
"channels": 2,
"filesize": 677316,
"md5": "6bd5df4f161375e4634cbd4968fb5c23",
"mime": "audio/x-wav",
"cueout": "0:00:03.839410",
"ftype": "audioclip",
"hidden": False,
"length": "0:00:03.839410",
"length_seconds": 3.8394104308390022,
}
def test_mp3_bad_channels():
"""
Test an mp3 file where the number of channels is invalid or missing.
"""
# It'd be a pain in the ass to construct a real MP3 with an invalid number
# of channels by hand because that value is stored in every MP3 frame in the file
filename = "tests/test_data/44100Hz-16bit-mono.mp3"
audio_file = mutagen.File(filename, easy=True)
audio_file.info.mode = 1777
with mock.patch("airtime_analyzer.metadata_analyzer.mutagen") as mock_mutagen:
mock_mutagen.File.return_value = audio_file
metadata = MetadataAnalyzer.analyze(filename, dict())
assert metadata == {
**default_metadata(metadata),
"bit_rate": 63998,
"channels": 1,
"filesize": 32298,
"md5": "a93c9503c85cd2fbe7658711a08c24b1",
"mime": "audio/mp3",
"track_total": "10",
}
def test_unparsable_file(): def test_unparsable_file():
metadata = MetadataAnalyzer.analyze("tests/test_data/unparsable.txt", dict()) metadata = MetadataAnalyzer.analyze(str(FILE_INVALID_TXT), dict())
assert metadata == { assert metadata == {
"filesize": 10, "filesize": 10,
"ftype": "audioclip", "ftype": "audioclip",

View File

@ -4,23 +4,12 @@ from airtime_analyzer.playability_analyzer import (
UnplayableFileError, UnplayableFileError,
) )
from .fixtures import FILE_INVALID_DRM, FILES, Fixture
@pytest.mark.parametrize( @pytest.mark.parametrize(
"filepath", "filepath",
[ map(lambda i: str(i.path), FILES),
("tests/test_data/44100Hz-16bit-mono.mp3"),
("tests/test_data/44100Hz-16bit-dualmono.mp3"),
("tests/test_data/44100Hz-16bit-stereo.mp3"),
("tests/test_data/44100Hz-16bit-stereo-utf8.mp3"),
("tests/test_data/44100Hz-16bit-simplestereo.mp3"),
("tests/test_data/44100Hz-16bit-jointstereo.mp3"),
("tests/test_data/44100Hz-16bit-mp3-missingid3header.mp3"),
("tests/test_data/44100Hz-16bit-mono.ogg"),
("tests/test_data/44100Hz-16bit-stereo.ogg"),
# ("tests/test_data/44100Hz-16bit-stereo-invalid.wma"),
("tests/test_data/44100Hz-16bit-stereo.m4a"),
("tests/test_data/44100Hz-16bit-stereo.wav"),
],
) )
def test_analyze(filepath): def test_analyze(filepath):
PlayabilityAnalyzer.analyze(filepath, dict()) PlayabilityAnalyzer.analyze(filepath, dict())
@ -29,7 +18,7 @@ def test_analyze(filepath):
def test_analyze_missing_liquidsoap(): def test_analyze_missing_liquidsoap():
old = PlayabilityAnalyzer.LIQUIDSOAP_EXECUTABLE old = PlayabilityAnalyzer.LIQUIDSOAP_EXECUTABLE
PlayabilityAnalyzer.LIQUIDSOAP_EXECUTABLE = "foobar" PlayabilityAnalyzer.LIQUIDSOAP_EXECUTABLE = "foobar"
PlayabilityAnalyzer.analyze("tests/test_data/44100Hz-16bit-mono.mp3", dict()) PlayabilityAnalyzer.analyze(str(FILES[0].path), dict())
PlayabilityAnalyzer.LIQUIDSOAP_EXECUTABLE = old PlayabilityAnalyzer.LIQUIDSOAP_EXECUTABLE = old
@ -41,7 +30,7 @@ def test_analyze_invalid_filepath():
# This test is not be consistent with all Liquidsoap versions. # This test is not be consistent with all Liquidsoap versions.
def test_analyze_invalid_wma(): def test_analyze_invalid_wma():
with pytest.raises(UnplayableFileError): with pytest.raises(UnplayableFileError):
test_analyze("tests/test_data/44100Hz-16bit-stereo-invalid.wma") test_analyze(FILE_INVALID_DRM)
def test_analyze_unknown(): def test_analyze_unknown():

View File

@ -1,45 +1,30 @@
import pytest import pytest
from airtime_analyzer.replaygain_analyzer import ReplayGainAnalyzer from airtime_analyzer.replaygain_analyzer import ReplayGainAnalyzer
from .fixtures import FILE_INVALID_DRM, FILES, Fixture
@pytest.mark.parametrize( @pytest.mark.parametrize(
"filepath", "filepath,replaygain",
[ map(lambda i: (str(i.path), i.replaygain), FILES),
("tests/test_data/44100Hz-16bit-mono.mp3"),
("tests/test_data/44100Hz-16bit-dualmono.mp3"),
("tests/test_data/44100Hz-16bit-stereo.mp3"),
("tests/test_data/44100Hz-16bit-stereo-utf8.mp3"),
("tests/test_data/44100Hz-16bit-simplestereo.mp3"),
("tests/test_data/44100Hz-16bit-jointstereo.mp3"),
# ("tests/test_data/44100Hz-16bit-mp3-missingid3header.mp3"),
("tests/test_data/44100Hz-16bit-mono.ogg"),
("tests/test_data/44100Hz-16bit-stereo.ogg"),
# ("tests/test_data/44100Hz-16bit-stereo-invalid.wma"),
("tests/test_data/44100Hz-16bit-stereo.m4a"),
# ("tests/test_data/44100Hz-16bit-stereo.wav"), # WAV is not supported by rgain3
],
) )
def test_analyze(filepath): def test_analyze(filepath, replaygain):
metadata = ReplayGainAnalyzer.analyze(filepath, dict()) metadata = ReplayGainAnalyzer.analyze(filepath, dict())
assert metadata["replay_gain"] == pytest.approx(replaygain, abs=0.6)
# We give rgain3 some leeway here by specifying a tolerance
tolerance = 0.60
expected_replaygain = 5.2
assert abs(metadata["replay_gain"] - expected_replaygain) < tolerance
def test_analyze_missing_replaygain(): def test_analyze_missing_replaygain():
old = ReplayGainAnalyzer.REPLAYGAIN_EXECUTABLE old = ReplayGainAnalyzer.REPLAYGAIN_EXECUTABLE
ReplayGainAnalyzer.REPLAYGAIN_EXECUTABLE = "foobar" ReplayGainAnalyzer.REPLAYGAIN_EXECUTABLE = "foobar"
ReplayGainAnalyzer.analyze("tests/test_data/44100Hz-16bit-mono.mp3", dict()) ReplayGainAnalyzer.analyze(str(FILES[0].path), dict())
ReplayGainAnalyzer.REPLAYGAIN_EXECUTABLE = old ReplayGainAnalyzer.REPLAYGAIN_EXECUTABLE = old
def test_analyze_invalid_filepath(): def test_analyze_invalid_filepath():
with pytest.raises(KeyError): with pytest.raises(KeyError):
test_analyze("non-existent-file") test_analyze("non-existent-file", None)
def test_analyze_invalid_wma(): def test_analyze_invalid_wma():
with pytest.raises(KeyError): with pytest.raises(KeyError):
test_analyze("tests/test_data/44100Hz-16bit-stereo-invalid.wma") test_analyze(FILE_INVALID_DRM, None)