use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; /// Needs a live MariaDB connection (SQL_URI, see src/db.rs); skips rather /// than fails when none is reachable, same as species.rs. BXD is used /// everywhere as a test fixture in the GeneNetwork2/GeneNetwork3 test /// suites and is always present in the database. #[tokio::test] async fn groups_returns_ok_with_bxd() { if !common::db_available() { eprintln!( "skipping groups_returns_ok_with_bxd: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!("skipping groups_returns_ok_with_bxd: no token (see docs/gn-auth.md)"); return; }; let response = common::get_with_token("GET /groups", "/groups", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let groups: Vec = common::data(&body); assert!(!groups.is_empty()); let names: Vec<&str> = groups .iter() .map(|record| record["name"].as_str().unwrap()) .collect(); assert!(names.contains(&"BXD")); } /// Every group carries its family and species name, and /groups takes /// `family`, `species` and `q` filters (case-insensitive, combined with /// AND). #[tokio::test] async fn groups_filters_by_family_species_and_text() { if !common::db_available() { eprintln!( "skipping groups_filters_by_family_species_and_text: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping groups_filters_by_family_species_and_text: no token (see docs/gn-auth.md)" ); return; }; let groups = |uri: &'static str| { let token = token.clone(); async move { let response = common::get_with_token(&format!("GET {uri}"), uri, &token).await; assert_eq!(response.status(), StatusCode::OK, "GET {uri}"); let body = response.into_body().collect().await.unwrap().to_bytes(); let groups: Vec = common::data(&body); groups } }; let names = |groups: &[Value]| -> Vec { groups .iter() .map(|g| g["name"].as_str().unwrap().to_string()) .collect() }; // Unfiltered: BXD is a mouse Reference Population. let all = groups("/groups").await; let bxd = all.iter().find(|g| g["name"] == "BXD").unwrap(); assert_eq!(bxd["species"], "mouse"); assert!( bxd["family"] .as_str() .unwrap() .starts_with("Reference Populations") ); // family: case-insensitive "contains". let reference = names(&groups("/groups?family=reference%20populations").await); for expected in [ "AKXD", "AXBXA", "BXD", "BXH", "CC", "CXB", "HXBBXH", "LXS", "MDP", ] { assert!( reference.contains(&expected.to_string()), "{expected} in {reference:?}" ); } assert!(!reference.contains(&"HSNIH-Palmer".to_string())); // species: the short name /species lists, case-insensitive. let rat = groups("/groups?species=RAT").await; assert!(!rat.is_empty() && rat.iter().all(|g| g["species"] == "rat")); // q: name, display name or full name. let alzheimer = names(&groups("/groups?q=alzheimer").await); assert!(alzheimer.contains(&"BXD-NIA-AD".to_string())); // Filters combine with AND. let rat_reference = names(&groups("/groups?species=rat&family=reference").await); assert!(rat_reference.contains(&"HXBBXH".to_string())); assert!(!rat_reference.contains(&"BXD".to_string())); } /// Each group reports the mapping tool offered for it (the MappingMethod /// its mapping_method_id points to), or null when it has none. #[tokio::test] async fn groups_report_the_mapping_tool_offered_for_them() { if !common::db_available() { eprintln!( "skipping groups_report_the_mapping_tool_offered_for_them: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping groups_report_the_mapping_tool_offered_for_them: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token("GET /groups", "/groups", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let groups: Vec = common::data(&body); let tool = |name: &str| -> Value { groups.iter().find(|g| g["name"] == name).unwrap()["mapping_method"].clone() }; assert_eq!(tool("BXD"), "qtlreaper"); assert_eq!(tool("HSNIH-Palmer"), "Rqtl"); assert_eq!(tool("HSNIH-RGSMC"), "qtlreaper"); // not every outbred set maps with Rqtl assert!(tool("Oregon-R_x_2b3").is_null()); } /// The values endpoint normalises on request, says so, and carries the same /// pipeline into the mapping suggestions -- a caller who fetched z-scores /// must not then map the raw numbers. #[tokio::test] async fn trait_values_can_be_normalized_and_the_suggestion_keeps_it() { if !common::db_available() { eprintln!("skipping trait_values_can_be_normalized_and_the_suggestion_keeps_it: no usable database"); return; } let Some(token) = common::auth_token().await else { eprintln!("skipping trait_values_can_be_normalized_and_the_suggestion_keeps_it: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); return; }; let uri = "/groups/BXD/traits/10010/values?normalize=zscore"; let response = common::get_with_token("GET values (zscore)", uri, &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 values: Vec = envelope["data"] .as_array() .unwrap() .iter() .map(|row| row["value"].as_f64().expect("a numeric value")) .collect(); assert!(values.len() > 20, "the trait has values: {}", values.len()); let mean = values.iter().sum::() / values.len() as f64; let sd = (values.iter().map(|v| (v - mean).powi(2)).sum::() / values.len() as f64).sqrt(); assert!(mean.abs() < 1e-9, "z-scores are mean 0, got {mean}"); assert!((sd - 1.0).abs() < 1e-9, "population sd 1, got {sd}"); assert!( envelope["notes"] .as_array() .unwrap() .iter() .any(|note| note.as_str().unwrap_or("").starts_with("Normalized: zscore")), "the note says what ran: {}", envelope["notes"] ); // ... and the mapping bodies carry it. for action in envelope["actions"].as_array().unwrap() { if let Some(href) = action["href"].as_str() { if href.contains("/compute/") { assert_eq!( action["body"]["normalize"], "zscore", "{href} maps the values that were shown: {action}" ); } } } // A normalisation that cannot apply is a 422, not a silent no-op. let response = common::get_with_token( "GET values (impossible)", "/groups/BXD/traits/10010/values?normalize=log2&winsor_quantile=0.05&winsor_fences=1.5", &token, ) .await; assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "invalid_normalization"); }