Add unit test
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 2m1s
Cargo CI / Lint (pull_request) Successful in 1m7s

This commit is contained in:
Wojciech Kozlowski 2024-03-17 17:14:15 +01:00
parent e008bf1b10
commit 600a5a44c5
3 changed files with 42 additions and 19 deletions

View File

@ -103,7 +103,7 @@ fn main() {
};
let matches = api
.search_release_group(&arid, album)
.search_release_group(&arid, &album)
.expect("failed to make API call");
println!("{matches:#?}");

View File

@ -16,7 +16,7 @@ pub trait IMusicBrainz {
fn search_release_group(
&mut self,
arid: &Mbid,
album: Album,
album: &Album,
) -> Result<Vec<Match<Album>>, Error>;
}
@ -44,7 +44,7 @@ impl IMusicBrainz for NullMusicBrainz {
fn search_release_group(
&mut self,
_arid: &Mbid,
_album: Album,
_album: &Album,
) -> Result<Vec<Match<Album>>, Error> {
Ok(vec![])
}
@ -143,7 +143,7 @@ mod tests {
let mbid: Mbid = "d368baa8-21ca-4759-9731-0b2753071ad8".try_into().unwrap();
let album = Album::new(AlbumId::new("an album"), AlbumDate::default(), None, vec![]);
assert!(musicbrainz
.search_release_group(&mbid, album)
.search_release_group(&mbid, &album)
.unwrap()
.is_empty());
}

View File

@ -70,14 +70,14 @@ impl<Mbc: IMusicBrainzApiClient> IMusicBrainz for MusicBrainzApi<Mbc> {
fn search_release_group(
&mut self,
arid: &Mbid,
album: Album,
album: &Album,
) -> Result<Vec<Match<Album>>, Error> {
let title = &album.id.title;
let arid = arid.uuid().as_hyphenated().to_string();
let mut query = format!("arid:{arid}");
match album.musicbrainz {
Some(mbref) => {
Some(ref mbref) => {
let rgid = mbref.mbid().uuid().as_hyphenated().to_string();
query.push_str(&format!(" AND rgid:{rgid}"));
}
@ -136,13 +136,13 @@ impl TryFrom<LookupReleaseGroup> for Album {
}
}
#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all(deserialize = "kebab-case"))]
struct ResponseSearchReleaseGroup {
release_groups: Vec<SearchReleaseGroup>,
}
#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all(deserialize = "kebab-case"))]
struct SearchReleaseGroup {
score: u8,
@ -199,7 +199,7 @@ pub enum SerdeAlbumPrimaryTypeDef {
Other,
}
#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
pub struct SerdeAlbumPrimaryType(#[serde(with = "SerdeAlbumPrimaryTypeDef")] AlbumPrimaryType);
impl From<SerdeAlbumPrimaryType> for AlbumPrimaryType {
@ -242,7 +242,7 @@ impl From<SerdeAlbumSecondaryType> for AlbumSecondaryType {
#[cfg(test)]
mod tests {
use mockall::predicate;
use mockall::{predicate, Sequence};
use crate::collection::album::AlbumId;
@ -298,14 +298,21 @@ mod tests {
#[test]
fn search_release_group() {
let mut client = MockIMusicBrainzApiClient::new();
let url = format!(
let url_title = format!(
"https://musicbrainz.org/ws/2\
/release-group\
?query=arid%3A{arid}+AND+releasegroup%3A%22{title}%22+AND+firstreleasedate%3A{year}",
title = "an+album",
arid = "00000000-0000-0000-0000-000000000000",
title = "an+album",
year = "1986"
);
let url_rgid = format!(
"https://musicbrainz.org/ws/2\
/release-group\
?query=arid%3A{arid}+AND+rgid%3A{rgid}",
arid = "00000000-0000-0000-0000-000000000000",
rgid = "11111111-1111-1111-1111-111111111111",
);
let release_group = SearchReleaseGroup {
score: 67,
@ -321,17 +328,23 @@ mod tests {
// For code coverage of derive(Debug).
assert!(!format!("{response:?}").is_empty());
let mut seq = Sequence::new();
let title_response = response.clone();
client
.expect_get()
.times(1)
.with(predicate::eq(url))
.return_once(|_| Ok(response));
.with(predicate::eq(url_title))
.return_once(|_| Ok(title_response))
.in_sequence(&mut seq);
let mut api = MusicBrainzApi::new(client);
let arid: Mbid = "00000000-0000-0000-0000-000000000000".try_into().unwrap();
let album = Album::new(AlbumId::new("an album"), (1986, 4), None, vec![]);
let matches = api.search_release_group(&arid, album).unwrap();
let rgid_response = response;
client
.expect_get()
.times(1)
.with(predicate::eq(url_rgid))
.return_once(|_| Ok(rgid_response))
.in_sequence(&mut seq);
let mut album = Album::new(
AlbumId::new("an album"),
@ -344,6 +357,16 @@ mod tests {
);
let expected = vec![Match::new(67, album)];
let mut api = MusicBrainzApi::new(client);
let arid: Mbid = "00000000-0000-0000-0000-000000000000".try_into().unwrap();
let mut album = Album::new(AlbumId::new("an album"), (1986, 4), None, vec![]);
let matches = api.search_release_group(&arid, &album).unwrap();
assert_eq!(matches, expected);
let rgid = MbAlbumRef::from_uuid_str("11111111-1111-1111-1111-111111111111").unwrap();
album.set_musicbrainz_ref(rgid);
let matches = api.search_release_group(&arid, &album).unwrap();
assert_eq!(matches, expected);
}