Skip to content

Commit

Permalink
refactor to use unittest instead of assert directly
Browse files Browse the repository at this point in the history
  • Loading branch information
sierra-moxon committed Jul 31, 2023
1 parent 40b3bc2 commit 573e4ed
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 401 deletions.
19 changes: 15 additions & 4 deletions app/routers/bioentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@


class RelationshipType(str, Enum):

"""
Enumeration for Gene Ontology relationship types used for filtering associations.
:param INVOLVED_IN: The 'involved_in' relationship type.
:param ACTS_UPSTREAM_OF_OR_WITHIN: The 'acts_upstream_of_or_within' relationship type.
:param INVOLVED_IN_REGULATION_OF: The 'involved_in_regulation_of' relationship type.
"""

INVOLVED_IN = INVOLVED_IN
ACTS_UPSTREAM_OF_OR_WITHIN = ACTS_UPSTREAM_OF_OR_WITHIN
INVOLVED_IN_REGULATION_OF = INVOLVED_IN_REGULATION_OF
Expand Down Expand Up @@ -67,7 +76,6 @@ async def get_bioentity_by_id(
'start' determines the starting index for fetching results, and 'rows' specifies
the number of results to be retrieved per page.
"""

# special case MGI, sigh
if id.startswith("MGI:"):
id = id.replace("MGI:", "MGI:MGI:")
Expand Down Expand Up @@ -97,7 +105,8 @@ async def get_annotations_by_goterm_id(
start: int = 0,
rows: int = 100,
):
"""Returns annotations using the provided GO term.
"""
Returns annotations using the provided GO term.
Retrieves annotations based on the provided Gene Ontology (GO) term identifier.
The GO term identifier should be represented in CURIE format (e.g., GO:0044598).
Expand Down Expand Up @@ -175,7 +184,8 @@ async def get_genes_by_goterm_id(
start: int = 0,
rows: int = 100,
):
"""Returns genes annotated to the provided GO Term.
"""
Returns genes annotated to the provided GO Term.
Retrieves genes annotated to the provided Gene Ontology (GO) term. The GO term should be
represented in CURIE format (e.g., GO:0044598).
Expand Down Expand Up @@ -261,7 +271,8 @@ async def get_taxon_by_goterm_id(
start: int = 0,
rows: int = 100,
):
"""Returns taxon information for genes annotated to the provided GO term.
"""
Returns taxon information for genes annotated to the provided GO term.
Retrieves taxon information for genes annotated to the provided Gene Ontology (GO) term.
The GO term should be represented in CURIE format (e.g., GO:0044598).
Expand Down
206 changes: 115 additions & 91 deletions tests/unit/test_models_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,101 +1,125 @@
import logging

import pytest
import unittest
from fastapi.testclient import TestClient

from app.main import app

test_client = TestClient(app)

gene_ids = ["ZFIN:ZDB-GENE-980526-388", "ZFIN:ZDB-GENE-990415-8", "MGI:3588192"]
go_ids = ["GO:0008150"]
subsets = ["goslim_agr"]
shared_ancestors = [("GO:0006259", "GO:0046483")]
uris = ["http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2FGO_0008150"]

logger = logging.getLogger(__name__)


def test_gometadata_by_model_ids():
data = {"gocams": ["59a6110e00000067", "SYNGO_369"]}
response = test_client.get("/api/models/go", params=data)
assert response.status_code == 200
assert len(response.json()) == 2


def test_geneproductmetadata_by_model_ids():
data = {"gocams": ["59a6110e00000067", "SYNGO_369"]}
response = test_client.get("/api/models/gp", params=data)
assert response.status_code == 200
assert len(response.json()) == 2


def test_pubmedmetadata_by_model_ids():
data = {"gocams": ["59a6110e00000067", "SYNGO_369"]}
response = test_client.get("/api/models/pmid", params=data)
assert response.status_code == 200
assert len(response.json()) == 2


@pytest.mark.parametrize("id", gene_ids)
def test_bioenty_id_endpoints(id):
response = test_client.get(f"/api/bioentity/{id}")
assert response.status_code == 200


def test_gocam_by_model_ids():
response = test_client.get("/api/models/581e072c00000820")
assert len(response.json()) > 125
assert response.status_code == 200


def test_models_size_endpoint():
data = {
"start": "32",
"size": "10",
}
response = test_client.get("/api/models", params=data)
for record in response.json():
assert type(record.get("orcids")) == list
assert response.status_code == 200


def test_userlist():
response = test_client.get("/api/users")
assert len(response.json()) > 100
assert response.status_code == 200


def test_grouplist():
response = test_client.get("/api/groups")
assert len(response.json()) > 15
assert response.status_code == 200


def test_groups_by_name():
response = test_client.get("/api/groups/MGI")
assert len(response.json()) > 10
assert response.status_code == 200


def test_get_modelid_by_pmid():
response = test_client.get("/api/pmid/15314168/models")
assert len(response.json()) == 1
assert response.status_code == 200


def test_get_go_term_detail_by_go_id():
response = test_client.get("/api/go/GO_0008150")
assert "goid" in response.json()
assert "label" in response.json()
assert response.json().get("goid") == "http://purl.obolibrary.org/obo/GO_0008150"
assert response.json().get("label") == "biological_process"
assert type(response.json()) == dict
assert response.status_code == 200

class TestApp(unittest.TestCase):

def test_gometadata_by_model_ids(self):
"""
Test the endpoint to retrieve GO metadata by model IDs.
"""
data = {"gocams": ["59a6110e00000067", "SYNGO_369"]}
response = test_client.get("/api/models/go", params=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 2)

def test_geneproductmetadata_by_model_ids(self):
"""
Test the endpoint to retrieve gene product metadata by model IDs.
"""
data = {"gocams": ["59a6110e00000067", "SYNGO_369"]}
response = test_client.get("/api/models/gp", params=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 2)

def test_pubmedmetadata_by_model_ids(self):
"""
Test the endpoint to retrieve PubMed metadata by model IDs.
"""
data = {"gocams": ["59a6110e00000067", "SYNGO_369"]}
response = test_client.get("/api/models/pmid", params=data)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()), 2)

def test_bioenty_id_endpoints(self):
"""
Test the bioentity endpoint for each given ID.
"""
for id in gene_ids:
response = test_client.get(f"/api/bioentity/{id}")
self.assertEqual(response.status_code, 200)

def test_gocam_by_model_ids(self):
"""
Test the endpoint to retrieve GO-CAMs by model IDs.
"""
response = test_client.get("/api/models/581e072c00000820")
self.assertGreater(len(response.json()), 125)
self.assertEqual(response.status_code, 200)

def test_models_size_endpoint(self):
"""
Test the endpoint to retrieve models with specified size.
"""
data = {
"start": "32",
"size": "10",
}
response = test_client.get("/api/models", params=data)
for record in response.json():
self.assertIsInstance(record.get("orcids"), list)
self.assertEqual(response.status_code, 200)

def test_userlist(self):
"""
Test the endpoint to retrieve the list of users.
"""
response = test_client.get("/api/users")
self.assertGreater(len(response.json()), 100)
self.assertEqual(response.status_code, 200)

def test_grouplist(self):
"""
Test the endpoint to retrieve the list of groups.
"""
response = test_client.get("/api/groups")
self.assertGreater(len(response.json()), 15)
self.assertEqual(response.status_code, 200)

def test_groups_by_name(self):
"""
Test the endpoint to retrieve groups by name.
"""
response = test_client.get("/api/groups/MGI")
self.assertGreater(len(response.json()), 10)
self.assertEqual(response.status_code, 200)

def test_get_modelid_by_pmid(self):
"""
Test the endpoint to retrieve model IDs by PubMed ID.
"""
response = test_client.get("/api/pmid/15314168/models")
self.assertEqual(len(response.json()), 1)
self.assertEqual(response.status_code, 200)

def test_get_go_term_detail_by_go_id(self):
"""
Test the endpoint to retrieve GO term details by GO ID.
"""
response = test_client.get("/api/go/GO_0008150")
self.assertIn("goid", response.json())
self.assertIn("label", response.json())
self.assertEqual(response.json()["goid"], "http://purl.obolibrary.org/obo/GO_0008150")
self.assertEqual(response.json()["label"], "biological_process")
self.assertIsInstance(response.json(), dict)
self.assertEqual(response.status_code, 200)

def test_get_gocam_models_by_go_id(self):
"""
Test the endpoint to retrieve GO-CAM models by GO ID.
"""
id = "GO:0008150"
response = test_client.get(f"/api/go/{id}/models")
self.assertGreater(len(response.json()), 100)
self.assertEqual(response.status_code, 200)


if __name__ == '__main__':
unittest.main()

def test_get_gocam_models_by_go_id():
id = "GO:0008150"
response = test_client.get(f"/api/go/{id}/models")
assert len(response.json()) > 100
assert response.status_code == 200
Loading

0 comments on commit 573e4ed

Please sign in to comment.