about summary refs log tree commit diff
path: root/tests/group_trait_info.rs
blob: a40dfd6f8715a5743a32118319f6c78ef09b415c (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
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) and a valid
/// token (every endpoint needs one now -- see docs/gn-auth.md); skips
/// rather than fails when either is missing, same as group_by_name.rs.
/// PublishXRef.Id 10001 in group BXD is the doc example from
/// precompute-publishdata.gmi (cerebellum weight, Airey/Lu/Williams 2001).
#[tokio::test]
async fn group_trait_info_bxd_returns_phenotype_and_publication() {
    if !common::db_available() {
        eprintln!(
            "skipping group_trait_info_bxd_returns_phenotype_and_publication: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!(
            "skipping group_trait_info_bxd_returns_phenotype_and_publication: no token (see docs/gn-auth.md)"
        );
        return;
    };

    let response = common::get_with_token(
        "GET /groups/BXD/traits/10001",
        "/groups/BXD/traits/10001",
        &token,
    )
    .await;

    assert_eq!(response.status(), StatusCode::OK);

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let info: Value = common::data(&body);

    assert_eq!(info["id"], 10001);
    assert_eq!(info["units"], "mg");
    assert!(
        info["post_publication_description"]
            .as_str()
            .unwrap()
            .contains("Cerebellum weight")
    );
    assert_eq!(info["pubmed_id"], 11438585);
    assert_eq!(info["authors"], "Airey DC, Lu L, Williams RW");
    assert_eq!(info["year"], "2001");
}

#[tokio::test]
async fn group_trait_info_unknown_group_returns_404() {
    if !common::db_available() {
        eprintln!(
            "skipping group_trait_info_unknown_group_returns_404: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!(
            "skipping group_trait_info_unknown_group_returns_404: no token (see docs/gn-auth.md)"
        );
        return;
    };

    let response = common::get_with_token(
        "GET /groups/not-a-real-group/traits/10001",
        "/groups/not-a-real-group/traits/10001",
        &token,
    )
    .await;

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
    let body = response.into_body().collect().await.unwrap().to_bytes();
    let error: Value = serde_json::from_slice(&body).unwrap();

    assert_eq!(error["status"], 404);
    assert_eq!(error["code"], "group_not_found");
    assert_eq!(error["operation_id"], "group_trait_info");
    assert_eq!(error["next"], "/groups");
}

#[tokio::test]
async fn group_trait_info_unknown_trait_in_group_returns_404() {
    if !common::db_available() {
        eprintln!(
            "skipping group_trait_info_unknown_trait_in_group_returns_404: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!(
            "skipping group_trait_info_unknown_trait_in_group_returns_404: no token (see docs/gn-auth.md)"
        );
        return;
    };

    let response = common::get_with_token(
        "GET /groups/BXD/traits/999999999",
        "/groups/BXD/traits/999999999",
        &token,
    )
    .await;

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
    let body = response.into_body().collect().await.unwrap().to_bytes();
    let error: Value = serde_json::from_slice(&body).unwrap();

    assert_eq!(error["status"], 404);
    assert_eq!(error["code"], "trait_not_found");
    assert_eq!(error["operation_id"], "group_trait_info");
}

/// The "highest LOD ... serotonin in BXD male midbrain" trait (see
/// tests/search.rs scenario 7): midbrain 5-HT level in adult males.
/// Pins the marker name and that `lod` is `lrs` / 4.604.
#[tokio::test]
async fn group_trait_info_reports_lod_and_locus() {
    if !common::db_available() {
        eprintln!(
            "skipping group_trait_info_reports_lod_and_locus: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!(
            "skipping group_trait_info_reports_lod_and_locus: no token (see docs/gn-auth.md)"
        );
        return;
    };

    let response = common::get_with_token(
        "GET /groups/BXD/traits/17985",
        "/groups/BXD/traits/17985",
        &token,
    )
    .await;

    assert_eq!(response.status(), StatusCode::OK);

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let info: Value = common::data(&body);

    assert_eq!(info["locus"], "rsm10000012461");
    assert_eq!(info["abbreviation"], "5HT_MB_M");
    assert_eq!(info["dataset"], "BXDPublish");
    assert_eq!(
        info["url"],
        "https://genenetwork.org/show_trait?trait_id=17985&dataset=BXDPublish"
    );
    let (lrs, lod) = (info["lrs"].as_f64().unwrap(), info["lod"].as_f64().unwrap());
    assert!((lod - lrs / 4.604).abs() < 1e-9);
    assert!((lod - 2.6063).abs() < 0.0001);
    assert!((info["additive"].as_f64().unwrap() - -1.3041).abs() < 0.0001);
}