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
|
"""Test cases for gn3.db.case_attributes.py"""
import pytest
import tempfile
import os
from pytest_mock import MockFixture
from gn3.db.case_attributes import queue_edit
from gn3.db.case_attributes import CaseAttributeEdit
from gn3.db.case_attributes import approve_case_attribute
@pytest.mark.unit_test
def test_queue_edit(mocker: MockFixture) -> None:
mock_conn = mocker.MagicMock()
with mock_conn.cursor() as cursor:
type(cursor).lastrowid = 28
TMPDIR = os.environ.get("TMPDIR", tempfile.gettempdir())
review_ids = queue_edit(
cursor,
directory=TMPDIR,
edit=CaseAttributeEdit(inbredset_id=1, user_id="xxxx", changes={"a": 1, "b": 2}))
cursor.execute.assert_called_once_with(
"INSERT INTO "
"caseattributes_audit(status, editor, json_diff_data) "
"VALUES (%s, %s, %s) "
"ON DUPLICATE KEY UPDATE status=%s",
('review', 'xxxx', '{"a": 1, "b": 2}', 'review'))
assert {28} == review_ids
@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)
|