diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-12 21:36:45 +0200 |
|---|---|---|
| committer | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-12 21:36:45 +0200 |
| commit | ba00d2650ddbb3c9b73e01b6537aa53ac04f3cc3 (patch) | |
| tree | 1c82d1b789b8937cbcb6da6b33ac55ae58759ac9 /tests | |
| parent | ad3bb1e37e5feb22d3a8eb9e99e072f613fa186c (diff) | |
| parent | 2456d66fc2c14345ab0730ef0a4ae07458af8ec6 (diff) | |
| download | genenetwork4-ba00d2650ddbb3c9b73e01b6537aa53ac04f3cc3.tar.gz | |
Merge branch 'gemma'
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/rqtl.rs | 287 | ||||
| -rw-r--r-- | tests/test_data/rqtl_sample1.geno | 18 | ||||
| -rw-r--r-- | tests/test_data/rqtl_sample1.pheno | 7 |
3 files changed, 312 insertions, 0 deletions
diff --git a/tests/rqtl.rs b/tests/rqtl.rs new file mode 100644 index 0000000..c341254 --- /dev/null +++ b/tests/rqtl.rs @@ -0,0 +1,287 @@ +//! Integration test for POST /compute/rqtl. Runs the real Rscript/R-qtl +//! against a small, hand-written BXD riset fixture (tests/test_data, +//! not copied from genenetwork3 verbatim: its own +//! tests/unit/db/.../genotype_sample1.geno has a genotype code outside +//! its own header's declared set and duplicate marker names across +//! chromosomes, neither of which actually matters for the unrelated +//! parsing unit it's written for, but both would break a real R/qtl +//! run); skips rather than fails when Rscript is not on PATH, same +//! "skip, don't fail" convention as tests/gemma.rs. + +use axum::http::StatusCode; +use http_body_util::BodyExt; +use serde_json::Value; + +mod common; + +fn rscript_available() -> bool { + std::process::Command::new("Rscript") + .arg("--version") + .output() + .is_ok() +} + +fn test_data_path(name: &str) -> String { + format!("{}/tests/test_data/{name}", env!("CARGO_MANIFEST_DIR")) +} + +fn bxd_samples() -> Value { + serde_json::json!([ + {"name": "BXD1", "value": 9.5}, + {"name": "BXD2", "value": 8.2}, + {"name": "BXD5", "value": 7.1}, + {"name": "BXD6", "value": 6.4}, + {"name": "BXD8", "value": 5.9}, + {"name": "BXD9", "value": 4.3}, + ]) +} + +#[tokio::test] +async fn compute_rqtl_maps_a_qtl_from_the_bxd_riset_fixture() { + if !rscript_available() { + eprintln!("skipping compute_rqtl_maps_a_qtl_from_the_bxd_riset_fixture: Rscript not on PATH"); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!("skipping compute_rqtl_maps_a_qtl_from_the_bxd_riset_fixture: no token (see docs/gn-auth.md)"); + return; + }; + + let body = serde_json::json!({ + "genotype_file": test_data_path("rqtl_sample1.geno"), + "trait_name": "mytrait", + "samples": bxd_samples(), + }); + let response = + common::post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &body).await; + let status = response.status(); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); + + let data: Value = common::data(&bytes); + let markers = data["markers"].as_array().unwrap(); + // The fixture has 6 markers (3 per chromosome, 2 chromosomes). + assert_eq!(markers.len(), 6); + assert_eq!(markers[0]["name"], "rs31443144"); + assert_eq!(markers[0]["chr"], "1"); + assert!(markers[0]["lod"].as_f64().unwrap() >= 0.0); + assert!(data["permutation"].is_null(), "nperm defaults to 0: {data}"); + + let envelope: Value = serde_json::from_slice(&bytes).unwrap(); + let notes = envelope["notes"].as_array().unwrap(); + let rscript_note = notes.iter().find(|n| n.as_str().unwrap().starts_with("Rscript")); + assert!(rscript_note.is_some(), "{notes:?}"); + let rscript_note = rscript_note.unwrap().as_str().unwrap(); + assert!(rscript_note.contains("--geno") && rscript_note.contains("--pheno"), "{rscript_note}"); +} + +#[tokio::test] +async fn compute_rqtl_with_permutations_returns_thresholds() { + if !rscript_available() { + eprintln!("skipping compute_rqtl_with_permutations_returns_thresholds: Rscript not on PATH"); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!("skipping compute_rqtl_with_permutations_returns_thresholds: no token (see docs/gn-auth.md)"); + return; + }; + + let body = serde_json::json!({ + "genotype_file": test_data_path("rqtl_sample1.geno"), + "trait_name": "mytrait", + "samples": bxd_samples(), + "nperm": 20, + }); + let response = common::post_json_with_token( + "POST /compute/rqtl (with permutations)", + "/compute/rqtl", + &token, + &body, + ) + .await; + let status = response.status(); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); + + let data: Value = common::data(&bytes); + let perm = &data["permutation"]; + assert!(!perm.is_null(), "{data}"); + assert_eq!(perm["values"].as_array().unwrap().len(), 20); + let suggestive = perm["suggestive"].as_f64().unwrap(); + let significant = perm["significant"].as_f64().unwrap(); + assert!(significant >= suggestive, "{perm}"); +} + +#[tokio::test] +async fn compute_rqtl_requires_a_token() { + let body = serde_json::json!({ + "genotype_file": test_data_path("rqtl_sample1.geno"), + "trait_name": "mytrait", + "samples": bxd_samples(), + }); + let request = axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri("/compute/rqtl") + .extension(axum::extract::ConnectInfo(std::net::SocketAddr::from(( + [127, 0, 0, 1], + 40000, + )))) + .header("content-type", "application/json") + .body(axum::body::Body::from(serde_json::to_vec(&body).unwrap())) + .unwrap(); + let response = tower::ServiceExt::oneshot(genenetwork4::app(), request) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); +} + +#[tokio::test] +async fn compute_rqtl_rejects_an_invalid_run_id() { + let Some(token) = common::auth_token().await else { + eprintln!("skipping compute_rqtl_rejects_an_invalid_run_id: no token (see docs/gn-auth.md)"); + return; + }; + + let body = serde_json::json!({ + "genotype_file": test_data_path("rqtl_sample1.geno"), + "trait_name": "mytrait", + "samples": bxd_samples(), + }); + let response = common::post_json_with_token( + "POST /compute/rqtl (bad id)", + "/compute/rqtl?id=../etc/passwd", + &token, + &body, + ) + .await; + assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); +} + +#[tokio::test] +async fn rqtl_status_unknown_run_id_returns_404() { + let Some(token) = common::auth_token().await else { + eprintln!("skipping rqtl_status_unknown_run_id_returns_404: no token (see docs/gn-auth.md)"); + return; + }; + let response = common::get_with_token( + "GET /compute/rqtl/status (unknown)", + "/compute/rqtl/status/no-such-gn4-rqtl-run?peek=0", + &token, + ) + .await; + assert_eq!(response.status(), StatusCode::NOT_FOUND); +} + +#[tokio::test] +async fn rqtl_status_requires_a_token() { + let request = axum::http::Request::builder() + .method(axum::http::Method::GET) + .uri("/compute/rqtl/status/some-run?peek=0") + .extension(axum::extract::ConnectInfo(std::net::SocketAddr::from(( + [127, 0, 0, 1], + 40000, + )))) + .body(axum::body::Body::empty()) + .unwrap(); + let response = tower::ServiceExt::oneshot(genenetwork4::app(), request) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); +} + +#[tokio::test] +async fn compute_rqtl_rejects_a_run_id_already_in_use() { + let Some(token) = common::auth_token().await else { + eprintln!("skipping compute_rqtl_rejects_a_run_id_already_in_use: no token (see docs/gn-auth.md)"); + return; + }; + + // Same reasoning as gemma's equivalent test: the uniqueness check + // runs before any subprocess call, so this doesn't need Rscript. + let cache_dir = genenetwork4::config::Config::load().rqtl_cache_dir; + std::fs::create_dir_all(&cache_dir).unwrap(); + let run_id = format!( + "dup-{}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + ); + std::fs::write( + std::path::Path::new(&cache_dir).join(format!("{run_id}.log")), + "a previous (or still running) call already owns this id\n", + ) + .unwrap(); + + let body = serde_json::json!({ + "genotype_file": test_data_path("rqtl_sample1.geno"), + "trait_name": "mytrait", + "samples": bxd_samples(), + }); + let response = common::post_json_with_token( + "POST /compute/rqtl (duplicate id)", + &format!("/compute/rqtl?id={run_id}"), + &token, + &body, + ) + .await; + assert_eq!(response.status(), StatusCode::CONFLICT); + + std::fs::remove_file(std::path::Path::new(&cache_dir).join(format!("{run_id}.log"))).unwrap(); +} + +#[tokio::test] +async fn compute_rqtl_streams_progress_while_running() { + if !rscript_available() { + eprintln!("skipping compute_rqtl_streams_progress_while_running: Rscript not on PATH"); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!("skipping compute_rqtl_streams_progress_while_running: no token (see docs/gn-auth.md)"); + return; + }; + + let run_id = format!( + "rqtl-test-{}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + ); + let body = serde_json::json!({ + "genotype_file": test_data_path("rqtl_sample1.geno"), + "trait_name": "mytrait", + "samples": bxd_samples(), + }); + let post_token = token.clone(); + let post_run_id = run_id.clone(); + let post_handle = tokio::spawn(async move { + common::post_json_with_token( + "POST /compute/rqtl (streamed)", + &format!("/compute/rqtl?id={post_run_id}"), + &post_token, + &body, + ) + .await + }); + + let response = post_handle.await.unwrap(); + assert_eq!(response.status(), StatusCode::OK); + + let final_response = common::get_with_token( + "GET /compute/rqtl/status (final)", + &format!("/compute/rqtl/status/{run_id}?peek=0"), + &token, + ) + .await; + assert_eq!(final_response.status(), StatusCode::OK); + let final_bytes = final_response.into_body().collect().await.unwrap().to_bytes(); + let final_status: Value = common::data(&final_bytes); + let lines: Vec<&str> = final_status["lines"] + .as_array() + .unwrap() + .iter() + .map(|l| l.as_str().unwrap()) + .collect(); + assert!(lines.contains(&"GN4: computation complete"), "{lines:?}"); +} diff --git a/tests/test_data/rqtl_sample1.geno b/tests/test_data/rqtl_sample1.geno new file mode 100644 index 0000000..999972c --- /dev/null +++ b/tests/test_data/rqtl_sample1.geno @@ -0,0 +1,18 @@ +# File name: rqtl_sample1 (minimal, valid BXD riset fixture for tests) + +# Metadata: Please retain this header information with file. + +@name: BXD +@type: riset +@mat: B +@pat: D +@het: H +@unk: U + +Chr Locus cM Mb BXD1 BXD2 BXD5 BXD6 BXD8 BXD9 +1 rs31443144 1.50 3.010274 B B D D D B +1 rs6269442 2.10 3.492195 B B D D H D +1 rs32285189 3.60 3.511204 B D D D D B +2 rs48756159 1.20 5.010274 D B B D B D +2 rs49742679 2.80 5.492195 D B H D B D +2 rs33198772 4.10 5.511204 D D B D B D diff --git a/tests/test_data/rqtl_sample1.pheno b/tests/test_data/rqtl_sample1.pheno new file mode 100644 index 0000000..870f662 --- /dev/null +++ b/tests/test_data/rqtl_sample1.pheno @@ -0,0 +1,7 @@ +Sample mytrait +BXD1 9.5 +BXD2 8.2 +BXD5 7.1 +BXD6 6.4 +BXD8 5.9 +BXD9 4.3 |
