aboutsummaryrefslogtreecommitdiff
path: root/gn2/tests/unit/wqflask/show_trait/test_export_trait_data.py
blob: 1ecf394b9238185b311e18fffc8548405355e7ff (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
139
140
141
142
143
144
145
146
147
148
149
150
151
import datetime
import unittest
from unittest import mock
from gn2.wqflask.show_trait.export_trait_data import dict_to_sorted_list
from gn2.wqflask.show_trait.export_trait_data import cmp_samples
from gn2.wqflask.show_trait.export_trait_data import export_sample_table
from gn2.wqflask.show_trait.export_trait_data import get_export_metadata


class AttributesSetter:
    def __init__(self, obj):
        for key, value in obj.items():
            setattr(self, key, value)


class TestExportTraits(unittest.TestCase):
    """Test methods for exporting traits and metadata"""

    @mock.patch("gn2.wqflask.show_trait.export_trait_data.datetime")
    @mock.patch("gn2.wqflask.show_trait.export_trait_data.create_trait")
    @mock.patch("gn2.wqflask.show_trait.export_trait_data.data_set")
    def test_get_export_metadata(self, data_mock, trait_mock, date_mock):
        """test for exporting metadata with dataset.type=Publish"""
        mock_dataset = AttributesSetter({"type": "Publish",
                                         "name": "HC_M2_0606_P",
                                         "dataset_name": "HC_M2_0606_P"})

        mock_dataset.group = AttributesSetter({"name": "C"})
        data_mock.create_dataset.return_value = mock_dataset

        trait_data = {
            "symbol": "Nr3c1",
            "description_display": "nuclear receptor subfamily 3,group C, member 1 (glucocorticoid receptor); distal 3' UTR",
            "title": "Trait_1 title",

            "authors": "XL_1",
            "journal": ""

        }

        date_mock.datetime.now.return_value = datetime.datetime(
            2022, 8, 8, 19, 2, 31, 628813)
        trait_mock.return_value = AttributesSetter(trait_data)

        results = get_export_metadata({
            "trait_id": "1460303_at",
            "trait_display_name": "1460303_at",
            "dataset": "HC_M2_0606_P",
            "group": "BXD",
        })

        expected = [["Phenotype ID:", "1460303_at"],
                    ["Phenotype URL: ", "http://genenetwork.org/show_trait?trait_id=1460303_at&dataset=HC_M2_0606_P"],
                    ["Group: ", "C"],
                    ["Phenotype: ",
                        'nuclear receptor subfamily 3","group C"," member 1 (glucocorticoid receptor); distal 3\' UTR'],
                    ["Authors: ", "XL_1"],
                    ["Title: ", "Trait_1 title"],
                    ["Journal: ", "N/A"],
                    ["Dataset Link: ", "http://gn1.genenetwork.org/webqtl/main.py?FormID=sharinginfo&InfoPageName=HC_M2_0606_P"],
                    ["Export Date: ", "August 08, 2022"],
                    ["Export Time: ", "19:02 GMT"]]

        self.assertEqual(results, expected)

    def test_dict_to_sortedlist(self):
        """test for conversion of dict to sorted list"""
        sample1 = {
            "other": "exp1",
            "name": "exp2"
        }
        sample2 = {
            "se": 1,
            "num_cases": 4,
            "value": 6,
            "name": 3

        }
        rever = {
            "name": 3,
            "value": 6,
            "num_cases": 4,
            "se": 1
        }
        oneItem = {
            "item1": "one"
        }

        self.assertEqual(["exp2", "exp1"], dict_to_sorted_list(sample1))
        self.assertEqual([3, 6, 1, 4], dict_to_sorted_list(sample2))
        self.assertEqual([3, 6, 1, 4], dict_to_sorted_list(rever))
        self.assertEqual(["one"], dict_to_sorted_list(oneItem))
        """test that the func returns the values not the keys"""
        self.assertFalse(["other", "name"] == dict_to_sorted_list(sample1))

    def test_cmp_samples(self):
        """test for comparing samples function"""
        sampleA = [
            [
                ("value", "other"),
                ("name", "test_name")
            ]
        ]
        sampleB = [
            [
                ("value", "other"),
                ("unknown", "test_name")
            ]
        ]
        sampleC = [
            [("other", "value"),
             ("name", "value")
             ],
            [
                ("name", "value"),
                ("value", "name")
            ],
            [
                ("other", "value"),
                ("name", "value"
                 )],
            [
                ("name", "name1"),
                ("se", "valuex")
            ],
            [(
                "value", "name1"),
                ("se", "valuex")
             ],
            [(
                "other", "name1"),
                ("se", "valuex"
                 )
             ],
            [(
                "name", "name_val"),
                ("num_cases", "num_val")
             ],
            [(
                "other_a", "val_a"),
                ("other_b", "val"
                 )
             ]
        ]
        results = [cmp_samples(val[0], val[1]) for val in sampleA]
        resultB = [cmp_samples(val[0], val[1]) for val in sampleB]
        resultC = [cmp_samples(val[0], val[1]) for val in sampleC]

        self.assertEqual(1, *results)
        self.assertEqual(-1, *resultB)
        self.assertEqual([1, -1, 1, -1, -1, 1, -1, -1], resultC)