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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
"""Test cases for wqflask.api.gen_menu"""
import unittest
from unittest import mock
from gn2.wqflask.api.gen_menu import gen_dropdown_json
from gn2.wqflask.api.gen_menu import get_groups
from gn2.wqflask.api.gen_menu import get_types
from gn2.wqflask.api.gen_menu import get_datasets
from gn2.wqflask.api.gen_menu import phenotypes_exist
from gn2.wqflask.api.gen_menu import genotypes_exist
from gn2.wqflask.api.gen_menu import build_datasets
from gn2.wqflask.api.gen_menu import build_types
class TestGenMenu(unittest.TestCase):
"""Tests for the gen_menu module"""
def setUp(self):
self.test_group = {
'mouse': [
['H_T1',
'H_T',
'Family:DescriptionA'
],
['H_T2', "H_T'", 'Family:None']
],
'human': [
['BXD', 'BXD', 'Family:None'],
['HLC', 'Liver: Normal Gene Expression with Genotypes (Merck)',
'Family:Test']
]
}
self.test_type = {
'mouse': {
'H_T2': [('Phenotypes',
'Traits and Cofactors',
'Phenotypes'),
('Genotypes',
'DNA Markers and SNPs',
'Genotypes'),
['M', 'M', 'Molecular Trait Datasets']],
'H_T1': [('Phenotypes',
'Traits and Cofactors',
'Phenotypes'),
('Genotypes',
'DNA Markers and SNPs',
'Genotypes'),
['M', 'M', 'Molecular Trait Datasets']]
},
'human': {
'HLC': [('Phenotypes',
'Traits and Cofactors',
'Phenotypes'),
('Genotypes',
'DNA Markers and SNPs',
'Genotypes'),
['M', 'M', 'Molecular Trait Datasets']],
'BXD': [('Phenotypes',
'Traits and Cofactors',
'Phenotypes'),
('Genotypes',
'DNA Markers and SNPs',
'Genotypes'),
['M', 'M', 'Molecular Trait Datasets']]
}
}
def test_get_groups(self):
"""Test that species groups are grouped correctly"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
cursor.fetchall.side_effect = [
# Mouse
(('BXD', 'BXD', None),
('HLC', ('Liver: Normal Gene Expression '
'with Genotypes (Merck)'),
'Test')),
# Human
(('H_T1', "H_T", "DescriptionA"),
('H_T2', "H_T'", None))
]
self.assertEqual(get_groups([["human", "Human"],
["mouse", "Mouse"]],
db_mock),
self.test_group)
for name in ["mouse", "human"]:
cursor.execute.assert_any_call(
("SELECT InbredSet.Name, InbredSet.FullName, "
"IFNULL(InbredSet.Family, 'None') "
"FROM InbredSet, Species WHERE Species.Name "
"= '{}' AND InbredSet.SpeciesId = Species.Id GROUP by "
"InbredSet.Name ORDER BY IFNULL(InbredSet.FamilyOrder, "
"InbredSet.FullName) ASC, IFNULL(InbredSet.Family, "
"InbredSet.FullName) ASC, InbredSet.FullName ASC, "
"InbredSet.MenuOrderId ASC").format(name)
)
def test_phenotypes_exist_called_with_correct_query(self):
"""Test that phenotypes_exist is called with the correct query"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
cursor.fetchone.return_value = None
phenotypes_exist("test", db_mock)
cursor.execute.assert_called_with(
"SELECT Name FROM PublishFreeze "
"WHERE PublishFreeze.Name = 'testPublish'"
)
def test_phenotypes_exist_with_falsy_values(self):
"""Test that phenotype check returns correctly when given
a None value"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
for x in [None, False, (), [], ""]:
cursor.fetchone.return_value = x
self.assertFalse(phenotypes_exist("test", db_mock))
def test_phenotypes_exist_with_truthy_value(self):
"""Test that phenotype check returns correctly when given Truthy"""
db_mock = mock.MagicMock()
with db_mock.cursor() as conn:
with conn.cursor() as cursor:
for x in ["x", ("result"), ["result"], [1]]:
cursor.fetchone.return_value = (x)
self.assertTrue(phenotypes_exist("test", db_mock))
def test_genotypes_exist_called_with_correct_query(self):
"""Test that genotypes_exist is called with the correct query"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
cursor.fetchone.return_value = None
genotypes_exist("test", db_mock)
cursor.execute.assert_called_with(
"SELECT Name FROM GenoFreeze WHERE "
"GenoFreeze.Name = 'testGeno'"
)
def test_genotypes_exist_with_falsy_values(self):
"""Test that genotype check returns correctly when given a None value
"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
for x in [None, False, (), [], ""]:
cursor.fetchone.return_value = x
self.assertFalse(genotypes_exist("test", db_mock))
def test_genotypes_exist_with_truthy_value(self):
"""Test that genotype check returns correctly when given Truthy """
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
for x in ["x", ("result"), ["result"], [1]]:
cursor.fetchone.return_value = (x)
self.assertTrue(phenotypes_exist("test", db_mock))
def test_build_datasets_with_type_phenotypes(self):
"""Test that correct dataset is returned for a phenotype type"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
cursor.fetchall.return_value = (
(602, "BXDPublish", "BXD Published Phenotypes"),
)
self.assertEqual(build_datasets("Mouse", "BXD",
"Phenotypes", db_mock),
[['602', "BXDPublish",
"BXD Published Phenotypes"]])
cursor.execute.assert_called_with(
"SELECT InfoFiles.GN_AccesionId, PublishFreeze.Name, "
+ "PublishFreeze.FullName FROM InfoFiles, PublishFreeze, "
+ "InbredSet WHERE InbredSet.Name = 'BXD' AND "
+ "PublishFreeze.InbredSetId = InbredSet.Id AND "
+ "InfoFiles.InfoPageName = PublishFreeze.Name "
+ "ORDER BY PublishFreeze.CreateTime ASC"
)
self.assertEqual(build_datasets("Mouse", "MDP",
"Phenotypes", db_mock),
[['602', "BXDPublish",
"Mouse Phenome Database"]])
cursor.fetchall.return_value = ()
cursor.fetchone.return_value = (
"BXDPublish", "Mouse Phenome Database"
)
self.assertEqual(build_datasets("Mouse", "MDP",
"Phenotypes", db_mock),
[["None", "BXDPublish",
"Mouse Phenome Database"]])
def test_build_datasets_with_type_phenotypes_and_no_results(self):
"""Test that correct dataset is returned for a phenotype type with no
results
"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
cursor.fetchall.return_value = None
cursor.fetchone.return_value = (121,
"text value")
self.assertEqual(build_datasets("Mouse", "BXD",
"Phenotypes", db_mock),
[["None", "121",
"text value"]])
cursor.execute.assert_called_with(
"SELECT PublishFreeze.Name, PublishFreeze.FullName "
"FROM PublishFreeze, InbredSet "
"WHERE InbredSet.Name = 'BXD' AND "
"PublishFreeze.InbredSetId = InbredSet.Id "
"ORDER BY PublishFreeze.CreateTime ASC"
)
def test_build_datasets_with_type_genotypes(self):
"""Test that correct dataset is returned for a phenotype type"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
cursor.fetchone.return_value = (
635, "HLCPublish", "HLC Published Genotypes"
)
self.assertEqual(build_datasets("Mouse", "HLC",
"Genotypes", db_mock),
[["635", "HLCGeno", "HLC Genotypes"]])
cursor.execute.assert_called_with(
"SELECT InfoFiles.GN_AccesionId FROM InfoFiles, "
"GenoFreeze, InbredSet WHERE InbredSet.Name = 'HLC' AND "
"GenoFreeze.InbredSetId = InbredSet.Id AND "
"InfoFiles.InfoPageName = GenoFreeze.ShortName "
"ORDER BY GenoFreeze.CreateTime DESC"
)
cursor.fetchone.return_value = ()
self.assertEqual(build_datasets("Mouse", "HLC",
"Genotypes", db_mock),
[["None", "HLCGeno", "HLC Genotypes"]])
def test_build_datasets_with_type_mrna(self):
"""Test that correct dataset is returned for a mRNA
expression/ Probeset"""
db_mock = mock.MagicMock()
with db_mock.cursor() as cursor:
cursor.fetchall.return_value = (
(112, "HC_M2_0606_P",
"Hippocampus Consortium M430v2 (Jun06) PDNN"), )
self.assertEqual(build_datasets("Mouse",
"HLC", "mRNA", db_mock),
[["112", 'HC_M2_0606_P',
"Hippocampus Consortium M430v2 (Jun06) PDNN"
]])
cursor.execute.assert_called_once_with(
"SELECT ProbeSetFreeze.Id, ProbeSetFreeze.Name, "
"ProbeSetFreeze.FullName FROM ProbeSetFreeze, "
"ProbeFreeze, InbredSet, Tissue, Species WHERE "
"Species.Name = 'Mouse' AND Species.Id = "
"InbredSet.SpeciesId AND InbredSet.Name = 'HLC' AND "
"ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id AND "
"Tissue.Name = 'mRNA' AND ProbeFreeze.TissueId = "
"Tissue.Id AND ProbeFreeze.InbredSetId = InbredSet.Id AND "
"ProbeSetFreeze.public > 0 "
"ORDER BY -ProbeSetFreeze.OrderList DESC, "
"ProbeSetFreeze.CreateTime DESC")
@mock.patch('gn2.wqflask.api.gen_menu.build_datasets')
def test_build_types(self, datasets_mock):
"""Test that correct tissue metadata is returned"""
db_mock = mock.MagicMock()
datasets_mock.return_value = [
["112", 'HC_M2_0606_P',
"Hippocampus Consortium M430v2 (Jun06) PDNN"]
]
with db_mock.cursor() as cursor:
cursor.fetchall.return_value = (
('Mouse Tissue'), ('Human Tissue'), ('Rat Tissue')
)
self.assertEqual(build_types('mouse', 'random group', db_mock),
[['M', 'M', 'Molecular Traits'],
['H', 'H', 'Molecular Traits'],
['R', 'R', 'Molecular Traits']])
cursor.execute.assert_called_once_with(
"SELECT DISTINCT Tissue.Name "
"FROM ProbeFreeze, ProbeSetFreeze, InbredSet, "
"Tissue, Species WHERE Species.Name = 'mouse' "
"AND Species.Id = InbredSet.SpeciesId AND "
"InbredSet.Name = 'random group' AND "
"ProbeFreeze.TissueId = Tissue.Id AND "
"ProbeFreeze.InbredSetId = InbredSet.Id AND "
"ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id "
"ORDER BY Tissue.Name"
)
@mock.patch('gn2.wqflask.api.gen_menu.build_types')
@mock.patch('gn2.wqflask.api.gen_menu.genotypes_exist')
@mock.patch('gn2.wqflask.api.gen_menu.phenotypes_exist')
def test_get_types_with_existing_genotype_and_phenotypes(
self,
phenotypes_exist_mock,
genotypes_exist_mock,
build_types_mock):
"""Test that build types are constructed correctly if phenotypes and genotypes
exist
"""
phenotypes_exist_mock.return_value = True
genotypes_exist_mock.return_value = True
expected_result = self.test_type
build_types_mock.return_value = [
['M', 'M', 'Molecular Trait Datasets']
]
self.assertEqual(get_types(self.test_group,
mock.MagicMock()),
expected_result)
@mock.patch('gn2.wqflask.api.gen_menu.build_types')
@mock.patch('gn2.wqflask.api.gen_menu.genotypes_exist')
@mock.patch('gn2.wqflask.api.gen_menu.phenotypes_exist')
def test_get_types_with_buildtype_and_non_existent_genotype_and_phenotypes(
self,
phenotypes_exist_mock,
genotypes_exist_mock,
build_types_mock):
"""Test that build types are constructed correctly if phenotypes_exist and
genotypes_exist are false but build_type is falsy
"""
phenotypes_exist_mock.return_value = False
genotypes_exist_mock.return_value = False
build_types_mock.return_value = []
self.assertEqual(get_types(self.test_group, mock.MagicMock()),
{'mouse': {}, 'human': {}})
@mock.patch('gn2.wqflask.api.gen_menu.build_types')
@mock.patch('gn2.wqflask.api.gen_menu.genotypes_exist')
@mock.patch('gn2.wqflask.api.gen_menu.phenotypes_exist')
def test_get_types_with_non_existent_genotype_phenotypes_and_buildtype(
self,
phenotypes_exist_mock,
genotypes_exist_mock,
build_types_mock):
"""Test that build types are constructed correctly if phenotypes_exist,
genotypes_exist and build_types are truthy
"""
phenotypes_exist_mock.return_value = False
genotypes_exist_mock.return_value = False
build_types_mock.return_value = [
['M', 'M', 'Molecular Trait Datasets']
]
expected_result = {
'mouse': {
'H_T2': [['M', 'M', 'Molecular Trait Datasets']],
'H_T1': [['M', 'M', 'Molecular Trait Datasets']]},
'human': {
'HLC': [['M', 'M', 'Molecular Trait Datasets']],
'BXD': [['M', 'M', 'Molecular Trait Datasets']]}}
self.assertEqual(get_types(self.test_group, mock.MagicMock()),
expected_result)
@mock.patch('gn2.wqflask.api.gen_menu.build_datasets')
def test_get_datasets_with_existent_datasets(self,
build_datasets_mock):
"""Test correct dataset is returned with existent build_datasets"""
build_datasets_mock.return_value = "Test"
expected_result = {
'mouse': {
'H_T2': {'Genotypes': 'Test',
'M': 'Test',
'Phenotypes': 'Test'},
'H_T1': {'Genotypes': 'Test',
'M': 'Test',
'Phenotypes': 'Test'}},
'human': {'HLC': {'Genotypes': 'Test',
'M': 'Test',
'Phenotypes': 'Test'},
'BXD': {'Genotypes': 'Test',
'M': 'Test',
'Phenotypes': 'Test'}}}
self.assertEqual(get_datasets(self.test_type, mock.MagicMock()),
expected_result)
@mock.patch('gn2.wqflask.api.gen_menu.build_datasets')
def test_get_datasets_with_non_existent_datasets(self,
build_datasets_mock):
"""Test correct dataset is returned with non-existent build_datasets"""
build_datasets_mock.return_value = None
expected_result = {
'mouse': {
'H_T2': {},
'H_T1': {}},
'human': {'HLC': {},
'BXD': {}}}
self.assertEqual(get_datasets(self.test_type, mock.MagicMock()),
expected_result)
@mock.patch('gn2.wqflask.api.gen_menu.get_datasets')
@mock.patch('gn2.wqflask.api.gen_menu.get_types')
@mock.patch('gn2.wqflask.api.gen_menu.get_groups')
@mock.patch('gn2.wqflask.api.gen_menu.get_all_species')
def test_gen_dropdown_json(self,
species_mock,
groups_mock,
types_mock,
datasets_mock):
"Test that the correct dictionary is constructed properly"
species_mock.return_value = ("speciesA speciesB speciesC speciesD"
.split(" "))
datasets_mock.return_value = ("datasetA datasetB datasetC datasetD"
.split(" "))
groups_mock.return_value = ("groupA groupB groupC groupD"
.split(" "))
types_mock.return_value = ("typeA typeB typeC typeD"
.split(" "))
datasets_mock.return_value = ("datasetA datasetB datasetC datasetD"
.split(" "))
expected_result = {
'datasets': ['datasetA', 'datasetB', 'datasetC', 'datasetD'],
'types': ['typeA', 'typeB', 'typeC', 'typeD'],
'groups': ['groupA', 'groupB', 'groupC', 'groupD'],
'species': ['speciesA', 'speciesB', 'speciesC', 'speciesD']}
self.assertEqual(gen_dropdown_json(mock.MagicMock()), expected_result)
|