Skip to content
Closed
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
1 change: 1 addition & 0 deletions stapi-fastapi/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies = [
"pyrfc3339>=1.1",
"types-pyRFC3339>=1.1.1",
"uvicorn>=0.29.0",
"schemathesis==3.37.0",
]

[tool.hatch.build.targets.sdist]
Expand Down
31 changes: 31 additions & 0 deletions stapi-fastapi/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from urllib.parse import urljoin

import pytest
import schemathesis
from fastapi import FastAPI
from fastapi.testclient import TestClient
from stapi_fastapi.models.product import (
Expand Down Expand Up @@ -182,3 +183,33 @@ def opportunity_search(limit) -> dict[str, Any]:
},
"limit": limit,
}

@pytest.fixture
def stapi_app(
mock_products: list[Product],
base_url: str,
mock_opportunities: list[Opportunity],
):
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[dict[str, Any]]:
try:
yield {
"_orders_db": InMemoryOrderDB(),
"_opportunities": mock_opportunities,
}
finally:
pass

root_router = RootRouter(
get_orders=mock_get_orders,
get_order=mock_get_order,
get_order_statuses=mock_get_order_statuses,
conformances=[CORE],
)

for mock_product in mock_products:
root_router.add_product(mock_product)

app = FastAPI(lifespan=lifespan)
app.include_router(root_router, prefix="")
return schemathesis.from_dict(app.openapi())
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json

import warnings
import pytest
import schemathesis
from schemathesis.checks import (
Expand All @@ -14,26 +14,24 @@
schemathesis.experimental.OPEN_API_3_1.enable()

SCHEMA_URL = "https://raw.githubusercontent.com/stapi-spec/stapi-spec/refs/heads/main/openapi.yaml"
schema = schemathesis.from_uri(SCHEMA_URL)

BASE_URL = "http://localhost:8000"

# Create a test schema from your ASGI app
schema = schemathesis.from_pytest_fixture("stapi_app")

@schema.parametrize()
def test_api(case):
response = case.call_and_validate(base_url=BASE_URL)
case.validate_response(response)
case.validate_response()

not_a_server_error(response, case)
status_code_conformance(response, case)
content_type_conformance(response, case)
response_schema_conformance(response, case)
response_headers_conformance(response, case)
negative_data_rejection(response, case)
not_a_server_error(case.response, case)
status_code_conformance(case.response, case)
content_type_conformance(case.response, case)
response_schema_conformance(case.response, case)
response_headers_conformance(case.response, case)
negative_data_rejection(case.response, case)


def test_openapi_specification():
assert schema.validate()
assert schema.validate_schema


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
Expand Down
14 changes: 8 additions & 6 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading