Move some tests around
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 2m23s
Cargo CI / Lint (pull_request) Successful in 42s

This commit is contained in:
Wojciech Kozlowski 2024-01-22 20:44:05 +01:00
parent 991f111ec5
commit b6a464f056
8 changed files with 149 additions and 133 deletions

View File

@ -38,3 +38,45 @@ impl Merge for Album {
self.tracks = MergeSorted::new(tracks.into_iter(), other.tracks.into_iter()).collect();
}
}
#[cfg(test)]
mod tests {
use crate::core::testmod::FULL_COLLECTION;
use super::*;
#[test]
fn merge_album_no_overlap() {
let left = FULL_COLLECTION[0].albums[0].to_owned();
let mut right = FULL_COLLECTION[0].albums[1].to_owned();
right.id = left.id.clone();
let mut expected = left.clone();
expected.tracks.append(&mut right.tracks.clone());
expected.tracks.sort_unstable();
let merged = left.clone().merge(right.clone());
assert_eq!(expected, merged);
// Non-overlapping merge should be commutative.
let merged = right.clone().merge(left.clone());
assert_eq!(expected, merged);
}
#[test]
fn merge_album_overlap() {
let mut left = FULL_COLLECTION[0].albums[0].to_owned();
let mut right = FULL_COLLECTION[0].albums[1].to_owned();
right.id = left.id.clone();
left.tracks.push(right.tracks[0].clone());
left.tracks.sort_unstable();
let mut expected = left.clone();
expected.tracks.append(&mut right.tracks.clone());
expected.tracks.sort_unstable();
expected.tracks.dedup();
let merged = left.clone().merge(right);
assert_eq!(expected, merged);
}
}

View File

@ -462,8 +462,16 @@ impl Display for Qobuz {
#[cfg(test)]
mod tests {
use crate::core::testmod::FULL_COLLECTION;
use super::*;
static MUSICBRAINZ: &str =
"https://musicbrainz.org/artist/d368baa8-21ca-4759-9731-0b2753071ad8";
static MUSICBUTLER: &str = "https://www.musicbutler.io/artist-page/483340948";
static BANDCAMP: &str = "https://thelasthangmen.bandcamp.com/";
static QOBUZ: &str = "https://www.qobuz.com/nl-nl/interpreter/the-last-hangmen/1244413";
#[test]
fn musicbrainz() {
let uuid = "d368baa8-21ca-4759-9731-0b2753071ad8";
@ -490,4 +498,65 @@ mod tests {
assert_eq!(actual_error, expected_error);
assert_eq!(actual_error.to_string(), expected_error.to_string());
}
#[test]
fn urls() {
assert!(MusicBrainz::new(MUSICBRAINZ).is_ok());
assert!(MusicBrainz::new(MUSICBUTLER).is_err());
assert!(MusicBrainz::new(BANDCAMP).is_err());
assert!(MusicBrainz::new(QOBUZ).is_err());
assert!(MusicButler::new(MUSICBRAINZ).is_err());
assert!(MusicButler::new(MUSICBUTLER).is_ok());
assert!(MusicButler::new(BANDCAMP).is_err());
assert!(MusicButler::new(QOBUZ).is_err());
assert!(Bandcamp::new(MUSICBRAINZ).is_err());
assert!(Bandcamp::new(MUSICBUTLER).is_err());
assert!(Bandcamp::new(BANDCAMP).is_ok());
assert!(Bandcamp::new(QOBUZ).is_err());
assert!(Qobuz::new(MUSICBRAINZ).is_err());
assert!(Qobuz::new(MUSICBUTLER).is_err());
assert!(Qobuz::new(BANDCAMP).is_err());
assert!(Qobuz::new(QOBUZ).is_ok());
}
#[test]
fn merge_artist_no_overlap() {
let left = FULL_COLLECTION[0].to_owned();
let mut right = FULL_COLLECTION[1].to_owned();
right.id = left.id.clone();
right.properties = ArtistProperties::default();
let mut expected = left.clone();
expected.properties = expected.properties.merge(right.clone().properties);
expected.albums.append(&mut right.albums.clone());
expected.albums.sort_unstable();
let merged = left.clone().merge(right.clone());
assert_eq!(expected, merged);
// Non-overlapping merge should be commutative.
let merged = right.clone().merge(left.clone());
assert_eq!(expected, merged);
}
#[test]
fn merge_artist_overlap() {
let mut left = FULL_COLLECTION[0].to_owned();
let mut right = FULL_COLLECTION[1].to_owned();
right.id = left.id.clone();
left.albums.push(right.albums[0].clone());
left.albums.sort_unstable();
let mut expected = left.clone();
expected.properties = expected.properties.merge(right.clone().properties);
expected.albums.append(&mut right.albums.clone());
expected.albums.sort_unstable();
expected.albums.dedup();
let merged = left.clone().merge(right);
assert_eq!(expected, merged);
}
}

View File

@ -48,3 +48,34 @@ impl Merge for Track {
assert_eq!(self.id, other.id);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn merge_track() {
let left = Track {
id: TrackId {
number: 4,
title: String::from("a title"),
},
artist: vec![String::from("left artist")],
quality: Quality {
format: Format::Flac,
bitrate: 1411,
},
};
let right = Track {
id: left.id.clone(),
artist: vec![String::from("right artist")],
quality: Quality {
format: Format::Mp3,
bitrate: 320,
},
};
let merged = left.clone().merge(right);
assert_eq!(left, merged);
}
}

View File

@ -66,7 +66,7 @@ mod tests {
use mockall::predicate;
use crate::{core::musichoard::testmod::FULL_COLLECTION, Artist, ArtistId, Collection};
use crate::{core::testmod::FULL_COLLECTION, Artist, ArtistId, Collection};
use super::*;
use testmod::DATABASE_JSON;

View File

@ -3,3 +3,6 @@ pub mod collection;
pub mod database;
pub mod library;
pub mod musichoard;
#[cfg(test)]
pub mod testmod;

View File

@ -54,6 +54,3 @@ impl From<database::SaveError> for Error {
Error::DatabaseError(err.to_string())
}
}
#[cfg(test)]
pub mod testmod;

View File

@ -337,15 +337,12 @@ mod tests {
use super::*;
// FIXME: check all crate::* imports - are the tests where they should be?
use crate::core::collection::{
artist::{ArtistId, ArtistProperties, Bandcamp, MusicBrainz, MusicButler, Qobuz},
track::Format,
Merge,
};
use crate::core::musichoard::testmod::{FULL_COLLECTION, LIBRARY_COLLECTION};
use crate::core::collection::artist::{ArtistId, Bandcamp, MusicBrainz, MusicButler, Qobuz};
use crate::core::testmod::{FULL_COLLECTION, LIBRARY_COLLECTION};
use crate::database::{self, MockIDatabase};
use crate::library::{self, testmod::LIBRARY_ITEMS, MockILibrary};
// FIXME: all tests with URLs should go to collection::artist
static MUSICBRAINZ: &str =
"https://musicbrainz.org/artist/d368baa8-21ca-4759-9731-0b2753071ad8";
static MUSICBRAINZ_2: &str =
@ -357,30 +354,6 @@ mod tests {
static QOBUZ: &str = "https://www.qobuz.com/nl-nl/interpreter/the-last-hangmen/1244413";
static QOBUZ_2: &str = "https://www.qobuz.com/nl-nl/interpreter/vicious-crusade/7522386";
// FIXME: all tests with URLs should go to collection::artist
#[test]
fn urls() {
assert!(MusicBrainz::new(MUSICBRAINZ).is_ok());
assert!(MusicBrainz::new(MUSICBUTLER).is_err());
assert!(MusicBrainz::new(BANDCAMP).is_err());
assert!(MusicBrainz::new(QOBUZ).is_err());
assert!(MusicButler::new(MUSICBRAINZ).is_err());
assert!(MusicButler::new(MUSICBUTLER).is_ok());
assert!(MusicButler::new(BANDCAMP).is_err());
assert!(MusicButler::new(QOBUZ).is_err());
assert!(Bandcamp::new(MUSICBRAINZ).is_err());
assert!(Bandcamp::new(MUSICBUTLER).is_err());
assert!(Bandcamp::new(BANDCAMP).is_ok());
assert!(Bandcamp::new(QOBUZ).is_err());
assert!(Qobuz::new(MUSICBRAINZ).is_err());
assert!(Qobuz::new(MUSICBUTLER).is_err());
assert!(Qobuz::new(BANDCAMP).is_err());
assert!(Qobuz::new(QOBUZ).is_ok());
}
#[test]
fn artist_new_delete() {
let artist_id = ArtistId::new("an artist");
@ -1071,105 +1044,6 @@ mod tests {
assert_eq!(music_hoard.collection[0].properties.qobuz, expected);
}
#[test]
fn merge_track() {
let left = Track {
id: TrackId {
number: 4,
title: String::from("a title"),
},
artist: vec![String::from("left artist")],
quality: Quality {
format: Format::Flac,
bitrate: 1411,
},
};
let right = Track {
id: left.id.clone(),
artist: vec![String::from("right artist")],
quality: Quality {
format: Format::Mp3,
bitrate: 320,
},
};
let merged = left.clone().merge(right);
assert_eq!(left, merged);
}
#[test]
fn merge_album_no_overlap() {
let left = FULL_COLLECTION[0].albums[0].to_owned();
let mut right = FULL_COLLECTION[0].albums[1].to_owned();
right.id = left.id.clone();
let mut expected = left.clone();
expected.tracks.append(&mut right.tracks.clone());
expected.tracks.sort_unstable();
let merged = left.clone().merge(right.clone());
assert_eq!(expected, merged);
// Non-overlapping merge should be commutative.
let merged = right.clone().merge(left.clone());
assert_eq!(expected, merged);
}
#[test]
fn merge_album_overlap() {
let mut left = FULL_COLLECTION[0].albums[0].to_owned();
let mut right = FULL_COLLECTION[0].albums[1].to_owned();
right.id = left.id.clone();
left.tracks.push(right.tracks[0].clone());
left.tracks.sort_unstable();
let mut expected = left.clone();
expected.tracks.append(&mut right.tracks.clone());
expected.tracks.sort_unstable();
expected.tracks.dedup();
let merged = left.clone().merge(right);
assert_eq!(expected, merged);
}
#[test]
fn merge_artist_no_overlap() {
let left = FULL_COLLECTION[0].to_owned();
let mut right = FULL_COLLECTION[1].to_owned();
right.id = left.id.clone();
right.properties = ArtistProperties::default();
let mut expected = left.clone();
expected.properties = expected.properties.merge(right.clone().properties);
expected.albums.append(&mut right.albums.clone());
expected.albums.sort_unstable();
let merged = left.clone().merge(right.clone());
assert_eq!(expected, merged);
// Non-overlapping merge should be commutative.
let merged = right.clone().merge(left.clone());
assert_eq!(expected, merged);
}
#[test]
fn merge_artist_overlap() {
let mut left = FULL_COLLECTION[0].to_owned();
let mut right = FULL_COLLECTION[1].to_owned();
right.id = left.id.clone();
left.albums.push(right.albums[0].clone());
left.albums.sort_unstable();
let mut expected = left.clone();
expected.properties = expected.properties.merge(right.clone().properties);
expected.albums.append(&mut right.albums.clone());
expected.albums.sort_unstable();
expected.albums.dedup();
let merged = left.clone().merge(right);
assert_eq!(expected, merged);
}
#[test]
fn merge_collection_no_overlap() {
let half: usize = FULL_COLLECTION.len() / 2;