mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-11-25 11:29:48 +01:00
fix: add input validation to chunks() for size < 1
This commit is contained in:
parent
85b1be10c8
commit
d5b4235968
2 changed files with 12 additions and 0 deletions
|
|
@ -577,6 +577,8 @@ def chunks(sequence: Iterable[T], size: int) -> Iterable[Iterable[T]]:
|
||||||
:param sequence: Any Python iterator
|
:param sequence: Any Python iterator
|
||||||
:param size: The size of each chunk
|
:param size: The size of each chunk
|
||||||
"""
|
"""
|
||||||
|
if size < 1:
|
||||||
|
raise ValueError(f"chunk size must be at least 1, got {size}")
|
||||||
iterator = iter(sequence)
|
iterator = iter(sequence)
|
||||||
for item in iterator:
|
for item in iterator:
|
||||||
yield itertools.chain([item], itertools.islice(iterator, size - 1))
|
yield itertools.chain([item], itertools.islice(iterator, size - 1))
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,16 @@ def test_chunks(size, expected):
|
||||||
assert chunks == expected
|
assert chunks == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_chunks_size_zero_raises():
|
||||||
|
with pytest.raises(ValueError, match="chunk size must be at least 1"):
|
||||||
|
list(utils.chunks([1, 2, 3], 0))
|
||||||
|
|
||||||
|
|
||||||
|
def test_chunks_negative_size_raises():
|
||||||
|
with pytest.raises(ValueError, match="chunk size must be at least 1"):
|
||||||
|
list(utils.chunks([1, 2, 3], -1))
|
||||||
|
|
||||||
|
|
||||||
def test_hash_record():
|
def test_hash_record():
|
||||||
expected = "d383e7c0ba88f5ffcdd09be660de164b3847401a"
|
expected = "d383e7c0ba88f5ffcdd09be660de164b3847401a"
|
||||||
assert utils.hash_record({"name": "Cleo", "twitter": "CleoPaws"}) == expected
|
assert utils.hash_record({"name": "Cleo", "twitter": "CleoPaws"}) == expected
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue