44
55from uuid import uuid4
66from typing import List
7+ import pytest
78from common .pool import Pool
89from common .block import Block
10+ from common .exceptions import InvalidBlockUUIDError
911from common .utils import bytes_to_str
1012
1113
@@ -45,13 +47,15 @@ def test_properties():
4547 assert pool .owner == Pool .Owner .LOCAL
4648
4749
48- def test_nr_unused_bytes ():
50+ def test_nr_used_and_unused_bytes ():
4951 """
5052 Number of unused bytes in the pool.
5153 """
5254 pool , _blocks = _create_test_pool_and_block ([10 , 20 , 30 ])
55+ assert pool .nr_used_bytes == 0
5356 assert pool .nr_unused_bytes == 60
5457 _allocation = pool .allocate (10 , purpose = "test" )
58+ assert pool .nr_used_bytes == 10
5559 assert pool .nr_unused_bytes == 50
5660
5761
@@ -74,3 +78,34 @@ def test_to_mgmt():
7478 ],
7579 "owner" : str (pool .owner ),
7680 }
81+
82+
83+ def test_get_block_success ():
84+ """
85+ Get a block by UUID (block exists).
86+ """
87+ pool , blocks = _create_test_pool_and_block ([10 , 20 ])
88+ for block in blocks :
89+ retrieved_block = pool .get_block (block .uuid )
90+ assert retrieved_block == block
91+
92+
93+ def test_get_block_unknown_uuid ():
94+ """
95+ Get a block by UUID (no block with the given UUID).
96+ """
97+ pool , _blocks = _create_test_pool_and_block ([10 , 20 ])
98+ uuid = uuid4 ()
99+ with pytest .raises (InvalidBlockUUIDError ):
100+ pool .get_block (uuid )
101+
102+
103+ def test_allocate_success_empty_pool_first_block ():
104+ """
105+ Allocate an allocation from a pool. The pool is empty. The allocation fits in the first block.
106+ """
107+ pool , _blocks = _create_test_pool_and_block ([10 , 20 ])
108+ allocation = pool .allocate (10 , purpose = "test" )
109+ assert allocation is not None
110+ assert allocation .data == bytes .fromhex ("00010203040506070809" )
111+ assert pool .nr_used_bytes == 10
0 commit comments