diff options
-rw-r--r-- | gn3/db/sample_data.py | 21 | ||||
-rw-r--r-- | tests/unit/db/test_sample_data.py | 59 |
2 files changed, 47 insertions, 33 deletions
diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py index 47be5b0..518d2a2 100644 --- a/gn3/db/sample_data.py +++ b/gn3/db/sample_data.py @@ -343,25 +343,28 @@ def insert_sample_data( def __insert_case_attribute(conn, case_attr, value): if value != "x": with conn.cursor() as cursor: - cursor.execute( - "SELECT Id FROM " "CaseAttribute WHERE Name = %s", - (case_attr,), - ) - if case_attr_id := cursor.fetchone(): - case_attr_id = case_attr_id[0] + (id_, name) = parse_csv_column(case_attr) + if not id_: + cursor.execute( + "SELECT Id FROM CaseAttribute WHERE Name = %s", + (case_attr,), + ) + if case_attr_id := cursor.fetchone(): + id_ = case_attr_id[0] + cursor.execute( "SELECT StrainId FROM " "CaseAttributeXRefNew WHERE StrainId = %s " "AND CaseAttributeId = %s " "AND InbredSetId = %s", - (strain_id, case_attr_id, inbredset_id), + (strain_id, id_, inbredset_id), ) - if (not cursor.fetchone()) and case_attr_id: + if (not cursor.fetchone()) and id_: cursor.execute( "INSERT INTO CaseAttributeXRefNew " "(StrainId, CaseAttributeId, Value, InbredSetId) " "VALUES (%s, %s, %s, %s)", - (strain_id, case_attr_id, value, inbredset_id), + (strain_id, id_, value, inbredset_id), ) row_count = cursor.rowcount return row_count diff --git a/tests/unit/db/test_sample_data.py b/tests/unit/db/test_sample_data.py index af00393..e7d7fb0 100644 --- a/tests/unit/db/test_sample_data.py +++ b/tests/unit/db/test_sample_data.py @@ -21,6 +21,7 @@ def test_insert_sample_data(mocker): 19, ], 0, + 0, ) mocker.patch( "gn3.db.sample_data.get_sample_data_ids", @@ -29,45 +30,55 @@ def test_insert_sample_data(mocker): insert_sample_data( conn=mock_conn, trait_name=35, - data="BXD1,18,3,0,M", - csv_header="Strain Name,Value,SE,Count,Sex", + data="BXD1,18,3,0,Red,M", + csv_header="Strain Name,Value,SE,Count,Color,Sex (13)", phenotype_id=10007, ) calls = [ mocker.call( - "SELECT Id FROM PublishData where Id = %s " - "AND StrainId = %s", - (data_id, strain_id), + "SELECT Id FROM PublishData where Id = %s AND StrainId = %s", + (17373, 1), + ), + mocker.call( + "INSERT INTO PublishData (StrainId, Id, value) " + "VALUES (%s, %s, %s)", + (1, 17373, "18"), + ), + mocker.call( + "INSERT INTO PublishSE (StrainId, DataId, error) VALUES " + "(%s, %s, %s)", + (1, 17373, "3"), ), mocker.call( - "INSERT INTO PublishData " - "(StrainId, Id, value) VALUES (%s, %s, %s)", - (strain_id, data_id, "18"), + "INSERT INTO NStrain (StrainId, DataId, count) VALUES " + "(%s, %s, %s)", + (1, 17373, "0"), ), mocker.call( - "INSERT INTO PublishSE " - "(StrainId, DataId, error) VALUES (%s, %s, %s)", - (strain_id, data_id, "3"), + "SELECT Id FROM CaseAttribute WHERE Name = %s", ("Color",) ), mocker.call( - "INSERT INTO NStrain " - "(StrainId, DataId, count) VALUES (%s, %s, %s)", - (strain_id, data_id, "0"), + "SELECT StrainId FROM CaseAttributeXRefNew WHERE " + "StrainId = %s AND CaseAttributeId = %s AND InbredSetId = %s", + (1, 19, 20), ), mocker.call( - "SELECT Id FROM CaseAttribute WHERE Name = %s", ("Sex",) + "INSERT INTO CaseAttributeXRefNew (StrainId, " + "CaseAttributeId, Value, InbredSetId) VALUES " + "(%s, %s, %s, %s)", + (1, 19, "Red", 20), ), mocker.call( - "SELECT StrainId FROM CaseAttributeXRefNew " - "WHERE StrainId = %s AND " - "CaseAttributeId = %s AND InbredSetId = %s", - (strain_id, 19, inbredset_id), + "SELECT StrainId FROM CaseAttributeXRefNew WHERE " + "StrainId = %s AND CaseAttributeId = %s AND " + "InbredSetId = %s", + (1, "13", 20), ), mocker.call( - "INSERT INTO CaseAttributeXRefNew " - "(StrainId, CaseAttributeId, Value, " - "InbredSetId) VALUES (%s, %s, %s, %s)", - (strain_id, 19, "M", inbredset_id), + "INSERT INTO CaseAttributeXRefNew (StrainId, " + "CaseAttributeId, Value, InbredSetId) VALUES " + "(%s, %s, %s, %s)", + (1, "13", "M", 20), ), ] cursor.execute.assert_has_calls(calls, any_order=False) @@ -206,7 +217,7 @@ def test_update_sample_data(mocker): "CaseAttribute WHERE Name = %s) " "AND InbredSetId = %s", ("Green", strain_id, "Color", inbredset_id), - ) + ), ], any_order=False, ) |