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()); }