about summary refs log tree commit diff
path: root/tests/group_case_attributes.rs
blob: 41e83e0e0dfbf6f63d9e0a9c88c335fc078803ec (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
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.
/// BXD (InbredSetId 1) has exactly 4 case attributes -- Epoch, RRID,
/// SeqCvge, Status -- each strain in the response should have exactly
/// those 4 keys, not more: db::get_group_case_attribute_values joins
/// CaseAttribute to CaseAttributeXRefNew on CaseAttributeId alone would
/// be wrong here, since CaseAttributeId isn't unique across groups (see
/// the comment in src/db.rs) -- getting that join wrong would pull in
/// unrelated groups' attributes too.
#[tokio::test]
async fn group_case_attributes_bxd_returns_names_and_values() {
    if !common::db_available() {
        eprintln!(
            "skipping group_case_attributes_bxd_returns_names_and_values: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!(
            "skipping group_case_attributes_bxd_returns_names_and_values: no token (see docs/gn-auth.md)"
        );
        return;
    };

    let response = common::get_with_token(
        "GET /groups/BXD/case-attributes",
        "/groups/BXD/case-attributes",
        &token,
    )
    .await;

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

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

    let names: Vec<&str> = case_attributes["names"]
        .as_array()
        .unwrap()
        .iter()
        .map(|n| n.as_str().unwrap())
        .collect();
    assert_eq!(names, vec!["Epoch", "RRID", "SeqCvge", "Status"]);

    // Every name says what it means, which is the only way to know that
    // SeqCvge is a sequencing depth rather than a phenotype.
    let descriptions = case_attributes["descriptions"].as_object().unwrap();
    assert_eq!(descriptions.len(), names.len());
    let seq_cvge = descriptions["SeqCvge"].as_str().unwrap();
    assert!(
        seq_cvge.contains("Depth of DNA sequencing"),
        "SeqCvge is explained: {seq_cvge}"
    );
    assert!(
        descriptions["Status"]
            .as_str()
            .unwrap()
            .contains("Cryopreserved"),
        "Status explains its own levels"
    );

    let values = case_attributes["values"].as_array().unwrap();
    assert!(!values.is_empty());
    let bxd1 = values
        .iter()
        .find(|v| v["strain"] == "BXD1")
        .expect("BXD1 in /groups/BXD/case-attributes");
    let attrs = bxd1["case_attributes"].as_object().unwrap();
    assert_eq!(
        attrs.len(),
        4,
        "BXD1 should have exactly BXD's 4 case attributes, not more"
    );
    for name in &names {
        assert!(attrs.contains_key(*name));
    }
}

/// LXS (InbredSetId 9) is a real group with no case attributes defined
/// at all -- an empty result, not a 404 (the group itself exists).
#[tokio::test]
async fn group_case_attributes_group_with_none_returns_empty() {
    if !common::db_available() {
        eprintln!(
            "skipping group_case_attributes_group_with_none_returns_empty: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!(
            "skipping group_case_attributes_group_with_none_returns_empty: no token (see docs/gn-auth.md)"
        );
        return;
    };

    let response = common::get_with_token(
        "GET /groups/LXS/case-attributes",
        "/groups/LXS/case-attributes",
        &token,
    )
    .await;

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

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let case_attributes: Value = common::data(&body);
    assert_eq!(case_attributes["names"].as_array().unwrap().len(), 0);
    assert_eq!(case_attributes["values"].as_array().unwrap().len(), 0);
}

#[tokio::test]
async fn group_case_attributes_without_a_token_returns_401() {
    if !common::db_available() {
        eprintln!(
            "skipping group_case_attributes_without_a_token_returns_401: no usable database (check SQL_URI credentials)"
        );
        return;
    }

    let response = common::get(
        "GET /groups/BXD/case-attributes (no token)",
        "/groups/BXD/case-attributes",
    )
    .await;

    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
}

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

    let response = common::get_with_token(
        "GET /groups/not-a-real-group/case-attributes",
        "/groups/not-a-real-group/case-attributes",
        &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_case_attributes");
    assert_eq!(error["next"], "/groups");
}