From dc71058e894c9857de68c7844bfc4b813c0532da Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Wed, 6 Apr 2022 16:25:23 +0300 Subject: Use case attribute id inside brackets if present during insertions * gn3/db/sample_data.py (insert_sample_data): If an id is present in the column header, use it. * tests/unit/db/test_sample_data.py (test_insert_sample_data): Update tests to capture the above. --- gn3/db/sample_data.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'gn3/db') 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 -- cgit v1.2.3