about summary refs log tree commit diff
path: root/tests/groups.rs
blob: 7035217ac0a4eb9682cbdc15f9b978122b662f39 (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
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); skips rather
/// than fails when none is reachable, same as species.rs. BXD is used
/// everywhere as a test fixture in the GeneNetwork2/GeneNetwork3 test
/// suites and is always present in the database.
#[tokio::test]
async fn groups_returns_ok_with_bxd() {
    if !common::db_available() {
        eprintln!(
            "skipping groups_returns_ok_with_bxd: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping groups_returns_ok_with_bxd: no token (see docs/gn-auth.md)");
        return;
    };

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

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

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let groups: Vec<Value> = common::data(&body);

    assert!(!groups.is_empty());
    let names: Vec<&str> = groups
        .iter()
        .map(|record| record["name"].as_str().unwrap())
        .collect();
    assert!(names.contains(&"BXD"));
}

/// Every group carries its family and species name, and /groups takes
/// `family`, `species` and `q` filters (case-insensitive, combined with
/// AND).
#[tokio::test]
async fn groups_filters_by_family_species_and_text() {
    if !common::db_available() {
        eprintln!(
            "skipping groups_filters_by_family_species_and_text: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping groups_filters_by_family_species_and_text: no token (see docs/gn-auth.md)");
        return;
    };

    let groups = |uri: &'static str| {
        let token = token.clone();
        async move {
            let response = common::get_with_token(&format!("GET {uri}"), uri, &token).await;
            assert_eq!(response.status(), StatusCode::OK, "GET {uri}");
            let body = response.into_body().collect().await.unwrap().to_bytes();
            let groups: Vec<Value> = common::data(&body);
            groups
        }
    };
    let names = |groups: &[Value]| -> Vec<String> {
        groups.iter().map(|g| g["name"].as_str().unwrap().to_string()).collect()
    };

    // Unfiltered: BXD is a mouse Reference Population.
    let all = groups("/groups").await;
    let bxd = all.iter().find(|g| g["name"] == "BXD").unwrap();
    assert_eq!(bxd["species"], "mouse");
    assert!(bxd["family"].as_str().unwrap().starts_with("Reference Populations"));

    // family: case-insensitive "contains".
    let reference = names(&groups("/groups?family=reference%20populations").await);
    for expected in ["AKXD", "AXBXA", "BXD", "BXH", "CC", "CXB", "HXBBXH", "LXS", "MDP"] {
        assert!(reference.contains(&expected.to_string()), "{expected} in {reference:?}");
    }
    assert!(!reference.contains(&"HSNIH-Palmer".to_string()));

    // species: the short name /species lists, case-insensitive.
    let rat = groups("/groups?species=RAT").await;
    assert!(!rat.is_empty() && rat.iter().all(|g| g["species"] == "rat"));

    // q: name, display name or full name.
    let alzheimer = names(&groups("/groups?q=alzheimer").await);
    assert!(alzheimer.contains(&"BXD-NIA-AD".to_string()));

    // Filters combine with AND.
    let rat_reference = names(&groups("/groups?species=rat&family=reference").await);
    assert!(rat_reference.contains(&"HXBBXH".to_string()));
    assert!(!rat_reference.contains(&"BXD".to_string()));
}

/// Each group reports the mapping tool offered for it (the MappingMethod
/// its mapping_method_id points to), or null when it has none.
#[tokio::test]
async fn groups_report_the_mapping_tool_offered_for_them() {
    if !common::db_available() {
        eprintln!(
            "skipping groups_report_the_mapping_tool_offered_for_them: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping groups_report_the_mapping_tool_offered_for_them: no token (see docs/gn-auth.md)");
        return;
    };

    let response = common::get_with_token("GET /groups", "/groups", &token).await;
    assert_eq!(response.status(), StatusCode::OK);
    let body = response.into_body().collect().await.unwrap().to_bytes();
    let groups: Vec<Value> = common::data(&body);
    let tool = |name: &str| -> Value {
        groups.iter().find(|g| g["name"] == name).unwrap()["mapping_method"].clone()
    };

    assert_eq!(tool("BXD"), "qtlreaper");
    assert_eq!(tool("HSNIH-Palmer"), "Rqtl");
    assert_eq!(tool("HSNIH-RGSMC"), "qtlreaper"); // not every outbred set maps with Rqtl
    assert!(tool("Oregon-R_x_2b3").is_null());
}