11from collections .abc import Callable
22from itertools import groupby
3- from random import choice , randrange , shuffle
3+ from random import choice , randrange , shuffle , sample
44
55import 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