MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / create_mock_engine

Function create_mock_engine

lib/sqlalchemy/engine/mock.py:72–133  ·  view source on GitHub ↗

Create a "mock" engine used for echoing DDL. This is a utility function used for debugging or storing the output of DDL sequences as generated by :meth:`_schema.MetaData.create_all` and related methods. The function accepts a URL which is used only to determine the kind of dial

(
    url: Union[str, URL], executor: Any, **kw: Any
)

Source from the content-addressed store, hash-verified

70
71
72def create_mock_engine(
73 url: Union[str, URL], executor: Any, **kw: Any
74) -> MockConnection:
75 """Create a "mock" engine used for echoing DDL.
76
77 This is a utility function used for debugging or storing the output of DDL
78 sequences as generated by :meth:`_schema.MetaData.create_all`
79 and related methods.
80
81 The function accepts a URL which is used only to determine the kind of
82 dialect to be used, as well as an "executor" callable function which
83 will receive a SQL expression object and parameters, which can then be
84 echoed or otherwise printed. The executor's return value is not handled,
85 nor does the engine allow regular string statements to be invoked, and
86 is therefore only useful for DDL that is sent to the database without
87 receiving any results.
88
89 E.g.::
90
91 from sqlalchemy import create_mock_engine
92
93
94 def dump(sql, *multiparams, **params):
95 print(sql.compile(dialect=engine.dialect))
96
97
98 engine = create_mock_engine("postgresql+psycopg2://", dump)
99 metadata.create_all(engine, checkfirst=False)
100
101 :param url: A string URL which typically needs to contain only the
102 database backend name.
103
104 :param executor: a callable which receives the arguments ``sql``,
105 ``*multiparams`` and ``**params``. The ``sql`` parameter is typically
106 an instance of :class:`.ExecutableDDLElement`, which can then be compiled
107 into a string using :meth:`.ExecutableDDLElement.compile`.
108
109 .. versionadded:: 1.4 - the :func:`.create_mock_engine` function replaces
110 the previous "mock" engine strategy used with
111 :func:`_sa.create_engine`.
112
113 .. seealso::
114
115 :ref:`faq_ddl_as_string`
116
117 """
118
119 # create url.URL object
120 u = _url.make_url(url)
121
122 dialect_cls = u.get_dialect()
123
124 dialect_args = {}
125 # consume dialect arguments from kwargs
126 for k in util.get_cls_kwargs(dialect_cls):
127 if k in kw:
128 dialect_args[k] = kw.pop(k)
129

Callers 4

mock_engineFunction · 0.90
mock_engineMethod · 0.90
_engine_fixtureMethod · 0.90
create_engineFunction · 0.85

Calls 3

MockConnectionClass · 0.85
get_dialectMethod · 0.45
popMethod · 0.45

Tested by 2

mock_engineMethod · 0.72
_engine_fixtureMethod · 0.72