Mercurial > hg > auditok
changeset 426:c5b4178aa80f
Refactor tests
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Tue, 29 Oct 2024 18:50:28 +0100 |
parents | 1b78211b7e07 |
children | e98507ccf4ed |
files | .github/workflows/ci.yml tests/images/plot_mono_region.png tests/images/plot_stereo_region.png tests/images/py34_py35/plot_mono_region.png tests/images/py34_py35/plot_stereo_region.png tests/images/py34_py35/split_and_plot_mono_region.png tests/images/py34_py35/split_and_plot_uc_0_stereo_region.png tests/images/py34_py35/split_and_plot_uc_1_stereo_region.png tests/images/py34_py35/split_and_plot_uc_any_stereo_region.png tests/images/py34_py35/split_and_plot_uc_mix_stereo_region.png tests/images/split_and_plot_mono_region.png tests/images/split_and_plot_uc_0_stereo_region.png tests/images/split_and_plot_uc_1_stereo_region.png tests/images/split_and_plot_uc_any_stereo_region.png tests/images/split_and_plot_uc_mix_stereo_region.png tests/test_AudioSource.py tests/test_cmdline_util.py tests/test_core.py tests/test_io.py tests/test_plotting.py tests/test_workers.py |
diffstat | 21 files changed, 120 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.github/workflows/ci.yml Tue Oct 29 18:50:28 2024 +0100 @@ -0,0 +1,42 @@ +name: CI + +on: + push: + branches: [master, dev] + pull_request: + branches: [master] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Set up PYTHONPATH + run: echo "PYTHONPATH=$PYTHONPATH:${{ github.workspace }}" >> $GITHUB_ENV + + - name: Install dependencies + run: | + sudo apt-get update --fix-missing + pip install numpy pytest pydub matplotlib +<<<<<<< HEAD +======= + + - name: Install specific package for Python 3.8 only + if: matrix.python-version == '3.13' + run: pip install audioop-lts +>>>>>>> fd245f3 (Refactor test_plotting.py) + + - name: Run tests + run: pytest -s -p no:warnings "tests"
--- a/tests/test_AudioSource.py Sat Oct 19 16:58:19 2024 +0200 +++ b/tests/test_AudioSource.py Tue Oct 29 18:50:28 2024 +0100 @@ -26,7 +26,7 @@ >>> list(gen) ["a", "A", "b", "B", "c", "C", "d", "D"] """ - frame_gen = zip(*data_buffers, strict=True) + frame_gen = zip(*data_buffers) return (sample for frame in frame_gen for sample in frame)
--- a/tests/test_cmdline_util.py Sat Oct 19 16:58:19 2024 +0200 +++ b/tests/test_cmdline_util.py Tue Oct 29 18:50:28 2024 +0100 @@ -265,7 +265,6 @@ CommandLineWorker, PrintWorker, ], - strict=True, ): assert isinstance(obs, cls) @@ -299,7 +298,6 @@ CommandLineWorker, PrintWorker, ], - strict=True, ): assert isinstance(obs, cls) @@ -328,7 +326,6 @@ for obs, cls in zip( tokenizer_worker._observers, [PlayerWorker, CommandLineWorker, PrintWorker], - strict=True, ): assert isinstance(obs, cls) @@ -357,7 +354,6 @@ for obs, cls in zip( tokenizer_worker._observers, [RegionSaverWorker, CommandLineWorker, PrintWorker], - strict=True, ): assert isinstance(obs, cls) @@ -386,7 +382,6 @@ for obs, cls in zip( tokenizer_worker._observers, [RegionSaverWorker, PlayerWorker, PrintWorker], - strict=True, ): assert isinstance(obs, cls) @@ -415,7 +410,6 @@ for obs, cls in zip( tokenizer_worker._observers, [RegionSaverWorker, PlayerWorker, CommandLineWorker], - strict=True, ): assert isinstance(obs, cls)
--- a/tests/test_core.py Sat Oct 19 16:58:19 2024 +0200 +++ b/tests/test_core.py Tue Oct 29 18:50:28 2024 +0100 @@ -3,7 +3,8 @@ from pathlib import Path from random import random from tempfile import TemporaryDirectory -from unittest.mock import Mock, patch +from unittest import mock +from unittest.mock import patch import numpy as np import pytest @@ -26,6 +27,8 @@ from auditok.signal import to_array from auditok.util import AudioReader +mock._magics.add("__round__") + def _make_random_length_regions( byte_seq, sampling_rate, sample_width, channels @@ -409,7 +412,7 @@ assert len(regions_ar) == len(expected), err_msg sample_width = 2 - for reg, reg_ar, exp in zip(regions, regions_ar, expected, strict=True): + for reg, reg_ar, exp in zip(regions, regions_ar, expected): onset, offset = exp exp_data = data[onset * sample_width : offset * sample_width] assert bytes(reg) == exp_data @@ -538,7 +541,11 @@ sample_width = 2 sample_size_bytes = sample_width * channels - for reg, reg_ar, exp in zip(regions, regions_ar, expected, strict=True): + for reg, reg_ar, exp in zip( + regions, + regions_ar, + expected, + ): onset, offset = exp exp_data = data[onset * sample_size_bytes : offset * sample_size_bytes] assert len(bytes(reg)) == len(exp_data) @@ -959,7 +966,11 @@ sample_width = 2 sample_size_bytes = sample_width * channels - for reg, reg_ar, exp in zip(regions, regions_ar, expected, strict=True): + for reg, reg_ar, exp in zip( + regions, + regions_ar, + expected, + ): onset, offset = exp exp_data = data[onset * sample_size_bytes : offset * sample_size_bytes] assert bytes(reg) == exp_data @@ -1007,7 +1018,11 @@ assert len(regions_ar) == len(expected), err_msg sample_size_bytes = 2 - for reg, reg_ar, exp in zip(regions, regions_ar, expected, strict=True): + for reg, reg_ar, exp in zip( + regions, + regions_ar, + expected, + ): onset, offset = exp exp_data = data[onset * sample_size_bytes : offset * sample_size_bytes] assert bytes(reg) == exp_data @@ -1095,7 +1110,10 @@ err_msg = "Wrong number of regions after split, expected: " err_msg += "{}, found: {}".format(expected, regions) assert len(regions) == len(expected), err_msg - for reg, exp in zip(regions, expected, strict=True): + for reg, exp in zip( + regions, + expected, + ): onset, offset = exp exp_data = data[onset * sample_width * 2 : offset * sample_width * 2] assert bytes(reg) == exp_data @@ -2157,7 +2175,10 @@ region = AudioRegion(b"".join(data), 80, 1, 1) sub_regions = region / len(data) - for data_i, region in zip(data, sub_regions, strict=True): + for data_i, region in zip( + data, + sub_regions, + ): assert len(data_i) == len(bytes(region))
--- a/tests/test_io.py Sat Oct 19 16:58:19 2024 +0200 +++ b/tests/test_io.py Tue Oct 29 18:50:28 2024 +0100 @@ -1,21 +1,15 @@ import filecmp -import math import os -import sys import wave -from array import array from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryDirectory from unittest.mock import Mock, patch import numpy as np import pytest -from test_AudioSource import ( - PURE_TONE_DICT, - _generate_pure_tone, - _sample_generator, -) +from test_AudioSource import PURE_TONE_DICT, _sample_generator +import auditok from auditok.io import ( AudioIOError, AudioParameterError, @@ -98,32 +92,34 @@ def test_get_audio_parameters_short_params(): expected = (8000, 2, 1) - params = dict(zip(("sr", "sw", "ch"), expected, strict=True)) + params = dict(zip(("sr", "sw", "ch"), expected)) result = _get_audio_parameters(params) assert result == expected def test_get_audio_parameters_long_params(): expected = (8000, 2, 1) + params = dict(zip(("sampling_rate", "sample_width", "channels"), expected)) + result = _get_audio_parameters(params) + assert result == expected + + +def test_get_audio_parameters_long_params_shadow_short_ones(): + expected = (8000, 2, 1) params = dict( zip( ("sampling_rate", "sample_width", "channels"), expected, - strict=True, ) ) - result = _get_audio_parameters(params) - assert result == expected - - -def test_get_audio_parameters_long_params_shadow_short_ones(): - expected = (8000, 2, 1) - params = dict( - zip( - ("sampling_rate", "sample_width", "channels"), expected, strict=True + params.update( + dict( + zip( + ("sr", "sw", "ch"), + "xxx", + ) ) ) - params.update(dict(zip(("sr", "sw", "ch"), "xxx", strict=True))) result = _get_audio_parameters(params) assert result == expected @@ -181,7 +177,10 @@ ) def test_get_audio_parameters_invalid(values): params = dict( - zip(("sampling_rate", "sample_width", "channels"), values, strict=True) + zip( + ("sampling_rate", "sample_width", "channels"), + values, + ) ) with pytest.raises(AudioParameterError): _get_audio_parameters(params)
--- a/tests/test_plotting.py Sat Oct 19 16:58:19 2024 +0200 +++ b/tests/test_plotting.py Tue Oct 29 18:50:28 2024 +0100 @@ -10,11 +10,6 @@ from auditok.core import AudioRegion # noqa E402 -if sys.version_info.minor <= 5: - PREFIX = "py34_py35/" -else: - PREFIX = "" - SAVE_NEW_IMAGES = False if SAVE_NEW_IMAGES: import shutil # noqa E402 @@ -26,7 +21,7 @@ def test_region_plot(channels): type_ = "mono" if channels == 1 else "stereo" audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_) - image_filename = "tests/images/{}plot_{}_region.png".format(PREFIX, type_) + image_filename = "tests/images/plot_{}_region.png".format(type_) expected_image = plt.imread(image_filename) with TemporaryDirectory() as tmpdir: output_image_filename = os.path.join(tmpdir, "image.png") @@ -54,10 +49,11 @@ type_ = "mono" if channels == 1 else "stereo" audio_filename = "tests/data/test_split_10HZ_{}.raw".format(type_) if type_ == "mono": - fmt = "tests/images/{}split_and_plot_mono_region.png" + image_filename = "tests/images/split_and_plot_mono_region.png" else: - fmt = "tests/images/{}split_and_plot_uc_{}_stereo_region.png" - image_filename = fmt.format(PREFIX, use_channel) + image_filename = ( + f"tests/images/split_and_plot_uc_{use_channel}_stereo_region.png" + ) expected_image = plt.imread(image_filename) with TemporaryDirectory() as tmpdir:
--- a/tests/test_workers.py Sat Oct 19 16:58:19 2024 +0200 +++ b/tests/test_workers.py Tue Oct 29 18:50:28 2024 +0100 @@ -66,7 +66,11 @@ ) assert len(tokenizer.detections) == len(expected_detections) for i, (det, exp, log_line) in enumerate( - zip(tokenizer.detections, expected_detections, log_lines, strict=True), + zip( + tokenizer.detections, + expected_detections, + log_lines, + ), 1, ): start, end = exp @@ -105,7 +109,11 @@ assert len(tokenizer.detections) == len(expected_detections) log_fmt = "[PLAY]: Detection {id} played" for i, (det, exp, log_line) in enumerate( - zip(tokenizer.detections, expected_detections, log_lines, strict=False), + zip( + tokenizer.detections, + expected_detections, + log_lines, + ), 1, ): start, end = exp @@ -159,7 +167,11 @@ log_fmt = "[SAVE]: Detection {id} saved as '{filename}'" for i, (det, exp, log_line) in enumerate( - zip(tokenizer.detections, expected_detections, log_lines, strict=False), + zip( + tokenizer.detections, + expected_detections, + log_lines, + ), 1, ): start, end = exp @@ -203,7 +215,11 @@ assert len(tokenizer.detections) == len(expected_detections) log_fmt = "[COMMAND]: Detection {id} command '{command}'" for i, (det, exp, log_line) in enumerate( - zip(tokenizer.detections, expected_detections, log_lines, strict=False), + zip( + tokenizer.detections, + expected_detections, + log_lines, + ), 1, ): start, end = exp @@ -242,7 +258,10 @@ ] assert patched_print.mock_calls == expected_print_calls assert len(tokenizer.detections) == len(expected_detections) - for det, exp in zip(tokenizer.detections, expected_detections, strict=True): + for det, exp in zip( + tokenizer.detections, + expected_detections, + ): start, end = exp assert pytest.approx(det.start) == start assert pytest.approx(det.end) == end