aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gn3/db/sample_data.py27
-rw-r--r--tests/unit/db/test_sample_data.py26
2 files changed, 53 insertions, 0 deletions
diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py
index 73fbd95..d7ea9d3 100644
--- a/gn3/db/sample_data.py
+++ b/gn3/db/sample_data.py
@@ -1,6 +1,7 @@
"""Module containing functions that work with sample data"""
from typing import Any, Tuple, Dict, Callable
+import collections
import MySQLdb
from gn3.csvcmp import extract_strain_name
@@ -394,3 +395,29 @@ def insert_sample_data(
except Exception as _e:
conn.rollback()
raise MySQLdb.Error(_e) from _e
+
+
+def get_case_attributes(conn) -> dict:
+ """Get all the case attributes as a dictionary from the database. Should there
+ exist more than one case attribute with the same name, put the id in
+ brackets."""
+ results = {}
+ with conn.cursor() as cursor:
+ cursor.execute("SELECT Id, Name, Description FROM CaseAttribute")
+ _r = cursor.fetchall()
+ _dups = [
+ item
+ for item, count in collections.Counter(
+ [name for _, name, _ in _r]
+ ).items()
+ if count > 1
+ ]
+ for _id, _name, _desc in _r:
+ _name = _name.strip()
+ _desc = _desc if _desc else ""
+ if _name in _dups:
+ results[f"{_name} ({_id})"] = _desc
+ else:
+ results[f"{_name}"] = _desc
+
+ return results
diff --git a/tests/unit/db/test_sample_data.py b/tests/unit/db/test_sample_data.py
index 607b278..dad7169 100644
--- a/tests/unit/db/test_sample_data.py
+++ b/tests/unit/db/test_sample_data.py
@@ -4,6 +4,7 @@ import gn3
from gn3.db.sample_data import __extract_actions
from gn3.db.sample_data import delete_sample_data
+from gn3.db.sample_data import get_case_attributes
from gn3.db.sample_data import insert_sample_data
from gn3.db.sample_data import update_sample_data
@@ -194,3 +195,28 @@ def test_update_sample_data(mocker):
],
any_order=False,
)
+
+
+@pytest.mark.unit_test
+def test_get_case_attributes(mocker):
+ """Test that case attributes work well"""
+ mock_conn = mocker.MagicMock()
+ with mock_conn.cursor() as cursor:
+ cursor.fetchall.return_value = (
+ (1, "Condition", None),
+ (2, "Tissue", None),
+ (3, "Age", "Cum sociis natoque penatibus et magnis dis"),
+ (4, "Condition", "Description A"),
+ (5, "Condition", "Description B"),
+ )
+ results = get_case_attributes(mock_conn)
+ cursor.execute.assert_called_once_with(
+ "SELECT Id, Name, Description FROM CaseAttribute"
+ )
+ assert results == {
+ "Condition (1)": "",
+ "Tissue": "",
+ "Age": "Cum sociis natoque penatibus et magnis dis",
+ "Condition (4)": "Description A",
+ "Condition (5)": "Description B",
+ }