diff --git a/Makefile b/Makefile index 41f538e..07c49ca 100644 --- a/Makefile +++ b/Makefile @@ -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)) @@ -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 ################################################### diff --git a/test_app/conftest.py b/test_app/conftest.py index 0101966..2c48927 100644 --- a/test_app/conftest.py +++ b/test_app/conftest.py @@ -6,6 +6,7 @@ import os import typing from typing import Optional +from platformics.api.core.error_handler import HandleErrors import boto3 import pytest @@ -38,8 +39,6 @@ from api.queries import Query import strawberry -from main import app - __all__ = [ "gql_client", "moto_client", @@ -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: @@ -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