aboutsummaryrefslogtreecommitdiff
path: root/gn2/tests/unit/base/test_data_set.py
blob: d3d11022d6159ca67d2a4b677c2656efdac9f839 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""Tests for wqflask/base/data_set.py"""

import unittest
from unittest import mock
from dataclasses import dataclass
from gn3.monads import MonadicDict

from gn2.wqflask import app
from gn2.base.data_set import DatasetType
from gn2.base.data_set.dataset import DataSet

GEN_MENU_JSON = """
{
  "datasets": {
    "human": {
      "HLC": {
        "Liver mRNA": [
          [
            "320",
            "HLC_0311",
            "GSE9588 Human Liver Normal (Mar11) Both Sexes"
          ]
        ],
        "Phenotypes": [
          [
            "635",
            "HLCPublish",
            "HLC Published Phenotypes"
          ]
        ]
      }
    },
    "mouse": {
      "BXD": {
        "Genotypes": [
          [
            "600",
            "BXDGeno",
            "BXD Genotypes"
          ]
        ],
        "Hippocampus mRNA": [
          [
            "112",
            "HC_M2_0606_P",
            "Hippocampus Consortium M430v2 (Jun06) PDNN"
          ]
        ],
        "Phenotypes": [
          [
            "602",
            "BXDPublish",
            "BXD Published Phenotypes"
          ]
        ]
      }
    }
  },
  "groups": {
    "human": [
      [
        "HLC",
        "Liver: Normal Gene Expression with Genotypes (Merck)",
        "Family:None"
      ]
    ],
    "mouse": [
      [
        "BXD",
        "BXD",
        "Family:None"
      ]
    ]
  },
  "species": [
    [
      "human",
      "Human"
    ],
    [
      "mouse",
      "Mouse"
    ]
  ],
  "types": {
    "human": {
      "HLC": [
        [
          "Phenotypes",
          "Traits and Cofactors",
          "Phenotypes"
        ],
        [
          "Liver mRNA",
          "Liver mRNA",
          "Molecular Trait Datasets"
        ]
      ]
    },
    "mouse": {
      "BXD": [
        [
          "Phenotypes",
          "Traits and Cofactors",
          "Phenotypes"
        ],
        [
          "Genotypes",
          "DNA Markers and SNPs",
          "Genotypes"
        ],
        [
          "Hippocampus mRNA",
          "Hippocampus mRNA",
          "Molecular Trait Datasets"
        ]
      ]
    }
  }
}
"""

class MockPhenotypeDataset(DataSet):
    def setup(self):
        self.type = "Publish"
        self.query_for_group = ""
        self.group = ""


    def check_confidentiality(self):
        pass

    def retrieve_other_names(self):
        pass

@dataclass
class MockGroup:
    name = "Group"

class TestDataSetTypes(unittest.TestCase):
    """Tests for the DataSetType class"""

    def setUp(self):
        self.test_dataset = """
            {
                "AD-cases-controls-MyersGeno": "Geno",
                "AD-cases-controls-MyersPublish": "Publish",
                "AKXDGeno": "Geno",
                "AXBXAGeno": "Geno",
                "AXBXAPublish": "Publish",
                "Aging-Brain-UCIPublish": "Publish",
                "All Phenotypes": "Publish",
                "B139_K_1206_M": "ProbeSet",
                "B139_K_1206_R": "ProbeSet"
            }
            """
        self.app_context = app.app_context()
        self.app_context.push()

    def tearDown(self):
        self.app_context.pop()

    def test_data_set_type(self):
        """Test that DatasetType returns correctly if the Redis Instance is not empty
        and the name variable exists in the dictionary

        """
        with app.app_context():
            redis_mock = mock.Mock()
            cursor_mock = mock.Mock()
            redis_mock.get.return_value = self.test_dataset
            self.assertEqual(DatasetType(redis_mock)
                             ("All Phenotypes", redis_mock, cursor_mock), "Publish")
            redis_mock.get.assert_called_once_with("dataset_structure")

    @mock.patch('gn2.base.data_set.datasettype.requests.get')
    def test_data_set_type_with_empty_redis(self, request_mock):
        """Test that DatasetType returns correctly if the Redis Instance is empty and
        the name variable exists in the dictionary

        """
        with app.app_context():
            request_mock.return_value.content = GEN_MENU_JSON
            redis_mock = mock.Mock()
            cursor_mock = mock.Mock()
            redis_mock.get.return_value = None
            data_set = DatasetType(redis_mock)
            self.assertEqual(data_set("BXDGeno", redis_mock, cursor_mock),
                             "Geno")
            self.assertEqual(data_set("BXDPublish", redis_mock, cursor_mock),
                             "Publish")
            self.assertEqual(data_set("HLC_0311", redis_mock, cursor_mock),
                             "ProbeSet")

            redis_mock.set.assert_called_once_with(
                "dataset_structure",
                ('{"HLC_0311": "ProbeSet", '
                 '"HLCPublish": "Publish", '
                 '"BXDGeno": "Geno", '
                 '"HC_M2_0606_P": "ProbeSet", '
                 '"BXDPublish": "Publish"}'))


class TestDatasetAccessionId(unittest.TestCase):
    """Tests for the DataSetType class"""

    @mock.patch("gn2.base.data_set.dataset.database_connection")
    @mock.patch("gn2.base.data_set.dataset.DatasetGroup")
    def test_get_accession_id(self, mock_dataset_group, conn):
        def mock_fn():
            yield MonadicDict({"accession_id": 7})
        mock_dataset_group.return_value = MockGroup()
        conn = mock.MagicMock()
        conn.return_value.__enter__ = mock_fn
        sample_dataset = MockPhenotypeDataset(
            name="BXD-LongevityPublish",
            get_samplelist=False,
            group_name="BXD",
            redis_conn=mock.Mock()
        )
        sample_dataset\
            .accession_id\
            .bind(lambda x: self.assertEqual(7, x))

    @mock.patch("gn2.base.data_set.dataset.database_connection")
    @mock.patch("gn2.base.data_set.dataset.DatasetGroup")
    def test_get_accession_id_empty_return(self, mock_dataset_group,
                                           conn):
        mock_dataset_group.return_value = MockGroup()
        conn = mock.MagicMock()
        conn.return_value.__enter__ = None
        sample_dataset = MockPhenotypeDataset(
            name="BXD-LongevityPublish",
            get_samplelist=False,
            group_name="BXD",
            redis_conn=mock.Mock()
        )
        sample_dataset\
            .accession_id\
            .bind(lambda x: self.assertNone(x))