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) and a valid /// token (every endpoint needs one now -- see docs/gn-auth.md); skips /// rather than fails when either is missing, same as group_by_name.rs. /// BXD (InbredSetId 1) has exactly 4 case attributes -- Epoch, RRID, /// SeqCvge, Status -- each strain in the response should have exactly /// those 4 keys, not more: db::get_group_case_attribute_values joins /// CaseAttribute to CaseAttributeXRefNew on CaseAttributeId alone would /// be wrong here, since CaseAttributeId isn't unique across groups (see /// the comment in src/db.rs) -- getting that join wrong would pull in /// unrelated groups' attributes too. #[tokio::test] async fn group_case_attributes_bxd_returns_names_and_values() { if !common::db_available() { eprintln!( "skipping group_case_attributes_bxd_returns_names_and_values: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_case_attributes_bxd_returns_names_and_values: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /groups/BXD/case-attributes", "/groups/BXD/case-attributes", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let case_attributes: Value = common::data(&body); let names: Vec<&str> = case_attributes["names"] .as_array() .unwrap() .iter() .map(|n| n.as_str().unwrap()) .collect(); assert_eq!(names, vec!["Epoch", "RRID", "SeqCvge", "Status"]); // Every name says what it means, which is the only way to know that // SeqCvge is a sequencing depth rather than a phenotype. let descriptions = case_attributes["descriptions"].as_object().unwrap(); assert_eq!(descriptions.len(), names.len()); let seq_cvge = descriptions["SeqCvge"].as_str().unwrap(); assert!( seq_cvge.contains("Depth of DNA sequencing"), "SeqCvge is explained: {seq_cvge}" ); assert!( descriptions["Status"] .as_str() .unwrap() .contains("Cryopreserved"), "Status explains its own levels" ); let values = case_attributes["values"].as_array().unwrap(); assert!(!values.is_empty()); let bxd1 = values .iter() .find(|v| v["strain"] == "BXD1") .expect("BXD1 in /groups/BXD/case-attributes"); let attrs = bxd1["case_attributes"].as_object().unwrap(); assert_eq!( attrs.len(), 4, "BXD1 should have exactly BXD's 4 case attributes, not more" ); for name in &names { assert!(attrs.contains_key(*name)); } } /// LXS (InbredSetId 9) is a real group with no case attributes defined /// at all -- an empty result, not a 404 (the group itself exists). #[tokio::test] async fn group_case_attributes_group_with_none_returns_empty() { if !common::db_available() { eprintln!( "skipping group_case_attributes_group_with_none_returns_empty: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_case_attributes_group_with_none_returns_empty: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /groups/LXS/case-attributes", "/groups/LXS/case-attributes", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let case_attributes: Value = common::data(&body); assert_eq!(case_attributes["names"].as_array().unwrap().len(), 0); assert_eq!(case_attributes["values"].as_array().unwrap().len(), 0); } #[tokio::test] async fn group_case_attributes_without_a_token_returns_401() { if !common::db_available() { eprintln!( "skipping group_case_attributes_without_a_token_returns_401: no usable database (check SQL_URI credentials)" ); return; } let response = common::get( "GET /groups/BXD/case-attributes (no token)", "/groups/BXD/case-attributes", ) .await; assert_eq!(response.status(), StatusCode::UNAUTHORIZED); } #[tokio::test] async fn group_case_attributes_unknown_group_returns_404() { if !common::db_available() { eprintln!( "skipping group_case_attributes_unknown_group_returns_404: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_case_attributes_unknown_group_returns_404: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /groups/not-a-real-group/case-attributes", "/groups/not-a-real-group/case-attributes", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let body = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["status"], 404); assert_eq!(error["code"], "group_not_found"); assert_eq!(error["operation_id"], "group_case_attributes"); assert_eq!(error["next"], "/groups"); }