aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/db/test_sample_data.py
blob: 0fa26045252625a8deba171c9337736c1b49b461 (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
import pytest
import gn3

from gn3.db.sample_data import __extract_actions
from gn3.db.sample_data import delete_sample_data
from gn3.db.sample_data import insert_sample_data
from gn3.db.sample_data import update_sample_data


@pytest.mark.unit_test
def test_insert_sample_data(mocker):
    mock_conn = mocker.MagicMock()
    strain_id, data_id, inbredset_id = 1, 17373, 20
    with mock_conn.cursor() as cursor:
        cursor.fetchone.side_effect = (0, [19, ], 0)
        mocker.patch('gn3.db.sample_data.get_sample_data_ids',
                     return_value=(strain_id, data_id, inbredset_id))
        insert_sample_data(conn=mock_conn,
                           trait_name=35,
                           data="BXD1,18,3,0,M",
                           csv_header="Strain Name,Value,SE,Count,Sex",
                           phenotype_id=10007)
        calls = [mocker.call("SELECT Id FROM PublishData where Id = %s "
                             "AND StrainId = %s",
                             (data_id, strain_id)),
                 mocker.call("INSERT INTO PublishData "
                             "(StrainId, Id, value) VALUES (%s, %s, %s)",
                             (strain_id, data_id, '18')),
                 mocker.call("INSERT INTO PublishSE "
                             "(StrainId, DataId, error) VALUES (%s, %s, %s)",
                             (strain_id, data_id, '3')),
                 mocker.call("INSERT INTO NStrain "
                             "(StrainId, DataId, count) VALUES (%s, %s, %s)",
                             (strain_id, data_id, '0')),
                 mocker.call("SELECT Id FROM CaseAttribute WHERE Name = %s",
                             ("Sex",)),
                 mocker.call("SELECT StrainId FROM CaseAttributeXRefNew "
                             "WHERE StrainId = %s AND "
                             "CaseAttributeId = %s AND InbredSetId = %s",
                             (strain_id, 19, inbredset_id)),
                 mocker.call("INSERT INTO CaseAttributeXRefNew "
                             "(StrainId, CaseAttributeId, Value, "
                             "InbredSetId) VALUES (%s, %s, %s, %s)",
                             (strain_id, 19, 'M', inbredset_id))
                 ]
        cursor.execute.assert_has_calls(calls, any_order=False)


@pytest.mark.unit_test
def test_delete_sample_data(mocker):
    mock_conn = mocker.MagicMock()
    strain_id, data_id, inbredset_id = 1, 17373, 20
    with mock_conn.cursor() as cursor:
        cursor.fetchone.side_effect = (0, [19, ], 0)
        mocker.patch('gn3.db.sample_data.get_sample_data_ids',
                     return_value=(strain_id, data_id, inbredset_id))
        delete_sample_data(conn=mock_conn,
                           trait_name=35,
                           data="BXD1,18,3,0,M",
                           csv_header="Strain Name,Value,SE,Count,Sex",
                           phenotype_id=10007)
        calls = [mocker.call("DELETE FROM PublishData WHERE "
                             "StrainId = %s AND Id = %s",
                             (strain_id, data_id)),
                 mocker.call("DELETE FROM PublishSE WHERE "
                             "StrainId = %s AND DataId = %s",
                             (strain_id, data_id)),
                 mocker.call("DELETE FROM NStrain WHERE "
                             "StrainId = %s AND DataId = %s",
                             (strain_id, data_id)),
                 mocker.call("DELETE FROM CaseAttributeXRefNew WHERE "
                             "StrainId = %s AND CaseAttributeId = "
                             "(SELECT CaseAttributeId FROM "
                             "CaseAttribute WHERE Name = %s) "
                             "AND InbredSetId = %s",
                             (strain_id, "Sex", inbredset_id)),
                 ]
        cursor.execute.assert_has_calls(calls, any_order=False)


@pytest.mark.unit_test
def test_extract_actions():
    assert (__extract_actions(original_data="BXD1,18,x,0,x",
                              updated_data="BXD1,x,2,1,F",
                              csv_header="Strain Name,Value,SE,Count,Sex") ==
            {
                "delete": {"data": "BXD1,18",
                           "csv_header": "Strain Name,Value"},
                "insert": {"data": "BXD1,2,F",
                           "csv_header": "Strain Name,SE,Sex"},
                "update": {"data": "BXD1,1",
                           "csv_header": "Strain Name,Count"},
    })
    assert(__extract_actions(original_data="BXD1,18,x,0,x",
                             updated_data="BXD1,19,2,1,F",
                             csv_header="Strain Name,Value,SE,Count,Sex") ==
           {
               "delete": None,
               "insert": {"data": "BXD1,2,F",
                          "csv_header": "Strain Name,SE,Sex"},
               "update": {"data": "BXD1,19,1",
                          "csv_header": "Strain Name,Value,Count"},
    })


@pytest.mark.unit_test
def test_update_sample_data(mocker):
    mock_conn = mocker.MagicMock()
    strain_id, data_id, inbredset_id = 1, 17373, 20
    with mock_conn.cursor() as cursor:
        # cursor.fetchone.side_effect = (0, [19, ], 0)
        mocker.patch('gn3.db.sample_data.get_sample_data_ids',
                     return_value=(strain_id, data_id, inbredset_id))
        mocker.patch('gn3.db.sample_data.insert_sample_data',
                     return_value=1)
        mocker.patch('gn3.db.sample_data.delete_sample_data',
                     return_value=1)
        update_sample_data(conn=mock_conn,
                           trait_name=35,
                           original_data="BXD1,18,x,0,x",
                           updated_data="BXD1,x,2,1,F",
                           csv_header="Strain Name,Value,SE,Count,Sex",
                           phenotype_id=10007)
        gn3.db.sample_data.insert_sample_data.assert_called_once_with(
            conn=mock_conn,
            trait_name=35,
            data="BXD1,2,F",
            csv_header="Strain Name,SE,Sex",
            phenotype_id=10007)
        gn3.db.sample_data.delete_sample_data.assert_called_once_with(
            conn=mock_conn,
            trait_name=35,
            data="BXD1,18",
            csv_header="Strain Name,Value",
            phenotype_id=10007)
        cursor.execute.assert_has_calls(
            [mocker.call("UPDATE NStrain SET count = %s "
                         "WHERE StrainId = %s AND DataId = %s",
                         ('1', strain_id, data_id))],
            any_order=False)