From d48394a5229aeec19568f0a1cea1d60d8fd0c567 Mon Sep 17 00:00:00 2001 From: The Wobbler Date: Tue, 18 Mar 2025 17:31:25 +0100 Subject: [PATCH] Added fast tests. --- smalltag/_formats/_id3.py | 12 ++++++------ tests/conftest.py | 17 +++++++++++++++++ tests/formats/test_id3.py | 36 ++++++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 tests/conftest.py diff --git a/smalltag/_formats/_id3.py b/smalltag/_formats/_id3.py index 380abc2..dbe9736 100644 --- a/smalltag/_formats/_id3.py +++ b/smalltag/_formats/_id3.py @@ -27,12 +27,6 @@ class _ID3(SmallTag, tinytag._ID3): def write(self, file_obj: BinaryIO=None): should_close_file = self._set_filehandler_for_writing(file_obj) - # change keys to values in the mapping dict - self._ID3_WRITE_MAPPING = {} # noqa - for frame_id, name in self._ID3_MAPPING.items(): - if len(frame_id) == 4: - self._ID3_WRITE_MAPPING[name] = frame_id - new_tag = self._compose_id3v2_tag() size, extended, major = self._parse_id3v2_header(self._filehandler) @@ -48,6 +42,12 @@ class _ID3(SmallTag, tinytag._ID3): self._filehandler.close() def _compose_id3v2_tag(self) -> bytes: + # change keys to values in the mapping dict + self._ID3_WRITE_MAPPING = {} # noqa + for frame_id, name in self._ID3_MAPPING.items(): + if len(frame_id) == 4: + self._ID3_WRITE_MAPPING[name] = frame_id + frames = self._compose_id3v2_frames() frame_size = len(frames) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..de249a2 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,17 @@ +#!/usr/bin/python3 + +import pytest + + +def pytest_addoption(parser): + parser.addoption( + "--fast", action="store_true", help="Run only fast tests") + + +def pytest_collection_modifyitems(config, items): + if config.getoption("--fast"): + skip_slow = pytest.mark.skip(reason="Runs only without --fast option.") + + for item in items: + if "slow" in item.keywords: + item.add_marker(skip_slow) diff --git a/tests/formats/test_id3.py b/tests/formats/test_id3.py index 458b54c..a816aa0 100644 --- a/tests/formats/test_id3.py +++ b/tests/formats/test_id3.py @@ -1,15 +1,35 @@ #!/usr/bin/python3 +import pytest +from io import BytesIO + from smalltag import SmallTag -# chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_ " +def test_written_gets_recognized(): + # avoid writing to the real file by using a BytesIO + tag = SmallTag.get("formats/test.mp3", file_obj=BytesIO(b"")) + tag.title = "A normal title." + tag.artist = "An artist." + tag.album = "The album." + tag.track = 1234 + + tag = SmallTag.get("formats/test.mp3", file_obj=BytesIO(tag._compose_id3v2_tag())) + + assert tag.title == "A normal title." + assert tag.artist == "An artist." + assert tag.album == "The album." + assert tag.track == 1234 + + +# chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_ " # no "b" because there is a bug in TinyTag 2.1.0 that removes "b"s if they are at the beginning of a string chars = "acdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-" -def test_written_gets_recognized(): +@pytest.mark.slow +def test_full_written_gets_recognized(): for char1 in chars: for char2 in chars: field_content = f"{char1}{char2}Test{char2}{char1}" @@ -27,15 +47,3 @@ def test_written_gets_recognized(): assert tag.title == field_content assert tag.artist == field_content assert tag.album == field_content - - -def test_int_converted_to_numerical_string(): - tag = SmallTag.get("formats/test.mp3") - - tag.track = 123 - - tag.write() - - tag = SmallTag.get("formats/test.mp3") - - assert tag.track == 123