Skip to content

Commit e5a197c

Browse files
committed
fix tests
1 parent ce40c62 commit e5a197c

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

musify/processors/sort.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ def sort_by_field(
9292

9393
# get sort key based on value type
9494
if isinstance(example_value, datetime): # key converts datetime to floats
95-
def sort_key(t: MusifyItem) -> float:
96-
"""Get the sort key for timestamp tags from the given ``t``"""
97-
value = t[tag_name]
95+
def sort_key(it: MusifyItem) -> float:
96+
"""Get the sort key for timestamp tags from the given ``it``"""
97+
value = it[tag_name]
9898
return value.timestamp() if value is not None else 0.0
9999
elif isinstance(example_value, str): # key strips ignore words from string
100-
def sort_key(t: MusifyItem) -> tuple[bool, str]:
101-
not_special_start, value = strip_ignore_words(t[tag_name], words=ignore_words)
100+
def sort_key(it: MusifyItem) -> tuple[bool, str]:
101+
"""Get the sort key for string tags from the given ``it``"""
102+
not_special_start, value = strip_ignore_words(it[tag_name], words=ignore_words)
102103
return not_special_start, value.casefold()
103104
else:
104105
sort_key: Callable[[MusifyItem], object] = lambda t: t[tag_name] if t[tag_name] else 0

tests/processors/test_sort.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections.abc import Callable
22
from itertools import groupby
3-
from random import choice, randrange, shuffle
3+
from random import choice, randrange, shuffle, sample
44

55
import pytest
66

@@ -56,11 +56,18 @@ def test_sort_by_date_added(self, tracks: list[LocalTrack]):
5656
assert tracks == tracks_sorted[::-1]
5757

5858
def test_sort_by_title_with_ignore_words(self, tracks: list[LocalTrack]):
59-
# sort on str, ignoring defined words like 'The' and 'A'
60-
tracks_sorted = sorted(tracks, key=lambda t: strip_ignore_words(t.title))
61-
ItemSorter.sort_by_field(tracks, field=TrackField.TITLE)
59+
ignore_words = ("This", "and", "that")
60+
for track in sample(tracks, k=len(tracks) // 2):
61+
track.title = f"{choice(ignore_words)} {track.title}"
62+
63+
def _sort_key(t: LocalTrack) -> tuple[bool, str]:
64+
not_special_start, value = strip_ignore_words(t.title, words=ignore_words)
65+
return not_special_start, value.lower()
66+
67+
tracks_sorted = sorted(tracks, key=_sort_key)
68+
ItemSorter.sort_by_field(tracks, field=TrackField.TITLE, ignore_words=ignore_words)
6269
assert tracks == tracks_sorted
63-
ItemSorter.sort_by_field(tracks, field=TrackField.TITLE, reverse=True)
70+
ItemSorter.sort_by_field(tracks, field=TrackField.TITLE, reverse=True, ignore_words=ignore_words)
6471
assert tracks == tracks_sorted[::-1]
6572

6673
def test_group_by_field(self, tracks: list[LocalTrack]):
@@ -71,15 +78,20 @@ def test_group_by_field(self, tracks: list[LocalTrack]):
7178
assert sum(map(len, groups.values())) == len(tracks)
7279

7380
def test_shuffle_random(self, tracks: list[LocalTrack]):
81+
7482
tracks_original = tracks.copy()
7583
ItemSorter().sort(tracks)
7684
assert tracks == tracks_original
7785
ItemSorter(shuffle_mode=ShuffleMode.RANDOM).sort(tracks)
7886
assert tracks != tracks_original
7987

88+
def _sort_key(t: LocalTrack) -> tuple[bool, str]:
89+
not_special_start, value = strip_ignore_words(t.title)
90+
return not_special_start, value.lower()
91+
8092
# shuffle settings ignored when ``fields`` are defined
8193
ItemSorter(fields=TrackField.TITLE, shuffle_mode=ShuffleMode.RANDOM).sort(tracks)
82-
assert tracks == sorted(tracks, key=lambda t: strip_ignore_words(t.title))
94+
assert tracks == sorted(tracks, key=_sort_key)
8395

8496
def test_shuffle_rating(self, tracks: list[LocalTrack]):
8597
assert tracks != sorted(tracks, key=lambda t: t.rating, reverse=True)

0 commit comments

Comments
 (0)