Skip to content

Commit

Permalink
add pagination and chnage related unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkudlej committed May 9, 2023
1 parent c7b5dc0 commit 10a6c21
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 101 deletions.
9 changes: 6 additions & 3 deletions tests/integration/test_integration_accounts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from tests.integration import asserts


def test_accounts_list(api):
services = api.accounts.list()
assert len(services) >= 1
def test_accounts_list(api, account):
accounts = api.accounts.list()
assert len(accounts) >= 1


def test_account_can_be_created(api, account, account_params):
Expand All @@ -22,3 +22,6 @@ def test_account_can_be_read_by_name(api, account, account_params):
read = api.accounts[account_name]
asserts.assert_resource(read)
asserts.assert_resource_params(read, account_params)

def test_users_list(api, account):
assert len(account.users.list()) >= 1
2 changes: 1 addition & 1 deletion tests/integration/test_integration_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_application_can_be_created(application, application_params):
asserts.assert_resource_params(application, application_params)


def test_application_list(account):
def test_application_list(account, application):
applications = account.applications.list()
assert len(applications) > 0

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_integration_backend_mapping_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


def test_should_list_mapping_rules(backend, backend_mapping_rule):
resource = backend.mapping_rules.list()
assert resource
resources = backend.mapping_rules.list()
assert len(resources) >= 1

def test_should_create_mapping_rule(backend_mapping_rule, backend_mapping_rule_params):
asserts.assert_resource(backend_mapping_rule)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_integration_backend_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def test_should_delete_metric(backend, backend_updated_metric_params):
assert not resource.exists()


def test_should_list_metrics(backend):
def test_should_list_metrics(backend, backend_metric):
resources = backend.metrics.list()
assert len(resources) > 1
assert len(resources) >= 1

def test_should_apicast_return_403_when_metric_is_disabled(
service, backend_metric_params, create_backend_mapping_rule,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_integration_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_3scale_url_is_set(api, url, token):
assert api.url is not None


def test_backends_list(api):
def test_backends_list(api, backend):
backends = api.backends.list()
assert len(backends) >= 1

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_integration_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from tests.integration import asserts

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

def test_should_create_method(method, method_params):
asserts.assert_resource(method)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/test_integration_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from tests.integration import asserts

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

def test_should_create_metric(metric, metric_params):
asserts.assert_resource(metric)
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/test_integration_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_3scale_url_is_set(api, url, token):
assert api.url is not None


def test_services_list(api):
def test_services_list(api, service):
services = api.services.list()
assert len(services) >= 1

Expand Down Expand Up @@ -107,6 +107,9 @@ def test_service_mapping_rules(service):
map_rules = service.mapping_rules.list()
assert len(map_rules) >= 1

def test_service_backend_usages_list(service, backend_usage):
back_usages = service.backend_usages.list()
assert len(back_usages) >= 1

def test_service_backend_usages_backend(backend_usage, backend):
assert backend_usage.backend.entity_id == backend.entity_id
Expand Down
47 changes: 46 additions & 1 deletion threescale_api/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,51 @@ def _invalidate(self):
self._entity = None


class DefaultPaginationClient(DefaultClient):
""" Client to handle API endpoints with pagination.
List of endpoints supporting pagination with per_page size:
- accounts 500
limits per app plan 50 - not implemented in client
application list for all services 500 - not implemented in client
- backend mapping rules 500
- backend method list 500
- backend metric 500
- backend 500
- service 500
invoice list by account 20 - not implemented by standard "list" method
- invoice list 20
- all cms 100
"""
def __init__(self, *args, per_page=500, **kwargs):
self.per_page = per_page
super().__init__(*args, **kwargs)

def _list(self, **kwargs):
""" List all objects via paginated API endpoint """
kwargs = kwargs.copy()
kwargs.setdefault("params", {})
if "page" in kwargs["params"] or self.per_page is None:
return super()._list(**kwargs)
pagenum = 1

kwargs["params"]["page"] = pagenum
kwargs["params"]["per_page"] = self.per_page

page = super()._list(**kwargs)
ret_list = page

while len(page):
pagenum += 1
kwargs["params"]["page"] = pagenum
page = super()._list(**kwargs)
ret_list += page

return ret_list

def __iter__(self):
return self._list()


class DefaultPlanClient(DefaultClient):
def set_default(self, entity_id: int, **kwargs) -> 'DefaultPlanResource':
"""Sets default plan for the entity
Expand Down Expand Up @@ -429,7 +474,7 @@ def is_default(self) -> bool:
return self['default'] is True


class DefaultStateClient(DefaultClient):
class DefaultStateClient(DefaultPaginationClient):
def set_state(self, entity_id, state: str, **kwargs):
"""Sets the state for the resource
Args:
Expand Down
Loading

0 comments on commit 10a6c21

Please sign in to comment.