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

fix complex parameter filter with between #150

Merged
merged 1 commit into from
Aug 11, 2024
Merged
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

# Macos
.DS_Store

.ruff_cache
.ruff_cache
8 changes: 6 additions & 2 deletions fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def _parse_filters(
for or_key, or_value in value.items()
if (
sqlalchemy_filter := self._get_sqlalchemy_filter(
or_key, value
or_key, or_value
)
)
is not None
Expand All @@ -327,7 +327,11 @@ def _parse_filters(
else:
sqlalchemy_filter = self._get_sqlalchemy_filter(op, value)
if sqlalchemy_filter:
filters.append(sqlalchemy_filter(column)(value))
filters.append(
sqlalchemy_filter(column)(value)
if op != "between"
else sqlalchemy_filter(column)(*value)
)
else:
column = getattr(model, key, None)
if column is not None:
Expand Down
6 changes: 3 additions & 3 deletions fastcrud/endpoint/crud_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ async def add_routes_to_router(self, ...):
session=session,
model=model,
crud=crud,
create_schema=create_schema,
update_schema=update_schema,
create_schema=create_schema, # type: ignore
update_schema=update_schema, # type: ignore
include_in_schema=include_in_schema,
delete_schema=delete_schema,
delete_schema=delete_schema, # type: ignore
path=path,
tags=tags,
is_deleted_column=is_deleted_column,
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlalchemy/core/test_parse_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ async def test_parse_filters_not_contained_in(test_model):
)


@pytest.mark.asyncio
async def test_parse_filters_between_condition(test_model):
fast_crud = FastCRUD(test_model)
filters = fast_crud._parse_filters(category_id__between=[1, 5])
assert len(filters) == 1
assert (
str(filters[0]) == "test.category_id BETWEEN :category_id_1 AND :category_id_2"
)


@pytest.mark.asyncio
@pytest.mark.parametrize("operator", ("in", "not_in", "between"))
async def test_parse_filters_raises_exception(test_model, operator: str):
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlmodel/core/test_parse_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ async def test_parse_filters_not_contained_in(test_model):
)


@pytest.mark.asyncio
async def test_parse_filters_between_condition(test_model):
fast_crud = FastCRUD(test_model)
filters = fast_crud._parse_filters(category_id__between=[1, 5])
assert len(filters) == 1
assert (
str(filters[0]) == "test.category_id BETWEEN :category_id_1 AND :category_id_2"
)


@pytest.mark.asyncio
@pytest.mark.parametrize("operator", ("in", "not_in", "between"))
async def test_parse_filters_raises_exception(test_model, operator: str):
Expand Down
Loading