mirror of
https://github.com/hay-kot/mealie.git
synced 2025-07-16 10:03:54 -07:00
feat: re-write get all routes to use pagination (#1424)
rewrite get_all routes to use a pagination pattern to allow for better implementations of search, filter, and sorting on the frontend or by any client without fetching all the data. Additionally we added a CI check for running the Nuxt built to confirm that no TS errors were present. Finally, I had to remove the header support for the Shopping lists as the browser caching based off last_updated header was not allowing it to read recent updates due to how we're handling the updated_at property in the database with nested fields. This will have to be looked at in the future to reimplement. I'm unsure how many other routes have a similar issue. Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
This commit is contained in:
parent
c158672d12
commit
cb15db2d27
55 changed files with 683 additions and 197 deletions
|
@ -10,13 +10,13 @@ class Routes:
|
|||
|
||||
def test_admin_server_tasks_test_and_get(api_client: TestClient, admin_user: TestUser):
|
||||
# Bootstrap Timer
|
||||
BackgroundExecutor.sleep_time = 0.1
|
||||
BackgroundExecutor.sleep_time = 1
|
||||
|
||||
response = api_client.post(Routes.base, headers=admin_user.token)
|
||||
assert response.status_code == 201
|
||||
|
||||
response = api_client.get(Routes.base, headers=admin_user.token)
|
||||
as_dict = response.json()
|
||||
as_dict = response.json()["items"]
|
||||
|
||||
assert len(as_dict) == 1
|
||||
|
||||
|
|
|
@ -111,7 +111,7 @@ def test_update_cookbooks_many(api_client: TestClient, unique_user: TestUser, co
|
|||
|
||||
known_ids = [x.id for x in cookbooks]
|
||||
|
||||
server_ids = [x["id"] for x in response.json()]
|
||||
server_ids = [x["id"] for x in response.json()["items"]]
|
||||
|
||||
for know in known_ids: # Hacky check, because other tests don't cleanup after themselves :(
|
||||
assert str(know) in server_ids
|
||||
|
|
|
@ -22,7 +22,7 @@ class Routes:
|
|||
def test_shopping_lists_get_all(api_client: TestClient, unique_user: TestUser, shopping_lists: list[ShoppingListOut]):
|
||||
all_lists = api_client.get(Routes.base, headers=unique_user.token)
|
||||
assert all_lists.status_code == 200
|
||||
all_lists = all_lists.json()
|
||||
all_lists = all_lists.json()["items"]
|
||||
|
||||
assert len(all_lists) == len(shopping_lists)
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ def test_get_all_only_includes_group_recipes(api_client: TestClient, unique_user
|
|||
|
||||
assert response.status_code == 200
|
||||
|
||||
recipes = response.json()
|
||||
recipes = response.json()["items"]
|
||||
|
||||
assert len(recipes) == 5
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ def test_multitenant_cases_get_all(
|
|||
response = test_case.get_all(token)
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
data = response.json()["items"]
|
||||
|
||||
assert len(data) == len(item_ids)
|
||||
|
||||
|
@ -84,7 +84,7 @@ def test_multitenant_cases_same_named_resources(
|
|||
response = test_case.get_all(token)
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
data = response.json()["items"]
|
||||
|
||||
assert len(data) == len(item_ids)
|
||||
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
from random import randint
|
||||
from urllib.parse import parse_qsl, urlsplit
|
||||
|
||||
from humps import camelize
|
||||
|
||||
from mealie.repos.repository_factory import AllRepositories
|
||||
from mealie.schema.response.pagination import PaginationQuery
|
||||
from mealie.services.seeder.seeder_service import SeederService
|
||||
|
@ -21,18 +26,98 @@ def test_repository_pagination(database: AllRepositories, unique_user: TestUser)
|
|||
seen = []
|
||||
|
||||
for _ in range(10):
|
||||
results = foods_repo.pagination(query)
|
||||
results = foods_repo.page_all(query)
|
||||
|
||||
assert len(results.data) == 10
|
||||
assert len(results.items) == 10
|
||||
|
||||
for result in results.data:
|
||||
for result in results.items:
|
||||
assert result.id not in seen
|
||||
|
||||
seen += [result.id for result in results.data]
|
||||
seen += [result.id for result in results.items]
|
||||
|
||||
query.page += 1
|
||||
|
||||
results = foods_repo.pagination(query)
|
||||
results = foods_repo.page_all(query)
|
||||
|
||||
for result in results.data:
|
||||
for result in results.items:
|
||||
assert result.id not in seen
|
||||
|
||||
|
||||
def test_pagination_response_and_metadata(database: AllRepositories, unique_user: TestUser):
|
||||
group = database.groups.get_one(unique_user.group_id)
|
||||
|
||||
seeder = SeederService(database, None, group)
|
||||
seeder.seed_foods("en-US")
|
||||
|
||||
foods_repo = database.ingredient_foods.by_group(unique_user.group_id) # type: ignore
|
||||
|
||||
# this should get all results
|
||||
query = PaginationQuery(
|
||||
page=1,
|
||||
per_page=-1,
|
||||
)
|
||||
|
||||
all_results = foods_repo.page_all(query)
|
||||
assert all_results.total == len(all_results.items)
|
||||
|
||||
# this should get the last page of results
|
||||
query = PaginationQuery(
|
||||
page=-1,
|
||||
per_page=1,
|
||||
)
|
||||
|
||||
last_page_of_results = foods_repo.page_all(query)
|
||||
assert last_page_of_results.page == last_page_of_results.total_pages
|
||||
assert last_page_of_results.items[-1] == all_results.items[-1]
|
||||
|
||||
|
||||
def test_pagination_guides(database: AllRepositories, unique_user: TestUser):
|
||||
group = database.groups.get_one(unique_user.group_id)
|
||||
|
||||
seeder = SeederService(database, None, group)
|
||||
seeder.seed_foods("en-US")
|
||||
|
||||
foods_repo = database.ingredient_foods.by_group(unique_user.group_id) # type: ignore
|
||||
foods_route = (
|
||||
"/foods" # this doesn't actually have to be accurate, it's just a placeholder to test for query params
|
||||
)
|
||||
|
||||
query = PaginationQuery(
|
||||
page=1,
|
||||
per_page=1,
|
||||
)
|
||||
|
||||
first_page_of_results = foods_repo.page_all(query)
|
||||
first_page_of_results.set_pagination_guides(foods_route, query.dict())
|
||||
assert first_page_of_results.next is not None
|
||||
assert first_page_of_results.previous is None
|
||||
|
||||
query = PaginationQuery(
|
||||
page=-1,
|
||||
per_page=1,
|
||||
)
|
||||
|
||||
last_page_of_results = foods_repo.page_all(query)
|
||||
last_page_of_results.set_pagination_guides(foods_route, query.dict())
|
||||
assert last_page_of_results.next is None
|
||||
assert last_page_of_results.previous is not None
|
||||
|
||||
random_page = randint(2, first_page_of_results.total_pages - 1)
|
||||
query = PaginationQuery(
|
||||
page=random_page,
|
||||
per_page=1,
|
||||
)
|
||||
|
||||
random_page_of_results = foods_repo.page_all(query)
|
||||
random_page_of_results.set_pagination_guides(foods_route, query.dict())
|
||||
|
||||
next_params = dict(parse_qsl(urlsplit(random_page_of_results.next).query))
|
||||
assert int(next_params["page"]) == random_page + 1
|
||||
|
||||
prev_params = dict(parse_qsl(urlsplit(random_page_of_results.previous).query))
|
||||
assert int(prev_params["page"]) == random_page - 1
|
||||
|
||||
source_params = camelize(query.dict())
|
||||
for source_param in source_params:
|
||||
assert source_param in next_params
|
||||
assert source_param in prev_params
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue