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
193
194
195
196
197
198
199
200
201
202
203
|
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); skips rather
/// than fails when none is reachable, same as species.rs. BXD is used
/// everywhere as a test fixture in the GeneNetwork2/GeneNetwork3 test
/// suites and is always present in the database.
#[tokio::test]
async fn groups_returns_ok_with_bxd() {
if !common::db_available() {
eprintln!(
"skipping groups_returns_ok_with_bxd: no usable database (check SQL_URI credentials)"
);
return;
}
let Some(token) = common::auth_token().await else {
eprintln!("skipping groups_returns_ok_with_bxd: no token (see docs/gn-auth.md)");
return;
};
let response = common::get_with_token("GET /groups", "/groups", &token).await;
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
let groups: Vec<Value> = common::data(&body);
assert!(!groups.is_empty());
let names: Vec<&str> = groups
.iter()
.map(|record| record["name"].as_str().unwrap())
.collect();
assert!(names.contains(&"BXD"));
}
/// Every group carries its family and species name, and /groups takes
/// `family`, `species` and `q` filters (case-insensitive, combined with
/// AND).
#[tokio::test]
async fn groups_filters_by_family_species_and_text() {
if !common::db_available() {
eprintln!(
"skipping groups_filters_by_family_species_and_text: no usable database (check SQL_URI credentials)"
);
return;
}
let Some(token) = common::auth_token().await else {
eprintln!(
"skipping groups_filters_by_family_species_and_text: no token (see docs/gn-auth.md)"
);
return;
};
let groups = |uri: &'static str| {
let token = token.clone();
async move {
let response = common::get_with_token(&format!("GET {uri}"), uri, &token).await;
assert_eq!(response.status(), StatusCode::OK, "GET {uri}");
let body = response.into_body().collect().await.unwrap().to_bytes();
let groups: Vec<Value> = common::data(&body);
groups
}
};
let names = |groups: &[Value]| -> Vec<String> {
groups
.iter()
.map(|g| g["name"].as_str().unwrap().to_string())
.collect()
};
// Unfiltered: BXD is a mouse Reference Population.
let all = groups("/groups").await;
let bxd = all.iter().find(|g| g["name"] == "BXD").unwrap();
assert_eq!(bxd["species"], "mouse");
assert!(
bxd["family"]
.as_str()
.unwrap()
.starts_with("Reference Populations")
);
// family: case-insensitive "contains".
let reference = names(&groups("/groups?family=reference%20populations").await);
for expected in [
"AKXD", "AXBXA", "BXD", "BXH", "CC", "CXB", "HXBBXH", "LXS", "MDP",
] {
assert!(
reference.contains(&expected.to_string()),
"{expected} in {reference:?}"
);
}
assert!(!reference.contains(&"HSNIH-Palmer".to_string()));
// species: the short name /species lists, case-insensitive.
let rat = groups("/groups?species=RAT").await;
assert!(!rat.is_empty() && rat.iter().all(|g| g["species"] == "rat"));
// q: name, display name or full name.
let alzheimer = names(&groups("/groups?q=alzheimer").await);
assert!(alzheimer.contains(&"BXD-NIA-AD".to_string()));
// Filters combine with AND.
let rat_reference = names(&groups("/groups?species=rat&family=reference").await);
assert!(rat_reference.contains(&"HXBBXH".to_string()));
assert!(!rat_reference.contains(&"BXD".to_string()));
}
/// Each group reports the mapping tool offered for it (the MappingMethod
/// its mapping_method_id points to), or null when it has none.
#[tokio::test]
async fn groups_report_the_mapping_tool_offered_for_them() {
if !common::db_available() {
eprintln!(
"skipping groups_report_the_mapping_tool_offered_for_them: no usable database (check SQL_URI credentials)"
);
return;
}
let Some(token) = common::auth_token().await else {
eprintln!(
"skipping groups_report_the_mapping_tool_offered_for_them: no token (see docs/gn-auth.md)"
);
return;
};
let response = common::get_with_token("GET /groups", "/groups", &token).await;
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
let groups: Vec<Value> = common::data(&body);
let tool = |name: &str| -> Value {
groups.iter().find(|g| g["name"] == name).unwrap()["mapping_method"].clone()
};
assert_eq!(tool("BXD"), "qtlreaper");
assert_eq!(tool("HSNIH-Palmer"), "Rqtl");
assert_eq!(tool("HSNIH-RGSMC"), "qtlreaper"); // not every outbred set maps with Rqtl
assert!(tool("Oregon-R_x_2b3").is_null());
}
/// The values endpoint normalises on request, says so, and carries the same
/// pipeline into the mapping suggestions -- a caller who fetched z-scores
/// must not then map the raw numbers.
#[tokio::test]
async fn trait_values_can_be_normalized_and_the_suggestion_keeps_it() {
if !common::db_available() {
eprintln!("skipping trait_values_can_be_normalized_and_the_suggestion_keeps_it: no usable database");
return;
}
let Some(token) = common::auth_token().await else {
eprintln!("skipping trait_values_can_be_normalized_and_the_suggestion_keeps_it: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
let uri = "/groups/BXD/traits/10010/values?normalize=zscore";
let response = common::get_with_token("GET values (zscore)", uri, &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 values: Vec<f64> = envelope["data"]
.as_array()
.unwrap()
.iter()
.map(|row| row["value"].as_f64().expect("a numeric value"))
.collect();
assert!(values.len() > 20, "the trait has values: {}", values.len());
let mean = values.iter().sum::<f64>() / values.len() as f64;
let sd = (values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64).sqrt();
assert!(mean.abs() < 1e-9, "z-scores are mean 0, got {mean}");
assert!((sd - 1.0).abs() < 1e-9, "population sd 1, got {sd}");
assert!(
envelope["notes"]
.as_array()
.unwrap()
.iter()
.any(|note| note.as_str().unwrap_or("").starts_with("Normalized: zscore")),
"the note says what ran: {}",
envelope["notes"]
);
// ... and the mapping bodies carry it.
for action in envelope["actions"].as_array().unwrap() {
if let Some(href) = action["href"].as_str() {
if href.contains("/compute/") {
assert_eq!(
action["body"]["normalize"], "zscore",
"{href} maps the values that were shown: {action}"
);
}
}
}
// A normalisation that cannot apply is a 422, not a silent no-op.
let response = common::get_with_token(
"GET values (impossible)",
"/groups/BXD/traits/10010/values?normalize=log2&winsor_quantile=0.05&winsor_fences=1.5",
&token,
)
.await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let error: Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(error["code"], "invalid_normalization");
}
|