From 2612957a9bd8d7efb868a6372de951ae98530b58 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 29 Nov 2023 09:59:28 +0200 Subject: [PATCH] Speed up tests by gently patching `mido` to not do useless work (#44) --- tests/conftest.py | 27 +++++++++++++++++++++++++++ tests/test_pianoroll.py | 2 +- tests/test_read_dump.py | 2 +- tests/test_utils.py | 4 +++- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..1a8d764 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,27 @@ +import pytest + + +@pytest.fixture() +def disable_mido_checks(monkeypatch): + """ + Disable internal checks in `mido`, for performance. + """ + from mido.messages import messages + + monkeypatch.setattr(messages, "check_msgdict", lambda d: d) + + +@pytest.fixture() +def disable_mido_merge_tracks(monkeypatch): + """ + Disallow `mido` from creating `merged_tracks` when files are loaded. + + We don't need `merged_tracks` in our tests, and it's slow to create. + + See https://github.com/mido/mido/pull/565. + TODO: this could maybe be removed after the + abovementioned PR is in a `mido` release. + """ + from mido.midifiles import midifiles + + monkeypatch.setattr(midifiles, "merge_tracks", lambda tracks: None) diff --git a/tests/test_pianoroll.py b/tests/test_pianoroll.py index 8e2168b..f59129e 100644 --- a/tests/test_pianoroll.py +++ b/tests/test_pianoroll.py @@ -19,7 +19,7 @@ @pytest.mark.parametrize("midi_path", MIDI_PATHS, ids=attrgetter("name")) @pytest.mark.parametrize("test_set", test_sets) -def test_pianoroll(midi_path, test_set): +def test_pianoroll(midi_path, test_set, disable_mido_checks, disable_mido_merge_tracks): """Testing creating pianorolls of notes.""" # Set pitch range parameters diff --git a/tests/test_read_dump.py b/tests/test_read_dump.py index 9efdcba..2e17f30 100644 --- a/tests/test_read_dump.py +++ b/tests/test_read_dump.py @@ -7,7 +7,7 @@ @pytest.mark.parametrize("midi_path", MIDI_PATHS, ids=attrgetter("name")) -def test_load_dump(midi_path, tmp_path): +def test_load_dump(midi_path, tmp_path, disable_mido_checks, disable_mido_merge_tracks): """Test that a MIDI loaded and saved unchanged is indeed the save as before.""" midi1 = MidiFile(midi_path) dump_path = tmp_path / midi_path.name diff --git a/tests/test_utils.py b/tests/test_utils.py index 1ca2872..0e54e50 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -7,7 +7,9 @@ @pytest.mark.parametrize("midi_path", MIDI_PATHS[:5], ids=attrgetter("name")) -def test_remove_notes_with_no_duration(midi_path, tmp_path): +def test_remove_notes_with_no_duration( + midi_path, tmp_path, disable_mido_checks, disable_mido_merge_tracks +): """Test that a MIDI loaded and saved unchanged is indeed the save as before.""" # Load the MIDI file and removes the notes with durations <= 0 midi = MidiFile(midi_path)