about summary refs log tree commit diff
path: root/gn3/db
diff options
context:
space:
mode:
authorBonfaceKilz2022-04-05 15:12:07 +0300
committerBonfaceKilz2022-04-07 11:54:28 +0300
commit9e905e596f2b1b17ad78a86fff01aafe1cc8b108 (patch)
tree73a5c7ac71aa93dafae752f06fb04d79a92fe2e4 /gn3/db
parentfdae7b15c4f5f29a16a55713937d49d7331cdfad (diff)
downloadgenenetwork3-9e905e596f2b1b17ad78a86fff01aafe1cc8b108.tar.gz
Add method for fetching the case_attributes
* gn3/db/sample_data.py (get_case_attributes): New function.
* tests/unit/db/test_sample_data.py (test_get_case_attributes): Test case for
the above.
Diffstat (limited to 'gn3/db')
-rw-r--r--gn3/db/sample_data.py27
1 files changed, 27 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