Unit tests
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 2m2s
Cargo CI / Lint (pull_request) Successful in 1m9s

This commit is contained in:
Wojciech Kozlowski 2024-09-29 16:44:19 +02:00
parent 6083e82924
commit 70e510a1de

View File

@ -87,7 +87,7 @@ pub struct BrowseReleaseGroupResponse {
pub release_groups: Vec<MbReleaseGroupMeta>, pub release_groups: Vec<MbReleaseGroupMeta>,
} }
#[derive(Deserialize)] #[derive(Clone, Deserialize)]
#[serde(rename_all(deserialize = "kebab-case"))] #[serde(rename_all(deserialize = "kebab-case"))]
struct DeserializeBrowseReleaseGroupResponse { struct DeserializeBrowseReleaseGroupResponse {
release_group_offset: usize, release_group_offset: usize,
@ -107,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);
}
}