Skip to content

Commit

Permalink
Speed up tests by gently patching mido to not do useless work (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Nov 29, 2023
1 parent 09122ef commit 2612957
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion tests/test_pianoroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2612957

Please sign in to comment.