Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small patch CLI list of items (fixes ignoring some files; also corrects limit, as expected, so then PAGE_SIZE will be *size*) #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions telegram_upload/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ async def aislice(iterator, limit):
items = []
i = 0
async for value in iterator:
if i > limit:
break
i += 1
items.append(value)
i += 1
if i >= limit:
break
return items


Expand Down
31 changes: 30 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest
from unittest.mock import patch, Mock

from telegram_upload.utils import sizeof_fmt, scantree
from telegram_upload.utils import sizeof_fmt, scantree, aislice

import asyncio # for TestAISlice's test_general


class TestSizeOfFmt(unittest.TestCase):
Expand Down Expand Up @@ -36,3 +38,30 @@ def test_directory(self, m):
side_effect = [[directory], [file] * 3]
m.side_effect = side_effect
self.assertEqual(list(scantree('foo')), side_effect[-1])

class TestAISlice(unittest.TestCase):
def test_general(self):
class asyncFromList:
def __init__(self, rawlist):
self.rawidx = -1
self.rawlist = rawlist.copy()
def __aiter__(self):
return self
async def __anext__(self):
await asyncio.sleep(0)
self.rawidx += 1
if self.rawidx >= len(self.rawlist):
raise StopAsyncIteration
return self.rawlist[self.rawidx]
async def async_collect_all_files(it, pagesize):
ret = []
while True:
aislice_res = await aislice(it, pagesize)
if len(aislice_res) == 0: break
self.assertLessEqual(len(aislice_res), pagesize, "limit returns more than asked")
ret.extend(aislice_res)
return ret
pagesize = 10
files = [ str(i) for i in range(51) ]
gotfiles = asyncio.get_event_loop().run_until_complete(async_collect_all_files(asyncFromList(files), pagesize))
self.assertEqual(gotfiles, files, "got different set of values")