Skip to content

Commit

Permalink
Add test for iamges API
Browse files Browse the repository at this point in the history
  • Loading branch information
hv0905 committed Jul 9, 2024
1 parent 6006bca commit e6c69ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
8 changes: 4 additions & 4 deletions app/Controllers/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ async def query_image_by_id(image_id: Annotated[UUID, Path(description="The id o
raise HTTPException(404, "Cannot find the image with the given ID.") from ex


@images_router.get("/", description="Query images in order.")
async def query_images(filter_param: Annotated[FilterParams, Depends()],
prev_offset_id: Annotated[UUID, Query(description="The previous offset image ID.")] = None,
count: Annotated[int, Query(description="The number of images to query.")] = 15):
@images_router.get("/", description="Query images in order of ID.")
async def scroll_images(filter_param: Annotated[FilterParams, Depends()],
prev_offset_id: Annotated[UUID, Query(description="The previous offset image ID.")] = None,
count: Annotated[int, Query(ge=1, le=100, description="The number of images to query.")] = 15):
# validate the offset ID
if prev_offset_id is not None and len(await services.db_context.validate_ids([str(prev_offset_id)])) == 0:
raise HTTPException(404, "The previous offset ID is invalid.")

Check warning on line 39 in app/Controllers/images.py

View check run for this annotation

Codecov / codecov/patch

app/Controllers/images.py#L39

Added line #L39 was not covered by tests
Expand Down
3 changes: 2 additions & 1 deletion app/Models/api_response/images_api_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ class QueryByIdApiResponse(NekoProtocol):

class QueryImagesApiResponse(NekoProtocol):
images: list[ImageData] = Field(description="The list of images.")
next_page_offset: str = Field(description="The offset ID for the next page query.")
next_page_offset: str | None = Field(description="The offset ID for the next page query. "
"If there are no more images, this field will be null.")
22 changes: 22 additions & 0 deletions tests/api/test_search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import itertools
import uuid

import pytest_asyncio

from .conftest import check_local_dir_empty
Expand Down Expand Up @@ -92,3 +95,22 @@ def test_search_filters(test_client, img_ids):
resp = test_client.get("/search/text/cat", params={'starred': True})
assert resp.status_code == 200
assert resp.json()['result'][0]['img']['id'] == img_ids['bsn'][0]


def test_images_query_by_id(test_client, img_ids):
resp = test_client.get(f"/images/id/{img_ids['bsn'][0]}")
assert resp.status_code == 200
assert resp.json()['img']['id'] == img_ids['bsn'][0]


def test_images_query_not_exist(test_client, img_ids):
resp = test_client.get(f"/images/id/{uuid.uuid4()}")
assert resp.status_code == 404


def test_images_query_scroll(test_client, img_ids):
resp = test_client.get("/images/", params={'count': 50})
assert resp.status_code == 200
all_images_id = list(itertools.chain(*img_ids.values()))
for item in resp.json()['images']:
assert item['id'] in all_images_id

0 comments on commit e6c69ac

Please sign in to comment.