diff options
Diffstat (limited to 'tests/plot.rs')
| -rw-r--r-- | tests/plot.rs | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/plot.rs b/tests/plot.rs new file mode 100644 index 0000000..012dc84 --- /dev/null +++ b/tests/plot.rs @@ -0,0 +1,123 @@ +//! `POST /plot/rqtl` and `POST /plot/gemma`: an SVG back, not an envelope. +//! +//! These run a real mapping, so they need the engines -- and the plotter +//! scripts, which are configured (`RQTL_PLOTTER`, `GEMMA_PLOTTER`, +//! `PLOT_PYTHON`). Skips when a piece is missing, like the mapping tests. + +use axum::http::StatusCode; +use http_body_util::BodyExt; +use serde_json::Value; + +mod common; + +use common::{auth_token, post_json_with_token}; + +fn tool_available(tool: &str, args: &[&str]) -> bool { + std::process::Command::new(tool) + .args(args) + .output() + .map(|output| output.status.success()) + .unwrap_or(false) +} + +fn rscript_available() -> bool { + tool_available("Rscript", &["--version"]) +} + +fn genotype_dir_present() -> bool { + let directory = genenetwork4::config::Config::load().genotype_files_dir; + std::path::Path::new(&directory).is_dir() +} + +fn plotter_present(which: &str) -> bool { + let config = genenetwork4::config::Config::load(); + let path = if which == "rqtl" { + config.rqtl_plotter + } else { + config.gemma_plotter + }; + std::path::Path::new(&path).is_file() && tool_available(&config.plot_python, &["--version"]) +} + +#[tokio::test] +async fn an_rqtl_mapping_can_be_plotted_and_the_suggestion_resolves() { + if !genotype_dir_present() || !rscript_available() || !plotter_present("rqtl") { + eprintln!( + "skipping an_rqtl_mapping_can_be_plotted_and_the_suggestion_resolves: needs \ + genotype files, Rscript and the plotter script" + ); + return; + } + let Some(token) = auth_token().await else { + eprintln!("skipping an_rqtl_mapping_can_be_plotted_and_the_suggestion_resolves: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); + return; + }; + + let samples: Vec<Value> = ["BXD1", "BXD2", "BXD5", "BXD6", "BXD8", "BXD9", "BXD11", "BXD12"] + .iter() + .enumerate() + .map(|(index, name)| serde_json::json!({"name": name, "value": index as f64 + 1.0})) + .collect(); + let body = serde_json::json!({"group": "BXD", "trait_name": "plot-check", "samples": samples}); + + // The mapping suggests the plot; the suggestion is runnable as it stands. + let response = post_json_with_token( + "POST /compute/rqtl", + "/compute/rqtl", + &token, + &body, + ) + .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(); + let plot_action = envelope["actions"] + .as_array() + .unwrap() + .iter() + .find(|action| action["href"] == "/plot/rqtl") + .expect("a plot suggestion") + .clone(); + + let response = post_json_with_token( + "POST /plot/rqtl", + "/plot/rqtl", + &token, + &plot_action["body"], + ) + .await; + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.headers().get("content-type").unwrap(), + "image/svg+xml; charset=utf-8" + ); + // The plotter's one-line peak summary rides along as a header. + let summary = response + .headers() + .get("x-plot-summary") + .expect("x-plot-summary") + .to_str() + .unwrap() + .to_string(); + assert!(summary.contains("peak"), "{summary}"); + let svg = response.into_body().collect().await.unwrap().to_bytes(); + let text = String::from_utf8_lossy(&svg); + assert!(text.starts_with("<svg"), "an SVG document: {}", &text[..40.min(text.len())]); + assert!(text.trim_end().ends_with("</svg>"), "a complete SVG"); +} + +#[tokio::test] +async fn plotting_refuses_an_invalid_mapping_request_like_the_mapping_does() { + let Some(token) = auth_token().await else { + eprintln!("skipping plotting_refuses_an_invalid_mapping_request_like_the_mapping_does: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); + return; + }; + // No group and no genotype_file: the mapping handler's own 422, before + // any plotting happens. + let body = serde_json::json!({"trait_name": "t", "samples": []}); + let response = post_json_with_token("POST /plot/rqtl", "/plot/rqtl", &token, &body).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"], "missing_genotype"); +} |
