Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error test. #62

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading