Skip to content

Commit b88f00e

Browse files
committed
cleanup pagination; add basic tests for it [#20]
1 parent ee78680 commit b88f00e

File tree

4 files changed

+172
-23
lines changed

4 files changed

+172
-23
lines changed

bunq/sdk/client.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ class Pagination(object):
384384
# Error constants
385385
_ERROR_NO_PREVIOUS_PAGE = 'Could not generate previous page URL params: ' \
386386
'there is no previous page.'
387+
_ERROR_NO_NEXT_PAGE = 'Could not generate next page URL params: ' \
388+
'there is no next page.'
387389

388390
# URL Param constants
389391
PARAM_OLDER_ID = 'older_id'
@@ -400,17 +402,30 @@ def __init__(self):
400402
def url_params_previous_page(self):
401403
"""
402404
:rtype: dict[str, str]
403-
:raise: exception.BunqException When there is no previous page.
404405
"""
405406

406-
if not self.has_previous_item():
407-
raise exception.BunqException(self._ERROR_NO_PREVIOUS_PAGE)
407+
self.assert_has_previous_page()
408408

409409
params = {self.PARAM_OLDER_ID: str(self.older_id)}
410410
self._add_count_to_params_if_needed(params)
411411

412412
return params
413413

414+
def assert_has_previous_page(self):
415+
"""
416+
:raise: exception.BunqException
417+
"""
418+
419+
if not self.has_previous_page():
420+
raise exception.BunqException(self._ERROR_NO_PREVIOUS_PAGE)
421+
422+
def has_previous_page(self):
423+
"""
424+
:rtype: bool
425+
"""
426+
427+
return self.older_id is not None
428+
414429
@property
415430
def url_params_count_only(self):
416431
"""
@@ -422,13 +437,6 @@ def url_params_count_only(self):
422437

423438
return params
424439

425-
def has_previous_item(self):
426-
"""
427-
:rtype: bool
428-
"""
429-
430-
return self.older_id is not None
431-
432440
def _add_count_to_params_if_needed(self, params):
433441
"""
434442
:type params: dict[str, str]
@@ -439,18 +447,7 @@ def _add_count_to_params_if_needed(self, params):
439447
if self.count is not None:
440448
params[self.PARAM_COUNT] = str(self.count)
441449

442-
@property
443-
def _next_id(self):
444-
"""
445-
:rtype: int
446-
"""
447-
448-
if self.has_next_item_assured():
449-
return self.newer_id
450-
451-
return self.future_id
452-
453-
def has_next_item_assured(self):
450+
def has_next_page_assured(self):
454451
"""
455452
:rtype: bool
456453
"""
@@ -463,7 +460,28 @@ def url_params_next_page(self):
463460
:rtype: dict[str, str]
464461
"""
465462

463+
self.assert_has_next_page()
464+
466465
params = {self.PARAM_NEWER_ID: str(self._next_id)}
467466
self._add_count_to_params_if_needed(params)
468467

469468
return params
469+
470+
def assert_has_next_page(self):
471+
"""
472+
:raise: exception.BunqException
473+
"""
474+
475+
if self._next_id is None:
476+
raise exception.BunqException(self._ERROR_NO_NEXT_PAGE)
477+
478+
@property
479+
def _next_id(self):
480+
"""
481+
:rtype: int
482+
"""
483+
484+
if self.has_next_page_assured():
485+
return self.newer_id
486+
487+
return self.future_id

examples/payment_list_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def run():
3232
for payment in payments_response.value:
3333
print(payment.id_)
3434

35-
if payments_response.pagination.has_previous_item():
35+
if payments_response.pagination.has_previous_page():
3636
print(_MESSAGE_SECOND_LATEST_PAGE_IDS)
3737
payments_response_previous = generated.Payment.list(
3838
api_context,

tests/test_pagination.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
from bunq.sdk import client
2+
from bunq.sdk import exception
3+
from tests.bunq_test import BunqSdkTestCase
4+
5+
6+
class TestPagination(BunqSdkTestCase):
7+
"""
8+
Tests:
9+
Pagination
10+
"""
11+
12+
_PAGINATION_OLDER_ID_CUSTOM = 1
13+
_PAGINATION_NEWER_ID_CUSTOM = 2
14+
_PAGINATION_FUTURE_ID_CUSTOM = 3
15+
_PAGINATION_COUNT_CUSTOM = 5
16+
17+
def test_get_url_params_count_only(self):
18+
pagination = self.create_pagination_with_all_properties_set()
19+
url_params_count_only_expected = {
20+
client.Pagination.PARAM_COUNT: str(self._PAGINATION_COUNT_CUSTOM),
21+
}
22+
23+
self.assertEqual(url_params_count_only_expected,
24+
pagination.url_params_count_only)
25+
26+
def create_pagination_with_all_properties_set(self):
27+
"""
28+
:rtype: Pagination
29+
"""
30+
31+
pagination = client.Pagination()
32+
pagination.older_id = self._PAGINATION_OLDER_ID_CUSTOM
33+
pagination.newer_id = self._PAGINATION_NEWER_ID_CUSTOM
34+
pagination.future_id = self._PAGINATION_FUTURE_ID_CUSTOM
35+
pagination.count = self._PAGINATION_COUNT_CUSTOM
36+
37+
return pagination
38+
39+
def test_get_url_params_previous_page(self):
40+
pagination = self.create_pagination_with_all_properties_set()
41+
url_params_previous_page_expected = {
42+
client.Pagination.PARAM_COUNT: str(self._PAGINATION_COUNT_CUSTOM),
43+
client.Pagination.PARAM_OLDER_ID:
44+
str(self._PAGINATION_OLDER_ID_CUSTOM),
45+
}
46+
47+
self.assertTrue(pagination.has_previous_page())
48+
self.assertEqual(url_params_previous_page_expected,
49+
pagination.url_params_previous_page)
50+
51+
def test_get_url_params_previous_page_no_count(self):
52+
pagination = self.create_pagination_with_all_properties_set()
53+
pagination.count = None
54+
url_params_previous_page_expected = {
55+
client.Pagination.PARAM_OLDER_ID:
56+
str(self._PAGINATION_OLDER_ID_CUSTOM),
57+
}
58+
59+
self.assertTrue(pagination.has_previous_page())
60+
self.assertEqual(url_params_previous_page_expected,
61+
pagination.url_params_previous_page)
62+
63+
def test_get_url_params_next_page_newer(self):
64+
pagination = self.create_pagination_with_all_properties_set()
65+
url_params_next_page_expected = {
66+
client.Pagination.PARAM_COUNT: str(self._PAGINATION_COUNT_CUSTOM),
67+
client.Pagination.PARAM_NEWER_ID:
68+
str(self._PAGINATION_NEWER_ID_CUSTOM),
69+
}
70+
71+
self.assertTrue(pagination.has_next_page_assured())
72+
self.assertEqual(url_params_next_page_expected,
73+
pagination.url_params_next_page)
74+
75+
def test_get_url_params_next_page_newer_no_count(self):
76+
pagination = self.create_pagination_with_all_properties_set()
77+
pagination.count = None
78+
url_params_next_page_expected = {
79+
client.Pagination.PARAM_NEWER_ID:
80+
str(self._PAGINATION_NEWER_ID_CUSTOM),
81+
}
82+
83+
self.assertTrue(pagination.has_next_page_assured())
84+
self.assertEqual(url_params_next_page_expected,
85+
pagination.url_params_next_page)
86+
87+
def test_get_url_params_next_page_future(self):
88+
pagination = self.create_pagination_with_all_properties_set()
89+
pagination.newer_id = None
90+
url_params_next_page_expected = {
91+
client.Pagination.PARAM_COUNT: str(self._PAGINATION_COUNT_CUSTOM),
92+
client.Pagination.PARAM_NEWER_ID:
93+
str(self._PAGINATION_FUTURE_ID_CUSTOM),
94+
}
95+
96+
self.assertFalse(pagination.has_next_page_assured())
97+
self.assertEqual(url_params_next_page_expected,
98+
pagination.url_params_next_page)
99+
100+
def test_get_url_params_next_page_future_no_count(self):
101+
pagination = self.create_pagination_with_all_properties_set()
102+
pagination.newer_id = None
103+
pagination.count = None
104+
url_params_next_page_expected = {
105+
client.Pagination.PARAM_NEWER_ID:
106+
str(self._PAGINATION_FUTURE_ID_CUSTOM),
107+
}
108+
109+
self.assertFalse(pagination.has_next_page_assured())
110+
self.assertEqual(url_params_next_page_expected,
111+
pagination.url_params_next_page)
112+
113+
def test_get_url_params_prev_page_from_pagination_with_no_prev_page(self):
114+
pagination = self.create_pagination_with_all_properties_set()
115+
pagination.older_id = None
116+
117+
def access_url_params_previous_page():
118+
_ = pagination.url_params_previous_page
119+
120+
self.assertRaises(exception.BunqException,
121+
access_url_params_previous_page)
122+
123+
def test_get_url_params_next_page_from_pagination_with_no_next_page(self):
124+
pagination = self.create_pagination_with_all_properties_set()
125+
pagination.newer_id = None
126+
pagination.future_id = None
127+
128+
def access_url_params_next_page():
129+
_ = pagination.url_params_next_page
130+
131+
self.assertRaises(exception.BunqException, access_url_params_next_page)

tests/test_pagination_scenario.py

Whitespace-only changes.

0 commit comments

Comments
 (0)