Skip to content

Commit

Permalink
test pagination iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
bisgaard-itis committed Nov 18, 2024
1 parent d8ef798 commit 5c2d570
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions clients/python/test/test_osparc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ def cfg(faker: Faker) -> osparc.Configuration:
@pytest.fixture
def api_client(cfg: osparc.Configuration) -> osparc.ApiClient:
return osparc.ApiClient(configuration=cfg)


@pytest.fixture
def page_file(faker: Faker) -> osparc.PageFile:
items = []
for _ in range(5):
items.append(
osparc.File(
id=faker.uuid4(),
filename=faker.file_name(),
content_type=None,
checksum=faker.sha256(),
e_tag=faker.sha256(),
)
)

return osparc.PageFile(
items=items,
total=faker.pyint(min_value=1, max_value=10) * len(items),
limit=len(items),
offset=faker.pyint(min_value=0),
links=osparc.Links(next=faker.url()),
)
42 changes: 42 additions & 0 deletions clients/python/test/test_osparc/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import osparc._settings
import pydantic
import pytest
import respx
from faker import Faker
import httpx
from urllib.parse import urlparse
from osparc._utils import PaginationIterator
from functools import partial

_CLIENTS_PYTHON_DIR: Path = Path(__file__).parent.parent.parent

Expand Down Expand Up @@ -87,3 +93,39 @@ def test_parent_project_validation(faker, valid: bool):
os.environ["OSPARC_NODE_ID"] = f"{faker.text()}"
with pytest.raises(pydantic.ValidationError):
_ = osparc._settings.ParentProjectInfo()


def test_pagination_iterator(
faker: Faker, page_file: osparc.PageFile, api_client: osparc.ApiClient
):
next_page_url = urlparse(page_file.links.next)
_base_url = f"{next_page_url.scheme}://{next_page_url.netloc}"
_auth = httpx.BasicAuth(
username=api_client.configuration.username,
password=api_client.configuration.password,
)

def _sideeffect(all_items: List, request: httpx.Request):
if len(all_items) > faker.pyint(min_value=10):
page_file.links.next = None
all_items += page_file.items
return httpx.Response(status_code=200, json=page_file.to_dict())

with respx.mock(
base_url=_base_url,
assert_all_called=True,
) as respx_mock:
server_items: List[osparc.File] = page_file.items
respx_mock.get(urlparse(page_file.links.next).path).mock(
side_effect=partial(_sideeffect, server_items)
)

pagination_iterator = PaginationIterator(
lambda: page_file, api_client=api_client, base_url=_base_url, auth=_auth
)
client_items = [item for item in pagination_iterator]
assert len(server_items) > 0
assert all(si == ci for si, ci in zip(server_items, client_items))

first_client_item = next(pagination_iterator)
assert first_client_item == server_items[0]

0 comments on commit 5c2d570

Please sign in to comment.