Skip to content

Commit 10a6c21

Browse files
author
Martin Kudlej
committed
add pagination and chnage related unit tests
1 parent c7b5dc0 commit 10a6c21

10 files changed

+115
-101
lines changed

tests/integration/test_integration_accounts.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from tests.integration import asserts
22

33

4-
def test_accounts_list(api):
5-
services = api.accounts.list()
6-
assert len(services) >= 1
4+
def test_accounts_list(api, account):
5+
accounts = api.accounts.list()
6+
assert len(accounts) >= 1
77

88

99
def test_account_can_be_created(api, account, account_params):
@@ -22,3 +22,6 @@ def test_account_can_be_read_by_name(api, account, account_params):
2222
read = api.accounts[account_name]
2323
asserts.assert_resource(read)
2424
asserts.assert_resource_params(read, account_params)
25+
26+
def test_users_list(api, account):
27+
assert len(account.users.list()) >= 1

tests/integration/test_integration_application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_application_can_be_created(application, application_params):
1616
asserts.assert_resource_params(application, application_params)
1717

1818

19-
def test_application_list(account):
19+
def test_application_list(account, application):
2020
applications = account.applications.list()
2121
assert len(applications) > 0
2222

tests/integration/test_integration_backend_mapping_rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55

66
def test_should_list_mapping_rules(backend, backend_mapping_rule):
7-
resource = backend.mapping_rules.list()
8-
assert resource
7+
resources = backend.mapping_rules.list()
8+
assert len(resources) >= 1
99

1010
def test_should_create_mapping_rule(backend_mapping_rule, backend_mapping_rule_params):
1111
asserts.assert_resource(backend_mapping_rule)

tests/integration/test_integration_backend_metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def test_should_delete_metric(backend, backend_updated_metric_params):
4545
assert not resource.exists()
4646

4747

48-
def test_should_list_metrics(backend):
48+
def test_should_list_metrics(backend, backend_metric):
4949
resources = backend.metrics.list()
50-
assert len(resources) > 1
50+
assert len(resources) >= 1
5151

5252
def test_should_apicast_return_403_when_metric_is_disabled(
5353
service, backend_metric_params, create_backend_mapping_rule,

tests/integration/test_integration_backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_3scale_url_is_set(api, url, token):
99
assert api.url is not None
1010

1111

12-
def test_backends_list(api):
12+
def test_backends_list(api, backend):
1313
backends = api.backends.list()
1414
assert len(backends) >= 1
1515

tests/integration/test_integration_methods.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from tests.integration import asserts
66

7+
def test_list_methods(metric, method):
8+
assert len(metric.methods.list()) >= 1
79

810
def test_should_create_method(method, method_params):
911
asserts.assert_resource(method)

tests/integration/test_integration_metrics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from tests.integration import asserts
77

8+
def test_list_metrics(service, metric):
9+
assert len(service.metrics.list()) >= 1
810

911
def test_should_create_metric(metric, metric_params):
1012
asserts.assert_resource(metric)

tests/integration/test_integration_services.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_3scale_url_is_set(api, url, token):
99
assert api.url is not None
1010

1111

12-
def test_services_list(api):
12+
def test_services_list(api, service):
1313
services = api.services.list()
1414
assert len(services) >= 1
1515

@@ -107,6 +107,9 @@ def test_service_mapping_rules(service):
107107
map_rules = service.mapping_rules.list()
108108
assert len(map_rules) >= 1
109109

110+
def test_service_backend_usages_list(service, backend_usage):
111+
back_usages = service.backend_usages.list()
112+
assert len(back_usages) >= 1
110113

111114
def test_service_backend_usages_backend(backend_usage, backend):
112115
assert backend_usage.backend.entity_id == backend.entity_id

threescale_api/defaults.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,51 @@ def _invalidate(self):
386386
self._entity = None
387387

388388

389+
class DefaultPaginationClient(DefaultClient):
390+
""" Client to handle API endpoints with pagination.
391+
List of endpoints supporting pagination with per_page size:
392+
- accounts 500
393+
limits per app plan 50 - not implemented in client
394+
application list for all services 500 - not implemented in client
395+
- backend mapping rules 500
396+
- backend method list 500
397+
- backend metric 500
398+
- backend 500
399+
- service 500
400+
invoice list by account 20 - not implemented by standard "list" method
401+
- invoice list 20
402+
- all cms 100
403+
"""
404+
def __init__(self, *args, per_page=500, **kwargs):
405+
self.per_page = per_page
406+
super().__init__(*args, **kwargs)
407+
408+
def _list(self, **kwargs):
409+
""" List all objects via paginated API endpoint """
410+
kwargs = kwargs.copy()
411+
kwargs.setdefault("params", {})
412+
if "page" in kwargs["params"] or self.per_page is None:
413+
return super()._list(**kwargs)
414+
pagenum = 1
415+
416+
kwargs["params"]["page"] = pagenum
417+
kwargs["params"]["per_page"] = self.per_page
418+
419+
page = super()._list(**kwargs)
420+
ret_list = page
421+
422+
while len(page):
423+
pagenum += 1
424+
kwargs["params"]["page"] = pagenum
425+
page = super()._list(**kwargs)
426+
ret_list += page
427+
428+
return ret_list
429+
430+
def __iter__(self):
431+
return self._list()
432+
433+
389434
class DefaultPlanClient(DefaultClient):
390435
def set_default(self, entity_id: int, **kwargs) -> 'DefaultPlanResource':
391436
"""Sets default plan for the entity
@@ -429,7 +474,7 @@ def is_default(self) -> bool:
429474
return self['default'] is True
430475

431476

432-
class DefaultStateClient(DefaultClient):
477+
class DefaultStateClient(DefaultPaginationClient):
433478
def set_state(self, entity_id, state: str, **kwargs):
434479
"""Sets the state for the resource
435480
Args:

0 commit comments

Comments
 (0)