about summary refs log tree commit diff
path: root/test/python/test_groups.py
blob: 2cd09de825538516ea6bfb7beb8ff84e23972358 (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
"""Mirrors tests/groups.rs. Needs a live MariaDB (SQL_URI) and a valid
token (every endpoint needs one now -- see docs/gn-auth.md); skips with
a message rather than failing when either is missing, same as
test_species.py.
"""

import http.client
import json
import unittest

from auth_helpers import TOKEN_ENV, auth_token
from server_test_case import database_reachable, HOST, PORT, ServerTestCase, envelope_data



class GroupsTest(ServerTestCase):
    EXTRA_ENV = TOKEN_ENV

    def test_groups_returns_ok_with_bxd(self):
        if not database_reachable():
            self.skipTest("no database on 127.0.0.1:3306")
        token = auth_token()
        if not token:
            self.skipTest("no token (see docs/gn-auth.md)")

        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
        try:
            conn.request("GET", "/groups", headers={"Authorization": f"Bearer {token}"})
            response = conn.getresponse()
            body = response.read()
        finally:
            conn.close()
        self.assertEqual(response.status, 200)

        groups = envelope_data(body)
        self.assertTrue(groups)
        names = {record["name"] for record in groups}
        self.assertIn("BXD", names)

    def test_groups_filters_by_family_species_and_text(self):
        # Every group has `family` and `species`; /groups takes `family`,
        # `species` and `q` filters (case-insensitive, combined with AND).
        if not database_reachable():
            self.skipTest("no database on 127.0.0.1:3306")
        token = auth_token()
        if not token:
            self.skipTest("no token (see docs/gn-auth.md)")

        def groups(path):
            conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
            try:
                conn.request("GET", path, headers={"Authorization": f"Bearer {token}"})
                response = conn.getresponse()
                body = response.read()
                self.assertEqual(response.status, 200, path)
                return envelope_data(body)
            finally:
                conn.close()

        bxd = next(g for g in groups("/groups") if g["name"] == "BXD")
        self.assertEqual(bxd["species"], "mouse")
        self.assertTrue(bxd["family"].startswith("Reference Populations"))

        reference = {g["name"] for g in groups("/groups?family=reference%20populations")}
        for expected in ("AKXD", "AXBXA", "BXD", "BXH", "CC", "CXB", "HXBBXH", "LXS", "MDP"):
            self.assertIn(expected, reference)
        self.assertNotIn("HSNIH-Palmer", reference)

        rat = groups("/groups?species=RAT")
        self.assertTrue(rat)
        self.assertTrue(all(g["species"] == "rat" for g in rat))

        self.assertIn("BXD-NIA-AD", {g["name"] for g in groups("/groups?q=alzheimer")})

        rat_reference = {g["name"] for g in groups("/groups?species=rat&family=reference")}
        self.assertIn("HXBBXH", rat_reference)
        self.assertNotIn("BXD", rat_reference)

    def test_groups_report_the_mapping_tool_offered_for_them(self):
        if not database_reachable():
            self.skipTest("no database on 127.0.0.1:3306")
        token = auth_token()
        if not token:
            self.skipTest("no token (see docs/gn-auth.md)")

        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10)
        try:
            conn.request("GET", "/groups", headers={"Authorization": f"Bearer {token}"})
            response = conn.getresponse()
            body = response.read()
        finally:
            conn.close()
        self.assertEqual(response.status, 200)
        tool = {g["name"]: g["mapping_method"] for g in envelope_data(body)}

        self.assertEqual(tool["BXD"], "qtlreaper")
        self.assertEqual(tool["HSNIH-Palmer"], "Rqtl")
        self.assertEqual(tool["HSNIH-RGSMC"], "qtlreaper")  # not every outbred set uses Rqtl
        self.assertIsNone(tool["Oregon-R_x_2b3"])


if __name__ == "__main__":
    unittest.main()