Skip to content

Commit f4dd554

Browse files
committed
Refactor progress
1 parent 5764026 commit f4dd554

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

common/fragment.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ def from_enc_str(
159159
except ValueError as exc:
160160
raise InvalidBlockUUIDError(block_uuid=block_uuid_str) from exc
161161
block = pool.get_block(block_uuid)
162-
start_byte = int(start_byte_str)
163-
size = int(size_str)
164-
data = block.take_data(start_byte, size)
165-
return Fragment(block=block, start=start_byte, size=size, data=data)
162+
try:
163+
start = int(start_byte_str)
164+
except ValueError as exc:
165+
raise InvalidEncodedFragment(encoded_fragment=enc_str) from exc
166+
try:
167+
size = int(size_str)
168+
except ValueError as exc:
169+
raise InvalidEncodedFragment(encoded_fragment=enc_str) from exc
170+
data = block.take_data(start, size)
171+
return Fragment(block=block, start=start, size=size, data=data)

common/tests/test_fragment.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from uuid import uuid4
66
import pytest
77
from common.block import Block
8-
from common.exceptions import InvalidBlockUUIDError
8+
from common.exceptions import InvalidBlockUUIDError, InvalidEncodedFragment
99
from common.fragment import APIFragment, Fragment
1010
from common.pool import Pool
1111
from common.utils import bytes_to_str
@@ -196,6 +196,26 @@ def test_from_enc_str_success():
196196
assert block._data == bytes.fromhex("00000000000506070809")
197197

198198

199+
def test_from_enc_str_bad_str():
200+
"""
201+
Attempt to create a Fragment from a bad encoded string (invalid encoded string format).
202+
"""
203+
# pylint: disable=protected-access
204+
(pool, block) = _create_test_pool_and_block(10)
205+
# No colons
206+
with pytest.raises(InvalidEncodedFragment):
207+
_fragment = Fragment.from_enc_str("not-an-encoded-str", pool)
208+
# Only one colon
209+
with pytest.raises(InvalidEncodedFragment):
210+
_fragment = Fragment.from_enc_str(f"{block.uuid}:only-one-colon", pool)
211+
# Start is not a number
212+
with pytest.raises(InvalidEncodedFragment):
213+
_fragment = Fragment.from_enc_str(f"{block.uuid}:not-a-number:5", pool)
214+
# Size is not a number
215+
with pytest.raises(InvalidEncodedFragment):
216+
_fragment = Fragment.from_enc_str(f"{block.uuid}:0:not-a-number", pool)
217+
218+
199219
def test_from_enc_str_bad_block_uuid():
200220
"""
201221
Attempt to create a Fragment from a bad encoded string (invalid block UUID).

0 commit comments

Comments
 (0)