about summary refs log tree commit diff
path: root/tests/probesets.rs
blob: 6c4ce9d2e981aab5912f82483dd00014d5cdb1b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! `GET /probesets/{name}` and `GET /genes/{symbol}/traits`: the cross-dataset
//! lookups the search index cannot do (a probeset id is not a Xapian field,
//! and a symbol search is capped and ungrouped).
//!
//! Both return metadata only -- which datasets carry the probeset and what
//! their scans found -- so the tests pin the row shape, the totals, the
//! filters and the fact that every link out of them is followable.

use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;

mod common;

async fn ready() -> Option<String> {
    if !common::db_available() {
        eprintln!("skipping: no usable database");
        return None;
    }
    common::auth_token().await
}

#[tokio::test]
async fn a_probeset_resolves_to_its_datasets() {
    let Some(token) = ready().await else { return };
    let response = common::get_with_token(
        "GET /probesets/{name}",
        "/probesets/1427571_at?limit=5",
        &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();
    let columns: Vec<&str> = envelope["meta"]["columns"]
        .as_array()
        .unwrap()
        .iter()
        .map(|column| column.as_str().unwrap())
        .collect();
    for expected in [
        "dataset", "group", "species", "platform", "name", "symbol", "chr", "mb", "mean", "lrs",
        "lod", "locus", "additive",
    ] {
        assert!(columns.contains(&expected), "{expected} in {columns:?}");
    }
    let rows = envelope["data"].as_array().unwrap();
    assert_eq!(rows.len(), 5, "the page size is respected");
    let name = columns.iter().position(|column| *column == "name").unwrap();
    let symbol = columns.iter().position(|column| *column == "symbol").unwrap();
    assert!(
        rows.iter().any(|row| row[name]
            .as_str()
            .unwrap()
            .starts_with("1427571_at")),
        "the rows are the probeset asked for: {rows:?}"
    );
    // 1427571_at is Shh; a row that matched a suffixed variant says so itself.
    assert!(
        rows.iter()
            .any(|row| row[symbol].as_str() == Some("Shh")),
        "the annotation travels with the row: {rows:?}"
    );
    // The totals are in notes, which is the point of the endpoint.
    let notes = envelope["notes"].as_array().unwrap();
    let first = notes[0].as_str().unwrap();
    assert!(first.contains("dataset(s)"), "{first}");
    assert!(first.contains("1427571_at"), "{first}");
    // And it says where the values (which it does not serve) live.
    let links: Vec<&str> = envelope["next"]
        .as_array()
        .unwrap()
        .iter()
        .map(|action| action["href"].as_str().unwrap())
        .collect();
    assert!(
        links
            .iter()
            .any(|href| href.contains("/datasets/") && href.ends_with("/values")),
        "the values are one link away: {links:?}"
    );
}

#[tokio::test]
async fn a_symbol_lists_its_traits_with_a_real_total() {
    let Some(token) = ready().await else { return };
    let response = common::get_with_token(
        "GET /genes/{symbol}/traits",
        "/genes/Shh/traits?sort=lod&min_lod=5&limit=5",
        &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();
    let rows = envelope["data"].as_array().unwrap();
    assert_eq!(rows.len(), 5);
    let columns: Vec<&str> = envelope["meta"]["columns"]
        .as_array()
        .unwrap()
        .iter()
        .map(|column| column.as_str().unwrap())
        .collect();
    let lod = columns.iter().position(|column| *column == "lod").unwrap();
    // ?sort=lod means the page starts at the strongest peak, and ?min_lod=5
    // means nothing weaker is in it.
    let lods: Vec<f64> = rows.iter().map(|row| row[lod].as_f64().unwrap()).collect();
    assert!(lods[0] >= lods[lods.len() - 1], "sorted by lod: {lods:?}");
    assert!(lods.iter().all(|value| *value >= 5.0), "{lods:?}");
    let notes = envelope["notes"].as_array().unwrap();
    let first = notes[0].as_str().unwrap();
    assert!(first.contains("expression trait row(s)"), "{first}");
    assert!(first.contains("after the filters"), "a filter count: {first}");

    // An unknown symbol is a 404, not an empty list.
    let response =
        common::get_with_token("GET /genes/{symbol}/traits", "/genes/NOSUCHGENE/traits", &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"], "gene_not_found");
}

#[tokio::test]
async fn the_filters_narrow_a_gene_to_one_group() {
    let Some(token) = ready().await else { return };
    let all = common::get_with_token("GET /genes/{symbol}/traits", "/genes/Shh/traits?limit=1", &token).await;
    assert_eq!(all.status(), StatusCode::OK);
    let bytes = all.into_body().collect().await.unwrap().to_bytes();
    let all: Value = serde_json::from_slice(&bytes).unwrap();
    let everything = all["notes"][0].as_str().unwrap().to_string();

    let one = common::get_with_token(
        "GET /genes/{symbol}/traits?group=",
        "/genes/Shh/traits?group=BXD&limit=1",
        &token,
    )
    .await;
    assert_eq!(one.status(), StatusCode::OK);
    let bytes = one.into_body().collect().await.unwrap().to_bytes();
    let one: Value = serde_json::from_slice(&bytes).unwrap();
    let narrowed = one["notes"][0].as_str().unwrap().to_string();
    assert_ne!(everything, narrowed, "the group filter changes the totals");
    let row = one["data"][0].as_array().unwrap();
    assert_eq!(row[1].as_str(), Some("BXD"), "every row is that group: {row:?}");
}