aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2022-04-06 16:36:52 +0300
committerBonfaceKilz2022-04-07 11:54:28 +0300
commitb18de873dbead76276ee5077bc8a2afe78f61606 (patch)
treed432548c9bb820653cb03e98324284f14bd699e1
parentdc71058e894c9857de68c7844bfc4b813c0532da (diff)
downloadgenenetwork3-b18de873dbead76276ee5077bc8a2afe78f61606.tar.gz
Use case attribute id inside brackets if present during insertions
* gn3/db/sample_data.py (delete_sample_data): If an id is present in the column header, use it. * tests/unit/db/test_sample_data.py (test_delete_sample_data): Update tests to capture the above.
-rw-r--r--gn3/db/sample_data.py25
-rw-r--r--tests/unit/db/test_sample_data.py25
2 files changed, 29 insertions, 21 deletions
diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py
index 518d2a2..de90162 100644
--- a/gn3/db/sample_data.py
+++ b/gn3/db/sample_data.py
@@ -275,14 +275,23 @@ def delete_sample_data(
def __delete_case_attribute(conn, strain_id, case_attr, inbredset_id):
with conn.cursor() as cursor:
- cursor.execute(
- "DELETE FROM CaseAttributeXRefNew "
- "WHERE StrainId = %s AND CaseAttributeId = "
- "(SELECT CaseAttributeId FROM "
- "CaseAttribute WHERE Name = %s) "
- "AND InbredSetId = %s",
- (strain_id, case_attr, inbredset_id),
- )
+ (id_, name) = parse_csv_column(case_attr)
+ if id_:
+ cursor.execute(
+ "DELETE FROM CaseAttributeXRefNew "
+ "WHERE StrainId = %s AND CaseAttributeId = %s "
+ "AND InbredSetId = %s",
+ (strain_id, id_, inbredset_id),
+ )
+ else:
+ cursor.execute(
+ "DELETE FROM CaseAttributeXRefNew "
+ "WHERE StrainId = %s AND CaseAttributeId = "
+ "(SELECT CaseAttributeId FROM "
+ "CaseAttribute WHERE Name = %s) "
+ "AND InbredSetId = %s",
+ (strain_id, name, inbredset_id),
+ )
return cursor.rowcount
strain_id, data_id, inbredset_id = get_sample_data_ids(
diff --git a/tests/unit/db/test_sample_data.py b/tests/unit/db/test_sample_data.py
index e7d7fb0..f631239 100644
--- a/tests/unit/db/test_sample_data.py
+++ b/tests/unit/db/test_sample_data.py
@@ -90,13 +90,6 @@ def test_delete_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),
@@ -104,21 +97,21 @@ def test_delete_sample_data(mocker):
delete_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 (17)",
phenotype_id=10007,
)
calls = [
mocker.call(
- "DELETE FROM PublishData WHERE " "StrainId = %s AND Id = %s",
+ "DELETE FROM PublishData WHERE StrainId = %s AND Id = %s",
(strain_id, data_id),
),
mocker.call(
- "DELETE FROM PublishSE WHERE " "StrainId = %s AND DataId = %s",
+ "DELETE FROM PublishSE WHERE StrainId = %s AND DataId = %s",
(strain_id, data_id),
),
mocker.call(
- "DELETE FROM NStrain WHERE " "StrainId = %s AND DataId = %s",
+ "DELETE FROM NStrain WHERE StrainId = %s AND DataId = %s",
(strain_id, data_id),
),
mocker.call(
@@ -127,7 +120,13 @@ def test_delete_sample_data(mocker):
"(SELECT CaseAttributeId FROM "
"CaseAttribute WHERE Name = %s) "
"AND InbredSetId = %s",
- (strain_id, "Sex", inbredset_id),
+ (strain_id, "Color", inbredset_id),
+ ),
+ mocker.call(
+ "DELETE FROM CaseAttributeXRefNew WHERE "
+ "StrainId = %s AND CaseAttributeId = %s "
+ "AND InbredSetId = %s",
+ (strain_id, "17", inbredset_id),
),
]
cursor.execute.assert_has_calls(calls, any_order=False)