-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from peplxx/main
[Feature] Custom Exception Classes Recognition
- Loading branch information
Showing
8 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
test: | ||
name: Run tests | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
# Checkout the code from the repository | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
# Set up Python | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.12' | ||
|
||
- name: Install Poetry | ||
run: | | ||
curl -sSL https://install.python-poetry.org | python3 - | ||
export PATH="$HOME/.local/bin:$PATH" | ||
# Install dependencies | ||
- name: Install dependencies | ||
run: | | ||
poetry install --with tests,dev | ||
# Run tests | ||
- name: Run tests | ||
run: | | ||
poetry run pytest . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
### VsCode ## | ||
.vscode | ||
|
||
### PyCharm ### | ||
.idea | ||
|
||
### Cache ### | ||
__pycache__ | ||
.ruff_cache | ||
.pytest_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# pytest.ini | ||
[pytest] | ||
testpaths = tests | ||
python_files = test*.py | ||
python_functions = test_* | ||
addopts = --disable-warnings | ||
verbosity = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from starlette.exceptions import HTTPException | ||
|
||
|
||
class ImportedCustomException(HTTPException): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from fastapi import FastAPI | ||
from starlette.testclient import TestClient | ||
|
||
from fastapi_derive_responses import AutoDeriveResponsesAPIRoute | ||
from tests.exceptions import ImportedCustomException | ||
|
||
|
||
def test_custom_exception_in_source(): | ||
from fastapi import HTTPException | ||
|
||
class CustomException(HTTPException): | ||
... | ||
|
||
app = FastAPI(title="Custom Exception App") | ||
app.router.route_class = AutoDeriveResponsesAPIRoute | ||
|
||
@app.get("/") | ||
def raise_custom_exception(): | ||
raise CustomException(status_code=601, detail="CustomException!") | ||
|
||
client = TestClient(app) | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200 | ||
actual_dict = response.json() | ||
responses = actual_dict["paths"]["/"]["get"]["responses"] | ||
assert responses.get("601") == {"description": "CustomException!"} | ||
|
||
|
||
def test_imported_custom_exception_in_source(): | ||
app = FastAPI(title="Custom Exception App") | ||
app.router.route_class = AutoDeriveResponsesAPIRoute | ||
|
||
@app.get("/") | ||
def raise_custom_exception(): | ||
raise ImportedCustomException(status_code=601, detail="CustomException!") | ||
|
||
client = TestClient(app) | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200 | ||
actual_dict = response.json() | ||
responses = actual_dict["paths"]["/"]["get"]["responses"] | ||
assert responses.get("601") == {"description": "CustomException!"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from fastapi import FastAPI | ||
from starlette.testclient import TestClient | ||
|
||
from fastapi_derive_responses import AutoDeriveResponsesAPIRoute | ||
|
||
def test_fake_http_exception_in_source(): | ||
class HTTPException(Exception): | ||
def __init__(self, status_code: int, detail: str): | ||
pass | ||
|
||
app = FastAPI(title="Custom Exception App") | ||
app.router.route_class = AutoDeriveResponsesAPIRoute | ||
|
||
@app.get("/") | ||
def raise_custom_exception(): | ||
raise HTTPException(status_code=601, detail="CustomException!") | ||
|
||
client = TestClient(app) | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200 | ||
actual_dict = response.json() | ||
responses = actual_dict["paths"]["/"]["get"]["responses"] | ||
assert responses.get("601") is None |