aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/db/test_case_attributes.py
blob: 175249be62372f9db6da45629eb2b17e6ef039b4 (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
"""Test cases for gn3.db.case_attributes.py"""

import pytest
from pytest_mock import MockFixture
from gn3.db.case_attributes import get_unreviewed_diffs
from gn3.db.case_attributes import get_case_attributes
from gn3.db.case_attributes import insert_case_attribute_audit
from gn3.db.case_attributes import approve_case_attribute
from gn3.db.case_attributes import reject_case_attribute


@pytest.mark.unit_test
def test_get_case_attributes(mocker: MockFixture) -> None:
    """Test that all the case attributes are fetched correctly"""
    mock_conn = mocker.MagicMock()
    with mock_conn.cursor() as cursor:
        cursor.fetchall.return_value = (
            (1, "Condition", None),
            (2, "Tissue", None),
            (3, "Age", "Cum sociis natoque penatibus et magnis dis"),
            (4, "Condition", "Description A"),
            (5, "Condition", "Description B"),
        )
        results = get_case_attributes(mock_conn)
        cursor.execute.assert_called_once_with(
            "SELECT Id, Name, Description FROM CaseAttribute"
        )
        assert results == (
            (1, "Condition", None),
            (2, "Tissue", None),
            (3, "Age", "Cum sociis natoque penatibus et magnis dis"),
            (4, "Condition", "Description A"),
            (5, "Condition", "Description B"),
        )


@pytest.mark.unit_test
def test_get_unreviewed_diffs(mocker: MockFixture) -> None:
    """Test that the correct query is called when fetching unreviewed
    case-attributes diff"""
    mock_conn = mocker.MagicMock()
    with mock_conn.cursor() as cursor:
        _ = get_unreviewed_diffs(mock_conn)
        cursor.fetchall.return_value = ((1, "editor", "diff_data_1"),)
        cursor.execute.assert_called_once_with(
            "SELECT id, editor, json_diff_data FROM "
            "caseattributes_audit WHERE status = 'review'"
        )


@pytest.mark.unit_test
def test_insert_case_attribute_audit(mocker: MockFixture) -> None:
    """Test that the updating case attributes uses the correct query"""
    mock_conn = mocker.MagicMock()
    with mock_conn.cursor() as cursor:
        _ = insert_case_attribute_audit(
            mock_conn, status="review", author="Author", data="diff_data"
        )
        cursor.execute.assert_called_once_with(
            "INSERT INTO caseattributes_audit "
            "(status, editor, json_diff_data) "
            "VALUES (%s, %s, %s)",
            ("review", "Author", "diff_data"),
        )


@pytest.mark.unit_test
def test_reject_case_attribute(mocker: MockFixture) -> None:
    """Test rejecting a case-attribute"""
    mock_conn = mocker.MagicMock()
    with mock_conn.cursor() as cursor:
        _ = reject_case_attribute(
            mock_conn,
            case_attr_audit_id=1,
        )
        cursor.execute.assert_called_once_with(
            "UPDATE caseattributes_audit SET "
            "status = 'rejected' WHERE id = %s",
            (1,),
        )


@pytest.mark.unit_test
def test_approve_inserting_case_attribute(mocker: MockFixture) -> None:
    """Test approving inserting a case-attribute"""
    mock_conn = mocker.MagicMock()
    with mock_conn.cursor() as cursor:
        type(cursor).rowcount = 1
        cursor.fetchone.return_value = (
            """
        {"Insert": {"name": "test", "description": "Random Description"}}
        """,
        )
        _ = approve_case_attribute(
            mock_conn,
            case_attr_audit_id=3,
        )
        calls = [
            mocker.call(
                "SELECT json_diff_data FROM caseattributes_audit "
                "WHERE id = %s",
                (3,),
            ),
            mocker.call(
                "INSERT INTO CaseAttribute "
                "(Name, Description) VALUES "
                "(%s, %s)",
                (
                    "test",
                    "Random Description",
                ),
            ),
            mocker.call(
                "UPDATE caseattributes_audit SET "
                "status = 'approved' WHERE id = %s",
                (3,),
            ),
        ]
        cursor.execute.assert_has_calls(calls, any_order=False)


@pytest.mark.unit_test
def test_approve_deleting_case_attribute(mocker: MockFixture) -> None:
    """Test deleting a case-attribute"""
    mock_conn = mocker.MagicMock()
    with mock_conn.cursor() as cursor:
        type(cursor).rowcount = 1
        cursor.fetchone.return_value = (
            """
        {"Deletion": {"id": "12", "name": "test", "description": ""}}
        """,
        )
        _ = approve_case_attribute(
            mock_conn,
            case_attr_audit_id=3,
        )
        calls = [
            mocker.call(
                "SELECT json_diff_data FROM caseattributes_audit "
                "WHERE id = %s",
                (3,),
            ),
            mocker.call("DELETE FROM CaseAttribute " "WHERE Id = %s", ("12",)),
            mocker.call(
                "UPDATE caseattributes_audit SET "
                "status = 'approved' WHERE id = %s",
                (3,),
            ),
        ]
        cursor.execute.assert_has_calls(calls, any_order=False)


@pytest.mark.unit_test
def test_approve_modifying_case_attribute(mocker: MockFixture) -> None:
    """Test modifying a case-attribute"""
    mock_conn = mocker.MagicMock()
    with mock_conn.cursor() as cursor:
        type(cursor).rowcount = 1
        cursor.fetchone.return_value = (
            """
{
  "id": "12",
  "Modification": {
    "description": {
      "Current": "Test",
      "Original": "A"
    },
    "name": {
      "Current": "Height (A)",
      "Original": "Height"
    }
  }
}""",
        )
        _ = approve_case_attribute(
            mock_conn,
            case_attr_audit_id=3,
        )
        calls = [
            mocker.call(
                "SELECT json_diff_data FROM caseattributes_audit "
                "WHERE id = %s",
                (3,),
            ),
            mocker.call(
                "UPDATE CaseAttribute SET " "Description = %s WHERE Id = %s",
                (
                    "Test",
                    "12",
                ),
            ),
            mocker.call(
                "UPDATE CaseAttribute SET " "Name = %s WHERE Id = %s",
                (
                    "Height (A)",
                    "12",
                ),
            ),
            mocker.call(
                "UPDATE caseattributes_audit SET "
                "status = 'approved' WHERE id = %s",
                (3,),
            ),
        ]
        cursor.execute.assert_has_calls(calls, any_order=False)