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

added contains express support #395

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ fastapi_filter.sqlite
poetry.toml
.pytest_cache/
.ruff_cache/
__pycache__
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ By default, **fastapi_filter** supports the following operators:
- `not`/`ne`
- `not_in`/`nin`
- `like`/`ilike`
- `contains`

_**Note:** Mysql excludes `None` values when using `in` filter_

Expand Down
1 change: 1 addition & 0 deletions examples/fastapi_filter_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class UserFilter(Filter):
name: Optional[str]
name__ilike: Optional[str]
name__like: Optional[str]
name__contains: Optional[str]
name__neq: Optional[str]
address: Optional[AddressFilter] = FilterDepends(with_prefix("address", AddressFilter))
age__lt: Optional[int]
Expand Down
1 change: 1 addition & 0 deletions fastapi_filter/contrib/sqlalchemy/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def _backward_compatible_value_for_like_and_ilike(value: str):
# XXX(arthurio): Mysql excludes None values when using `in` or `not in` filters.
"not": lambda value: ("is_not", value),
"not_in": lambda value: ("not_in", value),
"contains": lambda value: ("contains", value),
}
"""Operators à la Django.

Expand Down
1 change: 1 addition & 0 deletions tests/test_sqlalchemy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class UserFilter(Filter): # type: ignore[misc, valid-type]
name: Optional[str]
name__neq: Optional[str]
name__like: Optional[str]
name__contains: Optional[str]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here name is not a list of enum so I'm not sure we are testing exactly what you are looking for... Could you add another enum list field instead and test against it?

name__ilike: Optional[str]
name__in: Optional[List[str]]
name__not: Optional[str]
Expand Down
7 changes: 6 additions & 1 deletion tests/test_sqlalchemy/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"filter_,expected_count",
[
[{"name": "Mr Praline"}, 1],
[{"name__contains": "Mr"}, 2],
[{"name__contains": "mR"}, 2],
[{"name__neq": "Mr Praline"}, 4],
[{"name__in": "Mr Praline,Mr Creosote,Gumbys,Knight"}, 3],
[{"name__like": "%Mr%"}, 2],
Expand Down Expand Up @@ -60,6 +62,8 @@ async def test_filter_deprecation_like_and_ilike(session, Address, User, UserFil
"filter_,expected_count",
[
[{"name": "Mr Praline"}, 1],
[{"name__contains": "Mr"}, 2],
[{"name__contains": "mR"}, 2],
[{"name__in": "Mr Praline,Mr Creosote,Gumbys,Knight"}, 3],
[{"name__isnull": True}, 1],
[{"name__isnull": False}, 5],
Expand Down Expand Up @@ -89,7 +93,8 @@ 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],
[{"is_individual": True, "bogus_filter": "bad"},
status.HTTP_422_UNPROCESSABLE_ENTITY],
Comment on lines -92 to +97
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reverted.

],
)
@pytest.mark.asyncio
Expand Down