MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / mock_connection

Function mock_connection

test/engine/test_reconnect.py:54–128  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

52
53
54def mock_connection():
55 def mock_cursor():
56 def execute(*args, **kwargs):
57 if conn.explode == "execute":
58 raise MockDisconnect("Lost the DB connection on execute")
59 elif conn.explode == "interrupt":
60 conn.explode = "explode_no_disconnect"
61 raise MockExitIsh("Keyboard / greenlet / etc interruption")
62 elif conn.explode == "interrupt_dont_break":
63 conn.explode = None
64 raise MockExitIsh("Keyboard / greenlet / etc interruption")
65 elif conn.explode in (
66 "execute_no_disconnect",
67 "explode_no_disconnect",
68 ):
69 raise MockError(
70 "something broke on execute but we didn't lose the "
71 "connection"
72 )
73 elif conn.explode in (
74 "rollback",
75 "rollback_no_disconnect",
76 "explode_no_disconnect",
77 ):
78 raise MockError(
79 "something broke on execute but we didn't lose the "
80 "connection"
81 )
82 elif args and "SELECT" in args[0]:
83 cursor.description = [("foo", None, None, None, None, None)]
84 else:
85 return
86
87 def close():
88 cursor.fetchall = cursor.fetchone = Mock(
89 side_effect=MockError("cursor closed")
90 )
91
92 cursor = Mock(
93 execute=Mock(side_effect=execute), close=Mock(side_effect=close)
94 )
95 return cursor
96
97 def cursor():
98 while True:
99 yield mock_cursor()
100
101 def rollback():
102 if conn.explode == "rollback":
103 raise MockDisconnect("Lost the DB connection on rollback")
104 if conn.explode == "rollback_no_disconnect":
105 raise MockError(
106 "something broke on rollback but we didn't lose the "
107 "connection"
108 )
109 else:
110 return
111

Callers 1

connectFunction · 0.85

Calls 1

cursorFunction · 0.70

Tested by

no test coverage detected