//! `GET /genes/{symbol}/aliases`: Wikidata's aliases, cached in LMDB. //! //! The cache is the point of the endpoint -- Wikidata is a shared public //! service and an alias list is not news -- so the tests pin the caching //! behaviour (a second call is served from `gene-aliases.lmdb`), the shape, //! and the species switch. They need the network for the first call; when //! Wikidata cannot be reached the test says so and checks the failure is //! reported honestly (a 502, not an empty alias list). use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; async fn ready() -> Option { common::auth_token().await } #[tokio::test] async fn a_symbol_gets_its_wikidata_aliases_and_they_are_cached() { let Some(token) = ready().await else { eprintln!("skipping a_symbol_gets_its_wikidata_aliases_and_they_are_cached: no token"); return; }; let first = common::get_with_token( "GET /genes/{symbol}/aliases", "/genes/Shh/aliases", &token, ) .await; let status = first.status(); let bytes = first.into_body().collect().await.unwrap().to_bytes(); if status != StatusCode::OK { // No network, or Wikidata is down: the honest answer is a 502 that // says so, never an empty alias list. eprintln!("skipping: Wikidata answered {status}: {}", String::from_utf8_lossy(&bytes)); assert_eq!(status, StatusCode::BAD_GATEWAY); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "wikidata_unavailable"); return; } let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let answer = &envelope["data"]; if answer["stale"] == serde_json::json!(true) { // Wikidata answered too slowly and the cached entity was served: the // endpoint is behaving, the test is not in a position to assert. eprintln!("skipping: the reply is stale (Wikidata timed out), not a failure"); return; } assert_eq!(answer["symbol"], "Shh"); assert!( answer["wikidata_id"].as_str().is_some_and(|id| id.starts_with('Q')), "a Wikidata entity: {answer}" ); assert!( answer["aliases"].as_array().is_some_and(|aliases| !aliases.is_empty()), "Shh has aliases: {answer}" ); assert_eq!(answer["stale"], serde_json::json!(false)); let notes: Vec<&str> = envelope["notes"] .as_array() .unwrap() .iter() .map(|note| note.as_str().unwrap()) .collect(); assert!( notes.iter().any(|note| note.contains("gene-aliases.lmdb")), "the cache file is named in the reply: {notes:?}" ); // The second call must be served from the cache: no SPARQL round trip. let second = common::get_with_token( "GET /genes/{symbol}/aliases (cached)", "/genes/Shh/aliases", &token, ) .await; assert_eq!(second.status(), StatusCode::OK); let bytes = second.into_body().collect().await.unwrap().to_bytes(); let cached: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!( cached["data"]["aliases"], answer["aliases"], "the cache answers the same list" ); assert_eq!( cached["data"]["fetched_at"], answer["fetched_at"], "and it is the same fetch, not a new one" ); } #[tokio::test] async fn the_species_switch_picks_a_different_entity() { let Some(token) = ready().await else { eprintln!("skipping the_species_switch_picks_a_different_entity: no token"); return; }; let human = common::get_with_token( "GET /genes/{symbol}/aliases?species=human", "/genes/Shh/aliases?species=human", &token, ) .await; if human.status() != StatusCode::OK { eprintln!("skipping: Wikidata unavailable for the species switch"); return; } let bytes = human.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(envelope["data"]["species"], "human"); // The human ortholog's labels are the ones GeneNetwork2's trait page // shows for a mouse trait: HLP3, HPE3 and the rest. let aliases = envelope["data"]["aliases"].as_array().unwrap(); let joined = aliases .iter() .filter_map(|alias| alias.as_str()) .collect::>() .join(","); assert!( joined.contains("HLP3") || joined.contains("HPE3"), "human SHH labels: {joined}" ); } /// The link-outs, and the `label` that makes the human-only services reachable /// from a rodent symbol: GTEx is human, so a mouse `Shh` gets `SHH` through /// `?species=human`, never a guess. #[tokio::test] async fn aliases_carry_link_outs_for_the_matched_species() { let Some(token) = ready().await else { eprintln!("skipping aliases_carry_link_outs_for_the_matched_species: no token"); return; }; let mouse = common::get_with_token( "GET /genes/Shh/aliases", "/genes/Shh/aliases", &token, ) .await; if mouse.status() != StatusCode::OK { eprintln!("skipping: Wikidata unavailable for the mouse entity"); return; } let bytes = mouse.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let answer = &envelope["data"]; let links = answer["links"].as_array().unwrap(); let names: Vec<&str> = links .iter() .map(|link| link["name"].as_str().unwrap()) .collect(); // A mouse gene: the mouse-specific services are there, GTEx is not. for expected in ["RGD", "MGI", "Alliance of Genome Resources", "NCBI Gene", "UniProt"] { assert!(names.contains(&expected), "{expected} in {names:?}"); } assert!(!names.contains(&"GTEx"), "GTEx is human-only: {names:?}"); let rgd = links .iter() .find(|link| link["name"] == "RGD") .unwrap(); assert!(rgd["href"].as_str().unwrap().contains("species=Mouse"), "{rgd}"); // The human ortholog: a label a human-only service can use, and its link. let human = common::get_with_token( "GET /genes/Shh/aliases?species=human", "/genes/Shh/aliases?species=human", &token, ) .await; if human.status() != StatusCode::OK { eprintln!("skipping the human half: Wikidata unavailable"); return; } let bytes = human.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let answer = &envelope["data"]; if answer["stale"] == serde_json::json!(true) { eprintln!("skipping the human half: the reply is stale, so its label may predate this field"); return; } assert_eq!(answer["label"], "SHH", "the human symbol, not the mouse one"); let gtex = answer["links"] .as_array() .unwrap() .iter() .find(|link| link["name"] == "GTEx") .expect("a human entity gets a GTEx link"); assert_eq!( gtex["href"], "https://www.gtexportal.org/home/gene/SHH", "built from the human label" ); }