diff options
| author | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-07 21:01:40 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-07 21:01:40 +0300 |
| commit | 85b63abcfdbae235d292a6de4bd47ffe6ba6dafd (patch) | |
| tree | 8b39f26fad90eaa27751fd061ab1cbc91ae95b08 /tests | |
| parent | b68531c40d91cadfd868e4b65ff92989ffd4fb27 (diff) | |
| parent | e912c87deaf220fd5fce1299d669e8fa992b5338 (diff) | |
| download | genenetwork4-85b63abcfdbae235d292a6de4bd47ffe6ba6dafd.tar.gz | |
Merge origin/main (e912c87: /search gains lod, locus, sort/order) into
native-search-engine. Resolution: his route-level additions compose above the engine seam — the handler now validates sort/order, asks the engine for SORT_FETCH_LIMIT (1000) when ranking, maps SearchError via the seam's engine_error, then computes lod, sorts, truncates to limit and fills loci; lod/locus join SearchHit in src/search/mod.rs. His tests pass through the seam. KNOWN at this commit: the parity floor is BLOCKED (36 shapes) — the route's new truncate(limit) exposes that route-level full-set grading was a calibration-stub artifact (real gn3 caps at per_page; the stub ignored it). Harness fix queued: set-equality moves to engine level, status + top-10 stay route level. Landed together with that fix.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/dataset_probeset_info.rs | 44 | ||||
| -rw-r--r-- | tests/dataset_probeset_values.rs | 42 | ||||
| -rw-r--r-- | tests/dataset_probesets.rs | 42 | ||||
| -rw-r--r-- | tests/group_case_attributes.rs | 145 | ||||
| -rw-r--r-- | tests/group_trait_info.rs | 35 | ||||
| -rw-r--r-- | tests/search.rs | 106 | ||||
| -rw-r--r-- | tests/traits.rs | 25 |
7 files changed, 439 insertions, 0 deletions
diff --git a/tests/dataset_probeset_info.rs b/tests/dataset_probeset_info.rs index 0c387ce..7dc012c 100644 --- a/tests/dataset_probeset_info.rs +++ b/tests/dataset_probeset_info.rs @@ -44,6 +44,8 @@ async fn dataset_probeset_info_returns_gene_annotation() { let info: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(info["name"], "100001_at"); + let (lrs, lod) = (info["lrs"].as_f64().unwrap(), info["lod"].as_f64().unwrap()); + assert!((lod - lrs / 4.61).abs() < 1e-9, "lod is lrs / 4.61"); assert_eq!(info["symbol"], "Cd3g"); assert!(info["mean"].as_f64().is_some()); } @@ -177,3 +179,45 @@ async fn dataset_probeset_info_non_public_unlinked_dataset_returns_403() { let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["code"], "forbidden"); } + +/// Same as above, but with a dataset genuinely less than a year old +/// (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 months +/// old by the time this test was written) -- the age-embargo rule and +/// the "not yet linked to any gn-auth resource" deny path both need to +/// hold for freshly-added data, not just for something that merely +/// hasn't aged into public yet. UTHSC_BXD_2wk_Cochlea_0926 +/// (public=1, confidentiality=1, created 2026-09-01) is the most +/// recently added ProbeSetFreeze in the database as of this writing. +#[tokio::test] +async fn dataset_probeset_info_recent_non_public_dataset_returns_403() { + if !common::db_available() { + eprintln!( + "skipping dataset_probeset_info_recent_non_public_dataset_returns_403: no usable database (check SQL_URI credentials)" + ); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!( + "skipping dataset_probeset_info_recent_non_public_dataset_returns_403: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" + ); + return; + }; + if !common::auth_server_available() { + eprintln!( + "skipping dataset_probeset_info_recent_non_public_dataset_returns_403: no gn-auth reachable" + ); + return; + } + + let response = common::get_with_token( + "GET /datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180", + "/datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::FORBIDDEN); + let body = response.into_body().collect().await.unwrap().to_bytes(); + let error: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(error["code"], "forbidden"); +} diff --git a/tests/dataset_probeset_values.rs b/tests/dataset_probeset_values.rs index 2e49735..fa32358 100644 --- a/tests/dataset_probeset_values.rs +++ b/tests/dataset_probeset_values.rs @@ -179,3 +179,45 @@ async fn dataset_probeset_values_non_public_unlinked_dataset_returns_403() { let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["code"], "forbidden"); } + +/// Same as above, but with a dataset genuinely less than a year old +/// (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 months +/// old by the time this test was written) -- the age-embargo rule and +/// the "not yet linked to any gn-auth resource" deny path both need to +/// hold for freshly-added data, not just for something that merely +/// hasn't aged into public yet. UTHSC_BXD_2wk_Cochlea_0926 +/// (public=1, confidentiality=1, created 2026-09-01) is the most +/// recently added ProbeSetFreeze in the database as of this writing. +#[tokio::test] +async fn dataset_probeset_values_recent_non_public_dataset_returns_403() { + if !common::db_available() { + eprintln!( + "skipping dataset_probeset_values_recent_non_public_dataset_returns_403: no usable database (check SQL_URI credentials)" + ); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!( + "skipping dataset_probeset_values_recent_non_public_dataset_returns_403: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" + ); + return; + }; + if !common::auth_server_available() { + eprintln!( + "skipping dataset_probeset_values_recent_non_public_dataset_returns_403: no gn-auth reachable" + ); + return; + } + + let response = common::get_with_token( + "GET /datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180/values", + "/datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180/values", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::FORBIDDEN); + let body = response.into_body().collect().await.unwrap().to_bytes(); + let error: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(error["code"], "forbidden"); +} diff --git a/tests/dataset_probesets.rs b/tests/dataset_probesets.rs index 149dd73..a68e523 100644 --- a/tests/dataset_probesets.rs +++ b/tests/dataset_probesets.rs @@ -142,3 +142,45 @@ async fn dataset_probesets_non_public_unlinked_dataset_returns_403() { let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["code"], "forbidden"); } + +/// Same as above, but with a dataset genuinely less than a year old +/// (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 months +/// old by the time this test was written) -- the age-embargo rule and +/// the "not yet linked to any gn-auth resource" deny path both need to +/// hold for freshly-added data, not just for something that merely +/// hasn't aged into public yet. UTHSC_BXD_2wk_Cochlea_0926 +/// (public=1, confidentiality=1, created 2026-09-01) is the most +/// recently added ProbeSetFreeze in the database as of this writing. +#[tokio::test] +async fn dataset_probesets_recent_non_public_dataset_returns_403() { + if !common::db_available() { + eprintln!( + "skipping dataset_probesets_recent_non_public_dataset_returns_403: no usable database (check SQL_URI credentials)" + ); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!( + "skipping dataset_probesets_recent_non_public_dataset_returns_403: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" + ); + return; + }; + if !common::auth_server_available() { + eprintln!( + "skipping dataset_probesets_recent_non_public_dataset_returns_403: no gn-auth reachable" + ); + return; + } + + let response = common::get_with_token( + "GET /datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets", + "/datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::FORBIDDEN); + let body = response.into_body().collect().await.unwrap().to_bytes(); + let error: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(error["code"], "forbidden"); +} diff --git a/tests/group_case_attributes.rs b/tests/group_case_attributes.rs new file mode 100644 index 0000000..245cf5b --- /dev/null +++ b/tests/group_case_attributes.rs @@ -0,0 +1,145 @@ +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 group_by_name.rs. +/// BXD (InbredSetId 1) has exactly 4 case attributes -- Epoch, RRID, +/// SeqCvge, Status -- each strain in the response should have exactly +/// those 4 keys, not more: db::get_group_case_attribute_values joins +/// CaseAttribute to CaseAttributeXRefNew on CaseAttributeId alone would +/// be wrong here, since CaseAttributeId isn't unique across groups (see +/// the comment in src/db.rs) -- getting that join wrong would pull in +/// unrelated groups' attributes too. +#[tokio::test] +async fn group_case_attributes_bxd_returns_names_and_values() { + if !common::db_available() { + eprintln!( + "skipping group_case_attributes_bxd_returns_names_and_values: no usable database (check SQL_URI credentials)" + ); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!( + "skipping group_case_attributes_bxd_returns_names_and_values: no token (see docs/gn-auth.md)" + ); + return; + }; + + let response = common::get_with_token( + "GET /groups/BXD/case-attributes", + "/groups/BXD/case-attributes", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let body = response.into_body().collect().await.unwrap().to_bytes(); + let case_attributes: Value = serde_json::from_slice(&body).unwrap(); + + let names: Vec<&str> = case_attributes["names"] + .as_array() + .unwrap() + .iter() + .map(|n| n.as_str().unwrap()) + .collect(); + assert_eq!(names, vec!["Epoch", "RRID", "SeqCvge", "Status"]); + + let values = case_attributes["values"].as_array().unwrap(); + assert!(!values.is_empty()); + let bxd1 = values + .iter() + .find(|v| v["strain"] == "BXD1") + .expect("BXD1 in /groups/BXD/case-attributes"); + let attrs = bxd1["case_attributes"].as_object().unwrap(); + assert_eq!(attrs.len(), 4, "BXD1 should have exactly BXD's 4 case attributes, not more"); + for name in &names { + assert!(attrs.contains_key(*name)); + } +} + +/// LXS (InbredSetId 9) is a real group with no case attributes defined +/// at all -- an empty result, not a 404 (the group itself exists). +#[tokio::test] +async fn group_case_attributes_group_with_none_returns_empty() { + if !common::db_available() { + eprintln!( + "skipping group_case_attributes_group_with_none_returns_empty: no usable database (check SQL_URI credentials)" + ); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!( + "skipping group_case_attributes_group_with_none_returns_empty: no token (see docs/gn-auth.md)" + ); + return; + }; + + let response = common::get_with_token( + "GET /groups/LXS/case-attributes", + "/groups/LXS/case-attributes", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let body = response.into_body().collect().await.unwrap().to_bytes(); + let case_attributes: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(case_attributes["names"].as_array().unwrap().len(), 0); + assert_eq!(case_attributes["values"].as_array().unwrap().len(), 0); +} + +#[tokio::test] +async fn group_case_attributes_without_a_token_returns_401() { + if !common::db_available() { + eprintln!( + "skipping group_case_attributes_without_a_token_returns_401: no usable database (check SQL_URI credentials)" + ); + return; + } + + let response = common::get( + "GET /groups/BXD/case-attributes (no token)", + "/groups/BXD/case-attributes", + ) + .await; + + assert_eq!(response.status(), StatusCode::UNAUTHORIZED); +} + +#[tokio::test] +async fn group_case_attributes_unknown_group_returns_404() { + if !common::db_available() { + eprintln!( + "skipping group_case_attributes_unknown_group_returns_404: no usable database (check SQL_URI credentials)" + ); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!( + "skipping group_case_attributes_unknown_group_returns_404: no token (see docs/gn-auth.md)" + ); + return; + }; + + let response = common::get_with_token( + "GET /groups/not-a-real-group/case-attributes", + "/groups/not-a-real-group/case-attributes", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::NOT_FOUND); + 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_case_attributes"); + assert_eq!(error["next"], "/groups"); +} diff --git a/tests/group_trait_info.rs b/tests/group_trait_info.rs index b8bda4c..f1d089a 100644 --- a/tests/group_trait_info.rs +++ b/tests/group_trait_info.rs @@ -109,3 +109,38 @@ async fn group_trait_info_unknown_trait_in_group_returns_404() { assert_eq!(error["code"], "trait_not_found"); assert_eq!(error["operation_id"], "group_trait_info"); } + +/// The "highest LOD ... serotonin in BXD male midbrain" trait (see +/// tests/search.rs scenario 7): midbrain 5-HT level in adult males. +/// Pins the marker name and that `lod` is `lrs` / 4.61. +#[tokio::test] +async fn group_trait_info_reports_lod_and_locus() { + if !common::db_available() { + eprintln!( + "skipping group_trait_info_reports_lod_and_locus: no usable database (check SQL_URI credentials)" + ); + return; + } + let Some(token) = common::auth_token().await else { + eprintln!("skipping group_trait_info_reports_lod_and_locus: no token (see docs/gn-auth.md)"); + return; + }; + + let response = common::get_with_token( + "GET /groups/BXD/traits/17985", + "/groups/BXD/traits/17985", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::OK); + + let body = response.into_body().collect().await.unwrap().to_bytes(); + let info: Value = serde_json::from_slice(&body).unwrap(); + + assert_eq!(info["locus"], "rsm10000012461"); + let (lrs, lod) = (info["lrs"].as_f64().unwrap(), info["lod"].as_f64().unwrap()); + assert!((lod - lrs / 4.61).abs() < 1e-9); + assert!((lod - 2.603).abs() < 0.001); + assert!((info["additive"].as_f64().unwrap() - -1.3041).abs() < 0.0001); +} diff --git a/tests/search.rs b/tests/search.rs index f10c05d..cc1c959 100644 --- a/tests/search.rs +++ b/tests/search.rs @@ -31,10 +31,19 @@ use serde_json::{Value, json}; use std::collections::HashMap; use tower::ServiceExt; +#[allow(dead_code)] +mod common; + /// Recorded from production on 2026-10-03: /// https://genenetwork.org/api3/search/?query=alcohol&type=phenotype&per_page=5 const FIXTURE: &str = include_str!("../test/data/search_alcohol_phenotype.json"); +/// Recorded from production on 2026-10-07: +/// https://genenetwork.org/api3/search/?query=serotonin+AND+midbrain+AND+group:BXD&type=phenotype&per_page=1000 +/// The hits behind the "highest LOD ... serotonin in BXD male midbrain" +/// scenario (7 below): 119 real BXD phenotype hits, noise included. +const SEROTONIN_FIXTURE: &str = include_str!("../test/data/search_serotonin_midbrain_bxd.json"); + /// Committed test-only key material: the JWKS gn-auth is stubbed with, /// and the matching RSA private key used to sign tokens locally. See /// test/data/test_jwks.json. @@ -43,6 +52,14 @@ const TEST_PRIVATE_KEY: &str = include_str!("../test/data/test_rsa_private.pem") /// Stands in for gn3's /api/search during tests. async fn serve_gn3(Query(params): Query<HashMap<String, String>>) -> (StatusCode, String) { + if params.get("query").is_some_and(|q| q.contains("serotonin")) { + // gn3 has no sort of its own: a sorted /search must ask for a + // wide page (1000), not just `limit`, to rank across it. + if params.get("per_page").map(String::as_str) != Some("1000") { + return (StatusCode::INTERNAL_SERVER_ERROR, "per_page must be 1000".into()); + } + return (StatusCode::OK, SEROTONIN_FIXTURE.to_string()); + } if params.get("query").is_some_and(|q| q.contains("&&")) { // Mirrors production gn3's shape for rejected xapian // queries: the diagnostic is under "error", and "error_type" @@ -203,6 +220,95 @@ async fn search_proxy_scenarios() { assert_eq!(status, StatusCode::BAD_GATEWAY); assert_eq!(body["code"], "backend_error"); + // 6. sort and order are validated before gn3 is touched. + let (status, body) = get_json("/search?q=alcohol&sort=mean", token).await; + assert_eq!(status, StatusCode::BAD_REQUEST); + assert_eq!(body["code"], "invalid_parameter"); + assert!(body["detail"].as_str().unwrap().contains("lrs")); + + let (status, body) = get_json("/search?q=alcohol&sort=lod&order=sideways", token).await; + assert_eq!(status, StatusCode::BAD_REQUEST); + assert!(body["detail"].as_str().unwrap().contains("asc")); + + // 7. The question: "What is the highest LOD score, locus marker, + // and additive effect for serotonin in BXD male midbrain?" + // Search the BXD group ranked by LOD, then take the best hit + // whose description is a male-midbrain trait. (Males only: + // " males [" matches "adult males [" but not "adult females [" + // or "adult males and females [".) + let (status, body) = get_json( + "/search?q=serotonin%20AND%20midbrain%20AND%20group:BXD&sort=lod&limit=100", + token, + ) + .await; + assert_eq!(status, StatusCode::OK); + let hits = body["data"].as_array().unwrap(); + assert_eq!(hits.len(), 100, "119 recorded hits, truncated to limit"); + let lods: Vec<f64> = hits.iter().map(|h| h["lod"].as_f64().unwrap()).collect(); + assert!(lods.windows(2).all(|w| w[0] >= w[1]), "descending by lod"); + for hit in hits { + let (lrs, lod) = (hit["lrs"].as_f64().unwrap(), hit["lod"].as_f64().unwrap()); + assert!((lod - lrs / 4.61).abs() < 1e-9, "lod is lrs / 4.61"); + } + let male_midbrain = |hit: &&Value| { + let d = hit["description"].as_str().unwrap(); + d.contains("Midbrain") && d.contains(" males [") + }; + let is_5ht_level = |hit: &&Value| { + hit["description"] + .as_str() + .unwrap() + .contains("5-hydroxytryptamine (5-HT, serotonin) level") + }; + + // Serotonin itself (the 5-HT level): trait 17985. + let serotonin = hits + .iter() + .filter(male_midbrain) + .find(is_5ht_level) + .expect("a male midbrain 5-HT level hit"); + assert_eq!(serotonin["name"], "17985"); + assert_eq!(serotonin["dataset"], "BXDPublish"); + assert!((serotonin["lod"].as_f64().unwrap() - 2.603).abs() < 0.001); + assert!((serotonin["additive"].as_f64().unwrap() - -1.3041).abs() < 0.0001); + assert_eq!(serotonin["geno_chr"], "16"); + + // The serotonin transporter (SERT) protein in the same tissue and + // sex has the higher LOD, and is the top male-midbrain hit overall: + // "serotonin" in the question is ambiguous between the two. + let top = hits.iter().find(male_midbrain).unwrap(); + assert_eq!(top["name"], "17994"); + assert!((top["lod"].as_f64().unwrap() - 3.577).abs() < 0.001); + assert!((top["additive"].as_f64().unwrap() - 233.99).abs() < 0.01); + + // The peak marker's name comes from PublishXRef, so this part needs + // a live MariaDB; without one, `locus` is null and the rest above + // still holds. + if common::db_available() { + assert_eq!(serotonin["locus"], "rsm10000012461"); + assert_eq!(top["locus"], "rsm10000013922"); + } else { + eprintln!("skipping locus checks: no usable database (check SQL_URI credentials)"); + } + + // 8. order=asc reverses the ranking. + let recorded: Vec<Value> = serde_json::from_str(SEROTONIN_FIXTURE).unwrap(); + let min_lrs = recorded + .iter() + .map(|h| h["lrs"].as_f64().unwrap()) + .fold(f64::INFINITY, f64::min); + let (status, body) = get_json( + "/search?q=serotonin&sort=lrs&order=asc&limit=3", + token, + ) + .await; + assert_eq!(status, StatusCode::OK); + let hits = body["data"].as_array().unwrap(); + assert_eq!(hits.len(), 3); + assert_eq!(hits[0]["lrs"].as_f64().unwrap(), min_lrs); + assert!(hits[0]["lrs"].as_f64() <= hits[1]["lrs"].as_f64()); + assert!(hits[1]["lrs"].as_f64() <= hits[2]["lrs"].as_f64()); + // A locally signed token with the wrong key (or a tampered one) is // rejected instead of reaching gn3 -- the point of the JWKS stub. let (status, body) = get_json("/search?q=alcohol", Some("not-a-real-token")).await; diff --git a/tests/traits.rs b/tests/traits.rs index 7e892ae..9ee1008 100644 --- a/tests/traits.rs +++ b/tests/traits.rs @@ -238,3 +238,28 @@ async fn trait_info_non_public_unlinked_probeset_dataset_returns_403() { let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["code"], "forbidden"); } + +/// Same as above, but with a dataset genuinely less than a year old +/// (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 months +/// old by the time this test was written) -- the age-embargo rule and +/// the "not yet linked to any gn-auth resource" deny path both need to +/// hold for freshly-added data, not just for something that merely +/// hasn't aged into public yet. UTHSC_BXD_2wk_Cochlea_0926 +/// (public=1, confidentiality=1, created 2026-09-01) is the most +/// recently added ProbeSetFreeze in the database as of this writing. +#[tokio::test] +async fn trait_info_recent_non_public_probeset_dataset_returns_403() { + let Some(token) = fixtures().await else { return }; + + let response = common::get_with_token( + "GET /traits/UTHSC_BXD_2wk_Cochlea_0926/ENSMUSG00000028180", + "/traits/UTHSC_BXD_2wk_Cochlea_0926/ENSMUSG00000028180", + &token, + ) + .await; + + assert_eq!(response.status(), StatusCode::FORBIDDEN); + let body = response.into_body().collect().await.unwrap().to_bytes(); + let error: Value = serde_json::from_slice(&body).unwrap(); + assert_eq!(error["code"], "forbidden"); +} |
