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
|
//! `GET /groups/{group}/traits/{trait_id}/case-attributes/{attribute}`:
//! one case attribute's values for one trait, in that trait's sample order.
//!
//! Needs the database and a token; skips without either, like the other
//! database-backed tests. Uses BXD-Longevity because it is a real study
//! with a `Sex` attribute on every one of its samples (BXD itself has no
//! `Sex` attribute at all, which the 404 case checks).
use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;
mod common;
use common::{auth_token, db_available, get_with_token};
async fn get_json(uri: &str, token: &str) -> (StatusCode, Value) {
let response = get_with_token(&format!("GET {uri}"), uri, token).await;
let status = response.status();
let bytes = response.into_body().collect().await.unwrap().to_bytes();
(status, serde_json::from_slice(&bytes).unwrap_or(Value::Null))
}
#[tokio::test]
async fn a_traits_case_attribute_lines_up_with_its_values() {
if !db_available() {
eprintln!(
"skipping a_traits_case_attribute_lines_up_with_its_values: no usable database \
(check SQL_URI credentials)"
);
return;
}
let Some(token) = auth_token().await else {
eprintln!("skipping a_traits_case_attribute_lines_up_with_its_values: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
// The trait's values first, so the comparison is against an independent
// response rather than against what this endpoint says about itself.
let (status, values) = get_json(
"/groups/BXD-Longevity/traits/10001/values",
&token,
)
.await;
assert_eq!(status, StatusCode::OK, "{values}");
let body_bytes = serde_json::to_vec(&values).unwrap();
let trait_values: Vec<Value> = common::data(&body_bytes);
let names: Vec<&str> = trait_values
.iter()
.map(|row| row["name"].as_str().unwrap())
.collect();
let (status, body) = get_json(
"/groups/BXD-Longevity/traits/10001/case-attributes/Sex",
&token,
)
.await;
assert_eq!(status, StatusCode::OK, "{body}");
let data: Value = common::data(&serde_json::to_vec(&body).unwrap());
assert_eq!(data["group"], "BXD-Longevity");
assert_eq!(data["trait_id"], 10001);
assert_eq!(data["attribute"], "Sex");
// And what `Sex` means comes with it: "F= female, M= male".
let description = data["description"].as_str().expect("description");
assert!(description.contains("female"), "{description}");
let rows = data["values"].as_array().expect("values");
assert_eq!(
rows.len(),
names.len(),
"one entry per sample of the trait: {} vs {}",
rows.len(),
names.len()
);
for (row, name) in rows.iter().zip(&names) {
assert_eq!(row["name"].as_str().unwrap(), *name, "same order as /values");
}
// Every sample of this trait happens to have a sex; and `distinct`
// accounts for exactly the values that were returned.
let with_values = rows.iter().filter(|row| !row["value"].is_null()).count();
assert_eq!(data["num_values"].as_u64().unwrap() as usize, with_values);
assert!(with_values > 0, "{body}");
let distinct = data["distinct"].as_array().expect("distinct");
let counted: usize = distinct
.iter()
.map(|entry| entry["count"].as_u64().unwrap() as usize)
.sum();
assert_eq!(counted, with_values, "{body}");
// Commonest first.
let counts: Vec<u64> = distinct
.iter()
.map(|entry| entry["count"].as_u64().unwrap())
.collect();
assert!(counts.windows(2).all(|pair| pair[0] >= pair[1]), "{counts:?}");
for entry in distinct {
assert!(!entry["value"].as_str().unwrap().is_empty());
}
}
#[tokio::test]
async fn an_attribute_a_group_does_not_define_is_a_404_pointing_at_its_list() {
if !db_available() {
eprintln!("skipping an_attribute_a_group_does_not_define_is_a_404_pointing_at_its_list: no usable database (check SQL_URI credentials)");
return;
}
let Some(token) = auth_token().await else {
eprintln!("skipping an_attribute_a_group_does_not_define_is_a_404_pointing_at_its_list: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
// BXD has no Sex attribute; BXD-Longevity has no "Nonsense" one.
for uri in [
"/groups/BXD/traits/10001/case-attributes/Sex",
"/groups/BXD-Longevity/traits/10001/case-attributes/Nonsense",
] {
let (status, body) = get_json(uri, &token).await;
assert_eq!(status, StatusCode::NOT_FOUND, "{uri}: {body}");
assert_eq!(body["code"], "not_found");
assert!(
body["next"]
.as_str()
.unwrap()
.ends_with("/case-attributes"),
"the 404 says where the attribute names are: {body}"
);
}
}
|