From 78f22407ee73c89eb7477e7e025c37c671037ab0 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Fri, 11 Mar 2022 11:35:34 +0300 Subject: Append the strain name when extracting "actions" * gn3/db/sample_data.py (__extract_actions): During updates, make sure that the strain name is part of the returned string when extracting "actions". * tests/unit/db/test_sample_data.py: Add test cases for the above. --- gn3/db/sample_data.py | 9 +++++++-- tests/unit/db/test_sample_data.py | 23 ++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py index e5c5e48..c697de0 100644 --- a/gn3/db/sample_data.py +++ b/gn3/db/sample_data.py @@ -22,7 +22,10 @@ def __extract_actions(original_data: str, "insert": {"data": [], "csv_header": []}, "update": {"data": [], "csv_header": []}, } + strain_name = "" for _o, _u, _h in zip(original_data, updated_data, csv_header): + if _h == "Strain Name": + strain_name = _o if _o == _u: # No change continue elif _o and _u == "x": # Deletion @@ -38,8 +41,10 @@ def __extract_actions(original_data: str, if not val["data"]: result[key] = None else: - result[key]["data"] = ",".join(result[key]["data"]) - result[key]["csv_header"] = ",".join(result[key]["csv_header"]) + result[key]["data"] = (f"{strain_name}," + + ",".join(result[key]["data"])) + result[key]["csv_header"] = ("Strain Name," + + ",".join(result[key]["csv_header"])) return result diff --git a/tests/unit/db/test_sample_data.py b/tests/unit/db/test_sample_data.py index 84ab1a1..0fa2604 100644 --- a/tests/unit/db/test_sample_data.py +++ b/tests/unit/db/test_sample_data.py @@ -84,17 +84,22 @@ def test_extract_actions(): updated_data="BXD1,x,2,1,F", csv_header="Strain Name,Value,SE,Count,Sex") == { - "delete": {"data": "18", "csv_header": "Value"}, - "insert": {"data": "2,F", "csv_header": "SE,Sex"}, - "update": {"data": "1", "csv_header": "Count"}, + "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": "2,F", "csv_header": "SE,Sex"}, - "update": {"data": "19,1", "csv_header": "Value,Count"}, + "insert": {"data": "BXD1,2,F", + "csv_header": "Strain Name,SE,Sex"}, + "update": {"data": "BXD1,19,1", + "csv_header": "Strain Name,Value,Count"}, }) @@ -119,14 +124,14 @@ def test_update_sample_data(mocker): gn3.db.sample_data.insert_sample_data.assert_called_once_with( conn=mock_conn, trait_name=35, - data="2,F", - csv_header="SE,Sex", + data="BXD1,2,F", + csv_header="Strain Name,SE,Sex", phenotype_id=10007) gn3.db.sample_data.delete_sample_data.assert_called_once_with( conn=mock_conn, trait_name=35, - data="18", - csv_header="Value", + data="BXD1,18", + csv_header="Strain Name,Value", phenotype_id=10007) cursor.execute.assert_has_calls( [mocker.call("UPDATE NStrain SET count = %s " -- cgit v1.2.3