about summary refs log tree commit diff
path: root/tests/group_by_name.rs
blob: 54d6ecbb28759df89fcc9d5e9f12636c86b91628 (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
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 species_by_name.rs.
#[tokio::test]
async fn group_by_name_bxd_returns_group_row() {
    if !common::db_available() {
        eprintln!(
            "skipping group_by_name_bxd_returns_group_row: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping group_by_name_bxd_returns_group_row: no token (see docs/gn-auth.md)");
        return;
    };

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

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

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

    assert_eq!(group["name"], "BXD");
    assert_eq!(group["display_name"], "BXD");
    assert_eq!(group["genetic_type"], "riset");
}

/// BXD's InbredSet.Id is 1, same as its InbredSetId; replicates
/// gn-guile's gn.data.population:population-info, which looks a group
/// up by numeric Id rather than by name.
#[tokio::test]
async fn group_by_name_numeric_id_returns_same_group_row() {
    if !common::db_available() {
        eprintln!(
            "skipping group_by_name_numeric_id_returns_same_group_row: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!(
            "skipping group_by_name_numeric_id_returns_same_group_row: no token (see docs/gn-auth.md)"
        );
        return;
    };

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

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

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

    assert_eq!(group["name"], "BXD");
    assert_eq!(group["id"], 1);
}

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

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

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
    let request_id = response
        .headers()
        .get("x-request-id")
        .unwrap()
        .to_str()
        .unwrap()
        .to_string();

    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_by_name");
    assert_eq!(error["spec_url"], "/openapi.json");
    assert_eq!(error["next"], "/groups");
    assert_eq!(error["request_id"], request_id);
}

/// A group's record names its mapping tool next to the raw id.
#[tokio::test]
async fn group_by_name_names_the_mapping_tool() {
    if !common::db_available() {
        eprintln!(
            "skipping group_by_name_names_the_mapping_tool: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping group_by_name_names_the_mapping_tool: no token (see docs/gn-auth.md)");
        return;
    };

    let response =
        common::get_with_token("GET /groups/HSNIH-Palmer", "/groups/HSNIH-Palmer", &token).await;
    assert_eq!(response.status(), StatusCode::OK);
    let body = response.into_body().collect().await.unwrap().to_bytes();
    let group: Value = common::data(&body);

    assert_eq!(group["mapping_method_id"], "2");
    assert_eq!(group["mapping_method"], "Rqtl");
}