diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/db/test_sample_data.py | 236 | ||||
-rw-r--r-- | tests/unit/test_csvcmp.py | 88 |
2 files changed, 189 insertions, 135 deletions
diff --git a/tests/unit/db/test_sample_data.py b/tests/unit/db/test_sample_data.py index 0fa2604..2524e07 100644 --- a/tests/unit/db/test_sample_data.py +++ b/tests/unit/db/test_sample_data.py @@ -1,3 +1,4 @@ +"""Tests for gn3.db.sample_data""" import pytest import gn3 @@ -9,132 +10,179 @@ from gn3.db.sample_data import update_sample_data @pytest.mark.unit_test def test_insert_sample_data(mocker): + """Test that inserts work properly""" 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.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): + """Test that deletes work properly""" 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.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"}, - }) + """Test that extracting the correct dict of 'actions' work properly""" + 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): + """Test that updates work properly""" 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) + 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, + ) + # pylint: disable=[E1101] 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) + phenotype_id=10007, + ) + # pylint: disable=[E1101] 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) + 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) + [ + mocker.call( + "UPDATE NStrain SET count = %s " + "WHERE StrainId = %s AND DataId = %s", + ("1", strain_id, data_id), + ) + ], + any_order=False, + ) diff --git a/tests/unit/test_csvcmp.py b/tests/unit/test_csvcmp.py index 3c9ba33..c7ffe2f 100644 --- a/tests/unit/test_csvcmp.py +++ b/tests/unit/test_csvcmp.py @@ -1,13 +1,15 @@ +"""Tests for gn3.csvcmp""" +import pytest + from gn3.csvcmp import csv_diff from gn3.csvcmp import fill_csv from gn3.csvcmp import remove_insignificant_edits from gn3.csvcmp import extract_strain_name -import pytest - @pytest.mark.unit_test def test_fill_csv(): + """Test that filling a csv works properly""" test_input = """ Strain Name,Value,SE,Count,Sex BXD1,18,x,0, @@ -20,45 +22,47 @@ BXD1,18,x,0,x BXD12,16,x,x,x BXD14,15,x,x,x BXD15,14,x,x,x""" - assert(fill_csv(test_input, width=5, value="x") == expected_output) + assert fill_csv(test_input, width=5, value="x") == expected_output + @pytest.mark.unit_test def test_remove_insignificant_data(): + """Test that values outside ε are removed/ ignored""" diff_data = { - 'Additions': [], - 'Deletions': [], - 'Modifications': [ - {'Current': '1.000001,3', 'Original': '1,3'}, - {'Current': '1,3', 'Original': '1.000001,3'}, - {'Current': '2.000001,3', 'Original': '2,2'}, - {'Current': '1.01,3', 'Original': '1,2'} - ] + "Additions": [], + "Deletions": [], + "Modifications": [ + {"Current": "1.000001,3", "Original": "1,3"}, + {"Current": "1,3", "Original": "1.000001,3"}, + {"Current": "2.000001,3", "Original": "2,2"}, + {"Current": "1.01,3", "Original": "1,2"}, + ], } expected_json = { - 'Additions': [], - 'Deletions': [], - 'Modifications': [ - {'Current': '2,3', 'Original': '2,2'}, - {'Current': '1.01,3', 'Original': '1,2'} - ] + "Additions": [], + "Deletions": [], + "Modifications": [ + {"Current": "2,3", "Original": "2,2"}, + {"Current": "1.01,3", "Original": "1,2"}, + ], } - assert (remove_insignificant_edits(diff_data) == - expected_json) + assert remove_insignificant_edits(diff_data) == expected_json @pytest.mark.unit_test def test_csv_diff_same_columns(): - assert(csv_diff(base_csv="a,b\n1,2\n", - delta_csv="a,b\n1,3") == { - 'Additions': [], - 'Deletions': [], - 'Columns': '', - 'Modifications': [{'Current': '1,3', - 'Original': '1,2'}]}) + """Test csv diffing on data with the same number of columns""" + assert csv_diff(base_csv="a,b\n1,2\n", delta_csv="a,b\n1,3") == { + "Additions": [], + "Deletions": [], + "Columns": "", + "Modifications": [{"Current": "1,3", "Original": "1,2"}], + } @pytest.mark.unit_test def test_csv_diff_different_columns(): + """Test csv diffing on data with different columns""" base_csv = """ Strain Name,Value,SE,Count BXD1,18,x,0 @@ -71,17 +75,17 @@ BXD1,18,x,0 BXD12,16,x,x,1 BXD14,15,x,x BXD15,14,x,x""" - assert(csv_diff(base_csv=base_csv, - delta_csv=delta_csv) == { - 'Additions': [], - 'Columns': 'Strain Name,Value,SE,Count,Sex', - 'Deletions': [], - 'Modifications': [{'Current': 'BXD12,16,x,x,1', - 'Original': 'BXD12,16,x,x,x'}]}) + assert csv_diff(base_csv=base_csv, delta_csv=delta_csv) == { + "Additions": [], + "Columns": "Strain Name,Value,SE,Count,Sex", + "Deletions": [], + "Modifications": [{"Current": "BXD12,16,x,x,1", "Original": "BXD12,16,x,x,x"}], + } @pytest.mark.unit_test def test_csv_diff_only_column_change(): + """Test csv diffing when only the column header change""" base_csv = """ Strain Name,Value,SE,Count BXD1,18,x,0 @@ -95,15 +99,17 @@ BXD12,16,x,x BXD14,15,x,x BXD15,14,x,x """ - assert(csv_diff(base_csv=base_csv, - delta_csv=delta_csv) == { - 'Additions': [], - 'Deletions': [], - 'Modifications': []}) + assert csv_diff(base_csv=base_csv, delta_csv=delta_csv) == { + "Additions": [], + "Deletions": [], + "Modifications": [], + } @pytest.mark.unit_test def test_extract_strain_name(): - assert(extract_strain_name(csv_header="Strain Name,Value,SE,Count", - data="BXD1,18,x,0") == - "BXD1") + """Test that the strain's name is extracted given a csv header""" + assert ( + extract_strain_name(csv_header="Strain Name,Value,SE,Count", data="BXD1,18,x,0") + == "BXD1" + ) |