feat: add pagination to packages list

This commit is contained in:
2023-09-11 18:07:31 +03:00
parent 8bf422b02a
commit 26b991bf47
6 changed files with 105 additions and 13 deletions

View File

@ -21,7 +21,7 @@ from ahriman.web.web import setup_service
@pytest.helpers.register
def request(application: Application, path: str, method: str, json: Any = None, data: Any = None,
def request(application: Application, path: str, method: str, params: Any = None, json: Any = None, data: Any = None,
extra: dict[str, Any] | None = None, resource: Resource | None = None) -> MagicMock:
"""
request generator helper
@ -30,6 +30,7 @@ def request(application: Application, path: str, method: str, json: Any = None,
application(Application): application fixture
path(str): path for the request
method(str): method for the request
params(Any, optional): query parameters (Default value = None)
json(Any, optional): json payload of the request (Default value = None)
data(Any, optional): form data payload of the request (Default value = None)
extra(dict[str, Any] | None, optional): extra info which will be injected for ``get_extra_info`` command
@ -42,6 +43,7 @@ def request(application: Application, path: str, method: str, json: Any = None,
request_mock.app = application
request_mock.path = path
request_mock.method = method
request_mock.query = params
request_mock.json = json
request_mock.post = data

View File

@ -2,6 +2,7 @@ import pytest
from multidict import MultiDict
from aiohttp.test_utils import TestClient
from aiohttp.web import HTTPBadRequest
from pytest_mock import MockerFixture
from unittest.mock import AsyncMock
@ -150,6 +151,41 @@ async def test_head_not_allowed(client: TestClient) -> None:
assert response.status == 405
def test_page(base: BaseView) -> None:
"""
must extract page from query parameters
"""
base._request = pytest.helpers.request(base.request.app, "", "", params=MultiDict(limit=2, offset=3))
assert base.page() == (2, 3)
base._request = pytest.helpers.request(base.request.app, "", "", params=MultiDict(offset=3))
assert base.page() == (-1, 3)
base._request = pytest.helpers.request(base.request.app, "", "", params=MultiDict(limit=2))
assert base.page() == (2, 0)
def test_page_bad_request(base: BaseView) -> None:
"""
must raise HTTPBadRequest in case if parameters are invalid
"""
with pytest.raises(HTTPBadRequest):
base._request = pytest.helpers.request(base.request.app, "", "", params=MultiDict(limit="string"))
base.page()
with pytest.raises(HTTPBadRequest):
base._request = pytest.helpers.request(base.request.app, "", "", params=MultiDict(offset="string"))
base.page()
with pytest.raises(HTTPBadRequest):
base._request = pytest.helpers.request(base.request.app, "", "", params=MultiDict(limit=-2))
base.page()
with pytest.raises(HTTPBadRequest):
base._request = pytest.helpers.request(base.request.app, "", "", params=MultiDict(offset=-1))
base.page()
async def test_username(base: BaseView, mocker: MockerFixture) -> None:
"""
must return identity of logged-in user

View File

@ -32,7 +32,7 @@ async def test_get(client: TestClient, package_ahriman: Package, package_python_
response_schema = pytest.helpers.schema_response(PackagesView.get)
response = await client.get("/api/v1/packages")
assert response.ok
assert response.status == 200
json = await response.json()
assert not response_schema.validate(json, many=True)
@ -41,6 +41,30 @@ async def test_get(client: TestClient, package_ahriman: Package, package_python_
assert {package.base for package in packages} == {package_ahriman.base, package_python_schedule.base}
async def test_get_with_pagination(client: TestClient, package_ahriman: Package,
package_python_schedule: Package) -> None:
"""
must return paginated status for packages
"""
await client.post(f"/api/v1/packages/{package_ahriman.base}",
json={"status": BuildStatusEnum.Success.value, "package": package_ahriman.view()})
await client.post(f"/api/v1/packages/{package_python_schedule.base}",
json={"status": BuildStatusEnum.Success.value, "package": package_python_schedule.view()})
request_schema = pytest.helpers.schema_request(PackagesView.get, location="querystring")
response_schema = pytest.helpers.schema_response(PackagesView.get)
payload = {"limit": 1, "offset": 1}
assert not request_schema.validate(payload)
response = await client.get("/api/v1/packages", params=payload)
assert response.status == 200
json = await response.json()
assert not response_schema.validate(json, many=True)
packages = [Package.from_json(item["package"]) for item in json]
assert packages
assert {package.base for package in packages} == {package_python_schedule.base}
async def test_post(client: TestClient, mocker: MockerFixture) -> None:
"""
must be able to reload packages