@@ -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