diff options
| -rw-r--r-- | gn3/db/case_attributes.py | 68 | ||||
| -rw-r--r-- | tests/unit/db/test_case_attributes.py | 126 |
2 files changed, 0 insertions, 194 deletions
diff --git a/gn3/db/case_attributes.py b/gn3/db/case_attributes.py index ea3bf27..7076ec8 100644 --- a/gn3/db/case_attributes.py +++ b/gn3/db/case_attributes.py @@ -77,71 +77,3 @@ def queue_edit(cursor, directory: Path, edit: CaseAttributeEdit) -> int: review_ids.add(cursor.lastrowid) txn.put(b"review", pickle.dumps(review_ids)) return review_ids - - -def approve_case_attribute(conn: Any, case_attr_audit_id: int) -> int: - """Given the id of the json_diff in the case_attribute_audit table, - approve it - - """ - rowcount = 0 - try: - with conn.cursor() as cursor: - cursor.execute( - "SELECT json_diff_data FROM caseattributes_audit " - "WHERE id = %s", - (case_attr_audit_id,), - ) - diff_data = cursor.fetchone() - if diff_data: - diff_data = json.loads(diff_data[0]) - # Insert (Most Important) - if diff_data.get("Insert"): - data = diff_data.get("Insert") - cursor.execute( - "INSERT INTO CaseAttribute " - "(Name, Description) VALUES " - "(%s, %s)", - ( - data.get("name").strip(), - data.get("description").strip(), - ), - ) - # Delete - elif diff_data.get("Deletion"): - data = diff_data.get("Deletion") - cursor.execute( - "DELETE FROM CaseAttribute WHERE Id = %s", - (data.get("id"),), - ) - # Modification - elif diff_data.get("Modification"): - data = diff_data.get("Modification") - if desc_ := data.get("description"): - cursor.execute( - "UPDATE CaseAttribute SET " - "Description = %s WHERE Id = %s", - ( - desc_.get("Current"), - diff_data.get("id"), - ), - ) - if name_ := data.get("name"): - cursor.execute( - "UPDATE CaseAttribute SET " - "Name = %s WHERE Id = %s", - ( - name_.get("Current"), - diff_data.get("id"), - ), - ) - if cursor.rowcount: - cursor.execute( - "UPDATE caseattributes_audit SET " - "status = 'approved' WHERE id = %s", - (case_attr_audit_id,), - ) - rowcount = cursor.rowcount - except Exception as _e: - raise MySQLdb.Error(_e) from _e - return rowcount diff --git a/tests/unit/db/test_case_attributes.py b/tests/unit/db/test_case_attributes.py index 5617362..1a2a8ae 100644 --- a/tests/unit/db/test_case_attributes.py +++ b/tests/unit/db/test_case_attributes.py @@ -6,7 +6,6 @@ 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 @@ -26,128 +25,3 @@ def test_queue_edit(mocker: MockFixture) -> None: "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) |
