Skip to content

Commit bc76b6e

Browse files
committed
Refactor progress
1 parent 8faacf3 commit bc76b6e

2 files changed

Lines changed: 62 additions & 13 deletions

File tree

common/tests/test_block.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ def test_is_fully_used():
238238
assert block.is_fully_used()
239239

240240

241+
def test_to_api():
242+
"""
243+
Create an APIBlock for a Block.
244+
"""
245+
block = _create_test_block(10)
246+
api_block = block.to_api()
247+
assert api_block.block_uuid == str(block.uuid)
248+
assert api_block.data == bytes_to_str(_bytes_test_pattern(10))
249+
250+
241251
def test_from_api_success():
242252
"""
243253
Create a Block from a valid APIBlock.
@@ -252,7 +262,7 @@ def test_from_api_success():
252262

253263
def test_from_api_bad_uuid():
254264
"""
255-
Create a Block from a valid APIBlock.
265+
Attempt to create a Block from a bad APIBlock (invalid block UUID).
256266
"""
257267
data = _bytes_test_pattern(10)
258268
api_block = APIBlock(block_uuid="bad-uuid", data=bytes_to_str(data))
@@ -262,19 +272,9 @@ def test_from_api_bad_uuid():
262272

263273
def test_from_api_bad_data():
264274
"""
265-
Create a Block from a valid APIBlock.
275+
Attempt to create a Block from a bad APIBlock (invalid data).
266276
"""
267277
uuid = uuid4()
268278
api_block = APIBlock(block_uuid=str(uuid), data="bad-data")
269279
with pytest.raises(InvalidPSRDDataError):
270280
_block = Block.from_api(api_block)
271-
272-
273-
def test_to_api():
274-
"""
275-
Create a Block from a valid APIBlock.
276-
"""
277-
block = _create_test_block(10)
278-
api_block = block.to_api()
279-
assert api_block.block_uuid == str(block.uuid)
280-
assert api_block.data == bytes_to_str(_bytes_test_pattern(10))

common/tests/test_fragment.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
"""
44

55
from uuid import uuid4
6+
import pytest
67
from common.block import Block
7-
from common.fragment import Fragment
8+
from common.exceptions import InvalidBlockUUIDError
9+
from common.fragment import APIFragment, Fragment
10+
from common.pool import Pool
811
from common.utils import bytes_to_str
912

1013

@@ -19,6 +22,13 @@ def _create_test_block(size):
1922
return block
2023

2124

25+
def _create_test_pool_and_block(block_size):
26+
pool = Pool(name="test_pool", owner=Pool.Owner.LOCAL)
27+
block = _create_test_block(block_size)
28+
pool.add_block(block)
29+
return (pool, block)
30+
31+
2232
def test_fragment_init():
2333
"""
2434
Initialize a fragment.
@@ -113,3 +123,42 @@ def test_fragment_to_mgmt():
113123
"size": 5,
114124
"data": bytes_to_str(bytes.fromhex("0001020304")),
115125
}
126+
127+
128+
def test_to_api():
129+
"""
130+
Create an APIFragment for a Fragment.
131+
"""
132+
block = _create_test_block(10)
133+
fragment = Fragment.allocate(block, 5)
134+
api_fragment = fragment.to_api()
135+
assert api_fragment.block_uuid == str(block.uuid)
136+
assert api_fragment.start == 0
137+
assert api_fragment.size == 5
138+
139+
140+
def test_from_api_success():
141+
"""
142+
Create a Fragment from a valid APIFragment.
143+
"""
144+
# pylint: disable=protected-access
145+
(pool, block) = _create_test_pool_and_block(10)
146+
api_fragment = APIFragment(block_uuid=str(block.uuid), start=0, size=5)
147+
fragment = Fragment.from_api(api_fragment, pool)
148+
assert fragment.block == block
149+
assert fragment.start == 0
150+
assert fragment.size == 5
151+
assert fragment.data == bytes.fromhex("0001020304")
152+
assert block.nr_used_bytes == 5
153+
assert block._data == bytes.fromhex("00000000000506070809")
154+
155+
156+
def test_from_api_bad_uuid():
157+
"""
158+
Attempt to create a Fragment from a bad APIFragment (invalid block UUID).
159+
"""
160+
# pylint: disable=protected-access
161+
(pool, block) = _create_test_pool_and_block(10)
162+
api_fragment = APIFragment(block_uuid="not-a-uuid", start=0, size=5)
163+
with pytest.raises(InvalidBlockUUIDError):
164+
fragment = Fragment.from_api(api_fragment, pool)

0 commit comments

Comments
 (0)