aboutsummaryrefslogtreecommitdiff
path: root/tests/integration/test_partial_correlations.py
blob: 4bf352a40741cd890e12f9ef6aecf40ebeb81ff4 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""Test partial correlations"""
from unittest import mock

import pytest

from gn3.computations.partial_correlations import partial_correlations_entry

@pytest.mark.integration_test
@pytest.mark.parametrize(
    "post_data", (
        None, {}, {
            "primary_trait": None,
            "control_traits": None,
            "method": None,
            "target_db": None
        }, {
            "primary_trait": None,
            "control_traits": None,
            "method": None,
            "target_db": "a_db"
        }, {
            "primary_trait": None,
            "control_traits": None,
            "method": "a_method",
            "target_db": None
        }, {
            "primary_trait": None,
            "control_traits": None,
            "method": "a_method",
            "target_db": "a_db"
        }, {
            "primary_trait": None,
            "control_traits": ["a_trait", "another"],
            "method": None,
            "target_db": None
        }, {
            "primary_trait": None,
            "control_traits": ["a_trait", "another"],
            "method": None,
            "target_db": "a_db"
        }, {
            "primary_trait": None,
            "control_traits": ["a_trait", "another"],
            "method": "a_method",
            "target_db": None
        }, {
            "primary_trait": None,
            "control_traits": ["a_trait", "another"],
            "method": "a_method",
            "target_db": "a_db"
        }, {
            "primary_trait": "a_trait",
            "control_traits": None,
            "method": None,
            "target_db": None
        }, {
            "primary_trait": "a_trait",
            "control_traits": None,
            "method": None,
            "target_db": "a_db"
        }, {
            "primary_trait": "a_trait",
            "control_traits": None,
            "method": "a_method",
            "target_db": None
        }, {
            "primary_trait": "a_trait",
            "control_traits": None,
            "method": "a_method",
            "target_db": "a_db"
        }, {
            "primary_trait": "a_trait",
            "control_traits": ["a_trait", "another"],
            "method": None,
            "target_db": None
        }, {
            "primary_trait": "a_trait",
            "control_traits": ["a_trait", "another"],
            "method": None,
            "target_db": "a_db"
        }, {
            "primary_trait": "a_trait",
            "control_traits": ["a_trait", "another"],
            "method": "a_method",
            "target_db": None
        }))
def test_partial_correlation_api_with_missing_request_data(client, post_data):
    """
    Test /api/correlations/partial endpoint with various expected request data
    missing.
    """
    response = client.post("/api/correlation/partial", json=post_data)
    assert (
        response.status_code == 400 and response.is_json and
        response.json.get("status") == "error")

@pytest.mark.integration_test
@pytest.mark.slow
@pytest.mark.parametrize(
    "post_data",
    ({# ProbeSet
        "primary_trait": {"dataset": "a_dataset", "trait_name": "a_name"},
        "control_traits": [
            {"dataset": "a_dataset", "trait_name": "a_name"},
            {"dataset": "a_dataset2", "trait_name": "a_name2"}],
        "method": "a_method",
        "target_db": "a_db"
    }, {# Publish
        "primary_trait": {
            "dataset": "a_Publish_dataset", "trait_name": "a_name"},
        "control_traits": [
            {"dataset": "a_dataset", "trait_name": "a_name"},
            {"dataset": "a_dataset2", "trait_name": "a_name2"}],
        "method": "a_method",
        "target_db": "a_db"
    }, {# Geno
        "primary_trait": {"dataset": "a_Geno_dataset", "trait_name": "a_name"},
        "control_traits": [
            {"dataset": "a_dataset", "trait_name": "a_name"},
            {"dataset": "a_dataset2", "trait_name": "a_name2"}],
        "method": "a_method",
        "target_db": "a_db"
    }, {# Temp
        "primary_trait": {"dataset": "a_Temp_dataset", "trait_name": "a_name"},
        "control_traits": [
            {"dataset": "a_dataset", "trait_name": "a_name"},
            {"dataset": "a_dataset2", "trait_name": "a_name2"}],
        "method": "a_method",
        "target_db": "a_db"
    }))
def test_partial_correlation_api_with_non_existent_primary_traits(
        client, post_data, mocker):
    """
    Check that the system responds appropriately in the case where the user
    makes a request with a non-existent primary trait.
    """
    mocker.patch("gn3.api.correlation.redis.Redis", mock.MagicMock())
    response = client.post("/api/correlation/partial", json=post_data)
    assert (
        response.status_code == 200 and response.is_json and
        response.json.get("status") == "success")

@pytest.mark.integration_test
@pytest.mark.slow
@pytest.mark.parametrize(
    "post_data",
    ({# ProbeSet
        "primary_trait": {
            "dataset": "UCLA_BXDBXH_CARTILAGE_V2",
            "trait_name": "ILM103710672"},
        "control_traits": [
            {"dataset": "a_dataset", "trait_name": "a_name"},
            {"dataset": "a_dataset2", "trait_name": "a_name2"}],
        "method": "a_method",
        "target_db": "a_db"
    }, {# Publish
        "primary_trait": {"dataset": "BXDPublish", "trait_name": "BXD_12557"},
        "control_traits": [
            {"dataset": "a_dataset", "trait_name": "a_name"},
            {"dataset": "a_dataset2", "trait_name": "a_name2"}],
        "method": "a_method",
        "target_db": "a_db"
    }, {# Geno
        "primary_trait": {"dataset": "AKXDGeno", "trait_name": "D4Mit16"},
        "control_traits": [
            {"dataset": "a_dataset", "trait_name": "a_name"},
            {"dataset": "a_dataset2", "trait_name": "a_name2"}],
        "method": "a_method",
        "target_db": "a_db"
    }
     # Temp -- the data in the database for these is ephemeral, making it
     #         difficult to test for this
     ))
def test_partial_correlation_api_with_non_existent_control_traits(client, post_data, mocker):
    """
    Check that the system responds appropriately in the case where the user
    makes a request with a non-existent control traits.

    The code repetition here is on purpose - valuing clarity over succinctness.
    """
    mocker.patch("gn3.api.correlation.redis.Redis", mock.MagicMock())
    response = client.post("/api/correlation/partial", json=post_data)
    assert (
        response.status_code == 200 and response.is_json and
        response.json.get("status") == "success")

@pytest.mark.integration_test
@pytest.mark.slow
@pytest.mark.parametrize(
    "primary,controls,method,target", (
        (# Probeset
            "UCLA_BXDBXH_CARTILAGE_V2::ILM103710672", (
                "UCLA_BXDBXH_CARTILAGE_V2::nonExisting01",
                "UCLA_BXDBXH_CARTILAGE_V2::nonExisting02",
                "UCLA_BXDBXH_CARTILAGE_V2::ILM380019"),
            "Genetic Correlation, Pearson's r", "BXDPublish"),
        (# Publish
            "BXDPublish::17937", (
                "BXDPublish::17940",
                "BXDPublish::nonExisting03"),
            "Genetic Correlation, Spearman's rho", "BXDPublish"),
        (# Geno
            "AKXDGeno::D4Mit16", (
                "AKXDGeno::D1Mit170",
                "AKXDGeno::nonExisting04",
                "AKXDGeno::D1Mit135",
                "AKXDGeno::nonExisting05",
                "AKXDGeno::nonExisting06"),
            "SGO Literature Correlation", "BXDPublish")
    )
    # Temp -- the data in the database for these is ephemeral, making it
    #         difficult to test for these without a temp database with the temp
    #         traits data set to something we are in control of
     )

def test_part_corr_api_with_mix_of_existing_and_non_existing_control_traits(
        db_conn, primary, controls, method, target):
    """
    Check that calling the function with a mix of existing and missing control
    traits raises an warning.
    """
    criteria = 10
    with pytest.warns(UserWarning):
        partial_correlations_entry(
            db_conn, primary, controls, method, criteria, target)