Skip to content

Commit

Permalink
Add filter validation errors (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurio authored Dec 14, 2022
1 parent 871f15d commit 9d52a3b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ plugins = [
module = [
"bson.objectid",
"mongoengine.*",
"sqlalchemy_utils.*",
"uvicorn.*",
]
ignore_missing_imports = true
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# type: ignore
from typing import List, Optional

import pytest
Expand Down
8 changes: 7 additions & 1 deletion tests/test_mongoengine/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bson.objectid import ObjectId
from fastapi import FastAPI, Query
from mongoengine import Document, connect, fields
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, validator

from fastapi_filter import FilterDepends, with_prefix
from fastapi_filter.contrib.mongoengine import Filter as MongoFilter
Expand Down Expand Up @@ -173,10 +173,16 @@ def SportFilter(Sport, Filter):
class SportFilter(Filter):
name: Optional[str] = Field(Query(description="Name of the sport", default=None))
is_individual: bool
bogus_filter: Optional[str]

class Constants(Filter.Constants):
model = Sport

@validator("bogus_filter")
def throw_exception(cls, value):
if value:
raise ValueError("You can't use this bogus filter")

yield SportFilter


Expand Down
6 changes: 6 additions & 0 deletions tests/test_mongoengine/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ async def test_api(test_client, Address, User, UserFilter, users, filter_, expec
({"is_individual": False}, status.HTTP_200_OK),
({}, status.HTTP_422_UNPROCESSABLE_ENTITY),
({"is_individual": None}, status.HTTP_422_UNPROCESSABLE_ENTITY),
[{"is_individual": True, "bogus_filter": "bad"}, status.HTTP_422_UNPROCESSABLE_ENTITY],
),
)
@pytest.mark.asyncio
async def test_required_filter(test_client, filter_, expected_status_code):
response = await (test_client.get(f"/sports?{urlencode(filter_)}"))
assert response.status_code == expected_status_code

if response.is_error:
error_json = response.json()
assert "detail" in error_json
assert isinstance(error_json["detail"], list)
8 changes: 7 additions & 1 deletion tests/test_sqlalchemy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import pytest_asyncio
from fastapi import Depends, FastAPI, Query
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, validator
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, select
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.ext.declarative import declarative_base
Expand Down Expand Up @@ -295,10 +295,16 @@ def SportFilter(Sport, Filter):
class SportFilter(Filter):
name: Optional[str] = Field(Query(description="Name of the sport", default=None))
is_individual: bool
bogus_filter: Optional[str]

class Constants(Filter.Constants):
model = Sport

@validator("bogus_filter")
def throw_exception(cls, value):
if value:
raise ValueError("You can't use this bogus filter")

yield SportFilter


Expand Down
6 changes: 6 additions & 0 deletions tests/test_sqlalchemy/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ async def test_api(test_client, users, filter_, expected_count):
[{"is_individual": False}, status.HTTP_200_OK],
[{}, status.HTTP_422_UNPROCESSABLE_ENTITY],
[{"is_individual": None}, status.HTTP_422_UNPROCESSABLE_ENTITY],
[{"is_individual": True, "bogus_filter": "bad"}, status.HTTP_422_UNPROCESSABLE_ENTITY],
],
)
@pytest.mark.asyncio
async def test_required_filter(test_client, sports, filter_, expected_status_code):
response = await test_client.get(f"/sports?{urlencode(filter_)}")
assert response.status_code == expected_status_code

if response.is_error:
error_json = response.json()
assert "detail" in error_json
assert isinstance(error_json["detail"], list)

0 comments on commit 9d52a3b

Please sign in to comment.