MCPcopy Create free account
hub / github.com/tox-dev/filelock / patch_connection

Function patch_connection

tests/sqlite/async_helpers.py:59–105  ·  view source on GitHub ↗
(
    mocker: MockerFixture,
    *,
    execute: Callable[[sqlite3.Connection, str], sqlite3.Cursor] | None = None,
    rollback: Callable[[sqlite3.Connection], None] | None = None,
)

Source from the content-addressed store, hash-verified

57
58
59def patch_connection(
60 mocker: MockerFixture,
61 *,
62 execute: Callable[[sqlite3.Connection, str], sqlite3.Cursor] | None = None,
63 rollback: Callable[[sqlite3.Connection], None] | None = None,
64) -> None:
65 real_connect = sqlite3.connect
66 connection_count = 0
67
68 def connect(
69 database: str,
70 *,
71 factory: type[sqlite3.Connection],
72 timeout: float,
73 ) -> sqlite3.Connection:
74 nonlocal connection_count
75 real_connection = real_connect(
76 database,
77 check_same_thread=False,
78 factory=factory,
79 cached_statements=0,
80 timeout=timeout,
81 )
82 connection_count += 1
83 if connection_count < 2:
84 return real_connection
85 configuration: dict[str, Callable[[str], sqlite3.Cursor] | Callable[[], None]] = {}
86 if execute is not None:
87 configuration["execute.side_effect"] = functools.partial(execute, real_connection)
88 if rollback is not None:
89 configuration["rollback.side_effect"] = functools.partial(rollback, real_connection)
90 connection = mocker.MagicMock(
91 spec_set=type(real_connection),
92 wraps=real_connection,
93 database_identity=None,
94 **configuration,
95 )
96 mocker.patch.object(
97 type(connection),
98 "in_transaction",
99 new_callable=mocker.PropertyMock,
100 create=True,
101 side_effect=lambda: real_connection.in_transaction,
102 )
103 return cast("sqlite3.Connection", connection)
104
105 mocker.patch("filelock._sqlite._transactions.connect", side_effect=connect, autospec=True)
106
107
108def fail_rollback(

Calls

no outgoing calls