Skip to content

Commit

Permalink
_read_paginated removed
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed Sep 15, 2024
1 parent ae87ec2 commit 7774989
Show file tree
Hide file tree
Showing 15 changed files with 4 additions and 266 deletions.
8 changes: 2 additions & 6 deletions fastcrud/endpoint/crud_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def crud_router(
create_deps: Sequence[Callable] = [],
read_deps: Sequence[Callable] = [],
read_multi_deps: Sequence[Callable] = [],
read_paginated_deps: Sequence[Callable] = [],
update_deps: Sequence[Callable] = [],
delete_deps: Sequence[Callable] = [],
db_delete_deps: Sequence[Callable] = [],
Expand Down Expand Up @@ -60,7 +59,6 @@ def crud_router(
create_deps: Optional list of functions to be injected as dependencies for the create endpoint.
read_deps: Optional list of functions to be injected as dependencies for the read endpoint.
read_multi_deps: Optional list of functions to be injected as dependencies for the read multiple items endpoint.
read_paginated_deps: Optional list of functions to be injected as dependencies for the read paginated endpoint.
update_deps: Optional list of functions to be injected as dependencies for the update endpoint.
delete_deps: Optional list of functions to be injected as dependencies for the delete endpoint.
db_delete_deps: Optional list of functions to be injected as dependencies for the hard delete endpoint.
Expand All @@ -71,9 +69,9 @@ def crud_router(
deleted_at_column: Optional column name to use for storing the timestamp of a soft delete. Defaults to `"deleted_at"`.
updated_at_column: Optional column name to use for storing the timestamp of an update. Defaults to `"updated_at"`.
endpoint_names: Optional dictionary to customize endpoint names for CRUD operations. Keys are operation types
(`"create"`, `"read"`, `"update"`, `"delete"`, `"db_delete"`, `"read_multi"`, `"read_paginated"`), and
(`"create"`, `"read"`, `"update"`, `"delete"`, `"db_delete"`, `"read_multi"`), and
values are the custom names to use. Unspecified operations will use default names.
filter_config: Optional `FilterConfig` instance or dictionary to configure filters for the `read_multi` and `read_paginated` endpoints.
filter_config: Optional `FilterConfig` instance or dictionary to configure filters for the `read_multi` endpoint.
Returns:
Configured `APIRouter` instance with the CRUD endpoints.
Expand Down Expand Up @@ -457,7 +455,6 @@ async def add_routes_to_router(self, ...):
"delete": "remove_task",
"db_delete": "permanently_remove_task",
"read_multi": "list_tasks",
"read_paginated": "paginate_tasks",
},
)
```
Expand Down Expand Up @@ -550,7 +547,6 @@ async def add_routes_to_router(self, ...):
create_deps=create_deps,
read_deps=read_deps,
read_multi_deps=read_multi_deps,
read_paginated_deps=read_paginated_deps,
update_deps=update_deps,
delete_deps=delete_deps,
db_delete_deps=db_delete_deps,
Expand Down
58 changes: 2 additions & 56 deletions fastcrud/endpoint/endpoint_creator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Type, Optional, Callable, Sequence, Union
from enum import Enum
import warnings

from fastapi import Depends, Body, Query, APIRouter
from pydantic import ValidationError
Expand Down Expand Up @@ -52,9 +51,9 @@ class EndpointCreator:
deleted_at_column: Optional column name to use for storing the timestamp of a soft delete. Defaults to `"deleted_at"`.
updated_at_column: Optional column name to use for storing the timestamp of an update. Defaults to `"updated_at"`.
endpoint_names: Optional dictionary to customize endpoint names for CRUD operations. Keys are operation types
(`"create"`, `"read"`, `"update"`, `"delete"`, `"db_delete"`, `"read_multi"`, `"read_paginated"`), and
(`"create"`, `"read"`, `"update"`, `"delete"`, `"db_delete"`, `"read_multi"`), and
values are the custom names to use. Unspecified operations will use default names.
filter_config: Optional `FilterConfig` instance or dictionary to configure filters for the `read_multi` and `read_paginated` endpoints.
filter_config: Optional `FilterConfig` instance or dictionary to configure filters for the `read_multi` endpoint.
Raises:
ValueError: If both `included_methods` and `deleted_methods` are provided.
Expand Down Expand Up @@ -279,7 +278,6 @@ def __init__(
"delete": "",
"db_delete": "",
"read_multi": "",
"read_paginated": "get_paginated",
}
self.endpoint_names = {**self.default_endpoint_names, **(endpoint_names or {})}
if filter_config:
Expand Down Expand Up @@ -358,42 +356,6 @@ async def endpoint(

return endpoint

def _read_paginated(self):
"""Creates an endpoint for reading multiple items from the database with pagination."""
dynamic_filters = _create_dynamic_filters(self.filter_config, self.column_types)
warnings.warn(
"_read_paginated endpoint is getting deprecated and mixed "
"into _read_items in the next major release. "
"Please use _read_items with optional page and items_per_page "
"query params instead, to achieve pagination as before."
"Simple _read_items behaviour persists with no breaking changes.",
DeprecationWarning,
)

async def endpoint(
db: AsyncSession = Depends(self.session),
page: int = Query(
1, alias="page", description="Page number, starting from 1"
),
items_per_page: int = Query(
10, alias="itemsPerPage", description="Number of items per page"
),
filters: dict = Depends(dynamic_filters),
):
if not (page and items_per_page): # pragma: no cover
return await self.crud.get_multi(db, offset=0, limit=100, **filters)

offset = compute_offset(page=page, items_per_page=items_per_page)
crud_data = await self.crud.get_multi(
db, offset=offset, limit=items_per_page, **filters
)

return paginated_response(
crud_data=crud_data, page=page, items_per_page=items_per_page
) # pragma: no cover

return endpoint

def _update_item(self):
"""Creates an endpoint for updating an existing item in the database."""

Expand Down Expand Up @@ -454,7 +416,6 @@ def add_routes_to_router(
create_deps: Sequence[Callable] = [],
read_deps: Sequence[Callable] = [],
read_multi_deps: Sequence[Callable] = [],
read_paginated_deps: Sequence[Callable] = [],
update_deps: Sequence[Callable] = [],
delete_deps: Sequence[Callable] = [],
db_delete_deps: Sequence[Callable] = [],
Expand All @@ -471,7 +432,6 @@ def add_routes_to_router(
create_deps: List of functions to be injected as dependencies for the create endpoint.
read_deps: List of functions to be injected as dependencies for the read endpoint.
read_multi_deps: List of functions to be injected as dependencies for the read multiple items endpoint.
read_paginated_deps: List of functions to be injected as dependencies for the read paginated endpoint.
update_deps: List of functions to be injected as dependencies for the update endpoint.
delete_deps: List of functions to be injected as dependencies for the delete endpoint.
db_delete_deps: List of functions to be injected as dependencies for the hard delete endpoint.
Expand Down Expand Up @@ -529,7 +489,6 @@ def get_current_user(...):
"create",
"read",
"read_multi",
"read_paginated",
"update",
"delete",
"db_delete",
Expand Down Expand Up @@ -589,19 +548,6 @@ def get_current_user(...):
description=f"Read multiple {self.model.__name__} rows from the database with a limit and an offset.",
)

if ("read_paginated" in included_methods) and (
"read_paginated" not in deleted_methods
):
self.router.add_api_route(
self._get_endpoint_path(operation="read_paginated"),
self._read_paginated(),
methods=["GET"],
include_in_schema=self.include_in_schema,
tags=self.tags,
dependencies=_inject_dependencies(read_paginated_deps),
description=f"Read multiple {self.model.__name__} rows from the database with pagination.",
)

if ("update" in included_methods) and ("update" not in deleted_methods):
self.router.add_api_route(
self._get_endpoint_path(operation="update"),
Expand Down
2 changes: 0 additions & 2 deletions fastcrud/endpoint/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class CRUDMethods(BaseModel):
"create",
"read",
"read_multi",
"read_paginated",
"update",
"delete",
"db_delete",
Expand All @@ -35,7 +34,6 @@ def check_valid_method(cls, values: Sequence[str]) -> Sequence[str]:
"create",
"read",
"read_multi",
"read_paginated",
"update",
"delete",
"db_delete",
Expand Down
7 changes: 0 additions & 7 deletions tests/sqlalchemy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ def client(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)
Expand All @@ -510,7 +509,6 @@ def client(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)
Expand All @@ -533,7 +531,6 @@ def client(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)
Expand Down Expand Up @@ -565,7 +562,6 @@ def filtered_client(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)
Expand Down Expand Up @@ -597,7 +593,6 @@ def dict_filtered_client(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
)
Expand Down Expand Up @@ -630,7 +625,6 @@ def invalid_filtered_client(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

Expand All @@ -654,6 +648,5 @@ def endpoint_creator(test_model, async_session) -> EndpointCreator:
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)
1 change: 0 additions & 1 deletion tests/sqlalchemy/core/test_valid_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def test_crud_methods_default_methods():
"create",
"read",
"read_multi",
"read_paginated",
"update",
"delete",
"db_delete",
Expand Down
3 changes: 0 additions & 3 deletions tests/sqlalchemy/endpoint/test_custom_endpoint_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def add_routes_to_router(
create_deps: list[Callable] = [],
read_deps: list[Callable] = [],
read_multi_deps: list[Callable] = [],
read_paginated_deps: list[Callable] = [],
update_deps: list[Callable] = [],
delete_deps: list[Callable] = [],
db_delete_deps: list[Callable] = [],
Expand All @@ -30,7 +29,6 @@ def add_routes_to_router(
create_deps,
read_deps,
read_multi_deps,
read_paginated_deps,
update_deps,
delete_deps,
db_delete_deps,
Expand Down Expand Up @@ -66,7 +64,6 @@ async def test_custom_endpoint_creator(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

Expand Down
1 change: 0 additions & 1 deletion tests/sqlalchemy/endpoint/test_deleted_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ async def test_deleted_methods(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

Expand Down
88 changes: 0 additions & 88 deletions tests/sqlalchemy/endpoint/test_get_paginated.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,6 @@
from fastapi.testclient import TestClient


@pytest.mark.asyncio
async def test_read_paginated(client: TestClient, async_session, test_model, test_data):
for data in test_data:
new_item = test_model(**data)
async_session.add(new_item)
await async_session.commit()

page = 1
items_per_page = 5

response = client.get(
f"/test/get_paginated?page={page}&itemsPerPage={items_per_page}"
)

assert response.status_code == 200

data = response.json()

assert "data" in data
assert "total_count" in data
assert "page" in data
assert "items_per_page" in data
assert "has_more" in data

assert len(data["data"]) > 0
assert len(data["data"]) <= items_per_page

test_item = test_data[0]
assert any(item["name"] == test_item["name"] for item in data["data"])

assert data["page"] == page
assert data["items_per_page"] == items_per_page


@pytest.mark.asyncio
async def test_read_paginated_with_filters(
filtered_client: TestClient, async_session, test_model, test_data
):
for data in test_data:
new_item = test_model(**data)
async_session.add(new_item)
await async_session.commit()

page = 1
items_per_page = 5

tier_id = 1
response = filtered_client.get(
f"/test/get_paginated?page={page}&itemsPerPage={items_per_page}&tier_id={tier_id}"
)

assert response.status_code == 200
data = response.json()

assert "data" in data
assert "total_count" in data
assert "page" in data
assert "items_per_page" in data
assert "has_more" in data

assert len(data["data"]) > 0
assert len(data["data"]) <= items_per_page

for item in data["data"]:
assert item["tier_id"] == tier_id

name = "Alice"
response = filtered_client.get(
f"/test/get_paginated?page={page}&itemsPerPage={items_per_page}&name={name}"
)

assert response.status_code == 200
data = response.json()

assert "data" in data
assert "total_count" in data
assert "page" in data
assert "items_per_page" in data
assert "has_more" in data

assert len(data["data"]) > 0
assert len(data["data"]) <= items_per_page

for item in data["data"]:
assert item["name"] == name


# ------ the following tests will completely replace the current ones in the next version of fastcrud ------
@pytest.mark.asyncio
async def test_read_items_with_pagination(
client: TestClient, async_session, test_model, test_data
Expand Down
1 change: 0 additions & 1 deletion tests/sqlalchemy/endpoint/test_included_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ async def test_included_methods(
"delete": "delete",
"db_delete": "db_delete",
"read_multi": "get_multi",
"read_paginated": "get_paginated",
},
)

Expand Down
Loading

0 comments on commit 7774989

Please sign in to comment.