def test_multiple_nesting(self): callable_fn = mock.Mock( default=mock.Mock(return_value=None), mysql=mock.Mock(return_value=None) ) dispatcher = utils.dispatch_for_dialect("*", multiple=True)( callable_fn.default) dispatcher = dispatcher.dispatch_for("mysql+mysqlconnector")( dispatcher.dispatch_for("mysql+mysqldb")( callable_fn.mysql ) ) mysqldb_url = sqlalchemy.engine.url.make_url("mysql+mysqldb://") mysqlconnector_url = sqlalchemy.engine.url.make_url( "mysql+mysqlconnector://") sqlite_url = sqlalchemy.engine.url.make_url("sqlite://") dispatcher(mysqldb_url, 1) dispatcher(mysqlconnector_url, 2) dispatcher(sqlite_url, 3) self.assertEqual( [ mock.call.mysql(mysqldb_url, 1), mock.call.default(mysqldb_url, 1), mock.call.mysql(mysqlconnector_url, 2), mock.call.default(mysqlconnector_url, 2), mock.call.default(sqlite_url, 3) ], callable_fn.mock_calls )
def _multiple_fixture(self): callable_fn = mock.Mock() for targ in [ callable_fn.default, callable_fn.sqlite, callable_fn.mysql_mysqldb, callable_fn.postgresql, callable_fn.postgresql_psycopg2, callable_fn.pyodbc ]: targ.return_value = None dispatcher = orig = utils.dispatch_for_dialect("*", multiple=True)( callable_fn.default) dispatcher = dispatcher.dispatch_for("sqlite")(callable_fn.sqlite) dispatcher = dispatcher.dispatch_for("mysql+mysqldb")( callable_fn.mysql_mysqldb) dispatcher = dispatcher.dispatch_for("postgresql+*")( callable_fn.postgresql) dispatcher = dispatcher.dispatch_for("postgresql+psycopg2")( callable_fn.postgresql_psycopg2) dispatcher = dispatcher.dispatch_for("*+pyodbc")( callable_fn.pyodbc) self.assertTrue(dispatcher is orig) return dispatcher, callable_fn
def test_multiple_nesting(self): callable_fn = mock.Mock(default=mock.Mock(return_value=None), mysql=mock.Mock(return_value=None)) dispatcher = utils.dispatch_for_dialect("*", multiple=True)( callable_fn.default) dispatcher = dispatcher.dispatch_for("mysql+mysqlconnector")( dispatcher.dispatch_for("mysql+mysqldb")(callable_fn.mysql)) mysqldb_url = sqlalchemy.engine.url.make_url("mysql+mysqldb://") mysqlconnector_url = sqlalchemy.engine.url.make_url( "mysql+mysqlconnector://") sqlite_url = sqlalchemy.engine.url.make_url("sqlite://") dispatcher(mysqldb_url, 1) dispatcher(mysqlconnector_url, 2) dispatcher(sqlite_url, 3) self.assertEqual([ mock.call.mysql(mysqldb_url, 1), mock.call.default(mysqldb_url, 1), mock.call.mysql(mysqlconnector_url, 2), mock.call.default(mysqlconnector_url, 2), mock.call.default(sqlite_url, 3) ], callable_fn.mock_calls)
def test_multiple_no_dispatcher(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("sqlite", multiple=True)( callable_fn.sqlite) dispatcher = dispatcher.dispatch_for("mysql")(callable_fn.mysql) dispatcher("postgresql://*****:*****@localhost/test") self.assertEqual([], callable_fn.mock_calls)
def test_invalid_dispatch(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("*")(callable_fn.default) exc = self.assertRaises(ValueError, dispatcher.dispatch_for("+pyodbc"), callable_fn.pyodbc) self.assertEqual("Couldn't parse database[+driver]: '+pyodbc'", str(exc))
def test_multiple_no_dispatcher(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("sqlite", multiple=True)( callable_fn.sqlite) dispatcher = dispatcher.dispatch_for("mysql")(callable_fn.mysql) dispatcher("postgresql://*****:*****@localhost/test") self.assertEqual( [], callable_fn.mock_calls )
def test_single_no_dispatcher(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("sqlite")(callable_fn.sqlite) dispatcher = dispatcher.dispatch_for("mysql")(callable_fn.mysql) exc = self.assertRaises(ValueError, dispatcher, "postgresql://*****:*****@localhost/test") self.assertEqual( "No default function found for driver: 'postgresql+psycopg2'", str(exc))
def test_single_only_one_target(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("*")(callable_fn.default) dispatcher = dispatcher.dispatch_for("sqlite")(callable_fn.sqlite) exc = self.assertRaises(TypeError, dispatcher.dispatch_for("sqlite"), callable_fn.sqlite2) self.assertEqual("Multiple functions for expression 'sqlite'", str(exc))
def test_multiple_no_driver(self): callable_fn = mock.Mock(default=mock.Mock(return_value=None), sqlite=mock.Mock(return_value=None)) dispatcher = utils.dispatch_for_dialect("*", multiple=True)( callable_fn.default) dispatcher = dispatcher.dispatch_for("sqlite")(callable_fn.sqlite) dispatcher.dispatch_on_drivername("sqlite")("foo") self.assertEqual([mock.call.sqlite("foo"), mock.call.default("foo")], callable_fn.mock_calls)
def test_single_only_one_target(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("*")(callable_fn.default) dispatcher = dispatcher.dispatch_for("sqlite")(callable_fn.sqlite) exc = self.assertRaises( TypeError, dispatcher.dispatch_for("sqlite"), callable_fn.sqlite2 ) self.assertEqual( "Multiple functions for expression 'sqlite'", str(exc) )
def test_invalid_dispatch(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("*")(callable_fn.default) exc = self.assertRaises( ValueError, dispatcher.dispatch_for("+pyodbc"), callable_fn.pyodbc ) self.assertEqual( "Couldn't parse database[+driver]: '+pyodbc'", str(exc) )
def test_single_no_dispatcher(self): callable_fn = mock.Mock() dispatcher = utils.dispatch_for_dialect("sqlite")(callable_fn.sqlite) dispatcher = dispatcher.dispatch_for("mysql")(callable_fn.mysql) exc = self.assertRaises( ValueError, dispatcher, "postgresql://*****:*****@localhost/test" ) self.assertEqual( "No default function found for driver: 'postgresql+psycopg2'", str(exc) )
def _single_fixture(self): callable_fn = mock.Mock() dispatcher = orig = utils.dispatch_for_dialect("*")( callable_fn.default) dispatcher = dispatcher.dispatch_for("sqlite")(callable_fn.sqlite) dispatcher = dispatcher.dispatch_for("mysql+mysqldb")( callable_fn.mysql_mysqldb) dispatcher = dispatcher.dispatch_for("postgresql")( callable_fn.postgresql) self.assertTrue(dispatcher is orig) return dispatcher, callable_fn
def test_multiple_no_driver(self): callable_fn = mock.Mock( default=mock.Mock(return_value=None), sqlite=mock.Mock(return_value=None) ) dispatcher = utils.dispatch_for_dialect("*", multiple=True)( callable_fn.default) dispatcher = dispatcher.dispatch_for("sqlite")( callable_fn.sqlite) dispatcher.dispatch_on_drivername("sqlite")("foo") self.assertEqual( [mock.call.sqlite("foo"), mock.call.default("foo")], callable_fn.mock_calls )