Skip to content

Commit 5764026

Browse files
committed
Refactor progress
1 parent bc76b6e commit 5764026

3 files changed

Lines changed: 72 additions & 8 deletions

File tree

common/exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,16 @@ def __init__(self):
304304
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
305305
message="PSRD data is invalid.",
306306
)
307+
308+
309+
class InvalidEncodedFragment(DSKEException):
310+
"""
311+
Exception raised when trying to parse an encoded fragment string that is invalid.
312+
"""
313+
314+
def __init__(self, encoded_fragment: str):
315+
super().__init__(
316+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
317+
message="Invalid encoded fragment.",
318+
details={"encoded_fragment": encoded_fragment},
319+
)

common/fragment.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from uuid import UUID
77
import pydantic
88
from common.block import Block
9-
from common.exceptions import InvalidBlockUUIDError
9+
from common.exceptions import InvalidBlockUUIDError, InvalidEncodedFragment
1010
from . import utils
1111

1212

@@ -152,11 +152,13 @@ def from_enc_str(
152152
# TODO: Add expected_max_size parameter to avoid insane large sizes.
153153
parts = enc_str.split(":")
154154
if len(parts) != 3:
155-
raise ValueError(f"Invalid fragment parameter string: {enc_str}")
155+
raise InvalidEncodedFragment(encoded_fragment=enc_str)
156156
block_uuid_str, start_byte_str, size_str = parts
157-
block = pool.get_block(UUID(block_uuid_str))
158-
if block is None:
159-
raise ValueError(f"Block not found: {block_uuid_str}")
157+
try:
158+
block_uuid = UUID(block_uuid_str)
159+
except ValueError as exc:
160+
raise InvalidBlockUUIDError(block_uuid=block_uuid_str) from exc
161+
block = pool.get_block(block_uuid)
160162
start_byte = int(start_byte_str)
161163
size = int(size_str)
162164
data = block.take_data(start_byte, size)

common/tests/test_fragment.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,61 @@ def test_from_api_success():
153153
assert block._data == bytes.fromhex("00000000000506070809")
154154

155155

156-
def test_from_api_bad_uuid():
156+
def test_from_api_bad_block_uuid():
157157
"""
158158
Attempt to create a Fragment from a bad APIFragment (invalid block UUID).
159159
"""
160160
# pylint: disable=protected-access
161-
(pool, block) = _create_test_pool_and_block(10)
161+
(pool, _block) = _create_test_pool_and_block(10)
162+
# UUID string is not correctly formatted
162163
api_fragment = APIFragment(block_uuid="not-a-uuid", start=0, size=5)
163164
with pytest.raises(InvalidBlockUUIDError):
164-
fragment = Fragment.from_api(api_fragment, pool)
165+
_fragment = Fragment.from_api(api_fragment, pool)
166+
# UUID string is correctly formatted, but not the UUID of any block in the pool
167+
uuid = uuid4()
168+
api_fragment = APIFragment(block_uuid=str(uuid), start=0, size=5)
169+
with pytest.raises(InvalidBlockUUIDError):
170+
_fragment = Fragment.from_api(api_fragment, pool)
171+
172+
173+
def test_to_enc_str():
174+
"""
175+
Create an APIFragment for an encoded string.
176+
"""
177+
block = _create_test_block(10)
178+
fragment = Fragment.allocate(block, 5)
179+
enc_str = fragment.to_enc_str()
180+
assert enc_str == f"{block.uuid}:0:5"
181+
182+
183+
def test_from_enc_str_success():
184+
"""
185+
Create a Fragment from a valid encoded string.
186+
"""
187+
# pylint: disable=protected-access
188+
(pool, block) = _create_test_pool_and_block(10)
189+
enc_str = f"{block.uuid}:0:5"
190+
fragment = Fragment.from_enc_str(enc_str, pool)
191+
assert fragment.block == block
192+
assert fragment.start == 0
193+
assert fragment.size == 5
194+
assert fragment.data == bytes.fromhex("0001020304")
195+
assert block.nr_used_bytes == 5
196+
assert block._data == bytes.fromhex("00000000000506070809")
197+
198+
199+
def test_from_enc_str_bad_block_uuid():
200+
"""
201+
Attempt to create a Fragment from a bad encoded string (invalid block UUID).
202+
"""
203+
# pylint: disable=protected-access
204+
(pool, _block) = _create_test_pool_and_block(10)
205+
# UUID string is not correctly formatted
206+
enc_str = "not-a-uuid:0:5"
207+
with pytest.raises(InvalidBlockUUIDError):
208+
_fragment = Fragment.from_enc_str(enc_str, pool)
209+
# UUID string is correctly formatted, but not the UUID of any block in the pool
210+
uuid = uuid4()
211+
enc_str = f"{uuid}:0:5"
212+
with pytest.raises(InvalidBlockUUIDError):
213+
_fragment = Fragment.from_enc_str(enc_str, pool)

0 commit comments

Comments
 (0)