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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
//! `GET /groups/{group}/genotypes`, and mapping by group + version instead
//! of by path.
//!
//! The listing needs the deployment's genotype-files directory (config key
//! `GENOTYPE_FILES`), so it skips -- like the database-backed tests -- when
//! that directory is not there. The two mapping errors asserted here are
//! resolution failures, so they need no Rscript and no GEMMA.
use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;
mod common;
use common::{auth_token, data, get_with_token, post_json_with_token};
fn genotype_dir_present() -> bool {
let directory = genenetwork4::config::Config::load().genotype_files_dir;
std::path::Path::new(&directory).is_dir()
}
#[tokio::test]
async fn the_listing_names_versions_without_paths_and_maps_by_group() {
let Some(token) = auth_token().await else {
eprintln!(
"skipping the_listing_names_versions_without_paths_and_maps_by_group: set \
GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"
);
return;
};
if !genotype_dir_present() {
eprintln!(
"skipping the_listing_names_versions_without_paths_and_maps_by_group: no \
genotype-files directory (GENOTYPE_FILES)"
);
return;
}
// 1. The listing: versions, one current, no paths and no file names.
let response = get_with_token(
"GET /groups/BXD/genotypes",
"/groups/BXD/genotypes",
&token,
)
.await;
assert_eq!(response.status(), StatusCode::OK);
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let listing: Value = data(&bytes);
let versions = listing["versions"].as_array().expect("versions");
assert!(!versions.is_empty(), "{listing}");
let current: Vec<&Value> = versions
.iter()
.filter(|version| version["current"] == true)
.collect();
assert_eq!(current.len(), 1, "exactly one current version: {listing}");
assert_eq!(versions[0]["current"], true, "current is first: {listing}");
for version in versions {
assert!(version["version"].is_string(), "{version}");
assert!(version["individuals"].as_u64().unwrap() > 0, "{version}");
assert!(version["markers"].as_u64().unwrap() > 0, "{version}");
}
// No paths -- but the file *name* is part of the metadata now.
let text = listing.to_string();
assert!(!text.contains('/'), "no path in the listing: {text}");
for version in versions {
let file = version["file"].as_str().expect("a file name");
assert!(file.ends_with(".geno"), "{version}");
assert!(!file.contains('/'), "{version}");
}
// The group record points at it.
let response = get_with_token("GET /groups/BXD", "/groups/BXD", &token).await;
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let group: Value = serde_json::from_slice(&bytes).unwrap();
assert!(
group["next"]
.as_array()
.unwrap()
.iter()
.any(|next| next["href"] == "/groups/BXD/genotypes"),
"{group}"
);
// 2. Mapping by group: the resolution errors need no R/GEMMA.
let unknown = serde_json::json!({
"group": "BXD", "genotype_version": "BXD.99", "trait_name": "t",
"samples": [{"name": "BXD1", "value": 1.0}],
});
let response =
post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &unknown).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(body["code"], "genotypes_not_found");
assert!(
body["detail"]
.as_str()
.unwrap()
.contains(&versions[0]["version"].as_str().unwrap().to_string()),
"the error lists what is available: {body}"
);
let both = serde_json::json!({
"group": "BXD", "genotype_file": "/tmp/x.geno", "trait_name": "t",
"samples": [],
});
let response =
post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &both).await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
let bytes = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(
serde_json::from_slice::<Value>(&bytes).unwrap()["code"],
"ambiguous_genotype"
);
// 3. A group with no genotype files is a 404, not an empty list.
let response = get_with_token(
"GET /groups/not-a-group/genotypes",
"/groups/not-a-group/genotypes",
&token,
)
.await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
/// The marker list is names only; a position is one call per marker. That
/// split keeps a page of names small and makes a position lookup a lookup
/// rather than a table scan in the client.
#[tokio::test]
async fn markers_are_names_and_positions_are_a_separate_call() {
let Some(token) = auth_token().await else {
eprintln!("skipping markers_are_names_and_positions_are_a_separate_call: no token");
return;
};
if !genotype_dir_present() {
eprintln!("skipping markers_are_names_and_positions_are_a_separate_call: no genotype dir");
return;
}
let response = get_with_token(
"GET /groups/BXD/genotypes/BXD.4/markers",
"/groups/BXD/genotypes/BXD.4/markers?limit=3",
&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();
assert_eq!(
envelope["meta"]["columns"],
serde_json::json!(["name"]),
"the list is names only: {envelope}"
);
let rows = envelope["data"].as_array().unwrap();
assert_eq!(rows.len(), 3);
for row in rows {
let row = row.as_array().unwrap();
assert_eq!(row.len(), 1, "one column per marker: {row:?}");
assert!(row[0].is_string(), "{row:?}");
}
let first = rows[0][0].as_str().unwrap().to_string();
// Page two continues rather than repeating.
let response = get_with_token(
"GET /groups/BXD/genotypes/BXD.4/markers?page=2",
"/groups/BXD/genotypes/BXD.4/markers?limit=3&page=2",
&token,
)
.await;
assert_eq!(response.status(), StatusCode::OK);
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let page_two: Value = serde_json::from_slice(&bytes).unwrap();
assert_ne!(
page_two["data"][0][0].as_str().unwrap(),
first,
"page two starts after page one"
);
// The position, as named fields.
let response = get_with_token(
"GET /groups/BXD/genotypes/BXD.4/markers/{marker}/location",
&format!("/groups/BXD/genotypes/BXD.4/markers/{first}/location"),
&token,
)
.await;
assert_eq!(response.status(), StatusCode::OK);
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let location: Value = data(&bytes);
assert_eq!(location["name"], serde_json::json!(first));
for field in ["chr", "cM", "Mb"] {
assert!(
location.get(field).is_some(),
"the location names {field}: {location}"
);
}
assert!(location["chr"].is_string(), "{location}");
// An absent marker is a 404 that says which marker.
let response = get_with_token(
"GET /groups/BXD/genotypes/BXD.4/markers/nonesuch/location",
"/groups/BXD/genotypes/BXD.4/markers/nonesuch/location",
&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"], "marker_not_found");
}
/// The `unlisted` namespace answers the same two calls for a file no group
/// lists.
#[tokio::test]
async fn unlisted_markers_answer_like_a_listed_version() {
let Some(token) = auth_token().await else {
eprintln!("skipping unlisted_markers_answer_like_a_listed_version: no token");
return;
};
if !genotype_dir_present() {
eprintln!("skipping unlisted_markers_answer_like_a_listed_version: no genotype dir");
return;
}
let listed = get_with_token(
"GET /groups/BXD/genotypes/BXD.4/markers",
"/groups/BXD/genotypes/BXD.4/markers?limit=2",
&token,
)
.await;
assert_eq!(listed.status(), StatusCode::OK);
let bytes = listed.into_body().collect().await.unwrap().to_bytes();
let listed: Value = serde_json::from_slice(&bytes).unwrap();
let unlisted = get_with_token(
"GET /groups/unlisted/genotypes/{name}/markers",
"/groups/unlisted/genotypes/BXD.4/markers?limit=2",
&token,
)
.await;
assert_eq!(unlisted.status(), StatusCode::OK);
let bytes = unlisted.into_body().collect().await.unwrap().to_bytes();
let unlisted: Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(listed["data"], unlisted["data"], "the same two names");
let marker = listed["data"][0][0].as_str().unwrap().to_string();
let location = get_with_token(
"GET /groups/unlisted/genotypes/{name}/markers/{marker}/location",
&format!("/groups/unlisted/genotypes/BXD.4/markers/{marker}/location"),
&token,
)
.await;
assert_eq!(location.status(), StatusCode::OK);
let bytes = location.into_body().collect().await.unwrap().to_bytes();
let location: Value = data(&bytes);
assert_eq!(location["name"], serde_json::json!(marker));
// A panel too large to read is refused, not waited for.
let oversized = get_with_token(
"GET /groups/unlisted/genotypes/HSNIH-Palmer_true/markers",
"/groups/unlisted/genotypes/HSNIH-Palmer_true/markers",
&token,
)
.await;
match oversized.status() {
StatusCode::PAYLOAD_TOO_LARGE => {
let bytes = oversized.into_body().collect().await.unwrap().to_bytes();
let error: Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(error["code"], "genotype_too_large");
}
StatusCode::NOT_FOUND => eprintln!("skipping the size check: the panel is not deployed"),
other => panic!("unexpected status {other}"),
}
}
|