|
| 1 | +"""Pin: client-layer classes raise a clear ``TypeError`` on pickle / |
| 2 | +copy / deepcopy. Symmetric with the dbapi's existing pickle guards |
| 3 | +on Connection / Cursor. |
| 4 | +
|
| 5 | +Without explicit ``__reduce__`` raises: |
| 6 | +- ``ConnectionPool`` / ``ClusterClient`` SILENTLY pickle (asyncio |
| 7 | + primitives became pickleable in 3.10+) — producing a |
| 8 | + "live"-looking duplicate detached from any loop. Any use yields |
| 9 | + opaque corruption. |
| 10 | +- ``DqliteConnection`` post-``connect()`` raises a cryptic |
| 11 | + ``AttributeError("Can't pickle local object |
| 12 | + 'WeakSet.__init__.<locals>._remove'")`` from the cursor-tracking |
| 13 | + weak set. |
| 14 | +- ``DqliteProtocol`` raises an opaque error from wrapped |
| 15 | + StreamReader / StreamWriter. |
| 16 | +
|
| 17 | +Mirror the ISSUE-791 pattern: every class raises a |
| 18 | +driver-level ``TypeError`` naming the specific class. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import copy |
| 24 | +import pickle |
| 25 | + |
| 26 | +import pytest |
| 27 | + |
| 28 | +from dqliteclient.cluster import ClusterClient |
| 29 | +from dqliteclient.connection import DqliteConnection |
| 30 | +from dqliteclient.node_store import MemoryNodeStore |
| 31 | +from dqliteclient.pool import ConnectionPool |
| 32 | + |
| 33 | + |
| 34 | +class TestDqliteConnectionPickleGuard: |
| 35 | + def test_pickle_raises(self) -> None: |
| 36 | + conn = DqliteConnection("localhost:9001") |
| 37 | + with pytest.raises(TypeError, match="DqliteConnection"): |
| 38 | + pickle.dumps(conn) |
| 39 | + |
| 40 | + def test_copy_copy_raises(self) -> None: |
| 41 | + conn = DqliteConnection("localhost:9001") |
| 42 | + with pytest.raises(TypeError, match="DqliteConnection"): |
| 43 | + copy.copy(conn) |
| 44 | + |
| 45 | + def test_copy_deepcopy_raises(self) -> None: |
| 46 | + conn = DqliteConnection("localhost:9001") |
| 47 | + with pytest.raises(TypeError, match="DqliteConnection"): |
| 48 | + copy.deepcopy(conn) |
| 49 | + |
| 50 | + |
| 51 | +class TestConnectionPoolPickleGuard: |
| 52 | + def test_pickle_raises(self) -> None: |
| 53 | + pool = ConnectionPool(addresses=["localhost:9001"]) |
| 54 | + with pytest.raises(TypeError, match="ConnectionPool"): |
| 55 | + pickle.dumps(pool) |
| 56 | + |
| 57 | + def test_copy_copy_raises(self) -> None: |
| 58 | + pool = ConnectionPool(addresses=["localhost:9001"]) |
| 59 | + with pytest.raises(TypeError, match="ConnectionPool"): |
| 60 | + copy.copy(pool) |
| 61 | + |
| 62 | + def test_copy_deepcopy_raises(self) -> None: |
| 63 | + pool = ConnectionPool(addresses=["localhost:9001"]) |
| 64 | + with pytest.raises(TypeError, match="ConnectionPool"): |
| 65 | + copy.deepcopy(pool) |
| 66 | + |
| 67 | + |
| 68 | +class TestClusterClientPickleGuard: |
| 69 | + def test_pickle_raises(self) -> None: |
| 70 | + cluster = ClusterClient(node_store=MemoryNodeStore(["localhost:9001"])) |
| 71 | + with pytest.raises(TypeError, match="ClusterClient"): |
| 72 | + pickle.dumps(cluster) |
| 73 | + |
| 74 | + def test_copy_copy_raises(self) -> None: |
| 75 | + cluster = ClusterClient(node_store=MemoryNodeStore(["localhost:9001"])) |
| 76 | + with pytest.raises(TypeError, match="ClusterClient"): |
| 77 | + copy.copy(cluster) |
| 78 | + |
| 79 | + |
| 80 | +class TestDqliteProtocolPickleGuard: |
| 81 | + def test_pickle_raises(self) -> None: |
| 82 | + import asyncio |
| 83 | + from unittest.mock import MagicMock |
| 84 | + |
| 85 | + from dqliteclient.protocol import DqliteProtocol |
| 86 | + |
| 87 | + reader = MagicMock(spec=asyncio.StreamReader) |
| 88 | + writer = MagicMock(spec=asyncio.StreamWriter) |
| 89 | + proto = DqliteProtocol(reader=reader, writer=writer) |
| 90 | + with pytest.raises(TypeError, match="DqliteProtocol"): |
| 91 | + pickle.dumps(proto) |
0 commit comments