Skip to content

Commit

Permalink
fix(files): Break when we have enough objects (and before loading the…
Browse files Browse the repository at this point in the history
… next page) (#525)

* chore: Add tests for the list_bucket_objects

* fix(files): Break when we have enough objects (and before loading the next page)
  • Loading branch information
qgerome authored Aug 11, 2023
1 parent 4531aae commit 204be98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion hexa/files/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def list_bucket_objects(
res = _prefix_to_dict(bucket_name, prefix)
if not ignore_hidden_files or not res["name"].startswith("."):
objects.append(res)
while len(objects) <= max_items:

while True:
for obj in current_page:
if _is_dir(obj):
# We ignore objects that are directories (object with a size = 0 and ending with a /)
Expand All @@ -184,6 +185,11 @@ def list_bucket_objects(
if not ignore_hidden_files or not res["name"].startswith("."):
objects.append(res)

if len(objects) >= max_items:
# We have enough items, let's break
break

# Otherwise we load the next page and continue our loop
current_page = next(pages)

return ObjectsPage(
Expand Down
28 changes: 28 additions & 0 deletions hexa/files/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,31 @@ def test_list_blobs_with_prefix(self):
"dir/readme.md",
],
)

@backend.mock_storage
def test_list_blobs_pagination(self):
bucket = create_bucket("my_bucket")
for i in range(0, 12):
bucket.blob(
f"test_{i}.txt",
size=123 * i,
content_type="text/plain",
)

res = list_bucket_objects(bucket.name, page=1, per_page=10)
self.assertTrue(res.has_next_page)
self.assertFalse(res.has_previous_page)
self.assertEqual(res.page_number, 1)

res = list_bucket_objects(bucket.name, page=1, per_page=20)
self.assertFalse(res.has_next_page)

res = list_bucket_objects(bucket.name, page=2, per_page=10)
self.assertFalse(res.has_next_page)
self.assertTrue(res.has_previous_page)
self.assertEqual(res.page_number, 2)

res = list_bucket_objects(bucket.name, page=2, per_page=5)
self.assertTrue(res.has_next_page)
self.assertTrue(res.has_previous_page)
self.assertEqual(res.page_number, 2)

0 comments on commit 204be98

Please sign in to comment.