From dfa6a6106718f0c88f1b09aedae42eb31aafbb6c Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Thu, 10 Mar 2022 17:18:57 +0300 Subject: Update how data is updated by re-using existing functions * gn3/db/sample_data.py (get_sample_data_ids): Re-use "delete_sample_data" and "insert_sample_data" when updating data; and also add logic for updating modified data. * tests/unit/db/test_sample_data.py: Add tests for the above. --- tests/unit/db/test_sample_data.py | 43 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/unit/db/test_sample_data.py b/tests/unit/db/test_sample_data.py index 4015ba5..80166fc 100644 --- a/tests/unit/db/test_sample_data.py +++ b/tests/unit/db/test_sample_data.py @@ -1,8 +1,10 @@ import pytest +import gn3 -from gn3.db.sample_data import insert_sample_data -from gn3.db.sample_data import delete_sample_data 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 @@ -94,3 +96,40 @@ def test_extract_actions(): "insert": {"data": "2,F", "csv_header": "SE,Sex"}, "update": {"data": "19,1", "csv_header": "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="2,F", + csv_header="SE,Sex", + phenotype_id=10007) + gn3.db.sample_data.delete_sample_data.assert_called_once_with( + conn=mock_conn, + trait_name=35, + data="18", + csv_header="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) -- cgit v1.2.3