about summary refs log tree commit diff
path: root/tests/wikidata.rs
blob: a32afbf23b5a250184f40187968585e080211124 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! `GET /genes/{symbol}/aliases`: Wikidata's aliases, cached in LMDB.
//!
//! The cache is the point of the endpoint -- Wikidata is a shared public
//! service and an alias list is not news -- so the tests pin the caching
//! behaviour (a second call is served from `gene-aliases.lmdb`), the shape,
//! and the species switch. They need the network for the first call; when
//! Wikidata cannot be reached the test says so and checks the failure is
//! reported honestly (a 502, not an empty alias list).

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

mod common;

async fn ready() -> Option<String> {
    common::auth_token().await
}

#[tokio::test]
async fn a_symbol_gets_its_wikidata_aliases_and_they_are_cached() {
    let Some(token) = ready().await else {
        eprintln!("skipping a_symbol_gets_its_wikidata_aliases_and_they_are_cached: no token");
        return;
    };
    let first = common::get_with_token(
        "GET /genes/{symbol}/aliases",
        "/genes/Shh/aliases",
        &token,
    )
    .await;
    let status = first.status();
    let bytes = first.into_body().collect().await.unwrap().to_bytes();
    if status != StatusCode::OK {
        // No network, or Wikidata is down: the honest answer is a 502 that
        // says so, never an empty alias list.
        eprintln!("skipping: Wikidata answered {status}: {}", String::from_utf8_lossy(&bytes));
        assert_eq!(status, StatusCode::BAD_GATEWAY);
        let error: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(error["code"], "wikidata_unavailable");
        return;
    }
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    let answer = &envelope["data"];
    if answer["stale"] == serde_json::json!(true) {
        // Wikidata answered too slowly and the cached entity was served: the
        // endpoint is behaving, the test is not in a position to assert.
        eprintln!("skipping: the reply is stale (Wikidata timed out), not a failure");
        return;
    }
    assert_eq!(answer["symbol"], "Shh");
    assert!(
        answer["wikidata_id"].as_str().is_some_and(|id| id.starts_with('Q')),
        "a Wikidata entity: {answer}"
    );
    assert!(
        answer["aliases"].as_array().is_some_and(|aliases| !aliases.is_empty()),
        "Shh has aliases: {answer}"
    );
    assert_eq!(answer["stale"], serde_json::json!(false));
    let notes: Vec<&str> = envelope["notes"]
        .as_array()
        .unwrap()
        .iter()
        .map(|note| note.as_str().unwrap())
        .collect();
    assert!(
        notes.iter().any(|note| note.contains("gene-aliases.lmdb")),
        "the cache file is named in the reply: {notes:?}"
    );
    // The second call must be served from the cache: no SPARQL round trip.
    let second = common::get_with_token(
        "GET /genes/{symbol}/aliases (cached)",
        "/genes/Shh/aliases",
        &token,
    )
    .await;
    assert_eq!(second.status(), StatusCode::OK);
    let bytes = second.into_body().collect().await.unwrap().to_bytes();
    let cached: Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(
        cached["data"]["aliases"], answer["aliases"],
        "the cache answers the same list"
    );
    assert_eq!(
        cached["data"]["fetched_at"], answer["fetched_at"],
        "and it is the same fetch, not a new one"
    );
}

#[tokio::test]
async fn the_species_switch_picks_a_different_entity() {
    let Some(token) = ready().await else {
        eprintln!("skipping the_species_switch_picks_a_different_entity: no token");
        return;
    };
    let human = common::get_with_token(
        "GET /genes/{symbol}/aliases?species=human",
        "/genes/Shh/aliases?species=human",
        &token,
    )
    .await;
    if human.status() != StatusCode::OK {
        eprintln!("skipping: Wikidata unavailable for the species switch");
        return;
    }
    let bytes = human.into_body().collect().await.unwrap().to_bytes();
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(envelope["data"]["species"], "human");
    // The human ortholog's labels are the ones GeneNetwork2's trait page
    // shows for a mouse trait: HLP3, HPE3 and the rest.
    let aliases = envelope["data"]["aliases"].as_array().unwrap();
    let joined = aliases
        .iter()
        .filter_map(|alias| alias.as_str())
        .collect::<Vec<_>>()
        .join(",");
    assert!(
        joined.contains("HLP3") || joined.contains("HPE3"),
        "human SHH labels: {joined}"
    );
}

/// The link-outs, and the `label` that makes the human-only services reachable
/// from a rodent symbol: GTEx is human, so a mouse `Shh` gets `SHH` through
/// `?species=human`, never a guess.
#[tokio::test]
async fn aliases_carry_link_outs_for_the_matched_species() {
    let Some(token) = ready().await else {
        eprintln!("skipping aliases_carry_link_outs_for_the_matched_species: no token");
        return;
    };
    let mouse = common::get_with_token(
        "GET /genes/Shh/aliases",
        "/genes/Shh/aliases",
        &token,
    )
    .await;
    if mouse.status() != StatusCode::OK {
        eprintln!("skipping: Wikidata unavailable for the mouse entity");
        return;
    }
    let bytes = mouse.into_body().collect().await.unwrap().to_bytes();
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    let answer = &envelope["data"];
    let links = answer["links"].as_array().unwrap();
    let names: Vec<&str> = links
        .iter()
        .map(|link| link["name"].as_str().unwrap())
        .collect();
    // A mouse gene: the mouse-specific services are there, GTEx is not.
    for expected in ["RGD", "MGI", "Alliance of Genome Resources", "NCBI Gene", "UniProt"] {
        assert!(names.contains(&expected), "{expected} in {names:?}");
    }
    assert!(!names.contains(&"GTEx"), "GTEx is human-only: {names:?}");
    let rgd = links
        .iter()
        .find(|link| link["name"] == "RGD")
        .unwrap();
    assert!(rgd["href"].as_str().unwrap().contains("species=Mouse"), "{rgd}");

    // The human ortholog: a label a human-only service can use, and its link.
    let human = common::get_with_token(
        "GET /genes/Shh/aliases?species=human",
        "/genes/Shh/aliases?species=human",
        &token,
    )
    .await;
    if human.status() != StatusCode::OK {
        eprintln!("skipping the human half: Wikidata unavailable");
        return;
    }
    let bytes = human.into_body().collect().await.unwrap().to_bytes();
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    let answer = &envelope["data"];
    if answer["stale"] == serde_json::json!(true) {
        eprintln!("skipping the human half: the reply is stale, so its label may predate this field");
        return;
    }
    assert_eq!(answer["label"], "SHH", "the human symbol, not the mouse one");
    let gtex = answer["links"]
        .as_array()
        .unwrap()
        .iter()
        .find(|link| link["name"] == "GTEx")
        .expect("a human entity gets a GTEx link");
    assert_eq!(
        gtex["href"],
        "https://www.gtexportal.org/home/gene/SHH",
        "built from the human label"
    );
}