Compare commits

...

2 Commits

Author SHA1 Message Date
70e510a1de Unit tests
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 2m2s
Cargo CI / Lint (pull_request) Successful in 1m9s
2024-09-29 16:44:19 +02:00
6083e82924 fix 2024-09-29 16:33:35 +02:00

View File

@ -5,13 +5,11 @@ use serde::Deserialize;
use crate::{
collection::musicbrainz::Mbid,
external::musicbrainz::{
api::{Error, MusicBrainzClient, MB_BASE_URL},
api::{Error, MbReleaseGroupMeta, MusicBrainzClient, SerdeMbReleaseGroupMeta, MB_BASE_URL},
IMusicBrainzHttp,
},
};
use super::{MbReleaseGroupMeta, SerdeMbReleaseGroupMeta};
const MB_MAX_BROWSE_LIMIT: usize = 100;
impl<Http: IMusicBrainzHttp> MusicBrainzClient<Http> {
@ -89,7 +87,7 @@ pub struct BrowseReleaseGroupResponse {
pub release_groups: Vec<MbReleaseGroupMeta>,
}
#[derive(Deserialize)]
#[derive(Clone, Deserialize)]
#[serde(rename_all(deserialize = "kebab-case"))]
struct DeserializeBrowseReleaseGroupResponse {
release_group_offset: usize,
@ -109,3 +107,95 @@ impl From<DeserializeBrowseReleaseGroupResponse> for BrowseReleaseGroupResponse
}
}
}
#[cfg(test)]
mod tests {
use mockall::{predicate, Sequence};
use crate::{
collection::album::{AlbumPrimaryType, AlbumSecondaryType},
external::musicbrainz::{
api::{SerdeAlbumDate, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType, SerdeMbid},
MockIMusicBrainzHttp,
},
};
use super::*;
#[test]
fn browse_release_group() {
let mbid = "00000000-0000-0000-0000-000000000000";
let mut http = MockIMusicBrainzHttp::new();
let de_release_group_offset = 24;
let de_release_group_count = 302;
let de_meta = SerdeMbReleaseGroupMeta {
id: SerdeMbid("11111111-1111-1111-1111-111111111111".try_into().unwrap()),
title: String::from("an album"),
first_release_date: SerdeAlbumDate((1986, 4).into()),
primary_type: Some(SerdeAlbumPrimaryType(AlbumPrimaryType::Album)),
secondary_types: Some(vec![SerdeAlbumSecondaryType(
AlbumSecondaryType::Compilation,
)]),
};
let de_response = DeserializeBrowseReleaseGroupResponse {
release_group_offset: de_release_group_offset,
release_group_count: de_release_group_count,
release_groups: Some(vec![de_meta.clone()]),
};
let response = BrowseReleaseGroupResponse {
release_group_offset: de_release_group_offset,
release_group_count: de_release_group_count,
release_groups: vec![de_meta.clone().into()],
};
let mut seq = Sequence::new();
let url = format!("https://musicbrainz.org/ws/2/release-group?artist={mbid}",);
let expect_response = de_response.clone();
http.expect_get()
.times(1)
.in_sequence(&mut seq)
.with(predicate::eq(url))
.return_once(|_| Ok(expect_response));
let url = format!(
"https://musicbrainz.org/ws/2/release-group?artist={mbid}&limit={MB_MAX_BROWSE_LIMIT}",
);
let expect_response = de_response.clone();
http.expect_get()
.times(1)
.in_sequence(&mut seq)
.with(predicate::eq(url))
.return_once(|_| Ok(expect_response));
let url = format!(
"https://musicbrainz.org/ws/2/release-group?artist={mbid}&limit={MB_MAX_BROWSE_LIMIT}&offset={offset}",
offset = de_release_group_offset,
);
let expect_response = de_response.clone();
http.expect_get()
.times(1)
.in_sequence(&mut seq)
.with(predicate::eq(url))
.return_once(|_| Ok(expect_response));
let mut client = MusicBrainzClient::new(http);
let mbid: Mbid = mbid.try_into().unwrap();
let request = BrowseReleaseGroupRequest::artist(&mbid);
let result = client.browse_release_group(&request).unwrap();
assert_eq!(result, response);
let request = request.with_max_limit();
let result = client.browse_release_group(&request).unwrap();
assert_eq!(result, response);
let mut request = request;
request.with_offset(de_release_group_offset);
let result = client.browse_release_group(&request).unwrap();
assert_eq!(result, response);
}
}