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. /// PublishXRef.Id 10001 in group BXD (InbredSetId 1) is a real published /// trait in the database, used here the same way BXD itself is used as /// a fixture elsewhere. #[tokio::test] async fn group_trait_values_bxd_returns_strain_values() { if !common::db_available() { eprintln!( "skipping group_trait_values_bxd_returns_strain_values: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_trait_values_bxd_returns_strain_values: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /groups/BXD/traits/10001/values", "/groups/BXD/traits/10001/values", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let values: Vec = common::data(&body); assert!(!values.is_empty()); let bxd1 = values .iter() .find(|v| v["name"] == "BXD1") .expect("BXD1 in /groups/BXD/traits/10001/values"); assert!(bxd1["value"].as_f64().is_some()); } #[tokio::test] async fn group_trait_values_unknown_group_returns_404() { if !common::db_available() { eprintln!( "skipping group_trait_values_unknown_group_returns_404: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_trait_values_unknown_group_returns_404: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /groups/not-a-real-group/traits/10001/values", "/groups/not-a-real-group/traits/10001/values", &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_trait_values"); assert_eq!(error["next"], "/groups"); } /// 10001 exists in other groups (PublishXRef.Id isn't unique by itself, /// only the pair (InbredSetId, Id) is), but not in rat -- so this checks /// the trait_not_found branch, not just an always-invalid id. #[tokio::test] async fn group_trait_values_unknown_trait_in_group_returns_404() { if !common::db_available() { eprintln!( "skipping group_trait_values_unknown_trait_in_group_returns_404: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_trait_values_unknown_trait_in_group_returns_404: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /groups/BXD/traits/999999999/values", "/groups/BXD/traits/999999999/values", &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"], "trait_not_found"); assert_eq!(error["operation_id"], "group_trait_values"); }