//! Uploading new traits and covariates as one named matrix. //! //! A matrix is checked against a genotype version's panel before it is stored: //! every individual of that version must appear exactly once. These tests walk //! the real loop -- build a table for BXD.4's own individuals, upload it, map a //! column with R/qtl2 -- and the refusals that keep a mismatched upload from //! becoming a silently wrong mapping. use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; fn unique() -> String { format!( "matrix-test-{}", std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|elapsed| elapsed.as_millis()) .unwrap_or(0) ) } /// The individuals of a genotype version, as `/groups/{group}/genotypes/{version}` /// reports them. async fn panel(token: &str) -> Vec { let response = common::get_with_token( "GET /groups/BXD/genotypes/BXD.4", "/groups/BXD/genotypes/BXD.4", token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let detail: Value = common::data(&bytes); detail["individuals"] .as_array() .unwrap() .iter() .map(|name| name.as_str().unwrap().to_string()) .collect() } /// A deterministic table for a panel: two traits and one covariate. fn table(individuals: &[String]) -> String { let mut text = String::from("id,body_weight,glucose,batch\n"); for (index, individual) in individuals.iter().enumerate() { text.push_str(&format!( "{individual},{:.3},{:.3},{}\n", 10.0 + (index % 17) as f64 * 1.5, 5.0 + (index % 11) as f64 * 0.75, 1 + index % 3 )); } text } #[tokio::test] async fn a_matrix_uploads_against_a_panel_and_its_columns_map() { let Some(token) = common::auth_token().await else { eprintln!("skipping a_matrix_uploads_against_a_panel_and_its_columns_map: no token"); return; }; if !common::db_available() { eprintln!("skipping a_matrix_uploads_against_a_panel_and_its_columns_map: no database"); return; } let individuals = panel(&token).await; assert!(individuals.len() > 50, "BXD.4's panel: {}", individuals.len()); let name = unique(); let response = common::post_json_with_token( "POST /matrices", "/matrices", &token, &serde_json::json!({ "name": name, "genotype_version": "BXD.4", "description": "an uploaded table", "contents": table(&individuals), "kinds": {"batch": "covariate"}, }), ) .await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); assert_eq!(status, StatusCode::CREATED, "{}", String::from_utf8_lossy(&bytes)); let matrix: Value = common::data(&bytes); assert_eq!(matrix["individuals"], individuals.len()); let columns = matrix["columns"].as_array().unwrap(); assert_eq!(columns.len(), 3); let names: Vec<&str> = columns .iter() .map(|column| column["name"].as_str().unwrap()) .collect(); assert_eq!(names, vec!["body_weight", "glucose", "batch"]); assert_eq!(columns[2]["kind"], "covariate", "kinds says which are covariates"); assert_eq!(columns[0]["kind"], "trait"); assert_eq!(columns[0]["basket"], serde_json::json!(format!("{name}.body_weight"))); assert_eq!(columns[0]["missing"], 0); // The matrix reads back as a table. let read_back = common::get_with_token( "GET /matrices/{name}/values", &format!("/matrices/{name}/values"), &token, ) .await; assert_eq!(read_back.status(), StatusCode::OK); let bytes = read_back.into_body().collect().await.unwrap().to_bytes(); let values: Value = common::data(&bytes); assert_eq!(values["individuals"].as_array().unwrap().len(), individuals.len()); assert_eq!(values["columns"].as_array().unwrap().len(), 3); assert_eq!(values["columns"][0]["values"][0].as_f64(), Some(10.0)); // A column maps like any basket, on any engine. let mapped = common::post_json_with_token( "POST /compute/qtl2 (matrix column)", "/compute/qtl2", &token, &serde_json::json!({ "group": "BXD", "genotype_version": "BXD.4", "trait_name": format!("{name}.body_weight"), "basket": format!("{name}.body_weight"), }), ) .await; let status = mapped.status(); let bytes = mapped.into_body().collect().await.unwrap().to_bytes(); assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); assert!(!envelope["data"].as_array().unwrap().is_empty()); let notes: Vec<&str> = envelope["notes"] .as_array() .unwrap() .iter() .map(|note| note.as_str().unwrap()) .collect(); assert!( notes .iter() .any(|note| note.starts_with("Basket ") && note.contains(".body_weight")), "the mapping says which column ran: {notes:?}" ); let deleted = common::delete_with_token( "DELETE /matrices/{name}", &format!("/matrices/{name}"), &token, ) .await; assert_eq!(deleted.status(), StatusCode::NO_CONTENT); let gone = common::get_with_token( "GET /matrices/{name}", &format!("/matrices/{name}"), &token, ) .await; assert_eq!(gone.status(), StatusCode::NOT_FOUND); } #[tokio::test] async fn an_upload_that_does_not_match_the_panel_is_refused() { let Some(token) = common::auth_token().await else { eprintln!("skipping an_upload_that_does_not_match_the_panel_is_refused: no token"); return; }; if !common::db_available() { eprintln!("skipping an_upload_that_does_not_match_the_panel_is_refused: no database"); return; } let individuals = panel(&token).await; // One individual short: the count is the first thing that must match. let short = table(&individuals[..individuals.len() - 1]); let response = common::post_json_with_token( "POST /matrices (too few)", "/matrices", &token, &serde_json::json!({ "name": unique(), "genotype_version": "BXD.4", "contents": short, }), ) .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_matrix"); let detail = error["detail"].as_str().unwrap(); assert!( detail.contains(&format!("{}", individuals.len())), "the refusal says how many the version has: {detail}" ); // A name the panel does not have, with the count unchanged. let mut renamed = individuals.clone(); renamed[0] = "NOT-AN-INDIVIDUAL".to_string(); let response = common::post_json_with_token( "POST /matrices (wrong names)", "/matrices", &token, &serde_json::json!({ "name": unique(), "genotype_version": "BXD.4", "contents": table(&renamed), }), ) .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!( error["detail"].as_str().unwrap().contains("NOT-AN-INDIVIDUAL"), "{error}" ); // A version that does not exist is a 404, not a validation puzzle. let response = common::post_json_with_token( "POST /matrices (unknown version)", "/matrices", &token, &serde_json::json!({ "name": unique(), "genotype_version": "NOSUCH.9", "contents": table(&individuals), }), ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); }