Update unit tests
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m44s
Cargo CI / Lint (pull_request) Successful in 1m15s

This commit is contained in:
Wojciech Kozlowski 2024-03-08 23:21:08 +01:00
parent 941bd04144
commit 1b4e82c17e
2 changed files with 42 additions and 13 deletions

View File

@ -54,12 +54,20 @@ impl AlbumDate {
impl From<u32> for AlbumDate {
fn from(year: u32) -> Self {
AlbumDate {
year,
month: AlbumMonth::default(),
day: 0,
AlbumDate::new(year, AlbumMonth::default(), 0)
}
}
impl<M: Into<AlbumMonth>> From<(u32, M)> for AlbumDate {
fn from(value: (u32, M)) -> Self {
AlbumDate::new(value.0, value.1, 0)
}
}
impl<M: Into<AlbumMonth>> From<(u32, M, u8)> for AlbumDate {
fn from(value: (u32, M, u8)) -> Self {
AlbumDate::new(value.0, value.1, value.2)
}
}
#[repr(u8)]
@ -224,6 +232,21 @@ mod tests {
assert_eq!(<u8 as Into<AlbumMonth>>::into(255), AlbumMonth::None);
}
#[test]
fn album_date_from() {
let date: AlbumDate = 1986.into();
assert_eq!(date, AlbumDate::new(1986, AlbumMonth::default(), 0));
let date: AlbumDate = (1986, 5).into();
assert_eq!(date, AlbumDate::new(1986, AlbumMonth::May, 0));
let date: AlbumDate = (1986, AlbumMonth::June).into();
assert_eq!(date, AlbumDate::new(1986, AlbumMonth::June, 0));
let date: AlbumDate = (1986, 6, 8).into();
assert_eq!(date, AlbumDate::new(1986, AlbumMonth::June, 8));
}
#[test]
fn same_date_seq_cmp() {
let date = AlbumDate::new(2024, 3, 2);

View File

@ -41,14 +41,11 @@ impl MusicBrainz {
return Err(Self::invalid_url_error(url, mb_type));
}
match url.path_segments().and_then(|mut ps| ps.nth(0)) {
Some(typ) => {
if typ != mb_type {
// path_segments only returns an empty iterator if the URL cannot-be-a-base. However, if the
// URL cannot-be-a-base then it will fail the check above already as it won't have a domain.
if url.path_segments().and_then(|mut ps| ps.nth(0)).unwrap() != mb_type {
return Err(Self::invalid_url_error(url, mb_type));
}
}
None => return Err(Self::invalid_url_error(url, mb_type)),
};
match url.path_segments().and_then(|mut ps| ps.nth(1)) {
Some(segment) => Uuid::try_parse(segment)?,
@ -156,6 +153,15 @@ mod tests {
assert_eq!(actual_error.to_string(), expected_error.to_string());
}
#[test]
fn missing_type() {
let url = "https://musicbrainz.org";
let expected_error = Error::UrlError(format!("invalid artist MusicBrainz URL: {url}/"));
let actual_error = MusicBrainz::artist_from_str(url).unwrap_err();
assert_eq!(actual_error, expected_error);
assert_eq!(actual_error.to_string(), expected_error.to_string());
}
#[test]
fn missing_uuid() {
let url = "https://musicbrainz.org/artist";