|
| 1 | +"""Validation of the ``max_total_rows`` constructor parameter.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from dqliteclient.connection import DqliteConnection |
| 8 | +from dqliteclient.pool import ConnectionPool |
| 9 | +from dqliteclient.protocol import DqliteProtocol, _validate_max_total_rows |
| 10 | + |
| 11 | + |
| 12 | +class TestValidator: |
| 13 | + def test_none_allowed(self) -> None: |
| 14 | + assert _validate_max_total_rows(None) is None |
| 15 | + |
| 16 | + def test_positive_int_allowed(self) -> None: |
| 17 | + assert _validate_max_total_rows(1) == 1 |
| 18 | + assert _validate_max_total_rows(10_000_000) == 10_000_000 |
| 19 | + |
| 20 | + def test_zero_rejected(self) -> None: |
| 21 | + with pytest.raises(ValueError, match="max_total_rows must be > 0"): |
| 22 | + _validate_max_total_rows(0) |
| 23 | + |
| 24 | + def test_negative_rejected(self) -> None: |
| 25 | + with pytest.raises(ValueError, match="max_total_rows must be > 0"): |
| 26 | + _validate_max_total_rows(-1) |
| 27 | + |
| 28 | + def test_float_rejected(self) -> None: |
| 29 | + with pytest.raises(TypeError, match="max_total_rows must be int or None"): |
| 30 | + _validate_max_total_rows(1.5) # type: ignore[arg-type] |
| 31 | + |
| 32 | + def test_bool_rejected(self) -> None: |
| 33 | + # True is technically int, but PEP-489-style APIs rightly reject it. |
| 34 | + with pytest.raises(TypeError, match="max_total_rows must be int or None"): |
| 35 | + _validate_max_total_rows(True) # type: ignore[arg-type] |
| 36 | + |
| 37 | + def test_string_rejected(self) -> None: |
| 38 | + with pytest.raises(TypeError, match="max_total_rows must be int or None"): |
| 39 | + _validate_max_total_rows("100") # type: ignore[arg-type] |
| 40 | + |
| 41 | + |
| 42 | +class TestConstructorValidation: |
| 43 | + def test_dqlite_connection_zero_rejected(self) -> None: |
| 44 | + with pytest.raises(ValueError): |
| 45 | + DqliteConnection("localhost:19001", max_total_rows=0) |
| 46 | + |
| 47 | + def test_dqlite_connection_negative_rejected(self) -> None: |
| 48 | + with pytest.raises(ValueError): |
| 49 | + DqliteConnection("localhost:19001", max_total_rows=-5) |
| 50 | + |
| 51 | + def test_dqlite_connection_bool_rejected(self) -> None: |
| 52 | + with pytest.raises(TypeError): |
| 53 | + DqliteConnection("localhost:19001", max_total_rows=True) # type: ignore[arg-type] |
| 54 | + |
| 55 | + def test_dqlite_connection_none_allowed(self) -> None: |
| 56 | + conn = DqliteConnection("localhost:19001", max_total_rows=None) |
| 57 | + assert conn._max_total_rows is None |
| 58 | + |
| 59 | + def test_pool_zero_rejected(self) -> None: |
| 60 | + with pytest.raises(ValueError): |
| 61 | + ConnectionPool(addresses=["localhost:19001"], max_total_rows=0) |
| 62 | + |
| 63 | + def test_pool_negative_rejected(self) -> None: |
| 64 | + with pytest.raises(ValueError): |
| 65 | + ConnectionPool(addresses=["localhost:19001"], max_total_rows=-1) |
| 66 | + |
| 67 | + @pytest.mark.asyncio |
| 68 | + async def test_protocol_zero_rejected(self) -> None: |
| 69 | + reader = asyncio.StreamReader() |
| 70 | + writer = _DummyWriter() |
| 71 | + with pytest.raises(ValueError): |
| 72 | + DqliteProtocol(reader, writer, max_total_rows=0) # type: ignore[arg-type] |
| 73 | + |
| 74 | + |
| 75 | +class _DummyWriter: |
| 76 | + def close(self) -> None: |
| 77 | + pass |
| 78 | + |
| 79 | + async def wait_closed(self) -> None: |
| 80 | + pass |
| 81 | + |
| 82 | + def write(self, data: bytes) -> None: |
| 83 | + pass |
| 84 | + |
| 85 | + async def drain(self) -> None: |
| 86 | + pass |
0 commit comments