def test_plugin_multiple_url_registration(self): from sqlalchemy.dialects import sqlite global MyEnginePlugin1 global MyEnginePlugin2 def side_effect_1(url, kw): eq_(kw, {"logging_name": "foob"}) kw["logging_name"] = "bar" url.query.pop("myplugin1_arg", None) return MyEnginePlugin1 def side_effect_2(url, kw): url.query.pop("myplugin2_arg", None) return MyEnginePlugin2 MyEnginePlugin1 = Mock(side_effect=side_effect_1) MyEnginePlugin2 = Mock(side_effect=side_effect_2) plugins.register("engineplugin1", __name__, "MyEnginePlugin1") plugins.register("engineplugin2", __name__, "MyEnginePlugin2") e = create_engine( "sqlite:///?plugin=engineplugin1&foo=bar&myplugin1_arg=bat" "&plugin=engineplugin2&myplugin2_arg=hoho", logging_name="foob", ) eq_(e.dialect.name, "sqlite") eq_(e.logging_name, "bar") # plugin args are removed from URL. eq_(e.url.query, {"foo": "bar"}) assert isinstance(e.dialect, sqlite.dialect) eq_( MyEnginePlugin1.mock_calls, [ call(url.make_url("sqlite:///?foo=bar"), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e), ], ) eq_( MyEnginePlugin2.mock_calls, [ call(url.make_url("sqlite:///?foo=bar"), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e), ], )
def test_plugin_registration(self): from sqlalchemy.dialects import sqlite global MyEnginePlugin def side_effect(url, kw): eq_(kw, {"logging_name": "foob"}) kw['logging_name'] = 'bar' return MyEnginePlugin MyEnginePlugin = Mock(side_effect=side_effect) plugins.register("engineplugin", __name__, "MyEnginePlugin") e = create_engine( "sqlite:///?plugin=engineplugin&foo=bar", logging_name='foob') eq_(e.dialect.name, "sqlite") eq_(e.logging_name, "bar") assert isinstance(e.dialect, sqlite.dialect) eq_( MyEnginePlugin.mock_calls, [ call(e.url, {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e) ] ) eq_( str(MyEnginePlugin.mock_calls[0][1][0]), "sqlite:///?foo=bar" )
def test_plugin_url_registration(self): from sqlalchemy.dialects import sqlite global MyEnginePlugin def side_effect(url, kw): eq_( url.query, { "plugin": "engineplugin", "myplugin_arg": "bat", "foo": "bar", }, ) eq_(kw, {"logging_name": "foob"}) kw["logging_name"] = "bar" url.query.pop("myplugin_arg", None) return MyEnginePlugin MyEnginePlugin = Mock(side_effect=side_effect) plugins.register("engineplugin", __name__, "MyEnginePlugin") e = create_engine( "sqlite:///?plugin=engineplugin&foo=bar&myplugin_arg=bat", logging_name="foob", ) eq_(e.dialect.name, "sqlite") eq_(e.logging_name, "bar") # plugin args are removed from URL. eq_(e.url.query, {"foo": "bar"}) assert isinstance(e.dialect, sqlite.dialect) eq_( MyEnginePlugin.mock_calls, [ call(url.make_url("sqlite:///?foo=bar"), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e), ], ) # url was modified in place by MyEnginePlugin eq_(str(MyEnginePlugin.mock_calls[0][1][0]), "sqlite:///?foo=bar")
def test_plugin_arg_registration(self): from sqlalchemy.dialects import sqlite global MyEnginePlugin def side_effect(url, kw): eq_( kw, { "logging_name": "foob", "plugins": ["engineplugin"], "myplugin_arg": "bat", }, ) kw["logging_name"] = "bar" kw.pop("myplugin_arg", None) return MyEnginePlugin def update_url(url): return url.difference_update_query(["myplugin_arg"]) MyEnginePlugin = Mock(side_effect=side_effect, update_url=update_url) plugins.register("engineplugin", __name__, "MyEnginePlugin") e = create_engine( "sqlite:///?foo=bar", logging_name="foob", plugins=["engineplugin"], myplugin_arg="bat", ) eq_(e.dialect.name, "sqlite") eq_(e.logging_name, "bar") assert isinstance(e.dialect, sqlite.dialect) eq_( MyEnginePlugin.mock_calls, [ call(url.make_url("sqlite:///?foo=bar"), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e), ], )
def test_plugin_arg_registration(self): from sqlalchemy.dialects import sqlite global MyEnginePlugin def side_effect(url, kw): eq_( kw, { "logging_name": "foob", "plugins": ["engineplugin"], "myplugin_arg": "bat", }, ) kw["logging_name"] = "bar" kw.pop("myplugin_arg", None) return MyEnginePlugin MyEnginePlugin = Mock(side_effect=side_effect) plugins.register("engineplugin", __name__, "MyEnginePlugin") e = create_engine( "sqlite:///?foo=bar", logging_name="foob", plugins=["engineplugin"], myplugin_arg="bat", ) eq_(e.dialect.name, "sqlite") eq_(e.logging_name, "bar") assert isinstance(e.dialect, sqlite.dialect) eq_( MyEnginePlugin.mock_calls, [ call(url.make_url("sqlite:///?foo=bar"), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e), ], )
def test_plugin_url_registration(self): from sqlalchemy.dialects import sqlite global MyEnginePlugin def side_effect(url, kw): eq_(url.query, { "plugin": "engineplugin", "myplugin_arg": "bat", "foo": "bar" }) eq_(kw, {"logging_name": "foob"}) kw['logging_name'] = 'bar' url.query.pop('myplugin_arg', None) return MyEnginePlugin MyEnginePlugin = Mock(side_effect=side_effect) plugins.register("engineplugin", __name__, "MyEnginePlugin") e = create_engine( "sqlite:///?plugin=engineplugin&foo=bar&myplugin_arg=bat", logging_name='foob') eq_(e.dialect.name, "sqlite") eq_(e.logging_name, "bar") # plugin args are removed from URL. eq_(e.url.query, {"foo": "bar"}) assert isinstance(e.dialect, sqlite.dialect) eq_(MyEnginePlugin.mock_calls, [ call(url.make_url("sqlite:///?foo=bar"), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e) ]) # url was modified in place by MyEnginePlugin eq_(str(MyEnginePlugin.mock_calls[0][1][0]), "sqlite:///?foo=bar")
def test_plugin_multiple_url_registration(self): from sqlalchemy.dialects import sqlite global MyEnginePlugin1 global MyEnginePlugin2 def side_effect_1(url, kw): eq_(kw, {"logging_name": "foob"}) kw["logging_name"] = "bar" return MyEnginePlugin1 def side_effect_2(url, kw): return MyEnginePlugin2 def update_url(url): return url.difference_update_query( ["myplugin1_arg", "myplugin2_arg"] ) MyEnginePlugin1 = Mock( side_effect=side_effect_1, update_url=update_url ) MyEnginePlugin2 = Mock( side_effect=side_effect_2, update_url=update_url ) plugins.register("engineplugin1", __name__, "MyEnginePlugin1") plugins.register("engineplugin2", __name__, "MyEnginePlugin2") url_str = ( "sqlite:///?plugin=engineplugin1&foo=bar&myplugin1_arg=bat" "&plugin=engineplugin2&myplugin2_arg=hoho" ) e = create_engine( url_str, logging_name="foob", ) eq_(e.dialect.name, "sqlite") eq_(e.logging_name, "bar") # plugin args are removed from URL. eq_(e.url.query, {"foo": "bar"}) assert isinstance(e.dialect, sqlite.dialect) eq_( MyEnginePlugin1.mock_calls, [ call(url.make_url(url_str), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e), ], ) eq_( MyEnginePlugin2.mock_calls, [ call(url.make_url(url_str), {}), call.handle_dialect_kwargs(sqlite.dialect, mock.ANY), call.handle_pool_kwargs(mock.ANY, {"dialect": e.dialect}), call.engine_created(e), ], )