import pytest from dopt_basics.batching import Chunk class _Item: def __init__( self, size: int, ) -> None: self.size = size def test_Chunk_base(): chunk: Chunk[int] = Chunk(3) assert chunk.capacity == 3 assert chunk.capacity_held == 0 assert chunk.capacity_left == 3 assert chunk.full is False assert len(chunk) == 0 assert not chunk.contents # TODO implement behaviour # def test_Chunk_base_overfill_append(): # chunk: Chunk[int] = Chunk(3) # chunk.append(1) # chunk.append(2) # chunk.append(3) # chunk.append(4) # allow once, then not again def test_Chunk_base_no_overfill_append(): chunk: Chunk[int] = Chunk(3, overfill_once=False) chunk.append(1) assert chunk[0] == 1 chunk.append(2) chunk.append(3) with pytest.raises(ValueError, match="Item can not be added due to capacity limit"): chunk.append(4) def test_Chunk_base_overfill_extend(): chunk: Chunk[int] = Chunk(3) chunk.extend((1, 2)) chunk.extend((3, 4)) with pytest.raises(ValueError, match="Item can not be added due to capacity limit"): chunk.extend((5, 6)) def test_Chunk_base_no_overfill_extend(): chunk: Chunk[int] = Chunk(3, overfill_once=False) chunk.extend((1, 2)) with pytest.raises(ValueError, match="Item can not be added due to capacity limit"): chunk.extend((3, 4)) def test_Chunk_base_remove(): chunk: Chunk[int] = Chunk(3, overfill_once=False) chunk.extend((1, 2)) assert chunk.capacity_held == 2 assert chunk.capacity_left == 1 chunk.remove(2) assert chunk.capacity_held == 1 assert chunk.capacity_left == 2 def test_Chunk_property_append_success(): CAP_PROP = "size" chunk: Chunk[_Item] = Chunk(3, overfill_once=False, cap_property=CAP_PROP) i1 = _Item(2) i2 = _Item(4) chunk.append(i1) with pytest.raises(ValueError, match="Item can not be added due to capacity limit"): chunk.append(i2) def test_Chunk_property_append_fail_no_attribute(): CAP_PROP = "size" chunk: Chunk = Chunk(3, overfill_once=False, cap_property=CAP_PROP) with pytest.raises(AttributeError, match="Object does not posses the wanted property"): chunk.append(1) def test_Chunk_property_append_fail_wrong_type(): CAP_PROP = "size" chunk: Chunk = Chunk(3, overfill_once=False, cap_property=CAP_PROP) i1 = _Item(4) i1.size = "wrong type" # type: ignore with pytest.raises(TypeError, match="Capacity property must be an integer"): chunk.append(i1) def test_Chunk_property_extend_fail_no_attribute(): CAP_PROP = "size" chunk: Chunk = Chunk(3, overfill_once=False, cap_property=CAP_PROP) i1 = _Item(2) with pytest.raises(AttributeError, match="Not all objects posses the wanted property"): chunk.extend((i1, 2)) def test_Chunk_property_extend_fail_wrong_type(): CAP_PROP = "size" chunk: Chunk = Chunk(3, overfill_once=False, cap_property=CAP_PROP) i1 = _Item(2) i2 = _Item(4) i2.size = "wrong type" # type: ignore with pytest.raises(TypeError, match="Capacity property must be an integer"): chunk.extend((i1, i2)) def test_Chunk_property_remove_fail_no_attribute(): CAP_PROP = "size" chunk: Chunk = Chunk(3, overfill_once=False, cap_property=CAP_PROP) with pytest.raises(AttributeError, match="Object does not posses the wanted property"): chunk.remove(1) def test_Chunk_property_remove_fail_wrong_type(): CAP_PROP = "size" chunk: Chunk = Chunk(3, overfill_once=False, cap_property=CAP_PROP) i1 = _Item(4) i1.size = "wrong type" # type: ignore with pytest.raises(TypeError, match="Capacity property must be an integer"): chunk.remove(i1)