From 74a68b550301228f5dd31cad68738394b290abe0 Mon Sep 17 00:00:00 2001 From: Vo Van Nghia Date: Fri, 2 Feb 2024 16:28:52 +0700 Subject: [PATCH] feat(utils/test): move assert song info to helper function --- src/open_subsonic/scan/scan_full.rs | 75 ++--------------------------- src/utils/test/media.rs | 32 ++++++++++++ 2 files changed, 36 insertions(+), 71 deletions(-) diff --git a/src/open_subsonic/scan/scan_full.rs b/src/open_subsonic/scan/scan_full.rs index 000c7b81..d96f8236 100644 --- a/src/open_subsonic/scan/scan_full.rs +++ b/src/open_subsonic/scan/scan_full.rs @@ -130,7 +130,7 @@ mod tests { open_subsonic::browsing::test::setup_user_and_music_folders, utils::{ song::file_type::{to_extension, to_extensions}, - test::media::query_all_songs_information, + test::media::assert_song_info, }, }; @@ -156,30 +156,8 @@ mod tests { scan_full::<&str>(db.get_pool(), &[], &music_folders) .await .unwrap(); - let mut song_db_info = query_all_songs_information(db.get_pool()).await; - for (song_key, song_tag) in song_fs_info { - let (song, album, artists, album_artists) = song_db_info.remove(&song_key).unwrap(); - assert_eq!(song_tag.title, song.title); - assert_eq!(song_tag.album, album.name); - assert_eq!( - song_tag.artists.into_iter().sorted().collect_vec(), - artists - .into_iter() - .map(|artist| artist.name) - .sorted() - .collect_vec() - ); - assert_eq!( - song_tag.album_artists.into_iter().sorted().collect_vec(), - album_artists - .into_iter() - .map(|artist| artist.name) - .sorted() - .collect_vec() - ); - } - assert!(song_db_info.is_empty()); + assert_song_info(db.get_pool(), song_fs_info).await; } #[tokio::test] @@ -228,30 +206,7 @@ mod tests { .await .unwrap(); - let mut song_db_info = query_all_songs_information(db.get_pool()).await; - - for (song_key, song_tag) in song_fs_info { - let (song, album, artists, album_artists) = song_db_info.remove(&song_key).unwrap(); - assert_eq!(song_tag.title, song.title); - assert_eq!(song_tag.album, album.name); - assert_eq!( - song_tag.artists.into_iter().sorted().collect_vec(), - artists - .into_iter() - .map(|artist| artist.name) - .sorted() - .collect_vec() - ); - assert_eq!( - song_tag.album_artists.into_iter().sorted().collect_vec(), - album_artists - .into_iter() - .map(|artist| artist.name) - .sorted() - .collect_vec() - ); - } - assert!(song_db_info.is_empty()); + assert_song_info(db.get_pool(), song_fs_info).await; } #[tokio::test] @@ -275,30 +230,8 @@ mod tests { scan_full::<&str>(db.get_pool(), &[], &music_folders) .await .unwrap(); - let mut song_db_info = query_all_songs_information(db.get_pool()).await; - for (song_key, song_tag) in song_fs_info { - let (song, album, artists, album_artists) = song_db_info.remove(&song_key).unwrap(); - assert_eq!(song_tag.title, song.title); - assert_eq!(song_tag.album, album.name); - assert_eq!( - song_tag.artists.into_iter().sorted().collect_vec(), - artists - .into_iter() - .map(|artist| artist.name) - .sorted() - .collect_vec() - ); - assert_eq!( - song_tag.album_artists.into_iter().sorted().collect_vec(), - album_artists - .into_iter() - .map(|artist| artist.name) - .sorted() - .collect_vec() - ); - } - assert!(song_db_info.is_empty()); + assert_song_info(db.get_pool(), song_fs_info).await; } #[tokio::test] diff --git a/src/utils/test/media.rs b/src/utils/test/media.rs index 04aac0d3..ab789acb 100644 --- a/src/utils/test/media.rs +++ b/src/utils/test/media.rs @@ -1,9 +1,11 @@ use crate::models::*; +use crate::utils::song::tag::SongTag; use crate::DatabasePool; use diesel::{ExpressionMethods, QueryDsl, SelectableHelper}; use diesel_async::RunQueryDsl; use futures::stream::{self, StreamExt}; +use itertools::Itertools; use std::collections::HashMap; use std::path::PathBuf; use uuid::Uuid; @@ -102,3 +104,33 @@ pub async fn query_all_songs_information( .collect::>() .await } + +pub async fn assert_song_info( + pool: &DatabasePool, + song_fs_info: HashMap<(Uuid, PathBuf), SongTag>, +) { + let mut song_db_info = query_all_songs_information(pool).await; + + for (song_key, song_tag) in song_fs_info { + let (song, album, artists, album_artists) = song_db_info.remove(&song_key).unwrap(); + assert_eq!(song_tag.title, song.title); + assert_eq!(song_tag.album, album.name); + assert_eq!( + song_tag.artists.into_iter().sorted().collect_vec(), + artists + .into_iter() + .map(|artist| artist.name) + .sorted() + .collect_vec() + ); + assert_eq!( + song_tag.album_artists.into_iter().sorted().collect_vec(), + album_artists + .into_iter() + .map(|artist| artist.name) + .sorted() + .collect_vec() + ); + } + assert!(song_db_info.is_empty()); +}