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 species_by_name.rs. #[tokio::test] async fn group_by_name_bxd_returns_group_row() { if !common::db_available() { eprintln!( "skipping group_by_name_bxd_returns_group_row: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!("skipping group_by_name_bxd_returns_group_row: no token (see docs/gn-auth.md)"); return; }; let response = common::get_with_token("GET /groups/BXD", "/groups/BXD", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let group: Value = common::data(&body); assert_eq!(group["name"], "BXD"); assert_eq!(group["display_name"], "BXD"); assert_eq!(group["genetic_type"], "riset"); } /// BXD's InbredSet.Id is 1, same as its InbredSetId; replicates /// gn-guile's gn.data.population:population-info, which looks a group /// up by numeric Id rather than by name. #[tokio::test] async fn group_by_name_numeric_id_returns_same_group_row() { if !common::db_available() { eprintln!( "skipping group_by_name_numeric_id_returns_same_group_row: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_by_name_numeric_id_returns_same_group_row: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token("GET /groups/1", "/groups/1", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let group: Value = common::data(&body); assert_eq!(group["name"], "BXD"); assert_eq!(group["id"], 1); } #[tokio::test] async fn group_by_name_unknown_group_returns_404() { if !common::db_available() { eprintln!( "skipping group_by_name_unknown_group_returns_404: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping group_by_name_unknown_group_returns_404: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /groups/not-a-real-group", "/groups/not-a-real-group", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let request_id = response .headers() .get("x-request-id") .unwrap() .to_str() .unwrap() .to_string(); 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_by_name"); assert_eq!(error["spec_url"], "/openapi.json"); assert_eq!(error["next"], "/groups"); assert_eq!(error["request_id"], request_id); } /// A group's record names its mapping tool next to the raw id. #[tokio::test] async fn group_by_name_names_the_mapping_tool() { if !common::db_available() { eprintln!( "skipping group_by_name_names_the_mapping_tool: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!("skipping group_by_name_names_the_mapping_tool: no token (see docs/gn-auth.md)"); return; }; let response = common::get_with_token("GET /groups/HSNIH-Palmer", "/groups/HSNIH-Palmer", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let group: Value = common::data(&body); assert_eq!(group["mapping_method_id"], "2"); assert_eq!(group["mapping_method"], "Rqtl"); }