Skip to content

Commit

Permalink
fix: Correctly handle pagination. (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
loganripplinger authored Dec 10, 2019
1 parent 76130c4 commit 1f4c9bb
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
11 changes: 7 additions & 4 deletions data_resource_api/api/v1_0_0/resource_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ def build_json_from_object(self, obj: object, restricted_fields: dict = []):
) if not key.startswith('_') and not callable(key) and key not in restricted_fields}
return resp

def compute_offset(self, page, items_per_page):
def compute_offset(self, page: int, items_per_page: int) -> int:
"""Compute the offset value for pagination.
Args:
page (int): The current page to compute the offset from.
items_per_page (int): Number of items per page.
Returns:
int: The offset value.
"""
return (page - 1) * items_per_page
return int(int(int(page) - 1) * int(items_per_page))

def compute_page(self, offset, items_per_page):
def compute_page(self, offset: int, items_per_page: int) -> int:
"""Compute the current page number based on offset.
Args:
offset (int): The offset to use to compute the page.
Expand All @@ -44,7 +44,7 @@ def compute_page(self, offset, items_per_page):
int: The page number.
"""

return int(math.ceil((int(offset) / int(items_per_page)))) + 1
return int(math.ceil(((int(offset) + 1) / int(items_per_page))))

def build_links(self, endpoint: str, offset: int, limit: int, rows: int):
"""Build links for a paginated response
Expand All @@ -57,6 +57,9 @@ def build_links(self, endpoint: str, offset: int, limit: int, rows: int):
Returns:
dict: The links based on the offset and limit
"""
offset = int(offset)
limit = int(limit)
rows = int(rows)

# URL and pages
url_link = '/{}?offset={}&limit={}'
Expand Down
44 changes: 44 additions & 0 deletions tests/resource_handler/test_compute_offset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from data_resource_api.api.v1_0_0 import ResourceHandler
from expects import expect, be_an, raise_error, have_property, equal, be_empty, be
import json


class TestComputeOffset(object):
def test_compute_offset(self):
handler = ResourceHandler()

expect(handler.compute_offset(1, 20)).to(equal(0))
# expect(handler.compute_offset(53, 19)).to(equal(0))
# expect(handler.compute_offset(0, 20)).to(equal(0))

# def test_build_links(self):
# handler = ResourceHandler()

# items = [
# handler.build_links('test', 0, 20, 5),
# handler.build_links('test', 1, 20, 55),
# handler.build_links('test', 1, -20, 55)
# ]

# for item in items:
# print(json.dumps(item, indent=4))

def test_compute_page(self):
handler = ResourceHandler()

items = [
handler.compute_page(0, 20),
handler.compute_page(1, 20),
handler.compute_page(20, 20),
handler.compute_page(21, 20),
handler.compute_page(25, 20)
]

for item in items:
print(item)

expect(handler.compute_page(0, 20)).to(equal(1))
expect(handler.compute_page(1, 20)).to(equal(1))
expect(handler.compute_page(19, 20)).to(equal(1))
expect(handler.compute_page(20, 20)).to(equal(2))
expect(handler.compute_page(99, 20)).to(equal(5))
51 changes: 51 additions & 0 deletions tests/test_default_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,54 @@ def test_programs(self, regular_client):

expect(response.status_code).to(equal(200))
# expect(body['message']).to(equal('Access Denied'))

def test_pagination(self, regular_client):
def post_a_credential():
post_body = {
"credential_name": "testtesttest"
}
response = regular_client.post('/credentials', json=post_body)

expect(response.status_code).to(equal(201))

body = json.loads(response.data)
return body

body = post_a_credential()
print(json.dumps(body, indent=4))

# do regular get
route = '/credentials'
response = regular_client.get(route)
body = json.loads(response.data)
print(json.dumps(body, indent=4))

expect(response.status_code).to(equal(200))
expect(len(body['credentials'])).not_to(equal(0))

# do get with pagination
route = '/credentials?offset=0&limit=20'
response = regular_client.get(route)
body = json.loads(response.data)
print(json.dumps(body, indent=4))

expect(response.status_code).to(equal(200))

# add items till we need the pagination
for _ in range(100):
body = post_a_credential()
print(json.dumps(body, indent=4))

# do get with pagination
route = '/credentials?offset=0&limit=20'
response = regular_client.get(route)
body = json.loads(response.data)
print(json.dumps(body, indent=4))
expect(len(body['credentials'])).to(equal(20))

# do get with pagination
route = '/credentials?offset=20&limit=20'
response = regular_client.get(route)
body = json.loads(response.data)
print(json.dumps(body, indent=4))
# expect(len(body['credentials'])).to(equal(20))

0 comments on commit 1f4c9bb

Please sign in to comment.