#![allow(
clippy::arithmetic_side_effects,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic,
clippy::unwrap_used
)]
use super::*;
use crate::pid::{PersistentIdentifierConvert, PID};
use crate::Validate;
const CONTRACT_CANONICAL: &str = "10.83962/fb5be317";
const CONTRACT_INVALID: &str = "11.83962/fb5be317";
const CONTRACT_VALID: &str = "https://raid.org/10.83962/fb5be317";
const FIXTURES: &str = "../../tests/fixtures";
#[test]
fn test_idutils_raid_cases() {
[
"10.26259/0e59e9a5",
"10.83962/fb5be317",
"https://raid.org/10.26259/0e59e9a5",
"https://raid.org/10.83962/f2a7645d",
"HTTPS://RAID.ORG/10.26259/0E59E9A5",
]
.into_iter()
.for_each(|value| assert!(RAID::is_valid(value), "expected valid RAiD: {value}"));
["", "not-a-raid", "11.26259/0e59e9a5", "102.26259/0e59e9a5"]
.into_iter()
.for_each(|value| assert!(!RAID::is_valid(value), "expected invalid RAiD: {value}"));
}
#[test]
fn test_raid() {
let valid = "https://raid.org/10.83962/fb5be317";
let pid = RAID::from_string(valid);
assert_eq!(pid.to_string(), "10.83962/fb5be317");
assert_eq!(pid.schema_uri(), "https://raid.org");
assert_eq!(valid.to_pid(PID::RAID).to_raid().to_string(), "10.83962/fb5be317");
assert!(valid.is_pid(PID::RAID));
assert!(RAID::is_valid(valid));
assert_eq!(pid.to_string(), "10.83962/fb5be317");
assert_eq!(valid.format_as(PID::RAID), "10.83962/fb5be317");
assert_eq!(RAID::format("10.1000/182"), "10.1000/182");
assert_eq!(RAID::format("https://raid.org/10.1000/182"), "10.1000/182");
}
#[test]
fn test_raid_vocabulary_conversions() {
assert!(matches!(
CreditRole::try_from("https://credit.niso.org/contributor-roles/software/"),
Ok(CreditRole::Software)
));
assert!(CreditRole::try_from("unknown").is_err());
assert!(matches!(ObjectType::from("Book Chapter"), ObjectType::BookChapter));
assert!(matches!(ObjectType::from("unknown"), ObjectType::Text));
assert!(matches!(
OrganizationRoleType::from("partner_organisation"),
OrganizationRoleType::PartnerOrganization
));
assert!(OrganizationRoleType::from("unknown").is_other_organization());
assert!(matches!(
PositionType::try_from("principal investigator"),
Ok(PositionType::PrincipalInvestigator)
));
assert!(PositionType::try_from("unknown").is_err());
assert!(matches!(RelatedRaidType::try_from("is derived from"), Ok(RelatedRaidType::IsDerivedFrom)));
assert!(RelatedRaidType::try_from("unknown").is_err());
}
#[test]
fn test_raid_contributor_accepts_orcid_and_isni() {
let contributor = |id: &str, schema_uri: &str| crate::pid::raid::Contributor {
id: Some(id.to_string()),
schema_uri: Some(schema_uri.to_string()),
status: None,
status_message: None,
position: Vec::new(),
leader: false,
contact: false,
email: None,
role: None,
uuid: None,
};
assert!(contributor("https://orcid.org/0000-0002-2057-9115", "https://orcid.org/")
.validate()
.is_ok());
assert!(contributor("https://isni.org/isni/0000000492299539", "https://isni.org/")
.validate()
.is_ok());
assert!(contributor("https://isni.org/isni/0000000492299538", "https://isni.org/")
.validate()
.is_err());
assert!(contributor("https://isni.org/isni/0000000492299539", "https://orcid.org/")
.validate()
.is_err());
assert!(contributor("https://isni.org/isni/0000000492299539", "https://example.org/")
.validate()
.is_err());
}
fn validation_metadata(contributor: Option<Vec<Contributor>>, organization: Option<Vec<Organization>>) -> Metadata {
Metadata {
access: None,
contributor,
date: None,
identifier: None,
title: None,
alternate_identifier: None,
alternate_url: None,
description: None,
metadata: None,
organization,
related_object: None,
related_raid: None,
spatial_coverage: None,
subject: None,
traditional_knowledge_label: None,
}
}
#[test]
fn metadata_accepts_non_contact_contributors_when_one_contact_exists() {
let contributor = |contact, leader| Contributor {
id: None,
schema_uri: None,
status: None,
status_message: None,
position: Vec::new(),
leader,
contact,
email: None,
role: None,
uuid: None,
};
let metadata = validation_metadata(Some(vec![contributor(true, true), contributor(false, false)]), None);
assert!(validate_contributors(&metadata, &()).is_ok());
}
#[test]
fn metadata_accepts_an_empty_organization_list() {
let metadata = validation_metadata(None, Some(Vec::new()));
assert!(validate_organization(&metadata, &()).is_ok());
}
#[test]
fn test_raid_metadatatools_cases() {
[
"10.26259/0e59e9a5",
"10.83962/fb5be317",
"https://raid.org/10.26259/0e59e9a5",
"https://raid.org/10.83962/f2a7645d",
"HTTPS://RAID.ORG/10.26259/0E59E9A5",
]
.into_iter()
.for_each(|value| assert!(RAID::is_valid(value), "expected valid RAiD: {value}"));
["", "not-a-raid", "10x26259/0e59e9a5", "11.26259/0e59e9a5", "102.26259/0e59e9a5"]
.into_iter()
.for_each(|value| assert!(!RAID::is_valid(value), "expected invalid RAiD: {value}"));
assert_eq!(RAID::format(" HTTPS://RAID.ORG/10.26259/0E59E9A5 "), "10.26259/0E59E9A5");
}
#[test]
fn test_raid_read() {
["response_01.json", "response_02.json"].into_iter().for_each(|fixture| {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(FIXTURES).join("raid").join(fixture);
let content = std::fs::read_to_string(path).expect("failed to read RAiD fixture");
let data: Metadata = serde_json::from_str(&content).expect("failed to deserialize RAiD fixture");
let _ = data.validate();
});
}
#[test]
fn test_raid_trait_contract() {
assert_eq!(RAID::new().to_string(), RAID::default().to_string());
assert!(RAID::is_valid(CONTRACT_VALID));
assert!(!RAID::is_valid(CONTRACT_INVALID));
assert_eq!(RAID::format(CONTRACT_VALID), CONTRACT_CANONICAL);
let pid = RAID::from_string(CONTRACT_VALID);
assert_eq!(pid.to_string(), CONTRACT_CANONICAL);
assert_eq!(pid.schema_uri(), "https://raid.org");
assert_eq!(pid.identifier(), CONTRACT_CANONICAL);
assert_eq!(pid.prefix(), Some("10.83962".to_string()));
assert_eq!(pid.suffix(), Some("fb5be317".to_string()));
assert!(pid.check_digit().is_none());
assert!(pid.url().is_empty());
let found = <RAID as PersistentIdentifierParse>::find_all(format!("Identifier: {CONTRACT_VALID}"));
assert_eq!(found.len(), 1);
assert_eq!(found.first().map(ToString::to_string).as_deref(), Some(CONTRACT_CANONICAL));
assert!(CONTRACT_VALID.is_pid(PID::RAID));
assert_eq!(CONTRACT_VALID.format_as(PID::RAID), CONTRACT_CANONICAL);
assert_eq!(CONTRACT_VALID.to_pid(PID::RAID).to_raid().to_string(), CONTRACT_CANONICAL);
}