diff --git a/tests/test_statistics.py b/tests/test_statistics.py index 4d0eb1a9..7c4806cf 100644 --- a/tests/test_statistics.py +++ b/tests/test_statistics.py @@ -53,9 +53,21 @@ def test_my_variants(_demo_client): resp = _demo_client.get("/my_variants?limit=10000") assert len(resp.json) == 4099 assert "'variant_id': [{'display': '14-95236097-C-A'" in str(resp.json) + resp = _demo_client.get("/my_variants?limit=100001") + assert resp.status_code == 400 + assert resp.json == {"message": "The maximum page size for variants is 100000"} def test_my_genes(_demo_client): resp = _demo_client.get("/my_genes") assert len(resp.json) == 3 assert "'percentage_gene_gc_content': 49.73" in str(resp.json) + + +def test_my_hpos(_demo_client): + resp = _demo_client.get("/my_hpos") + assert len(resp.json) == 7 + assert "'Abnormal retinal morphology'" in str(resp.json) + resp = _demo_client.get("/my_hpos?limit=100001") + assert resp.status_code == 400 + assert resp.json == {"message": "The maximum page size for variants is 100000"} diff --git a/tests/test_users.py b/tests/test_users.py index 42c70766..5776debe 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -102,6 +102,9 @@ def test_enable_user(_admin): assert status == 400 assert not response.json.get("success") assert response.json.get("message") == "Cannot change the status of Admin user!" + response, status = enable_user("abcdefxyz", "True") + assert status == 404 + assert response.json.get("message") == "User not found" def test_bad_attempt_to_disable_user(_admin): diff --git a/tests/test_variants.py b/tests/test_variants.py index bbaa8235..9efe6224 100644 --- a/tests/test_variants.py +++ b/tests/test_variants.py @@ -1,7 +1,7 @@ import sys from io import StringIO from views.variant import _get_genotypes # noqa: F401 -from views.variant import variant, variant_preview +from views.variant import variant, variant_preview, _get_variants def test_get_genotypes_exception(): @@ -67,3 +67,8 @@ def test_variant_preview(_demo): def test_wrong_variant_preview(_demo): response = variant_preview("something-else") assert response.status_code == 400 + + +def test_get_variants(_demo): + response = _get_variants("wrong") + assert not response diff --git a/views/hpo.py b/views/hpo.py index e619393a..ae813150 100644 --- a/views/hpo.py +++ b/views/hpo.py @@ -3,10 +3,12 @@ """ from flask import session, jsonify from psycopg2 import sql -from views import application +from views import MAX_PAGE_SIZE, application from views.auth import requires_auth, USER +from views.exceptions import PhenopolisException +from views.individual import _get_authorized_individuals from views.postgres import get_db, session_scope -from views.general import process_for_display, cache_on_browser +from views.general import _get_pagination_parameters, process_for_display, cache_on_browser from db.helpers import cursor2dict, query_user_config @@ -174,3 +176,33 @@ def _preview(cur, user, hpo_id): q = q1 + q2 cur.execute(q, [user]) return cur.rowcount + + +@application.route("/my_hpos") +@requires_auth +def get_all_hpos(): + with session_scope() as db_session: + individuals = _get_authorized_individuals(db_session) + sqlq_all_hpos = sql.SQL( + """ + select distinct t.hpo_id, t."name" from phenopolis.individual_feature ife + join hpo.term t on t.id = ife.feature_id + where ife.individual_id = any(%s) and ife.type in ('observed') + """ + ) + try: + limit, offset = _get_pagination_parameters() + if limit > MAX_PAGE_SIZE: + return ( + jsonify(message="The maximum page size for variants is {}".format(MAX_PAGE_SIZE)), + 400, + ) + sqlq = sqlq_all_hpos + sql.SQL("limit {} offset {}".format(limit, offset)) + with get_db() as conn: + with conn.cursor() as cur: + cur.execute(sqlq, [[x.id for x in individuals]]) + hpos = cursor2dict(cur) + process_for_display(db_session, hpos) + except PhenopolisException as e: + return jsonify(success=False, message=str(e)), e.http_status + return jsonify(hpos), 200 diff --git a/views/variant.py b/views/variant.py index 2d56cc45..2eea8c00 100644 --- a/views/variant.py +++ b/views/variant.py @@ -75,7 +75,7 @@ def _get_variants(target: str): * variant_id (e.g '14-76156575-A-G') - and it will return the variant(s) dict for it '12-7241974-C-T', e.g., returns 2 dicts because of 'phenopolis.individual_variant' * gene_id (e.g. 'ENSG00000144285') - and it will return all variants linked to that gene - * phenopolis_id (e.g. 'PH00008256') - aand it will return all variants linked to that patient + * phenopolis_id (e.g. 'PH00008256') - and it will return all variants linked to that patient input 'target' must obey its respective string format. Returns: List[dict variant]: empty ([]), one or more variants depending on input target