diff --git a/src/lib.rs b/src/lib.rs index 90c040b..60e1ebc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -403,6 +403,122 @@ mod tests { items } + #[test] + fn merge_track() { + let left = Track { + id: TrackId { + number: 04, + 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 = COLLECTION[0].albums[0].to_owned(); + let mut right = 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); + assert_eq!(expected, merged); + } + + #[test] + fn merge_album_overlap() { + let mut left = COLLECTION[0].albums[0].to_owned(); + let mut right = 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 = COLLECTION[0].to_owned(); + let mut right = COLLECTION[1].to_owned(); + right.id = left.id.clone(); + + let mut expected = left.clone(); + expected.albums.append(&mut right.albums.clone()); + expected.albums.sort_unstable(); + + let merged = left.clone().merge(right); + assert_eq!(expected, merged); + } + + #[test] + fn merge_artist_overlap() { + let mut left = COLLECTION[0].to_owned(); + let mut right = 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.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 = COLLECTION.len() / 2; + + let left = COLLECTION[..half].to_owned(); + let right = COLLECTION[half..].to_owned(); + + let mut expected = COLLECTION.to_owned(); + expected.sort_unstable(); + + let merged = MusicHoard::::merge(left.clone(), right); + assert_eq!(expected, merged); + } + + #[test] + fn merge_collection_overlap() { + let half: usize = COLLECTION.len() / 2; + + let left = COLLECTION[..(half+1)].to_owned(); + let right = COLLECTION[half..].to_owned(); + + let mut expected = COLLECTION.to_owned(); + expected.sort_unstable(); + + let merged = MusicHoard::::merge(left.clone(), right); + assert_eq!(expected, merged); + } + #[test] fn rescan_library_ordered() { let mut library = MockILibrary::new(); @@ -472,7 +588,26 @@ mod tests { } #[test] - fn read_get_write() { + fn load_database() { + let library = MockILibrary::new(); + let mut database = MockIDatabase::new(); + + database + .expect_load() + .times(1) + .return_once(|coll: &mut Collection| { + *coll = COLLECTION.to_owned(); + Ok(()) + }); + + let mut music_hoard = MusicHoard::new(library, database); + + music_hoard.load_from_database().unwrap(); + assert_eq!(music_hoard.get_collection(), &*COLLECTION); + } + + #[test] + fn rescan_get_save() { let mut library = MockILibrary::new(); let mut database = MockIDatabase::new(); @@ -524,7 +659,30 @@ mod tests { } #[test] - fn database_error() { + fn database_load_error() { + let library = MockILibrary::new(); + let mut database = MockIDatabase::new(); + + let database_result = Err(database::LoadError::IoError(String::from("I/O error"))); + + database + .expect_load() + .times(1) + .return_once(|_: &mut Collection| database_result); + + let mut music_hoard = MusicHoard::new(library, database); + + let actual_err = music_hoard.load_from_database().unwrap_err(); + let expected_err = Error::DatabaseError( + database::LoadError::IoError(String::from("I/O error")).to_string(), + ); + + assert_eq!(actual_err, expected_err); + assert_eq!(actual_err.to_string(), expected_err.to_string()); + } + + #[test] + fn database_save_error() { let library = MockILibrary::new(); let mut database = MockIDatabase::new();