about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBonfaceKilz2022-04-06 15:53:18 +0300
committerBonfaceKilz2022-04-07 11:54:28 +0300
commita7b7cdd1a5f1a9d071c5fd11c6f1fefa5302a838 (patch)
tree9bbbc43c05d55afcd8961a666ec4b7b936bf5de9
parent9e905e596f2b1b17ad78a86fff01aafe1cc8b108 (diff)
downloadgenenetwork3-a7b7cdd1a5f1a9d071c5fd11c6f1fefa5302a838.tar.gz
Use case attribute id inside brackets if present during updates
* gn3/db/sample_data.py: Import "parse_csv_column".
(update_sample_data): If an id is present in the column header, use it.
* tests/unit/db/test_sample_data.py (test_update_sample_data): Update tests to
capture the above.
-rw-r--r--gn3/db/sample_data.py32
-rw-r--r--tests/unit/db/test_sample_data.py27
2 files changed, 43 insertions, 16 deletions
diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py
index d7ea9d3..47be5b0 100644
--- a/gn3/db/sample_data.py
+++ b/gn3/db/sample_data.py
@@ -5,6 +5,7 @@ import collections
 import MySQLdb
 
 from gn3.csvcmp import extract_strain_name
+from gn3.csvcmp import parse_csv_column
 
 
 _MAP = {
@@ -169,16 +170,26 @@ def update_sample_data(
         conn, value, strain_id, case_attr, inbredset_id
     ):
         if value != "x":
+            (id_, name) = parse_csv_column(case_attr)
             with conn.cursor() as cursor:
-                cursor.execute(
-                    "UPDATE CaseAttributeXRefNew "
-                    "SET Value = %s "
-                    "WHERE StrainId = %s AND CaseAttributeId = "
-                    "(SELECT CaseAttributeId FROM "
-                    "CaseAttribute WHERE Name = %s) "
-                    "AND InbredSetId = %s",
-                    (value, strain_id, case_attr, inbredset_id),
-                )
+                if id_:
+                    cursor.execute(
+                        "UPDATE CaseAttributeXRefNew "
+                        "SET Value = %s "
+                        f"WHERE StrainId = %s AND CaseAttributeId = %s "
+                        "AND InbredSetId = %s",
+                        (value, strain_id, id_, inbredset_id),
+                    )
+                else:
+                    cursor.execute(
+                        "UPDATE CaseAttributeXRefNew "
+                        "SET Value = %s "
+                        "WHERE StrainId = %s AND CaseAttributeId = "
+                        "(SELECT CaseAttributeId FROM "
+                        "CaseAttribute WHERE Name = %s) "
+                        "AND InbredSetId = %s",
+                        (value, strain_id, name, inbredset_id),
+                    )
                 return cursor.rowcount
         return 0
 
@@ -202,6 +213,7 @@ def update_sample_data(
             updated_data=updated_data,
             csv_header=csv_header,
         )
+
         if __actions.get("update"):
             _csv_header = __actions["update"]["csv_header"]
             _data = __actions["update"]["data"]
@@ -214,7 +226,7 @@ def update_sample_data(
                 else:
                     count += __update_case_attribute(
                         conn=conn,
-                        value=none_case_attrs[header](value),
+                        value=value,
                         strain_id=strain_id,
                         case_attr=header,
                         inbredset_id=inbredset_id,
diff --git a/tests/unit/db/test_sample_data.py b/tests/unit/db/test_sample_data.py
index dad7169..af00393 100644
--- a/tests/unit/db/test_sample_data.py
+++ b/tests/unit/db/test_sample_data.py
@@ -154,7 +154,6 @@ 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),
@@ -164,19 +163,20 @@ def test_update_sample_data(mocker):
         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",
+            original_data="BXD1,18,x,0,x,M,x,Red",
+            updated_data="BXD1,x,2,1,2,F,2,Green",
+            csv_header="Strain Name,Value,SE,Count,pH,Sex (13),Age,Color",
             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",
+            data="BXD1,2,2,2",
+            csv_header="Strain Name,SE,pH,Age",
             phenotype_id=10007,
         )
+
         # pylint: disable=[E1101]
         gn3.db.sample_data.delete_sample_data.assert_called_once_with(
             conn=mock_conn,
@@ -185,12 +185,27 @@ def test_update_sample_data(mocker):
             csv_header="Strain Name,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),
+                ),
+                mocker.call(
+                    "UPDATE CaseAttributeXRefNew SET Value = %s "
+                    "WHERE StrainId = %s AND CaseAttributeId = %s AND "
+                    "InbredSetId = %s",
+                    ("F", strain_id, "13", inbredset_id),
+                ),
+                mocker.call(
+                    "UPDATE CaseAttributeXRefNew SET Value = %s "
+                    "WHERE StrainId = %s AND CaseAttributeId = "
+                    "(SELECT CaseAttributeId FROM "
+                    "CaseAttribute WHERE Name = %s) "
+                    "AND InbredSetId = %s",
+                    ("Green", strain_id, "Color", inbredset_id),
                 )
             ],
             any_order=False,