@@ -3,7 +3,7 @@ use sqlx::{Connection, SqliteConnection, sqlite};
33use std:: path:: PathBuf ;
44
55use super :: database:: DB ;
6- use super :: track:: Track ;
6+ use super :: track:: { Artist , Track } ;
77
88/** ----------------------------------------------------------------------------
99 * Test data
@@ -15,8 +15,9 @@ fn sample_track_1() -> Track {
1515 path : "/music/artist1/album1/track1.mp3" . to_string ( ) ,
1616 title : "Song One" . to_string ( ) ,
1717 album : "Album One" . to_string ( ) ,
18- album_artist : "Artist One" . to_string ( ) ,
19- artists : vec ! [ "Artist One" . to_string( ) ] ,
18+ album_artist : "A Perfect Circle" . to_string ( ) ,
19+ album_artist_sort : "Perfect Circle, A" . to_string ( ) ,
20+ artists : vec ! [ "A Perfect Circle" . to_string( ) ] ,
2021 genres : vec ! [ "Pop" . to_string( ) , "Rock" . to_string( ) ] ,
2122 year : Some ( 2023 ) ,
2223 duration : 210 ,
@@ -34,8 +35,9 @@ fn sample_track_2() -> Track {
3435 path : "/music/artist2/album2/track2.mp3" . to_string ( ) ,
3536 title : "Song Two" . to_string ( ) ,
3637 album : "Album Two" . to_string ( ) ,
37- album_artist : "Artist Two" . to_string ( ) ,
38- artists : vec ! [ "Artist Two" . to_string( ) ] ,
38+ album_artist : "The Beatles" . to_string ( ) ,
39+ album_artist_sort : "Beatles, The" . to_string ( ) ,
40+ artists : vec ! [ "The Beatles" . to_string( ) ] ,
3941 genres : vec ! [ "Jazz" . to_string( ) ] ,
4042 year : None ,
4143 duration : 180 ,
@@ -53,8 +55,9 @@ fn sample_track_3() -> Track {
5355 path : "/music/artist3/album3/track3.mp3" . to_string ( ) ,
5456 title : "Song Three" . to_string ( ) ,
5557 album : "Album Three" . to_string ( ) ,
56- album_artist : "Artist Three" . to_string ( ) ,
57- artists : vec ! [ "Artist Three" . to_string( ) , "Artist Four" . to_string( ) ] ,
58+ album_artist : "#1 Artist" . to_string ( ) ,
59+ album_artist_sort : "#1 Artist" . to_string ( ) ,
60+ artists : vec ! [ "#1 Artist" . to_string( ) ] ,
5861 genres : vec ! [ "Hip-Hop" . to_string( ) ] ,
5962 year : Some ( 2022 ) ,
6063 duration : 240 ,
@@ -66,6 +69,46 @@ fn sample_track_3() -> Track {
6669 }
6770}
6871
72+ fn sample_track_beatles_secondary_sort ( ) -> Track {
73+ Track {
74+ id : "artist-b-2" . to_string ( ) ,
75+ path : "/music/b2.mp3" . to_string ( ) ,
76+ title : "Track B2" . to_string ( ) ,
77+ album : "Album B" . to_string ( ) ,
78+ album_artist : "The Beatles" . to_string ( ) ,
79+ album_artist_sort : "Beatles" . to_string ( ) ,
80+ artists : vec ! [ "The Beatles" . to_string( ) ] ,
81+ genres : vec ! [ "Rock" . to_string( ) ] ,
82+ year : Some ( 2024 ) ,
83+ duration : 180 ,
84+ track_no : Some ( 1 ) ,
85+ track_of : Some ( 1 ) ,
86+ disk_no : Some ( 1 ) ,
87+ disk_of : Some ( 1 ) ,
88+ is_compilation : false ,
89+ }
90+ }
91+
92+ fn sample_track_compilation ( ) -> Track {
93+ Track {
94+ id : "artist-compilation" . to_string ( ) ,
95+ path : "/music/compilation.mp3" . to_string ( ) ,
96+ title : "Track C" . to_string ( ) ,
97+ album : "Compilation" . to_string ( ) ,
98+ album_artist : "Various Artists" . to_string ( ) ,
99+ album_artist_sort : "Various Artists" . to_string ( ) ,
100+ artists : vec ! [ "Various Artists" . to_string( ) ] ,
101+ genres : vec ! [ ] ,
102+ year : Some ( 2024 ) ,
103+ duration : 180 ,
104+ track_no : Some ( 1 ) ,
105+ track_of : Some ( 1 ) ,
106+ disk_no : Some ( 1 ) ,
107+ disk_of : Some ( 1 ) ,
108+ is_compilation : true ,
109+ }
110+ }
111+
69112async fn get_test_db ( ) -> DB {
70113 let options = SqliteConnectOptions :: new ( )
71114 . in_memory ( true )
@@ -115,32 +158,56 @@ async fn test_tracks_db() {
115158 track_to_update. title = "Song Two Point Five" . to_string ( ) ;
116159 db. update_track ( track_to_update) . await . unwrap ( ) ;
117160 tracks = db. get_tracks ( & vec ! [ "2" . to_string( ) ] ) . await . unwrap ( ) ;
118- assert_eq ! (
119- tracks,
120- vec![ Track {
121- id: "2" . to_string( ) ,
122- path: "/music/artist2/album2/track2.mp3" . to_string( ) ,
123- title: "Song Two Point Five" . to_string( ) ,
124- album: "Album Two" . to_string( ) ,
125- album_artist: "Artist Two" . to_string( ) ,
126- artists: vec![ "Artist Two" . to_string( ) ] ,
127- genres: vec![ "Jazz" . to_string( ) ] ,
128- year: None ,
129- duration: 180 ,
130- track_no: None ,
131- track_of: None ,
132- disk_no: None ,
133- disk_of: None ,
134- is_compilation: false ,
135- } ]
136- ) ;
161+ let mut expected = sample_track_2 ( ) ;
162+ expected. title = "Song Two Point Five" . to_string ( ) ;
163+ assert_eq ! ( tracks, vec![ expected] ) ;
137164
138165 // Test deletion
139166 db. remove_tracks ( & vec ! [ "2" . to_string( ) ] ) . await . unwrap ( ) ;
140167 all_tracks = db. get_all_tracks ( ) . await . unwrap ( ) ;
141168 assert_eq ! ( all_tracks, vec![ sample_track_1( ) , sample_track_3( ) ] ) ;
142169}
143170
171+ /** ----------------------------------------------------------------------------
172+ * Integration Test - Artists Sorting
173+ * -------------------------------------------------------------------------- */
174+
175+ #[ tokio:: test]
176+ async fn test_get_artists_uses_sort_as_and_excludes_compilations ( ) {
177+ let mut db = get_test_db ( ) . await ;
178+
179+ db. insert_tracks ( vec ! [
180+ sample_track_1( ) ,
181+ sample_track_2( ) ,
182+ // Duplicate display artist with a lexicographically lower sort key.
183+ sample_track_beatles_secondary_sort( ) ,
184+ // Compilation artists must be excluded from get_artists.
185+ sample_track_compilation( ) ,
186+ sample_track_3( ) ,
187+ ] )
188+ . await
189+ . unwrap ( ) ;
190+
191+ let artists: Vec < Artist > = db. get_artists ( ) . await . unwrap ( ) ;
192+
193+ assert_eq ! ( artists. len( ) , 3 ) ;
194+ assert ! (
195+ artists
196+ . iter( )
197+ . all( |artist| artist. label != "Various Artists" )
198+ ) ;
199+
200+ // Order is based on sort_as and groups symbols after A-Z labels.
201+ assert_eq ! ( artists[ 0 ] . label, "The Beatles" ) ;
202+ assert_eq ! ( artists[ 0 ] . sort_as, "Beatles" ) ;
203+
204+ assert_eq ! ( artists[ 1 ] . label, "A Perfect Circle" ) ;
205+ assert_eq ! ( artists[ 1 ] . sort_as, "Perfect Circle, A" ) ;
206+
207+ assert_eq ! ( artists[ 2 ] . label, "#1 Artist" ) ;
208+ assert_eq ! ( artists[ 2 ] . sort_as, "#1 Artist" ) ;
209+ }
210+
144211/** ----------------------------------------------------------------------------
145212 * Integration Test - Playlists
146213 * -------------------------------------------------------------------------- */
0 commit comments