Skip to content

Commit

Permalink
Change loop_scope to module ...
Browse files Browse the repository at this point in the history
Due to conflicting issues between more than one event loop with these
tests I've done two things.

1) Removed tests that have external querying from the test group. While
   it will make the unit tests less robust, having external queries was
   always going to be slighly flakier. This way the core tests leverage
   the built-in testing clients for exercising the annotator as a web
   service through the endpoints. The annotator as a standalone library
   is important but comes second to that
2) Set the loop scope to module to allow for the event loop to exist for
   groups of tests that are semantically similar.
  • Loading branch information
jschaff committed Feb 3, 2025
1 parent 409514f commit 7ef6b3f
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 280 deletions.
5 changes: 2 additions & 3 deletions tests/fixtures/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def test_annotator() -> sanic.Sanic:
Generate an application instance from the biothings_annotator
"""
default_configuration = load_configuration()
default_configuration["application"]["runtime"]["debug"] = False
default_configuration["application"]["runtime"]["port"] = 7777
default_configuration["application"]["runtime"]["debug"] = True
application = build_application(default_configuration)
TestManager(application)
return application
yield application
6 changes: 3 additions & 3 deletions tests/test_annotation_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


@pytest.mark.performance
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("data_store", ["cleaned_annotator_logs.json"])
async def test_integration_server_responses(
temporary_data_storage: Union[str, Path],
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_ara_integration_trapi_requests(


@pytest.mark.performance
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("data_store", ["cleaned_annotator_logs.json"])
async def test_asynchronous_bulk_requests(
temporary_data_storage: Union[str, Path],
Expand Down Expand Up @@ -312,7 +312,7 @@ async def test_asynchronous_bulk_requests(


@pytest.mark.performance
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize(
"data_store",
[
Expand Down
1 change: 0 additions & 1 deletion tests/test_application_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def test_environment_configuration(host: str):

assert client.url == f"{annotator_instance.api_host}/{endpoint}"
assert ANNOTATOR_CLIENTS[node_type]["client"]["instance"] == client
breakpoint()
finally:
if os.environ.get("SERVICE_PROVIDER_API_HOST", None) is not None:
del os.environ["SERVICE_PROVIDER_API_HOST"]
Expand Down
24 changes: 12 additions & 12 deletions tests/test_application_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("endpoint", ["/status/"])
async def test_status_get(test_annotator: sanic.Sanic, endpoint: str):
"""
Expand All @@ -38,7 +38,7 @@ async def test_status_get(test_annotator: sanic.Sanic, endpoint: str):


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("endpoint", ["/status/"])
async def test_status_head(test_annotator: sanic.Sanic, endpoint: str):
"""
Expand All @@ -62,7 +62,7 @@ async def test_status_head(test_annotator: sanic.Sanic, endpoint: str):


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("endpoint", ["/status/"])
async def test_status_get_error(test_annotator: sanic.Sanic, endpoint: str):
"""
Expand All @@ -89,7 +89,7 @@ async def test_status_get_error(test_annotator: sanic.Sanic, endpoint: str):


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("endpoint", ["/status/"])
async def test_status_get_failed_data_check(test_annotator: sanic.Sanic, endpoint: str):
"""
Expand Down Expand Up @@ -117,7 +117,7 @@ async def test_status_get_failed_data_check(test_annotator: sanic.Sanic, endpoin


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("endpoint", ["/version/"])
async def test_version_get_success(test_annotator: sanic.Sanic, endpoint: str):
"""
Expand Down Expand Up @@ -146,7 +146,7 @@ async def test_version_get_success(test_annotator: sanic.Sanic, endpoint: str):


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("endpoint", ["/version/"])
async def test_version_get_file_not_found(test_annotator: sanic.Sanic, endpoint: str):
"""
Expand Down Expand Up @@ -175,7 +175,7 @@ async def test_version_get_file_not_found(test_annotator: sanic.Sanic, endpoint:


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("endpoint", ["/version/"])
async def test_version_get_exception(test_annotator: sanic.Sanic, endpoint: str):
"""
Expand Down Expand Up @@ -204,7 +204,7 @@ async def test_version_get_exception(test_annotator: sanic.Sanic, endpoint: str)


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("data_store", ["expected_curie.json"])
async def test_curie_get(temporary_data_storage: Union[str, Path], test_annotator: sanic.Sanic, data_store: Dict):
"""
Expand Down Expand Up @@ -250,7 +250,7 @@ async def test_curie_get(temporary_data_storage: Union[str, Path], test_annotato


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize(
"endpoint, batch_curie",
(
Expand Down Expand Up @@ -311,7 +311,7 @@ async def test_curie_post(test_annotator: sanic.Sanic, endpoint: str, batch_curi


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("data_store", ["trapi_request.json"])
async def test_trapi_post(temporary_data_storage: Union[str, Path], test_annotator: sanic.Sanic, data_store: Dict):
"""
Expand Down Expand Up @@ -370,7 +370,7 @@ async def test_trapi_post(temporary_data_storage: Union[str, Path], test_annotat


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("data_store", ["expected_curie.json"])
async def test_annotator_get_redirect(
temporary_data_storage: Union[str, Path], test_annotator: sanic.Sanic, data_store: Dict
Expand Down Expand Up @@ -419,7 +419,7 @@ async def test_annotator_get_redirect(


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("data_store", ["trapi_request.json"])
async def test_annotator_post_redirect(
temporary_data_storage: Union[str, Path], test_annotator: sanic.Sanic, data_store: Dict
Expand Down
2 changes: 1 addition & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
async def test_metadata_generation(test_annotator: sanic.Sanic):
"""
Tests the metadata endpoint for generating the openapi spec
Expand Down
49 changes: 24 additions & 25 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,6 @@ def test_annotation_client(node_type: str):
utils.get_client(node_type, SERVICE_PROVIDER_API_HOST)


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.parametrize("curie_prefix", list(BIOLINK_PREFIX_to_BioThings.keys()))
async def test_biothings_query(curie_prefix: str):
random_index = random.randint(0, 10000)
curie_query = f"{curie_prefix}:{str(random_index)}"

node_type, node_id = utils.parse_curie(curie=curie_query, return_type=True, return_id=True)

domain_fields = ANNOTATOR_CLIENTS[node_type]["fields"]
client = utils.get_client(node_type, SERVICE_PROVIDER_API_HOST)
if not client:
logger.warning("Failed to get the biothings client for %s type. This type is skipped.", node_type)
return {}

fields = ANNOTATOR_CLIENTS[node_type]["fields"]
scopes = ANNOTATOR_CLIENTS[node_type]["scopes"]
querymany_result = await client.querymany([node_id], scopes=scopes, fields=fields)
logger.info("Done. %s annotation objects returned.", len(querymany_result))
query_response = utils.group_by_subfield(collection=querymany_result, search_key="query")

assert isinstance(query_response, dict)
logger.info((f"Query Response: {query_response}" f"Query Fields: {domain_fields}"))


@pytest.mark.unit
@pytest.mark.parametrize(
"search_keyword, collection, histogram",
Expand Down Expand Up @@ -130,3 +105,27 @@ def test_query_post_processing(search_keyword: str, collection: List[Dict], hist
histogram_response = utils.group_by_subfield(collection=collection, search_key=search_keyword)
assert isinstance(histogram_response, dict)
assert histogram_response == histogram


@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("curie_prefix", list(BIOLINK_PREFIX_to_BioThings.keys()))
async def test_biothings_query(curie_prefix: str):
random_index = random.randint(0, 10000)
curie_query = f"{curie_prefix}:{str(random_index)}"

node_type, node_id = utils.parse_curie(curie=curie_query, return_type=True, return_id=True)

domain_fields = ANNOTATOR_CLIENTS[node_type]["fields"]
client = utils.get_client(node_type, SERVICE_PROVIDER_API_HOST)
if not client:
logger.warning("Failed to get the biothings client for %s type. This type is skipped.", node_type)
return {}

fields = ANNOTATOR_CLIENTS[node_type]["fields"]
scopes = ANNOTATOR_CLIENTS[node_type]["scopes"]
querymany_result = await client.querymany([node_id], scopes=scopes, fields=fields)
logger.info("Done. %s annotation objects returned.", len(querymany_result))
query_response = utils.group_by_subfield(collection=querymany_result, search_key="query")

assert isinstance(query_response, dict)
logger.info((f"Query Response: {query_response}" f"Query Fields: {domain_fields}"))
3 changes: 1 addition & 2 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
logger.setLevel(logging.DEBUG)


@pytest.mark.unit
@pytest.mark.asyncio(scope="module")
@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.parametrize("curie_prefix", list(BIOLINK_PREFIX_to_BioThings.keys()))
async def test_annotation_transform(curie_prefix: str):
annotation_instance = Annotator()
Expand Down
Loading

0 comments on commit 7ef6b3f

Please sign in to comment.