Skip to content

Commit

Permalink
Fix error test. (#62)
Browse files Browse the repository at this point in the history
* Fix error test.
  • Loading branch information
jgadling committed Jun 21, 2024
1 parent 1026670 commit 9d77c58
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ help: ## display help for this makefile
.PHONY: codegen
codegen: build-base ## Run codegen to convert the LinkML schema to a GQL API
$(docker_compose_run) $(BUILD_CONTAINER) api generate --schemafile ./schema/schema.yaml --output-prefix .
$(docker_compose_run) $(CONTAINER) black .
#$(docker_compose_run) $(CONTAINER) ruff check --fix .

.PHONY: rm-pycache
rm-pycache: ## remove all __pycache__ files (run if encountering issues with pycharm debugger (containers exiting prematurely))
Expand Down Expand Up @@ -56,9 +58,6 @@ lint: ## Check for / fix bad linting
.PHONY: codegen-tests
codegen-tests: codegen ## Run tests
$(docker_compose) up -d
$(docker_compose_run) -v platformics api generate --schemafile /app/schema/test_app.yaml --output-prefix /app
$(docker_compose_run) $(CONTAINER) black .
$(docker_compose_run) $(CONTAINER) ruff check --fix .
$(docker_compose_run) $(CONTAINER) pytest

### GitHub Actions ###################################################
Expand Down
24 changes: 14 additions & 10 deletions test_app/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import typing
from typing import Optional
from platformics.api.core.error_handler import HandleErrors

import boto3
import pytest
Expand Down Expand Up @@ -38,8 +39,6 @@
from api.queries import Query
import strawberry

from main import app

__all__ = [
"gql_client",
"moto_client",
Expand Down Expand Up @@ -119,12 +118,6 @@ async def async_db(sync_db: SyncDB, test_db: NoopExecutor) -> typing.AsyncGenera
yield db


@pytest_asyncio.fixture()
async def api_test_schema(async_db: AsyncDB) -> FastAPI:
overwrite_api(app, async_db)
return app


# When importing `gql_client`, it will use the `http_client` below, which uses the test schema
@pytest_asyncio.fixture()
async def http_client(api_test_schema: FastAPI) -> AsyncClient:
Expand Down Expand Up @@ -235,14 +228,25 @@ async def patched_session() -> typing.AsyncGenerator[AsyncSession, None]:
api.dependency_overrides[get_s3_client] = patched_s3_client


def raise_exception() -> str:
raise Exception("Unexpected error")

# Subclass Query with an additional field to test Exception handling.
@strawberry.type
class MyQuery(Query):
@strawberry.field
def uncaught_exception(self) -> str:
# Trigger an AttributeException
return self.kaboom # type: ignore

@pytest_asyncio.fixture()
async def api(async_db: AsyncDB) -> FastAPI:
async def api_test_schema(async_db: AsyncDB) -> FastAPI:
"""
Create an API instance using the real schema.
"""
settings = APISettings.model_validate({}) # Workaround for https://github.com/pydantic/pydantic/issues/3753
strawberry_config = get_strawberry_config()
schema = strawberry.Schema(query=Query, mutation=Mutation, config=strawberry_config)
schema = strawberry.Schema(query=MyQuery, mutation=Mutation, config=strawberry_config, extensions=[HandleErrors()])
api = get_app(settings, schema, models)
overwrite_api(api, async_db)
return api
2 changes: 1 addition & 1 deletion test_app/tests/test_test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ async def test_graphql_query(gql_client: GQLTestClient, api_test_schema: FastAPI
"""
output = await gql_client.query(query)
assert output["data"] is None
assert output["errors"][0]["message"] == "Cannot query field 'referenceGenomes' on type 'Query'."
assert output["errors"][0]["message"].startswith("Cannot query field 'referenceGenomes' on type")

0 comments on commit 9d77c58

Please sign in to comment.