about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/bnw.rs207
1 files changed, 207 insertions, 0 deletions
diff --git a/tests/bnw.rs b/tests/bnw.rs
new file mode 100644
index 0000000..c26c1c8
--- /dev/null
+++ b/tests/bnw.rs
@@ -0,0 +1,207 @@
+//! `POST /compute/bnw`: a Bayesian network over a set of traits.
+//!
+//! The endpoint calls out to BNW's headless driver (the `bnw` program from the
+//! gn-bnw repository). These tests use the six continuous traits of BNW's own
+//! 8-node example -- the same fixture the driver's parity check compares
+//! against the web application -- so "the endpoint works" means "the edges are
+//! the ones BNW itself produces", not merely "a 200 came back".
+//!
+//! They skip when the driver is not installed, and also when the driver is
+//! installed but its C programs are not: that is a deployment that has not
+//! finished, not a bug here.
+
+use axum::http::StatusCode;
+use http_body_util::BodyExt;
+use serde_json::{json, Value};
+
+mod common;
+
+/// True only when the BNW driver can actually run a network: the program
+/// exists *and* the compiled C programs it needs are beside it.
+fn bnw_available() -> bool {
+    let config = genenetwork4::config::Config::load();
+    if std::process::Command::new(&config.bnw_cmd)
+        .arg("version")
+        .output()
+        .is_err()
+    {
+        return false;
+    }
+    // A driver with no binaries exits 77 ("skip"), which is exactly the
+    // condition this guard is for.
+    matches!(
+        std::process::Command::new(&config.bnw_cmd)
+            .args(["learn", "--input", "/dev/null"])
+            .output(),
+        Ok(output) if output.status.code() != Some(77)
+    )
+}
+
+/// The six continuous traits of BNW's own example, as an explicit matrix.
+fn six_traits() -> Value {
+    // The fixture lives in a sibling repository: <code>/gn-bnw, while this
+    // checkout is <code>/genenetwork/genenetwork4. GN_BNW_DIR overrides both.
+    let candidates = [
+        std::env::var("GN_BNW_DIR")
+            .map(|dir| format!("{dir}/test/golden/six_traits_matrix.tsv"))
+            .unwrap_or_default(),
+        format!("{}/../../gn-bnw/test/golden/six_traits_matrix.tsv", env!("CARGO_MANIFEST_DIR")),
+        format!("{}/../gn-bnw/test/golden/six_traits_matrix.tsv", env!("CARGO_MANIFEST_DIR")),
+    ];
+    let Some(text) = candidates
+        .iter()
+        .filter(|path| !path.is_empty())
+        .find_map(|path| std::fs::read_to_string(path).ok())
+    else {
+        return Value::Null;
+    };
+    let mut lines = text.lines().filter(|line| !line.trim().is_empty());
+    let Some(header) = lines.next() else {
+        return Value::Null;
+    };
+    let names: Vec<&str> = header.split('\t').collect();
+    let rows: Vec<Vec<&str>> = lines.map(|line| line.split('\t').collect()).collect();
+    let mut variables = Vec::with_capacity(names.len());
+    for (column, name) in names.iter().enumerate() {
+        let samples: Vec<Value> = rows
+            .iter()
+            .enumerate()
+            .map(|(index, row)| {
+                json!({"name": format!("s{index}"), "value": row[column].parse::<f64>().unwrap()})
+            })
+            .collect();
+        variables.push(json!({"name": name, "samples": samples}));
+    }
+    Value::Array(variables)
+}
+
+async fn post(body: Value, token: &str) -> (StatusCode, Value) {
+    let response = common::post_json_with_token(
+        "POST /compute/bnw",
+        "/compute/bnw",
+        token,
+        &body,
+    )
+    .await;
+    let status = response.status();
+    let bytes = response.into_body().collect().await.unwrap().to_bytes();
+    (status, serde_json::from_slice(&bytes).unwrap())
+}
+
+#[tokio::test]
+async fn a_network_reports_the_edges_bnw_finds() {
+    let Some(token) = common::auth_token().await else {
+        eprintln!("skipping a_network_reports_the_edges_bnw_finds: set GN4_TEST_AUTH_TOKEN");
+        return;
+    };
+    if !bnw_available() {
+        eprintln!(
+            "skipping a_network_reports_the_edges_bnw_finds: no BNW driver (set BNW_CMD / \
+             BNW_BIN_DIR; see gn-bnw/var_lib_genenet_bnw/build.sh)"
+        );
+        return;
+    }
+    let variables = six_traits();
+    if variables.is_null() {
+        eprintln!("skipping a_network_reports_the_edges_bnw_finds: gn-bnw fixtures not found");
+        return;
+    }
+
+    let (status, body) = post(
+        json!({"values": variables, "trait_name": "golden-6-traits"}),
+        &token,
+    )
+    .await;
+    assert_eq!(status, StatusCode::OK, "{body}");
+    assert_eq!(body["meta"]["columns"], json!(["from", "to", "posterior", "in_structure"]));
+
+    let rows = body["data"].as_array().unwrap();
+    // 6 variables: 6 x 5 directed pairs, self-edges excluded.
+    assert_eq!(rows.len(), 30, "one row per candidate edge");
+    let in_structure: Vec<(String, String)> = rows
+        .iter()
+        .filter(|row| row[3].as_bool().unwrap_or(false))
+        .map(|row| {
+            (
+                row[0].as_str().unwrap().to_string(),
+                row[1].as_str().unwrap().to_string(),
+            )
+        })
+        .collect();
+    // The web application's own answer for this fixture (see
+    // gn-bnw/test/golden/six_traits_structure_input.txt):
+    let mut expected = vec![
+        ("Trait2".to_string(), "Trait1".to_string()),
+        ("Trait2".to_string(), "Trait4".to_string()),
+        ("Trait3".to_string(), "Trait4".to_string()),
+        ("Trait6".to_string(), "Trait3".to_string()),
+        ("Trait6".to_string(), "Trait4".to_string()),
+    ];
+    let mut got = in_structure.clone();
+    expected.sort();
+    got.sort();
+    assert_eq!(got, expected, "the network must be BNW's own");
+
+    // The notes have to name the parameters and say what a posterior is.
+    let notes = body["notes"].as_array().unwrap();
+    let notes: Vec<&str> = notes.iter().map(|note| note.as_str().unwrap()).collect();
+    assert!(
+        notes.iter().any(|note| note.contains("maxparents 4") && note.contains("threshold 0.5")),
+        "the parameters used are not named: {notes:?}"
+    );
+    assert!(
+        notes.iter().any(|note| note.contains("not a p-value")),
+        "the posterior caveat is missing: {notes:?}"
+    );
+}
+
+#[tokio::test]
+async fn an_unusable_network_request_is_refused_before_any_work() {
+    let Some(token) = common::auth_token().await else {
+        eprintln!(
+            "skipping an_unusable_network_request_is_refused_before_any_work: set GN4_TEST_AUTH_TOKEN"
+        );
+        return;
+    };
+    let cases = [
+        // one variable is not a network
+        (
+            json!({"values": [{"name": "a", "samples": [
+                {"name": "s1", "value": 1.0}, {"name": "s2", "value": 2.0},
+                {"name": "s3", "value": 3.0}]}]}),
+            "invalid_network_input",
+        ),
+        // one trait is not a network either
+        (
+            json!({"group": "BXD", "traits": [10001]}),
+            "too_few_variables",
+        ),
+        // a threshold that is not a posterior
+        (
+            json!({"values": [
+                {"name": "a", "samples": [{"name": "s1", "value": 1.0},
+                                          {"name": "s2", "value": 2.0},
+                                          {"name": "s3", "value": 3.0}]},
+                {"name": "b", "samples": [{"name": "s1", "value": 1.0},
+                                          {"name": "s2", "value": 2.5},
+                                          {"name": "s3", "value": 2.0}]}],
+             "threshold": 1.5}),
+            "invalid_threshold",
+        ),
+        // both shapes at once
+        (
+            json!({"group": "BXD", "traits": [10001, 10002],
+                   "values": [{"name": "a", "samples": []}]}),
+            "ambiguous_input",
+        ),
+    ];
+    for (body, expected) in cases {
+        let (status, reply) = post(body, &token).await;
+        assert_eq!(
+            status,
+            StatusCode::UNPROCESSABLE_ENTITY,
+            "{expected}: {reply}"
+        );
+        assert_eq!(reply["code"], expected, "{reply}");
+    }
+}