From 8d1b52bb56d652aca465798ea87fd34b0490f3be Mon Sep 17 00:00:00 2001 From: Vo Van Nghia Date: Thu, 10 Oct 2024 23:24:36 +0200 Subject: [PATCH] add album cleanup test --- nghe-backend/src/file/audio/artist.rs | 4 +- nghe-backend/src/file/audio/name_date_mbz.rs | 49 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/nghe-backend/src/file/audio/artist.rs b/nghe-backend/src/file/audio/artist.rs index cdf33dec..1d3bb9f3 100644 --- a/nghe-backend/src/file/audio/artist.rs +++ b/nghe-backend/src/file/audio/artist.rs @@ -527,7 +527,7 @@ mod tests { #[case] n_subset: usize, #[values(true, false)] compilation: bool, ) { - let artist: Artist = "Artist".into(); + let artist: Artist = Faker.fake(); let song_ids: Vec<_> = stream::iter(0..n_song) .then(async |_| { Mock::information() @@ -575,7 +575,7 @@ mod tests { #[case] n_album: usize, #[case] n_subset: usize, ) { - let artist: Artist = "Artist".into(); + let artist: Artist = Faker.fake(); let album_song_ids: Vec<(Uuid, Vec<_>)> = stream::iter(0..n_album) .then(async |_| { let album: audio::Album = Faker.fake(); diff --git a/nghe-backend/src/file/audio/name_date_mbz.rs b/nghe-backend/src/file/audio/name_date_mbz.rs index 3ffd0b37..4df7ff26 100644 --- a/nghe-backend/src/file/audio/name_date_mbz.rs +++ b/nghe-backend/src/file/audio/name_date_mbz.rs @@ -63,6 +63,7 @@ impl<'a> Album<'a> { mod test { use diesel::{ExpressionMethods, QueryDsl, SelectableHelper}; use diesel_async::RunQueryDsl; + use futures_lite::{stream, StreamExt}; use super::*; use crate::test::Mock; @@ -84,12 +85,23 @@ mod test { .try_into() .unwrap() } + + pub async fn queries(mock: &Mock) -> Vec { + let ids = albums::table + .select(albums::id) + .order_by(albums::name) + .get_results(&mut mock.get().await) + .await + .unwrap(); + stream::iter(ids).then(async |id| Self::query(mock, id).await).collect().await + } } } #[cfg(test)] mod tests { use fake::{Fake, Faker}; + use futures_lite::{stream, StreamExt}; use rstest::rstest; use super::*; @@ -133,4 +145,41 @@ mod tests { let update_id = album.upsert_mock(&mock, 0).await; assert_eq!(update_id, id); } + + mod cleanup { + use super::*; + + #[rstest] + #[case(1, 0)] + #[case(1, 1)] + #[case(5, 3)] + #[case(5, 5)] + #[tokio::test] + async fn test_album( + #[future(awt)] mock: Mock, + #[case] n_song: usize, + #[case] n_subset: usize, + ) { + let album: Album = Faker.fake(); + let song_ids: Vec<_> = stream::iter(0..n_song) + .then(async |_| { + Mock::information() + .album(album.clone()) + .call() + .upsert_mock(&mock, 0, Faker.fake::(), None) + .await + }) + .collect() + .await; + assert!(Album::queries(&mock).await.contains(&album)); + + diesel::delete(songs::table) + .filter(songs::id.eq_any(&song_ids[0..n_subset])) + .execute(&mut mock.get().await) + .await + .unwrap(); + Album::cleanup(mock.database()).await.unwrap(); + assert_eq!(Album::queries(&mock).await.contains(&album), n_subset < n_song); + } + } }