|
8 | 8 | import osparc._settings
|
9 | 9 | import pydantic
|
10 | 10 | import pytest
|
| 11 | +import respx |
| 12 | +from faker import Faker |
| 13 | +import httpx |
| 14 | +from urllib.parse import urlparse |
| 15 | +from osparc._utils import PaginationIterator |
| 16 | +from functools import partial |
11 | 17 |
|
12 | 18 | _CLIENTS_PYTHON_DIR: Path = Path(__file__).parent.parent.parent
|
13 | 19 |
|
@@ -87,3 +93,39 @@ def test_parent_project_validation(faker, valid: bool):
|
87 | 93 | os.environ["OSPARC_NODE_ID"] = f"{faker.text()}"
|
88 | 94 | with pytest.raises(pydantic.ValidationError):
|
89 | 95 | _ = osparc._settings.ParentProjectInfo()
|
| 96 | + |
| 97 | + |
| 98 | +def test_pagination_iterator( |
| 99 | + faker: Faker, page_file: osparc.PageFile, api_client: osparc.ApiClient |
| 100 | +): |
| 101 | + next_page_url = urlparse(page_file.links.next) |
| 102 | + _base_url = f"{next_page_url.scheme}://{next_page_url.netloc}" |
| 103 | + _auth = httpx.BasicAuth( |
| 104 | + username=api_client.configuration.username, |
| 105 | + password=api_client.configuration.password, |
| 106 | + ) |
| 107 | + |
| 108 | + def _sideeffect(all_items: List, request: httpx.Request): |
| 109 | + if len(all_items) > faker.pyint(min_value=10): |
| 110 | + page_file.links.next = None |
| 111 | + all_items += page_file.items |
| 112 | + return httpx.Response(status_code=200, json=page_file.to_dict()) |
| 113 | + |
| 114 | + with respx.mock( |
| 115 | + base_url=_base_url, |
| 116 | + assert_all_called=True, |
| 117 | + ) as respx_mock: |
| 118 | + server_items: List[osparc.File] = page_file.items |
| 119 | + respx_mock.get(urlparse(page_file.links.next).path).mock( |
| 120 | + side_effect=partial(_sideeffect, server_items) |
| 121 | + ) |
| 122 | + |
| 123 | + pagination_iterator = PaginationIterator( |
| 124 | + lambda: page_file, api_client=api_client, base_url=_base_url, auth=_auth |
| 125 | + ) |
| 126 | + client_items = [item for item in pagination_iterator] |
| 127 | + assert len(server_items) > 0 |
| 128 | + assert all(si == ci for si, ci in zip(server_items, client_items)) |
| 129 | + |
| 130 | + first_client_item = next(pagination_iterator) |
| 131 | + assert first_client_item == server_items[0] |
0 commit comments