//! `GET /groups/{group}/genotypes`, and mapping by group + version instead //! of by path. //! //! The listing needs the deployment's genotype-files directory (config key //! `GENOTYPE_FILES`), so it skips -- like the database-backed tests -- when //! that directory is not there. The two mapping errors asserted here are //! resolution failures, so they need no Rscript and no GEMMA. use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; use common::{auth_token, data, get_with_token, post_json_with_token}; fn genotype_dir_present() -> bool { let directory = genenetwork4::config::Config::load().genotype_files_dir; std::path::Path::new(&directory).is_dir() } #[tokio::test] async fn the_listing_names_versions_without_paths_and_maps_by_group() { let Some(token) = auth_token().await else { eprintln!( "skipping the_listing_names_versions_without_paths_and_maps_by_group: set \ GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" ); return; }; if !genotype_dir_present() { eprintln!( "skipping the_listing_names_versions_without_paths_and_maps_by_group: no \ genotype-files directory (GENOTYPE_FILES)" ); return; } // 1. The listing: versions, one current, no paths and no file names. let response = get_with_token( "GET /groups/BXD/genotypes", "/groups/BXD/genotypes", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let listing: Value = data(&bytes); let versions = listing["versions"].as_array().expect("versions"); assert!(!versions.is_empty(), "{listing}"); let current: Vec<&Value> = versions .iter() .filter(|version| version["current"] == true) .collect(); assert_eq!(current.len(), 1, "exactly one current version: {listing}"); assert_eq!(versions[0]["current"], true, "current is first: {listing}"); for version in versions { assert!(version["version"].is_string(), "{version}"); assert!(version["individuals"].as_u64().unwrap() > 0, "{version}"); assert!(version["markers"].as_u64().unwrap() > 0, "{version}"); } // No paths -- but the file *name* is part of the metadata now. let text = listing.to_string(); assert!(!text.contains('/'), "no path in the listing: {text}"); for version in versions { let file = version["file"].as_str().expect("a file name"); assert!(file.ends_with(".geno"), "{version}"); assert!(!file.contains('/'), "{version}"); } // The group record points at it. let response = get_with_token("GET /groups/BXD", "/groups/BXD", &token).await; let bytes = response.into_body().collect().await.unwrap().to_bytes(); let group: Value = serde_json::from_slice(&bytes).unwrap(); assert!( group["next"] .as_array() .unwrap() .iter() .any(|next| next["href"] == "/groups/BXD/genotypes"), "{group}" ); // 2. Mapping by group: the resolution errors need no R/GEMMA. let unknown = serde_json::json!({ "group": "BXD", "genotype_version": "BXD.99", "trait_name": "t", "samples": [{"name": "BXD1", "value": 1.0}], }); let response = post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &unknown).await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let body: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(body["code"], "genotypes_not_found"); assert!( body["detail"] .as_str() .unwrap() .contains(&versions[0]["version"].as_str().unwrap().to_string()), "the error lists what is available: {body}" ); let both = serde_json::json!({ "group": "BXD", "genotype_file": "/tmp/x.geno", "trait_name": "t", "samples": [], }); let response = post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &both).await; assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); let bytes = response.into_body().collect().await.unwrap().to_bytes(); assert_eq!( serde_json::from_slice::(&bytes).unwrap()["code"], "ambiguous_genotype" ); // 3. A group with no genotype files is a 404, not an empty list. let response = get_with_token( "GET /groups/not-a-group/genotypes", "/groups/not-a-group/genotypes", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); } /// The marker list is names only; a position is one call per marker. That /// split keeps a page of names small and makes a position lookup a lookup /// rather than a table scan in the client. #[tokio::test] async fn markers_are_names_and_positions_are_a_separate_call() { let Some(token) = auth_token().await else { eprintln!("skipping markers_are_names_and_positions_are_a_separate_call: no token"); return; }; if !genotype_dir_present() { eprintln!("skipping markers_are_names_and_positions_are_a_separate_call: no genotype dir"); return; } let response = get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers", "/groups/BXD/genotypes/BXD.4/markers?limit=3", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!( envelope["meta"]["columns"], serde_json::json!(["name"]), "the list is names only: {envelope}" ); let rows = envelope["data"].as_array().unwrap(); assert_eq!(rows.len(), 3); for row in rows { let row = row.as_array().unwrap(); assert_eq!(row.len(), 1, "one column per marker: {row:?}"); assert!(row[0].is_string(), "{row:?}"); } let first = rows[0][0].as_str().unwrap().to_string(); // Page two continues rather than repeating. let response = get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers?page=2", "/groups/BXD/genotypes/BXD.4/markers?limit=3&page=2", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let page_two: Value = serde_json::from_slice(&bytes).unwrap(); assert_ne!( page_two["data"][0][0].as_str().unwrap(), first, "page two starts after page one" ); // The position, as named fields. let response = get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers/{marker}/location", &format!("/groups/BXD/genotypes/BXD.4/markers/{first}/location"), &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let location: Value = data(&bytes); assert_eq!(location["name"], serde_json::json!(first)); for field in ["chr", "cM", "Mb"] { assert!( location.get(field).is_some(), "the location names {field}: {location}" ); } assert!(location["chr"].is_string(), "{location}"); // An absent marker is a 404 that says which marker. let response = get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers/nonesuch/location", "/groups/BXD/genotypes/BXD.4/markers/nonesuch/location", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "marker_not_found"); } /// The `unlisted` namespace answers the same two calls for a file no group /// lists. #[tokio::test] async fn unlisted_markers_answer_like_a_listed_version() { let Some(token) = auth_token().await else { eprintln!("skipping unlisted_markers_answer_like_a_listed_version: no token"); return; }; if !genotype_dir_present() { eprintln!("skipping unlisted_markers_answer_like_a_listed_version: no genotype dir"); return; } let listed = get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers", "/groups/BXD/genotypes/BXD.4/markers?limit=2", &token, ) .await; assert_eq!(listed.status(), StatusCode::OK); let bytes = listed.into_body().collect().await.unwrap().to_bytes(); let listed: Value = serde_json::from_slice(&bytes).unwrap(); let unlisted = get_with_token( "GET /groups/unlisted/genotypes/{name}/markers", "/groups/unlisted/genotypes/BXD.4/markers?limit=2", &token, ) .await; assert_eq!(unlisted.status(), StatusCode::OK); let bytes = unlisted.into_body().collect().await.unwrap().to_bytes(); let unlisted: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(listed["data"], unlisted["data"], "the same two names"); let marker = listed["data"][0][0].as_str().unwrap().to_string(); let location = get_with_token( "GET /groups/unlisted/genotypes/{name}/markers/{marker}/location", &format!("/groups/unlisted/genotypes/BXD.4/markers/{marker}/location"), &token, ) .await; assert_eq!(location.status(), StatusCode::OK); let bytes = location.into_body().collect().await.unwrap().to_bytes(); let location: Value = data(&bytes); assert_eq!(location["name"], serde_json::json!(marker)); // A panel too large to read is refused, not waited for. let oversized = get_with_token( "GET /groups/unlisted/genotypes/HSNIH-Palmer_true/markers", "/groups/unlisted/genotypes/HSNIH-Palmer_true/markers", &token, ) .await; match oversized.status() { StatusCode::PAYLOAD_TOO_LARGE => { let bytes = oversized.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "genotype_too_large"); } StatusCode::NOT_FOUND => eprintln!("skipping the size check: the panel is not deployed"), other => panic!("unexpected status {other}"), } }