aboutsummaryrefslogtreecommitdiff
path: root/tests/integration/test_gemma.py
blob: 1f59530e614f3b10f7ca37fc5b1edb4390cf313e (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
"""Integration tests for gemma API endpoints"""
import os
import unittest

from dataclasses import dataclass
from typing import Callable
from unittest import mock
from gn3.app import create_app


@dataclass
class MockRedis:
    """Mock the Redis Module"""
    redis: Callable
    hget: Callable


class GemmaAPITest(unittest.TestCase):
    """Test cases for the Gemma API"""
    def setUp(self):
        self.app = create_app({
            "APP_DEFAULTS": {
                "GENODIR": os.path.abspath(
                    os.path.join(os.path.dirname(__file__),
                                 "test_data/")),
                "TMPDIR": "/tmp",
                "REDIS_JOB_QUEUE": "GN3::job-queue",
                "GEMMA_WRAPPER_CMD": "gemma-wrapper"}}).test_client()

    @mock.patch("gn3.api.gemma.run_cmd")
    def test_get_version(self, mock_run_cmd):
        """Test that the correct response is returned"""
        mock_run_cmd.return_value = {"status": 0, "output": "v1.9"}
        response = self.app.get("/gemma/version", follow_redirects=True)
        self.assertEqual(response.get_json(),
                         {"status": 0, "output": "v1.9"})
        self.assertEqual(response.status_code, 200)

    @mock.patch("gn3.api.gemma.redis.Redis")
    @mock.patch("gn3.api.gemma.queue_cmd")
    @mock.patch("gn3.api.gemma.generate_gemma_computation_cmd")
    def test_run_gemma_no_loco(self, mock_gemma_computation_cmd,
                               mock_queue_cmd, mock_redis):
        """Test that gemma composes the command correctly without loco"""
        _redis_conn = MockRedis(redis=mock.MagicMock(), hget=mock.MagicMock())
        mock_redis.return_value = _redis_conn
        mock_gemma_computation_cmd.side_effect = [
            ("gemma-wrapper --json -- "
             "-g genofile.txt -p "
             "test.txt -a genofile_snps.txt "
             "-gk > /tmp/gn2/"
             "bxd_K_gUFhGu4rLG7k+CXLPk1OUg.txt"),
            ("gemma-wrapper --json --input /tmp/gn2/"
             "bxd_K_gUFhGu4rLG7k+CXLPk1OUg.txt -- "
             "-a genofile_snps.txt -lmm 9 "
             "-g genofile.txt -p "
             "test.txt -a genofile_snps.txt "
             "-gk > /tmp/gn2/"
             "bxd_GWA_gUFhGu4rLG7k+CXLPk1OUg.txt")
        ]
        mock_queue_cmd.return_value = "my-unique-id"
        response = self.app.post("/gemma/k-gwa-computation", json={
            "trait_filename": "BXD.txt",
            "geno_filename": "BXD_geno",
            "values": ["X", "N/A", "X"],
            "dataset_groupname": "BXD",
            "trait_name": "Height",
            "email": "me@me.com",
            "dataset_name": "BXD"
        })
        mock_queue_cmd.assert_has_calls(
            [mock.call(conn=_redis_conn,
                       email="me@me.com",
                       job_queue="GN3::job-queue",
                       cmd=("gemma-wrapper --json -- -g "
                            "genofile.txt -p test.txt "
                            "-a genofile_snps.txt -gk > "
                            "/tmp/gn2/bxd_K_gUFhGu4rLG7k+CXLPk1OUg.txt "
                            "&& gemma-wrapper --json --input "
                            "/tmp/gn2/bxd_K_gUFhGu4rLG7k+CXLPk1OUg.txt"
                            " -- -a genofile_snps.txt -lmm 9 -g "
                            "genofile.txt -p test.txt "
                            "-a genofile_snps.txt "
                            "-gk > "
                            "/tmp/gn2/"
                            "bxd_GWA_gUFhGu4rLG7k+CXLPk1OUg.txt"))]
        )
        # mock_pheno_txt_file.return_value = "/tmp/gn2/BXD_6OBEPW."
        self.assertEqual(
            response.get_json(),
            {"unique_id": 'my-unique-id',
             "status": "queued",
             "output_file": "BXD_GWA_WzxVcfhKAn4fJnSWpsBq0g.txt"})

    @mock.patch("gn3.api.gemma.redis.Redis")
    @mock.patch("gn3.api.gemma.queue_cmd")
    def test_run_gemma_with_loco(self,
                                 mock_queue_cmd, mock_redis):
        """Test that gemma composes the command correctly with loco"""
        _redis_conn = MockRedis(redis=mock.MagicMock(), hget=mock.MagicMock())
        mock_redis.return_value = _redis_conn
        mock_queue_cmd.return_value = "my-unique-id"
        response = self.app.post("/gemma/k-gwa-computation", json={
            "trait_filename": os.path.abspath(os.path.join(
                os.path.dirname(__file__),
                "test_data/"
            )),
            "geno_filename": "BXD_geno.txt.gz",
            "values": ["X", "N/A", "X"],
            "dataset_groupname": "BXD",
            "trait_name": "Height",
            "email": "me@me.com",
            "dataset_name": "BXD",
            "loco": "1,2,3,4,5,6"
        })
        mock_queue_cmd.called_with(
            conn=_redis_conn,
            email="me@me.com",
            job_queue="GN3::job-queue",
            cmd=("gemma-wrapper --json -- -g "
                 "genofile.txt -p test.txt "
                 "-a genofile_snps.txt -gk > "
                 "/tmp/gn2/bxd_K_gUFhGu4rLG7k+CXLPk1OUg.txt "
                 "&& gemma-wrapper --json --input "
                 "/tmp/gn2/bxd_K_gUFhGu4rLG7k+CXLPk1OUg.txt"
                 " -- -a genofile_snps.txt -lmm 9 -g "
                 "genofile.txt -p test.txt "
                 "-a genofile_snps.txt "
                 "-gk > "
                 "/tmp/gn2/"
                 "bxd_GWA_gUFhGu4rLG7k+CXLPk1OUg.txt"))
        self.assertEqual(
            response.get_json(),
            {"unique_id": 'my-unique-id',
             "status": "queued",
             "output_file": "BXD_GWA_WzxVcfhKAn4fJnSWpsBq0g.txt"})

    @mock.patch("gn3.api.gemma.redis.Redis")
    def test_check_cmd_status(self, mock_redis):
        """Test that you can check the status of a given command"""
        mock_hget = mock.MagicMock()
        mock_hget.return_value = b"test"
        _redis_conn = MockRedis(redis=mock.MagicMock(), hget=mock_hget)
        mock_redis.return_value = _redis_conn
        response = self.app.get(("/gemma/status/"
                                 "cmd%3A%3A2021-02-1217-3224-3224-1234"),
                                follow_redirects=True)
        mock_hget.assert_called_once_with(
            name="cmd::2021-02-1217-3224-3224-1234",
            key="status")
        self.assertEqual(response.get_json(),
                         {"status": "test"})