Skip to content

Commit

Permalink
add album cleanup test
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Oct 10, 2024
1 parent 1ac33bd commit 8d1b52b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions nghe-backend/src/file/audio/artist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down
49 changes: 49 additions & 0 deletions nghe-backend/src/file/audio/name_date_mbz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -84,12 +85,23 @@ mod test {
.try_into()
.unwrap()
}

pub async fn queries(mock: &Mock) -> Vec<Self> {
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::*;
Expand Down Expand Up @@ -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::<String>(), 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);
}
}
}

0 comments on commit 8d1b52b

Please sign in to comment.