Exemplo n.º 1
0
 def test_db_unknown_exception(self):
     try:
         create_connection({"database": "unknown"})
     except mariadb.ProgrammingError as err:
         self.assertEqual(err.sqlstate, "42000")
         self.assertEqual(err.errno, 1049)
         self.assertTrue(err.errmsg.find("Unknown database 'unknown'") > -1)
         pass
Exemplo n.º 2
0
 def test_conpy62(self):
     con= create_connection()
     cur = con.cursor()
     con= create_connection()
     query = "select round(.75 * (? / 3), 2) as val"
     cur.execute(query,[5])
     row= cur.fetchone()
     self.assertEqual(row[0], Decimal(1.25))
Exemplo n.º 3
0
    def test_ed25519(self):
        if is_skysql():
            self.skipTest("Test fail on SkySQL")
        default_conf = conf()
        if os.environ.get("MAXSCALE_VERSION"):
            self.skipTest("MAXSCALE doesn't support ed25519 for now")
        if self.connection.server_version < 100122:
            self.skipTest("ed25519 not supported")

        conn = self.connection
        curs = conn.cursor(buffered=True)

        if self.connection.server_name == "localhost":
            curs.execute(
                "select * from information_schema.plugins where plugin_name='unix_socket' and plugin_status='ACTIVE'"
            )
            if curs.rowcount > 0:
                del curs
                self.skipTest("unix_socket is active")

        cursor = conn.cursor()
        try:
            cursor.execute("INSTALL SONAME 'auth_ed25519'")
        except (mariadb.DatabaseError, mariadb.OperationalError):
            self.skipTest("Server couldn't load auth_ed25519")
        cursor.execute("DROP USER IF EXISTS eduser")
        if self.connection.server_version < 100400:
            cursor.execute(
                "CREATE USER eduser@'%' IDENTIFIED VIA ed25519 "
                "USING '6aW9C7ENlasUfymtfMvMZZtnkCVlcb1ssxOLJ0kj/AA'")
        else:
            cursor.execute("CREATE USER eduser@'%' IDENTIFIED VIA ed25519 "
                           "USING PASSWORD('MySup8%rPassw@ord')")
        cursor.execute("GRANT ALL on " + default_conf["database"] +
                       ".* to eduser@'%'")
        conn2 = create_connection({
            "user": "******",
            "password": "******"
        })
        cursor.execute("DROP USER IF EXISTS eduser")
        try:
            create_connection({
                "user": "******",
                "password": "******",
                "plugin_dir": "wrong_plugin_dir"
            })
            self.fail(
                "wrong plugin directory, must not have found authentication plugin"
            )
        except (mariadb.OperationalError):
            pass
        cursor.execute("DROP USER IF EXISTS eduser")
        del cursor, conn2
Exemplo n.º 4
0
 def test_conn_timeout_exception(self):
     start = datetime.today()
     try:
         create_connection({"connect_timeout": 1, "host": "8.8.8.8"})
     except mariadb.DatabaseError as err:
         self.assertEqual(err.sqlstate, "HY000")
         self.assertEqual(err.errno, 2002)
         self.assertTrue(err.errmsg.find("Can't connect to MySQL server on '8.8.8.8'") > -1)
         end = datetime.today()
         difference = end - start
         self.assertEqual(difference.days, 0)
         self.assertTrue(difference.seconds, 1)
         pass
Exemplo n.º 5
0
    def test_conpy61(self):
        con = create_connection()
        cursor = con.cursor()
        cursor.execute(
            "CREATE TEMPORARY TABLE ind1 (a int, b int default 2,c int)")
        vals = [(1, 4, 3), (None, 2, 3)]
        cursor.executemany("INSERT INTO ind1 VALUES (?,?,?)", vals)
        cursor.execute("SELECT a, b, c FROM ind1")
        row = cursor.fetchone()
        self.assertEqual(row[0], 1)
        row = cursor.fetchone()
        self.assertEqual(row[0], None)
        cursor.execute("DELETE FROM ind1")
        vals = [(1, 4, 3),
                (mariadb.indicator_null, mariadb.indicator_default, None)]

        cursor.executemany("INSERT INTO ind1 VALUES (?,?,?)", vals)
        cursor.execute("SELECT a, b, c FROM ind1")
        row = cursor.fetchone()
        self.assertEqual(row[0], 1)
        row = cursor.fetchone()
        self.assertEqual(row[0], None)
        self.assertEqual(row[1], 2)
        self.assertEqual(row[2], None)

        del cursor
 def test_tpc_begin(self):
     con = create_connection()
     xid = con.xid(0, "1234567890", "2345")
     try:
         con.tpc_begin(xid)
     except mariadb.NotSupportedError:
         pass
Exemplo n.º 7
0
    def test_conpy69(self):
        conn = create_connection()
        cursor1 = conn.cursor()
        cursor1.execute("CREATE SCHEMA IF NOT EXISTS 中文考试")
        default_conf = conf()
        default_conf["database"] = "中文考试"
        pool = mariadb.ConnectionPool(pool_name="test_conpy69")
        try:
            pool.set_config(**default_conf)
        except:
            del pool
            raise

        for i in range(1, 6):
            pool.add_connection()
        conn = mariadb.connect(pool_name="test_conpy69")
        cursor = conn.cursor()
        cursor.execute("select database()")
        row = cursor.fetchone()
        self.assertEqual(row[0], "中文考试")
        cursor.execute(
            "CREATE TABLE t1 (a varchar(255)) character set utf8mb4")
        cursor.execute("insert into t1 values (?)", ("123.45 中文考试", ))
        cursor.execute("select a from t1")
        row = cursor.fetchone()
        self.assertEqual(row[0], "123.45 中文考试")
        cursor1.execute("DROP SCHEMA 中文考试")
        del pool
Exemplo n.º 8
0
    def test_change_user(self):
        if is_skysql():
            self.skipTest("SkySQL failure")
        if self.connection.server_version < 100103:
            self.skipTest("CREATE OR REPLACE USER not supported")
        if is_maxscale():
            self.skipTest("MAXSCALE doesn't get new user immediately")
        if self.connection.server_name == "localhost":
            curs = self.connection.cursor(buffered=True)
            curs.execute(
                "select * from information_schema.plugins where plugin_name='unix_socket' and plugin_status='ACTIVE'"
            )
            if curs.rowcount > 0:
                del curs
                self.skipTest("unix_socket is active")
            del curs

        default_conf = conf()
        cursor = self.connection.cursor()
        cursor.execute("drop user if exists foo")
        if is_mysql() and self.connection.server_version < 80000:
            cursor.execute("create user foo@'%'")
            cursor.execute("GRANT ALL on `" + default_conf["database"] +
                           "`.* TO foo@'%' IDENTIFIED BY 'heyPassw-!µ20§rd'")
        else:
            cursor.execute(
                "create user foo@'%' IDENTIFIED BY 'heyPassw-!µ20§rd'")
            cursor.execute("GRANT ALL on `" + default_conf["database"] +
                           "`.* TO foo@'%'")
        new_conn = create_connection()
        new_conn.change_user("foo", "heyPassw-!µ20§rd", "")
        self.assertEqual("foo", new_conn.user)
        cursor.execute("drop user foo")
        del new_conn
        del cursor
Exemplo n.º 9
0
    def test_conpy61(self):
        if os.environ.get("MAXSCALE_VERSION"):
            self.skipTest("MAXSCALE doesn't support BULK yet")
        con= create_connection()
        if self.connection.server_version < server_indicator_version:
            self.skipTest("Requires server version >= 10.2.6")
        cursor=con.cursor()
        cursor.execute("CREATE TEMPORARY TABLE ind1 (a int, b int default 2,c int)")
        vals = [(1,4,3),(None, 2, 3)]
        cursor.executemany("INSERT INTO ind1 VALUES (?,?,?)", vals)
        cursor.execute("SELECT a, b, c FROM ind1")
        row= cursor.fetchone()
        self.assertEqual(row[0], 1)
        row= cursor.fetchone()
        self.assertEqual(row[0], None)
        cursor.execute("DELETE FROM ind1")
        vals=[(1,4,3), (INDICATOR.NULL, INDICATOR.DEFAULT, None)]
        cursor.executemany("INSERT INTO ind1 VALUES (?,?,?)", vals)
        cursor.execute("SELECT a, b, c FROM ind1")
        row= cursor.fetchone()
        self.assertEqual(row[0], 1)
        row= cursor.fetchone()
        self.assertEqual(row[0], None)
        self.assertEqual(row[1], 2)
        self.assertEqual(row[2], None)

        del cursor
Exemplo n.º 10
0
 def test_conpy98(self):
     con= create_connection()
     cursor=con.cursor()
     cursor.execute("SELECT CAST('foo' AS BINARY) AS anon_1")
     row= cursor.fetchone()
     self.assertEqual(row[0], b'foo')
     del cursor
    def test_change_user(self):
        if self.connection.server_version < 100103:
            self.skipTest("CREATE OR REPLACE USER not supported")
        if os.environ.get("MAXSCALE_VERSION"):
            self.skipTest("MAXSCALE doesn't get new user immediately")
        if self.connection.server_name == "localhost":
            curs = self.connection.cursor(buffered=True)
            curs.execute(
                "select * from information_schema.plugins where plugin_name='unix_socket' and plugin_status='ACTIVE'"
            )
            if curs.rowcount > 0:
                del curs
                self.skipTest("unix_socket is active")
            del curs

        default_conf = conf()
        cursor = self.connection.cursor()
        cursor.execute("drop user if exists foo")
        cursor.execute("create user foo@'%'")
        cursor.execute("GRANT ALL on `" + default_conf["database"] +
                       "`.* TO foo@'%'")
        new_conn = create_connection()
        new_conn.change_user("foo", "", "")
        self.assertEqual("foo", new_conn.user)
        cursor.execute("drop user foo")
        del new_conn
        del cursor
Exemplo n.º 12
0
 def test_conpy91(self):
     with create_connection() as connection:
         with connection.cursor() as cursor:
             for parameter_type in (int, decimal.Decimal):
                 with self.subTest(parameter_type=parameter_type):
                     with self.subTest(parameter_count=1):
                         with self.subTest(parameter_style='?'):
                             cursor.execute('select ?', [parameter_type(1)])
                             [[value]] = cursor.fetchall()
                             self.assertEqual(value, 1)
                         with self.subTest(parameter_style='%s'):
                             cursor.execute('select %s', [parameter_type(1)])
                             [[value]] = cursor.fetchall()
                             self.assertEqual(value, 1)
                         with self.subTest(parameter_style='%(name)s'):
                             cursor.execute('select %(value)s', dict(value=parameter_type(1)))
                             [[value]] = cursor.fetchall()
                             self.assertEqual(value, 1)
                     with self.subTest(parameter_count=2):
                         with self.subTest(parameter_style='?'):
                             cursor.execute('select ?, ?', [parameter_type(1), 1])
                             [[value, _]] = cursor.fetchall()
                             self.assertEqual(value, 1)
                         with self.subTest(parameter_style='%s'):
                             cursor.execute('select %s, %s', [parameter_type(1), 1])
                             [[value, _]] = cursor.fetchall()
                             self.assertEqual(value, 1)
                         with self.subTest(parameter_style='%(name)s'):
                             cursor.execute('select %(value)s, %(dummy)s', dict(value=parameter_type(1), dummy=1))
                             [[value, _]] = cursor.fetchall()
                             self.assertEqual(value, 1)
Exemplo n.º 13
0
 def test_conpy56(self):
     con= create_connection()
     cur=con.cursor(dictionary=True)
     cur.execute("select 'foo' as bar, 'bar' as foo")
     row= cur.fetchone()
     self.assertEqual(row["foo"], "bar")
     self.assertEqual(row["bar"], "foo")
     del con
Exemplo n.º 14
0
 def test_conpy129(self):
     conn= create_connection()
     server_version= conn.server_version
     major = int(server_version / 10000)
     minor = int((server_version % 10000) / 100)
     patch = server_version % 100;
     self.assertEqual(conn.server_version_info, (major, minor, patch))
     self.assertEqual(conn.get_server_version(), (major, minor, patch))
Exemplo n.º 15
0
 def test_conpy103(self):
     con= create_connection()
     cursor= con.cursor()
     cursor.execute("CREATE TEMPORARY TABLE t1 (a decimal(10,2))")
     cursor.executemany("INSERT INTO t1 VALUES (?)", [[decimal.Decimal(1)]])
     cursor.execute("SELECT a FROM t1")
     row= cursor.fetchone()
     self.assertEqual(row[0], decimal.Decimal(1))
Exemplo n.º 16
0
 def test_conpy94(self):
     con= create_connection()
     cur= con.cursor()
     a= foo(2)
     cur.execute("SELECT ?", (a,))
     row= cur.fetchone()
     self.assertEqual(row[0], 2)
     del cur
 def test_reconnect(self):
     new_conn = create_connection()
     conn1_id = new_conn.connection_id
     self.connection.kill(conn1_id)
     new_conn.reconnect()
     conn2_id = new_conn.connection_id
     self.assertFalse(conn1_id == conn2_id)
     del new_conn
Exemplo n.º 18
0
 def test_sp1(self):
     con= create_connection()
     cursor= con.cursor()
     cursor.execute("DROP PROCEDURE IF EXISTS p1")
     cursor.execute("CREATE PROCEDURE p1( )\nBEGIN\n SELECT 1;\nEND")
     cursor.callproc("p1")
     row= cursor.fetchone()
     self.assertEqual(row[0], 1)
     cursor.execute("DROP PROCEDURE IF EXISTS p1")
    def test_commit_in_tpc_fails(self):
        con = create_connection()
        try:
            xid = con.xid(0, "1234567897", "2345")
            con.tpc_begin(xid)

            self.assertRaises(mariadb.ProgrammingError, con.commit)
        finally:
            con.close()
Exemplo n.º 20
0
 def test_conpy49(self):
     con= create_connection()
     cur=con.cursor()
     cur.execute("create temporary table t1 (a decimal(10,2))")
     cur.execute("insert into t1 values (?)", (Decimal('10.2'),))
     cur.execute("select a from t1")
     row=cur.fetchone()
     self.assertEqual(row[0], Decimal('10.20'))
     del con
 def test_conpy42(self):
     con= create_connection()
     cursor = con.cursor()
     cursor.execute("CREATE TEMPORARY TABLE conpy42(a GEOMETRY)")
     cursor.execute("INSERT INTO conpy42 VALUES (PointFromText('point(1 1)'))")
     cursor.execute("SELECT a FROM conpy42")
     row= cursor.fetchone()
     self.assertEqual(row[0], b'\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?')
     del cursor
Exemplo n.º 22
0
 def test_conpy59(self):
     con= create_connection()
     cursor=con.cursor()
     cursor.execute("CREATE TEMPORARY TABLE t1 (a date)")
     cursor.execute("INSERT INTO t1 VALUES('0000-01-01')")
     cursor.execute("SELECT a FROM t1")
     row= cursor.fetchone()
     self.assertEqual(row[0], None)
     del con
Exemplo n.º 23
0
 def test_reconnect(self):
     if is_maxscale():
         self.skipTest("MAXSCALE wrong thread id")
     new_conn = create_connection()
     conn1_id = new_conn.connection_id
     self.connection.kill(conn1_id)
     new_conn.reconnect()
     conn2_id = new_conn.connection_id
     self.assertFalse(conn1_id == conn2_id)
     del new_conn
Exemplo n.º 24
0
 def test_none_val(self):
     con= create_connection()
     cur = con.cursor()
     cur.execute("CREATE TEMPORARY TABLE t1 (a int)")
     vals= [(1,),(2,), (4,), (None,), (3,)]
     cur.executemany("INSERT INTO t1 VALUES (?)", vals)
     cur.execute("select a from t1 order by a")
     rows= cur.fetchall()
     self.assertEqual(rows[0][0], None);
     del cur
Exemplo n.º 25
0
 def test_reconnect(self):
     if os.environ.get("MAXSCALE_VERSION"):
         self.skipTest("MAXSCALE wrong thread id")
     new_conn = create_connection()
     conn1_id = new_conn.connection_id
     self.connection.kill(conn1_id)
     new_conn.reconnect()
     conn2_id = new_conn.connection_id
     self.assertFalse(conn1_id == conn2_id)
     del new_conn
Exemplo n.º 26
0
 def test_conpy27(self):
     con = create_connection()
     cursor = con.cursor(prepared=True)
     cursor.execute("SELECT ?", (1,))
     row = cursor.fetchone()
     self.assertEqual(row[0], 1)
     cursor.execute("SELECT ?, ?, ?", ('foo',))
     row = cursor.fetchone()
     self.assertEqual(row[0], 'foo')
     del cursor, con
Exemplo n.º 27
0
 def test_conpy53(self):
     con= create_connection()
     cur=con.cursor()
     cur.execute("select 1", ())
     row=cur.fetchone()
     self.assertEqual(row[0], 1)
     cur.execute("select 1", [])
     row=cur.fetchone()
     self.assertEqual(row[0], 1)
     del con
Exemplo n.º 28
0
 def test_conpy47(self):
     con= create_connection()
     cursor=con.cursor()
     cursor.execute("SELECT ?", (True, ))
     row= cursor.fetchone()
     self.assertEqual(row[0], 1)
     cursor.execute("SELECT ?", (False, ))
     row= cursor.fetchone()
     self.assertEqual(row[0], 0)
     del con
Exemplo n.º 29
0
 def test_conpy46(self):
     con= create_connection()
     with con.cursor() as cursor:
        cursor.execute("SELECT 'foo'")
        row= cursor.fetchone()
     self.assertEqual(row[0], "foo")
     try:
        cursor.execute("SELECT 'bar'")
     except mariadb.ProgrammingError:
         pass
     del con
    def test_ping(self):
        if os.environ.get("MAXSCALE_VERSION"):
            self.skipTest("MAXSCALE return wrong thread id")

        new_conn = create_connection()
        id = new_conn.connection_id
        self.connection.kill(id)
        try:
            new_conn.ping()
        except mariadb.DatabaseError:
            pass
        del new_conn
        new_conn = create_connection()
        new_conn.auto_reconnect = True
        id = new_conn.connection_id
        self.connection.kill(id)
        new_conn.ping()
        new_id = new_conn.connection_id
        self.assertTrue(id != new_id)
        del new_conn