//! `GET /probesets/{name}` and `GET /genes/{symbol}/traits`: the cross-dataset //! lookups the search index cannot do (a probeset id is not a Xapian field, //! and a symbol search is capped and ungrouped). //! //! Both return metadata only -- which datasets carry the probeset and what //! their scans found -- so the tests pin the row shape, the totals, the //! filters and the fact that every link out of them is followable. use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; async fn ready() -> Option { if !common::db_available() { eprintln!("skipping: no usable database"); return None; } common::auth_token().await } #[tokio::test] async fn a_probeset_resolves_to_its_datasets() { let Some(token) = ready().await else { return }; let response = common::get_with_token( "GET /probesets/{name}", "/probesets/1427571_at?limit=5", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let columns: Vec<&str> = envelope["meta"]["columns"] .as_array() .unwrap() .iter() .map(|column| column.as_str().unwrap()) .collect(); for expected in [ "dataset", "group", "species", "platform", "name", "symbol", "chr", "mb", "mean", "lrs", "lod", "locus", "additive", ] { assert!(columns.contains(&expected), "{expected} in {columns:?}"); } let rows = envelope["data"].as_array().unwrap(); assert_eq!(rows.len(), 5, "the page size is respected"); let name = columns.iter().position(|column| *column == "name").unwrap(); let symbol = columns.iter().position(|column| *column == "symbol").unwrap(); assert!( rows.iter().any(|row| row[name] .as_str() .unwrap() .starts_with("1427571_at")), "the rows are the probeset asked for: {rows:?}" ); // 1427571_at is Shh; a row that matched a suffixed variant says so itself. assert!( rows.iter() .any(|row| row[symbol].as_str() == Some("Shh")), "the annotation travels with the row: {rows:?}" ); // The totals are in notes, which is the point of the endpoint. let notes = envelope["notes"].as_array().unwrap(); let first = notes[0].as_str().unwrap(); assert!(first.contains("dataset(s)"), "{first}"); assert!(first.contains("1427571_at"), "{first}"); // And it says where the values (which it does not serve) live. let links: Vec<&str> = envelope["next"] .as_array() .unwrap() .iter() .map(|action| action["href"].as_str().unwrap()) .collect(); assert!( links .iter() .any(|href| href.contains("/datasets/") && href.ends_with("/values")), "the values are one link away: {links:?}" ); } #[tokio::test] async fn a_symbol_lists_its_traits_with_a_real_total() { let Some(token) = ready().await else { return }; let response = common::get_with_token( "GET /genes/{symbol}/traits", "/genes/Shh/traits?sort=lod&min_lod=5&limit=5", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let rows = envelope["data"].as_array().unwrap(); assert_eq!(rows.len(), 5); let columns: Vec<&str> = envelope["meta"]["columns"] .as_array() .unwrap() .iter() .map(|column| column.as_str().unwrap()) .collect(); let lod = columns.iter().position(|column| *column == "lod").unwrap(); // ?sort=lod means the page starts at the strongest peak, and ?min_lod=5 // means nothing weaker is in it. let lods: Vec = rows.iter().map(|row| row[lod].as_f64().unwrap()).collect(); assert!(lods[0] >= lods[lods.len() - 1], "sorted by lod: {lods:?}"); assert!(lods.iter().all(|value| *value >= 5.0), "{lods:?}"); let notes = envelope["notes"].as_array().unwrap(); let first = notes[0].as_str().unwrap(); assert!(first.contains("expression trait row(s)"), "{first}"); assert!(first.contains("after the filters"), "a filter count: {first}"); // An unknown symbol is a 404, not an empty list. let response = common::get_with_token("GET /genes/{symbol}/traits", "/genes/NOSUCHGENE/traits", &token) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "gene_not_found"); } #[tokio::test] async fn the_filters_narrow_a_gene_to_one_group() { let Some(token) = ready().await else { return }; let all = common::get_with_token("GET /genes/{symbol}/traits", "/genes/Shh/traits?limit=1", &token).await; assert_eq!(all.status(), StatusCode::OK); let bytes = all.into_body().collect().await.unwrap().to_bytes(); let all: Value = serde_json::from_slice(&bytes).unwrap(); let everything = all["notes"][0].as_str().unwrap().to_string(); let one = common::get_with_token( "GET /genes/{symbol}/traits?group=", "/genes/Shh/traits?group=BXD&limit=1", &token, ) .await; assert_eq!(one.status(), StatusCode::OK); let bytes = one.into_body().collect().await.unwrap().to_bytes(); let one: Value = serde_json::from_slice(&bytes).unwrap(); let narrowed = one["notes"][0].as_str().unwrap().to_string(); assert_ne!(everything, narrowed, "the group filter changes the totals"); let row = one["data"][0].as_array().unwrap(); assert_eq!(row[1].as_str(), Some("BXD"), "every row is that group: {row:?}"); }