def test_comparison(self):
        components = (
            "drivername",
            "username",
            "password",
            "host",
            "database",
            "query",
            "port",
        )

        common_url = (
            "dbtype://*****:*****@[2001:da8:2004:1000:202:116:160:90]:80/database?foo=bar"
        )
        other_url = "dbtype://*****:*****@host/"

        url1 = url.make_url(common_url)
        url2 = url.make_url(common_url)
        url3 = url.make_url(other_url)

        is_true(url1 == url2)
        is_false(url1 != url2)
        is_true(url1 != url3)
        is_false(url1 == url3)

        for curr_component in components:
            setattr(url2, curr_component, "new_changed_value")
            is_true(url1 != url2)
            is_false(url1 == url2)
            setattr(url2, curr_component, getattr(url1, curr_component))
    def test_rfc1738_password(self):
        u = url.make_url("dbtype://*****:*****@host/dbname")
        eq_(u.password, "pass word + other:words")
        eq_(str(u), "dbtype://*****:*****@host/dbname")

        u = url.make_url(
            "dbtype://*****:*****@hostspec/database"
        )
        eq_(u.password, "apples/oranges")
        eq_(str(u), "dbtype://*****:*****@hostspec/database")

        u = url.make_url(
            "dbtype://*****:*****@hostspec/database"
        )
        eq_(u.password, "apples@oranges@@")
        eq_(
            str(u),
            "dbtype://*****:*****@hostspec/database",
        )

        u = url.make_url("dbtype://username%40:@hostspec/database")
        eq_(u.password, "")
        eq_(u.username, "username@")
        eq_(str(u), "dbtype://username%40:@hostspec/database")

        u = url.make_url("dbtype://*****:*****@hostspec/database")
        eq_(u.password, "pass/word")
        eq_(str(u), "dbtype://*****:*****@hostspec/database")
    def test_plugin_multiple_url_registration(self):
        from sqlalchemy_1_3.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),
            ],
        )
Example #4
0
 def test_psycopg2_nonempty_connection_string_w_query_two(self):
     dialect = psycopg2_dialect.dialect()
     url_string = "postgresql://*****:*****@/DB?host=hostA"
     u = url.make_url(url_string)
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams["host"], "hostA")
Example #5
0
 def test_threaded_deprecated_at_dialect_level(self):
     with testing.expect_deprecated(
             "The 'threaded' parameter to the cx_oracle dialect"):
         dialect = cx_oracle.dialect(threaded=False)
     arg, kw = dialect.create_connect_args(
         url.make_url("oracle+cx_oracle://scott:tiger@dsn"))
     eq_(kw["threaded"], False)
Example #6
0
    def _test_db_opt_unpresent(self, url_string, key):
        import cx_Oracle

        url_obj = url.make_url(url_string)
        dialect = cx_oracle.dialect(dbapi=cx_Oracle)
        arg, kw = dialect.create_connect_args(url_obj)
        assert key not in kw
Example #7
0
    def _test_db_opt(self, url_string, key, value):
        import cx_Oracle

        url_obj = url.make_url(url_string)
        dialect = cx_oracle.dialect(dbapi=cx_Oracle)
        arg, kw = dialect.create_connect_args(url_obj)
        eq_(kw[key], value)
Example #8
0
 def _test_ssl_arguments(self, dialect):
     kwarg = dialect.create_connect_args(
         make_url(
             "mysql://*****:*****@localhost:3306/test"
             "?ssl_ca=/ca.pem&ssl_cert=/cert.pem&ssl_key=/key.pem"
         )
     )[1]
     # args that differ among mysqldb and oursql
     for k in ("use_unicode", "found_rows", "client_flag"):
         kwarg.pop(k, None)
     eq_(
         kwarg,
         {
             "passwd": "tiger",
             "db": "test",
             "ssl": {
                 "ca": "/ca.pem",
                 "cert": "/cert.pem",
                 "key": "/key.pem",
             },
             "host": "localhost",
             "user": "******",
             "port": 3306,
         },
     )
    def test_wrapper_hooks(self):
        def get_dialect_cls(url):
            url.drivername = "sqlite"
            return url.get_dialect()

        global WrapperFactory
        WrapperFactory = Mock()
        WrapperFactory.get_dialect_cls.side_effect = get_dialect_cls

        registry.register("wrapperdialect", __name__, "WrapperFactory")

        from sqlalchemy_1_3.dialects import sqlite

        e = create_engine("wrapperdialect://")

        eq_(e.dialect.name, "sqlite")
        assert isinstance(e.dialect, sqlite.dialect)

        eq_(
            WrapperFactory.mock_calls,
            [
                call.get_dialect_cls(url.make_url("sqlite://")),
                call.engine_created(e),
            ],
        )
    def test_query_string(self):
        u = url.make_url("dialect://*****:*****@host/db?arg1=param1&arg2=param2")
        eq_(u.query, {"arg1": "param1", "arg2": "param2"})
        eq_(str(u), "dialect://*****:*****@host/db?arg1=param1&arg2=param2")

        u = url.make_url(
            "dialect://*****:*****@host/db?arg1=param1&arg2=param2&arg2=param3"
        )
        eq_(u.query, {"arg1": "param1", "arg2": ["param2", "param3"]})
        eq_(
            str(u),
            "dialect://*****:*****@host/db?arg1=param1&arg2=param2&arg2=param3",
        )

        test_url = "dialect://*****:*****@host/db?arg1%3D=param1&arg2=param+2"
        u = url.make_url(test_url)
        eq_(u.query, {"arg1=": "param1", "arg2": "param 2"})
        eq_(str(u), test_url)
Example #11
0
    def test_mysqlconnector_buffered_arg(self):
        from sqlalchemy_1_3.dialects.mysql import mysqlconnector

        dialect = mysqlconnector.dialect()
        kw = dialect.create_connect_args(
            make_url("mysql+mysqlconnector://u:p@host/db?buffered=true")
        )[1]
        eq_(kw["buffered"], True)

        kw = dialect.create_connect_args(
            make_url("mysql+mysqlconnector://u:p@host/db?buffered=false")
        )[1]
        eq_(kw["buffered"], False)

        kw = dialect.create_connect_args(
            make_url("mysql+mysqlconnector://u:p@host/db")
        )[1]
        eq_(kw["buffered"], True)
Example #12
0
 def test_psycopg2_nonempty_connection_string_w_query_three(self):
     dialect = psycopg2_dialect.dialect()
     url_string = (
         "postgresql://*****:*****@/DB"
         "?host=hostA:portA&host=hostB&host=hostC"
     )
     u = url.make_url(url_string)
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams["host"], "hostA:portA,hostB,hostC")
Example #13
0
    def test_normal_arguments_mysqldb(self, kwarg, value):
        from sqlalchemy_1_3.dialects.mysql import mysqldb

        dialect = mysqldb.dialect()
        connect_args = dialect.create_connect_args(
            make_url(
                "mysql://*****:*****@localhost:3306/test"
                "?%s=%s" % (kwarg, value)
            )
        )

        eq_(connect_args[1][kwarg], value)
Example #14
0
    def _test_dialect_param_from_url(self, url_string, key, value):
        import cx_Oracle

        url_obj = url.make_url(url_string)
        dialect = cx_oracle.dialect(dbapi=cx_Oracle)
        with testing.expect_deprecated("cx_oracle dialect option %r should" %
                                       key):
            arg, kw = dialect.create_connect_args(url_obj)
        eq_(getattr(dialect, key), value)

        # test setting it on the dialect normally
        dialect = cx_oracle.dialect(dbapi=cx_Oracle, **{key: value})
        eq_(getattr(dialect, key), value)
Example #15
0
    def test_mysqlconnector_raise_on_warnings_arg(self):
        from sqlalchemy_1_3.dialects.mysql import mysqlconnector

        dialect = mysqlconnector.dialect()
        kw = dialect.create_connect_args(
            make_url(
                "mysql+mysqlconnector://u:p@host/db?raise_on_warnings=true"
            )
        )[1]
        eq_(kw["raise_on_warnings"], True)

        kw = dialect.create_connect_args(
            make_url(
                "mysql+mysqlconnector://u:p@host/db?raise_on_warnings=false"
            )
        )[1]
        eq_(kw["raise_on_warnings"], False)

        kw = dialect.create_connect_args(
            make_url("mysql+mysqlconnector://u:p@host/db")
        )[1]
        assert "raise_on_warnings" not in kw
Example #16
0
    def _pool_fixture(self, pre_ping):
        dialect = url.make_url(
            "postgresql://*****:*****@localhost/test").get_dialect()()
        dialect.dbapi = self.dbapi
        _pool = pool.QueuePool(
            creator=lambda: self.dbapi.connect("foo.db"),
            pre_ping=pre_ping,
            dialect=dialect,
        )

        dialect.is_disconnect = lambda e, conn, cursor: isinstance(
            e, MockDisconnect)
        return _pool
    def test_rfc1738(self):
        for text in (
            "dbtype://*****:*****@hostspec:110//usr/db_file.db",
            "dbtype://*****:*****@hostspec/database",
            "dbtype+apitype://username:password@hostspec/database",
            "dbtype://*****:*****@hostspec",
            "dbtype://*****:*****@/database",
            "dbtype://username@hostspec",
            "dbtype://*****:*****@127.0.0.1:1521",
            "dbtype://hostspec/database",
            "dbtype://hostspec",
            "dbtype://hostspec/?arg1=val1&arg2=val2",
            "dbtype+apitype:///database",
            "dbtype:///:memory:",
            "dbtype:///foo/bar/im/a/file",
            "dbtype:///E:/work/src/LEM/db/hello.db",
            "dbtype:///E:/work/src/LEM/db/hello.db?foo=bar&hoho=lala",
            "dbtype:///E:/work/src/LEM/db/hello.db?foo=bar&hoho=lala&hoho=bat",
            "dbtype://",
            "dbtype://*****:*****@/database",
            "dbtype:////usr/local/[email protected]/members.db",
            "dbtype://*****:*****@hostspec/database",
            "dbtype://*****:*****@[2001:da8:2004:1000:202:116:160:90]"
            "/database?foo=bar",
            "dbtype://*****:*****@[2001:da8:2004:1000:202:116:160:90]:80"
            "/database?foo=bar",
        ):
            u = url.make_url(text)

            assert u.drivername in ("dbtype", "dbtype+apitype")
            assert u.username in ("username", None)
            assert u.password in ("password", "apples/oranges", None)
            assert u.host in (
                "hostspec",
                "127.0.0.1",
                "2001:da8:2004:1000:202:116:160:90",
                "",
                None,
            ), u.host
            assert u.database in (
                "database",
                "/usr/local/[email protected]/members.db",
                "/usr/db_file.db",
                ":memory:",
                "",
                "foo/bar/im/a/file",
                "E:/work/src/LEM/db/hello.db",
                None,
            ), u.database
            eq_(str(u), text)
    def test_engine_from_config(self):
        dbapi = mock_dbapi

        config = {
            "sqlalchemy.url": "postgresql://*****:*****@somehost/test"
            "?fooz=somevalue",
            "sqlalchemy.pool_recycle": "50",
            "sqlalchemy.echo": "true",
        }

        e = engine_from_config(config, module=dbapi, _initialize=False)
        assert e.pool._recycle == 50
        assert e.url == url.make_url(
            "postgresql://*****:*****@somehost/test?foo" "z=somevalue"
        )
        assert e.echo is True
    def test_plugin_url_registration(self):
        from sqlalchemy_1_3.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_urlattr(self):
        """test the url attribute on ``Engine``."""

        e = create_engine(
            "mysql://*****:*****@localhost/test",
            module=mock_dbapi,
            _initialize=False,
        )
        u = url.make_url("mysql://*****:*****@localhost/test")
        e2 = create_engine(u, module=mock_dbapi, _initialize=False)
        assert e.url.drivername == e2.url.drivername == "mysql"
        assert e.url.username == e2.url.username == "scott"
        assert e2.url is u
        assert str(u) == "mysql://*****:*****@localhost/test"
        assert repr(u) == "mysql://*****:*****@localhost/test"
        assert repr(e) == "Engine(mysql://scott:***@localhost/test)"
        assert repr(e2) == "Engine(mysql://scott:***@localhost/test)"
    def test_plugin_arg_registration(self):
        from sqlalchemy_1_3.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),
            ],
        )
Example #22
0
def testing_engine(url=None, options=None):
    """Produce an engine configured by --options with optional overrides."""

    from sqlalchemy_1_3 import create_engine
    from sqlalchemy_1_3.engine.url import make_url

    if not options:
        use_reaper = True
    else:
        use_reaper = options.pop("use_reaper", True)

    url = url or config.db.url

    url = make_url(url)
    if options is None:
        if config.db is None or url.drivername == config.db.url.drivername:
            options = config.db_opts
        else:
            options = {}
    elif config.db is not None and url.drivername == config.db.url.drivername:
        default_opt = config.db_opts.copy()
        default_opt.update(options)

    engine = create_engine(url, **options)
    engine._has_events = True  # enable event blocks, helps with profiling

    if isinstance(engine.pool, pool.QueuePool):
        engine.pool._timeout = 0
        engine.pool._max_overflow = 0
    if use_reaper:
        event.listen(engine.pool, "connect", testing_reaper.connect)
        event.listen(engine.pool, "checkout", testing_reaper.checkout)
        event.listen(engine.pool, "invalidate", testing_reaper.invalidate)
        testing_reaper.add_engine(engine)

    return engine
Example #23
0
 def test_psycopg2_nonempty_connection_string_w_query(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql://somehost/?any_random_thing=yes")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"host": "somehost", "any_random_thing": "yes"})
Example #24
0
 def test_random_arg(self):
     dialect = testing.db.dialect
     kw = dialect.create_connect_args(
         make_url("mysql://*****:*****@host/db?foo=true")
     )[1]
     eq_(kw["foo"], "true")
Example #25
0
 def test_psycopg2_nonempty_connection_string(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql://host")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"host": "host"})
Example #26
0
 def test_psycopg2_empty_connection_string_w_query_one(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql:///?service=swh-log")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"service": "swh-log"})