Complete unit testing

This commit is contained in:
Wojciech Kozlowski 2023-05-21 17:20:01 +02:00
parent 001f0e2dfc
commit 8d7e25aca6

View File

@ -28,7 +28,7 @@ pub trait IMbid {
fn mbid(&self) -> &str; fn mbid(&self) -> &str;
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug)]
enum UrlType { enum UrlType {
MusicBrainz, MusicBrainz,
MusicButler, MusicButler,
@ -36,12 +36,17 @@ enum UrlType {
Qobuz, Qobuz,
} }
#[derive(Debug, PartialEq, Eq)]
struct InvalidUrlError { struct InvalidUrlError {
url_type: UrlType, url_type: UrlType,
url: String, url: String,
} }
impl fmt::Display for InvalidUrlError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid url of type {:?}: {}", self.url_type, self.url)
}
}
/// MusicBrainz reference. /// MusicBrainz reference.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct MusicBrainz(Url); pub struct MusicBrainz(Url);
@ -422,7 +427,7 @@ impl From<uuid::Error> for Error {
impl From<InvalidUrlError> for Error { impl From<InvalidUrlError> for Error {
fn from(err: InvalidUrlError) -> Error { fn from(err: InvalidUrlError) -> Error {
Error::InvalidUrlError(format!("{:?} - {}", err.url_type, err.url)) Error::InvalidUrlError(err.to_string())
} }
} }
@ -620,11 +625,27 @@ mod tests {
assert_eq!(url, mb.url()); assert_eq!(url, mb.url());
assert_eq!(uuid, mb.mbid()); assert_eq!(uuid, mb.mbid());
let url = format!("not a url at all");
let expected_error: Error = url::ParseError::RelativeUrlWithoutBase.into();
let actual_error = MusicBrainz::new(&url).unwrap_err();
assert_eq!(actual_error, expected_error);
assert_eq!(actual_error.to_string(), expected_error.to_string());
let url = format!("https://musicbrainz.org/artist/i-am-not-a-uuid"); let url = format!("https://musicbrainz.org/artist/i-am-not-a-uuid");
assert!(MusicBrainz::new(&url).is_err()); let expected_error: Error = Uuid::try_parse("i-am-not-a-uuid").unwrap_err().into();
let actual_error = MusicBrainz::new(&url).unwrap_err();
assert_eq!(actual_error, expected_error);
assert_eq!(actual_error.to_string(), expected_error.to_string());
let url = format!("https://musicbrainz.org/artist"); let url = format!("https://musicbrainz.org/artist");
assert!(MusicBrainz::new(&url).is_err()); let expected_error: Error = InvalidUrlError {
url_type: UrlType::MusicBrainz,
url: url.clone(),
}
.into();
let actual_error = MusicBrainz::new(&url).unwrap_err();
assert_eq!(actual_error, expected_error);
assert_eq!(actual_error.to_string(), expected_error.to_string());
} }
#[test] #[test]