Skip to content

Commit

Permalink
Fix remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bergsalex committed Nov 15, 2024
1 parent 5ec3829 commit 57de6d7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 40 deletions.
1 change: 1 addition & 0 deletions tests/controllers/test_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
from unittest.mock import patch

from jax.apiutils import Response

from tests.data import test_gene_homolog_data, test_gene_mapping_data, test_genes_data
Expand Down
52 changes: 31 additions & 21 deletions tests/controllers/test_genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_get_geneset_response(mock_get_genenset, client):

response = client.get("/api/genesets/1234")
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")
assert response.json()["object"] == geneset_by_id_resp.get("geneset")


@patch("geneweaver.api.services.geneset.get_geneset")
Expand All @@ -33,7 +33,7 @@ def test_get_geneset_in_threshold_response(mock_get_genenset, client):

response = client.get("/api/genesets/1234?in_threshold=True")
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")
assert response.json()["object"] == geneset_by_id_resp.get("geneset")


@patch("geneweaver.api.services.geneset.get_geneset")
Expand All @@ -56,8 +56,8 @@ def test_get_geneset_w_gene_id_type(mock_service_get_geneset_w_gene_id_type, cli
mock_service_get_geneset_w_gene_id_type.return_value = geneset_w_gene_id_type_resp
response = client.get("/api/genesets/1234?gene_id_type=2")

assert response.json() == geneset_w_gene_id_type_resp
assert response.status_code == 200
assert response.json()["object"] == geneset_w_gene_id_type_resp


@patch("geneweaver.api.services.geneset.get_geneset_w_gene_id_type")
Expand Down Expand Up @@ -97,11 +97,11 @@ def test_export_geneset_errors(mock_get_geneset, client):
@patch("geneweaver.api.services.geneset.get_geneset_metadata")
def test_get_geneset_metadata(mock_get_genenset, client):
"""Test get geneset metadata."""
mock_get_genenset.return_value = geneset_by_id_resp.get("geneset")
mock_get_genenset.return_value = {"object": geneset_by_id_resp.get("geneset")}
response = client.get("/api/genesets/1234/metadata")

assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")
assert response.json()["object"] == geneset_by_id_resp.get("geneset")


@patch("geneweaver.api.services.geneset.get_geneset_metadata")
Expand All @@ -124,11 +124,11 @@ def test_get_geneset_metadata_errors(mock_get_geneset_metadata, client):
@patch("geneweaver.api.services.geneset.get_geneset_metadata")
def test_get_geneset_metadata_w_publication(mock_get_genenset, client):
"""Test get geneset ID data response."""
mock_get_genenset.return_value = geneset_metadata_w_pub_info
mock_get_genenset.return_value = {"object": geneset_metadata_w_pub_info["geneset"]}

response = client.get("/api/genesets/1234/metadata?include_pub_info=true")
assert response.status_code == 200
assert response.json() == geneset_metadata_w_pub_info
assert response.json()["object"] == geneset_metadata_w_pub_info["geneset"]


@patch("geneweaver.api.services.geneset.get_geneset_metadata")
Expand All @@ -137,14 +137,16 @@ def test_publication_for_geneset(
mock_pub_service_call, mock_get_genenset_metadata, client
):
"""Test valid url request to get publication for a geneset."""
mock_get_genenset_metadata.return_value = geneset_metadata_w_pub_info
mock_get_genenset_metadata.return_value = {
"object": geneset_metadata_w_pub_info["geneset"]
}

mock_pub_service_call.return_value = publication_by_id_resp

response = client.get("/api/genesets/1234/publication")

assert response.status_code == 200
assert response.json() == publication_by_id_resp
assert response.json()["object"] == publication_by_id_resp


@patch("geneweaver.api.services.geneset.get_geneset_metadata")
Expand Down Expand Up @@ -189,11 +191,13 @@ def test_get_publication_errors(mock_get_geneset_metadata, client):
@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_get_visible_geneset_response(mock_get_visible_genesets, client):
"""Test get geneset ID data response."""
mock_get_visible_genesets.return_value = geneset_by_id_resp.get("geneset")
mock_get_visible_genesets.return_value = {
"data": [geneset_by_id_resp.get("geneset")]
}

response = client.get("/api/genesets?gs_id=1234")
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")
assert response.json()["data"][0] == geneset_by_id_resp.get("geneset")


@patch("geneweaver.api.services.geneset.get_visible_genesets")
Expand All @@ -220,7 +224,7 @@ def test_get_geneset_gene_values_url_response(mock_get_geneset_gene_values, clie

response = client.get("/api/genesets/1234/values")
assert response.status_code == 200
assert response.json() == geneset_genes_values_resp
assert response.json()["data"] == geneset_genes_values_resp["data"]


@patch("geneweaver.api.services.geneset.get_geneset_gene_values")
Expand Down Expand Up @@ -249,7 +253,7 @@ def test_get_geneset_gene_values_w_gs_id_type(mock_get_geneset_gene_values, clie

response = client.get("/api/genesets/1234/values?gene_id_type=2")
assert response.status_code == 200
assert response.json() == geneset_genes_values_resp
assert response.json()["data"] == geneset_genes_values_resp["data"]


@patch("geneweaver.api.services.geneset.get_geneset_gene_values")
Expand All @@ -261,7 +265,7 @@ def test_get_geneset_gene_values_w_gs_id_type_and_in_threshold(

response = client.get("/api/genesets/1234/values?gene_id_type=2&in_threshold=True")
assert response.status_code == 200
assert response.json() == geneset_genes_values_resp
assert response.json()["data"] == geneset_genes_values_resp["data"]


@patch("geneweaver.api.services.geneset.update_geneset_threshold")
Expand Down Expand Up @@ -297,7 +301,7 @@ def test_get_geneset_ontology_terms_response(mock_get_genenset_onto_terms, clien

response = client.get("/api/genesets/1234/ontologies")
assert response.status_code == 200
assert response.json() == mock_resp
assert response.json()["data"] == mock_resp["data"]


@patch("geneweaver.api.services.geneset.get_geneset_ontology_terms")
Expand Down Expand Up @@ -418,11 +422,13 @@ def test_delete_geneset_ontology_terms_errors(mock_delete_genenset_onto_terms, c
@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_get_geneset_by_score_type(mock_get_visible_genesets, score_type, client):
"""Test get geneset data response."""
mock_get_visible_genesets.return_value = geneset_by_id_resp.get("geneset")
mock_get_visible_genesets.return_value = {
"data": [geneset_by_id_resp.get("geneset")]
}

response = client.get("/api/genesets?" + score_type)
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")
assert response.json()["data"][0] == geneset_by_id_resp.get("geneset")


@pytest.mark.parametrize(
Expand All @@ -443,25 +449,29 @@ def test_get_geneset_by_invalid_score_type(
@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_get_geneset_by_create_date(mock_get_visible_genesets, client):
"""Test get geneset data response."""
mock_get_visible_genesets.return_value = geneset_by_id_resp.get("geneset")
mock_get_visible_genesets.return_value = {
"data": [geneset_by_id_resp.get("geneset")]
}

response = client.get(
"/api/genesets?created_after=2023-08-01&created_before=2024-07-01"
)
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")
assert response.json().get("data")[0] == geneset_by_id_resp.get("geneset")


@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_get_geneset_by_update_date(mock_get_visible_genesets, client):
"""Test get geneset data response."""
mock_get_visible_genesets.return_value = geneset_by_id_resp.get("geneset")
mock_get_visible_genesets.return_value = {
"data": [geneset_by_id_resp.get("geneset")]
}

response = client.get(
"/api/genesets?updated_after=2023-08-01&updated_before=2024-07-01"
)
assert response.status_code == 200
assert response.json() == geneset_by_id_resp.get("geneset")
assert response.json().get("data")[0] == geneset_by_id_resp.get("geneset")


@pytest.mark.parametrize("created_before", ["20-23-20", "08-01-2023", "80/01/2022"])
Expand Down
2 changes: 1 addition & 1 deletion tests/controllers/test_publications.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ def test_get_req(mock_pub_service_call, client):
)

assert response.status_code == 200
assert response.json().get('data') == get_publications.get('data')
assert response.json().get("data") == get_publications.get("data")
20 changes: 10 additions & 10 deletions tests/controllers/test_return_schemas.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Test that all GET endpoints return one of the standard responses."""

import asyncio
from typing import get_args, get_origin, get_type_hints

from fastapi import FastAPI
from fastapi.routing import APIRoute
from jax.apiutils import CollectionResponse, StreamingResponse, Response
from jax.apiutils import CollectionResponse, Response, StreamingResponse


def get_return_type(route: APIRoute) -> type:
Expand All @@ -22,14 +24,18 @@ def get_return_type(route: APIRoute) -> type:


def is_valid_return_type(typ: type) -> bool:
"""Check if a type is either Response, CollectionResponse, or an Optional/Union containing them.
"""Check if a type is one of the standard response types.
Check if a type is either Response, CollectionResponse, StreamingResponse,
or an Optional/Union containing them.
Also handles async return types.
"""
# Handle Optional/Union types
origin = get_origin(typ)
if origin is not None:
# If it's a Union/Optional, check all possible types
if origin in (Union, types.UnionType): # type: ignore
if origin in (Union, types.UnionType): # noqa: F821
return any(is_valid_return_type(arg) for arg in get_args(typ))
# Handle async return types
if asyncio.iscoroutinefunction(origin):
Expand All @@ -46,13 +52,7 @@ def is_valid_return_type(typ: type) -> bool:


def test_get_endpoint_return_types(app: FastAPI):
"""Test that all GET endpoints return either Response or CollectionResponse.
Args:
----
app: Your FastAPI application instance
"""
"""Test that all GET endpoints return either Response or CollectionResponse."""
invalid_routes = []

for route in app.routes:
Expand Down
4 changes: 3 additions & 1 deletion tests/controllers/test_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ def test_valid_url_species_by_id(mock_species_service_call, client):
response = client.get(url="/api/species/5")

assert response.status_code == 200
_validate_species_response(response.json().get("object"), species_by_gene_id_type_flybase)
_validate_species_response(
response.json().get("object"), species_by_gene_id_type_flybase
)
14 changes: 7 additions & 7 deletions tests/services/test_genset.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,16 @@ def test_get_geneset_metadata(mock_db_geneset):
mock_db_geneset.get.return_value = [geneset_by_id_resp.get("geneset")]

response = geneset.get_geneset_metadata(None, 1234, mock_user)
assert "geneset" in response
assert response.get("geneset") == geneset_by_id_resp["geneset"]
assert "object" in response
assert response.get("object") == geneset_by_id_resp["geneset"]

response = geneset.get_geneset_metadata(None, 1234, None)
assert "geneset" in response
assert response.get("geneset") == geneset_by_id_resp["geneset"]
assert "object" in response
assert response.get("object") == geneset_by_id_resp["geneset"]

response = geneset.get_geneset_metadata(None, 1234, User(id=None))
assert "geneset" in response
assert response.get("geneset") == geneset_by_id_resp["geneset"]
assert "object" in response
assert response.get("object") == geneset_by_id_resp["geneset"]


@patch("geneweaver.api.services.geneset.db_geneset")
Expand All @@ -189,7 +189,7 @@ def test_get_geneset_metadata_w_pub_info(mock_db_geneset):
mock_db_geneset.get.return_value = [geneset_metadata_w_pub_info]
response = geneset.get_geneset_metadata(None, 1234, mock_user, True)

assert response.get("geneset") == geneset_metadata_w_pub_info
assert response.get("object") == geneset_metadata_w_pub_info


@patch("geneweaver.api.services.geneset.db_geneset")
Expand Down

0 comments on commit 57de6d7

Please sign in to comment.