aboutsummaryrefslogtreecommitdiff
path: root/wqflask/tests/base/test_data_set.py
blob: dd7f5051ae9e8ffb056a1f9398839f46232e1027 (about) (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
"""Tests for wqflask/base/data_set.py"""

import unittest
import mock

from wqflask import app
from data import gen_menu_json
from base.data_set import DatasetType


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()

    @mock.patch('base.data_set.g')
    def test_data_set_type(self, db_mock):
        """Test that DatasetType returns correctly if the Redis Instance is not empty
        and the name variable exists in the dictionary

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

    @mock.patch('base.data_set.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()
            redis_mock.get.return_value = None
            data_set = DatasetType(redis_mock)
            self.assertEqual(data_set("BXDGeno"), "Geno")
            self.assertEqual(data_set("BXDPublish"), "Publish")
            self.assertEqual(data_set("HLC_0311"), "ProbeSet")
            redis_mock.set.assert_called_once_with(
                "dataset_structure",
                '{"BXDGeno": "Geno", "BXDPublish": "Publish", "HLCPublish": "Publish", "HLC_0311": "ProbeSet", "HC_M2_0606_P": "ProbeSet"}')

    @mock.patch('base.data_set.g')
    def test_set_dataset_key_mrna(self, db_mock):
        with app.app_context():
            db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
            redis_mock = mock.Mock()
            redis_mock.get.return_value = self.test_dataset
            data_set = DatasetType(redis_mock)
            data_set.set_dataset_key("mrna_expr", "Test")
            self.assertEqual(data_set("Test"), "ProbeSet")
            redis_mock.set.assert_called_once_with(
                "dataset_structure",
                '{"Aging-Brain-UCIPublish": "Publish", "AKXDGeno": "Geno", "B139_K_1206_M": "ProbeSet", "AD-cases-controls-MyersGeno": "Geno", "AD-cases-controls-MyersPublish": "Publish", "All Phenotypes": "Publish", "Test": "ProbeSet", "AXBXAPublish": "Publish", "B139_K_1206_R": "ProbeSet", "AXBXAGeno": "Geno"}')
            expected_db_call = """"""
            db_mock.db.execute.assert_called_with(
                ("SELECT ProbeSetFreeze.Id FROM ProbeSetFreeze " +
                 "WHERE ProbeSetFreeze.Name = \"Test\" ")
            )

    @mock.patch('base.data_set.g')
    def test_set_dataset_key_pheno(self, db_mock):
        with app.app_context():
            db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
            redis_mock = mock.Mock()
            redis_mock.get.return_value = self.test_dataset
            data_set = DatasetType(redis_mock)
            data_set.set_dataset_key("pheno", "Test")
            self.assertEqual(data_set("Test"), "Publish")
            redis_mock.set.assert_called_once_with(
                "dataset_structure",
                '{"Aging-Brain-UCIPublish": "Publish", "AKXDGeno": "Geno", "B139_K_1206_M": "ProbeSet", "AD-cases-controls-MyersGeno": "Geno", "AD-cases-controls-MyersPublish": "Publish", "All Phenotypes": "Publish", "Test": "Publish", "AXBXAPublish": "Publish", "B139_K_1206_R": "ProbeSet", "AXBXAGeno": "Geno"}')
            db_mock.db.execute.assert_called_with(
                ("SELECT InfoFiles.GN_AccesionId " +
                 "FROM InfoFiles, PublishFreeze, InbredSet " +
                 "WHERE InbredSet.Name = 'Test' AND "
                 "PublishFreeze.InbredSetId = InbredSet.Id AND " +
                 "InfoFiles.InfoPageName = PublishFreeze.Name")
            )

    @mock.patch('base.data_set.g')
    def test_set_dataset_other_pheno(self, db_mock):
        with app.app_context():
            db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
            redis_mock = mock.Mock()
            redis_mock.get.return_value = self.test_dataset
            data_set = DatasetType(redis_mock)
            data_set.set_dataset_key("other_pheno", "Test")
            self.assertEqual(data_set("Test"), "Publish")
            redis_mock.set.assert_called_once_with(
                "dataset_structure",
                '{"Aging-Brain-UCIPublish": "Publish", "AKXDGeno": "Geno", "B139_K_1206_M": "ProbeSet", "AD-cases-controls-MyersGeno": "Geno", "AD-cases-controls-MyersPublish": "Publish", "All Phenotypes": "Publish", "Test": "Publish", "AXBXAPublish": "Publish", "B139_K_1206_R": "ProbeSet", "AXBXAGeno": "Geno"}')
            db_mock.db.execute.assert_called_with(
                ("SELECT PublishFreeze.Name " +
                 "FROM PublishFreeze, InbredSet " +
                 "WHERE InbredSet.Name = 'Test' AND "
                 "PublishFreeze.InbredSetId = InbredSet.Id")
            )

    @mock.patch('base.data_set.g')
    def test_set_dataset_geno(self, db_mock):
        with app.app_context():
            db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
            redis_mock = mock.Mock()
            redis_mock.get.return_value = self.test_dataset
            data_set = DatasetType(redis_mock)
            data_set.set_dataset_key("geno", "Test")
            self.assertEqual(data_set("Test"), "Geno")
            redis_mock.set.assert_called_once_with(
                "dataset_structure",
                '{"Aging-Brain-UCIPublish": "Publish", "AKXDGeno": "Geno", "B139_K_1206_M": "ProbeSet", "AD-cases-controls-MyersGeno": "Geno", "AD-cases-controls-MyersPublish": "Publish", "All Phenotypes": "Publish", "Test": "Geno", "AXBXAPublish": "Publish", "B139_K_1206_R": "ProbeSet", "AXBXAGeno": "Geno"}')
            expected_db_call = """"""
            db_mock.db.execute.assert_called_with(
                ("SELECT GenoFreeze.Id FROM GenoFreeze WHERE GenoFreeze.Name = \"Test\" ")
            )