Python pymysql.Warning() Examples

The following are 16 code examples of pymysql.Warning(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pymysql , or try the search function .
Example #1
Source File: test_load_local.py    From ServerlessCrawler-VancouverRealState with MIT License 6 votes vote down vote up
def test_load_warnings(self):
        """Test load local infile produces the appropriate warnings"""
        conn = self.connections[0]
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'data',
                                'load_local_warn_data.txt')
        try:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                c.execute(
                    ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
                     "test_load_local FIELDS TERMINATED BY ','").format(filename)
                )
                self.assertEqual(w[0].category, Warning)
                expected_message = "Incorrect integer value"
                if expected_message not in str(w[-1].message):
                    self.fail("%r not in %r" % (expected_message, w[-1].message))
        finally:
            c.execute("DROP TABLE test_load_local")
            c.close() 
Example #2
Source File: test_issues.py    From ServerlessCrawler-VancouverRealState with MIT License 6 votes vote down vote up
def test_issue_491(self):
        """ Test warning propagation """
        conn = pymysql.connect(charset="utf8", **self.databases[0])

        with warnings.catch_warnings():
            # Ignore all warnings other than pymysql generated ones
            warnings.simplefilter("ignore")
            warnings.simplefilter("error", category=pymysql.Warning)

            # verify for both buffered and unbuffered cursor types
            for cursor_class in (cursors.Cursor, cursors.SSCursor):
                c = conn.cursor(cursor_class)
                try:
                    c.execute("SELECT CAST('124b' AS SIGNED)")
                    c.fetchall()
                except pymysql.Warning as e:
                    # Warnings should have errorcode and string message, just like exceptions
                    self.assertEqual(len(e.args), 2)
                    self.assertEqual(e.args[0], 1292)
                    self.assertTrue(isinstance(e.args[1], text_type))
                else:
                    self.fail("Should raise Warning")
                finally:
                    c.close() 
Example #3
Source File: test_load_local.py    From VaspCZ with MIT License 6 votes vote down vote up
def test_load_warnings(self):
        """Test load local infile produces the appropriate warnings"""
        conn = self.connections[0]
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'data',
                                'load_local_warn_data.txt')
        try:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                c.execute(
                    ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
                     "test_load_local FIELDS TERMINATED BY ','").format(filename)
                )
                self.assertEqual(w[0].category, Warning)
                self.assertTrue("Incorrect integer value" in str(w[-1].message))
        finally:
            c.execute("DROP TABLE test_load_local") 
Example #4
Source File: test_load_local.py    From satori with Apache License 2.0 6 votes vote down vote up
def test_load_warnings(self):
        """Test load local infile produces the appropriate warnings"""
        conn = self.connections[0]
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'data',
                                'load_local_warn_data.txt')
        try:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                c.execute(
                    ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
                     "test_load_local FIELDS TERMINATED BY ','").format(filename)
                )
                self.assertEqual(w[0].category, Warning)
                expected_message = "Incorrect integer value"
                if expected_message not in str(w[-1].message):
                    self.fail("%r not in %r" % (expected_message, w[-1].message))
        finally:
            c.execute("DROP TABLE test_load_local")
            c.close() 
Example #5
Source File: test_issues.py    From satori with Apache License 2.0 6 votes vote down vote up
def test_issue_491(self):
        """ Test warning propagation """
        conn = pymysql.connect(charset="utf8", **self.databases[0])

        with warnings.catch_warnings():
            # Ignore all warnings other than pymysql generated ones
            warnings.simplefilter("ignore")
            warnings.simplefilter("error", category=pymysql.Warning)

            # verify for both buffered and unbuffered cursor types
            for cursor_class in (cursors.Cursor, cursors.SSCursor):
                c = conn.cursor(cursor_class)
                try:
                    c.execute("SELECT CAST('124b' AS SIGNED)")
                    c.fetchall()
                except pymysql.Warning as e:
                    # Warnings should have errorcode and string message, just like exceptions
                    self.assertEqual(len(e.args), 2)
                    self.assertEqual(e.args[0], 1292)
                    self.assertTrue(isinstance(e.args[1], text_type))
                else:
                    self.fail("Should raise Warning")
                finally:
                    c.close() 
Example #6
Source File: test_load_local.py    From aws-servicebroker with Apache License 2.0 6 votes vote down vote up
def test_load_warnings(self):
        """Test load local infile produces the appropriate warnings"""
        conn = self.connections[0]
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'data',
                                'load_local_warn_data.txt')
        try:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                c.execute(
                    ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
                     "test_load_local FIELDS TERMINATED BY ','").format(filename)
                )
                self.assertEqual(w[0].category, Warning)
                expected_message = "Incorrect integer value"
                if expected_message not in str(w[-1].message):
                    self.fail("%r not in %r" % (expected_message, w[-1].message))
        finally:
            c.execute("DROP TABLE test_load_local")
            c.close() 
Example #7
Source File: test_issues.py    From aws-servicebroker with Apache License 2.0 6 votes vote down vote up
def test_issue_491(self):
        """ Test warning propagation """
        conn = pymysql.connect(charset="utf8", **self.databases[0])

        with warnings.catch_warnings():
            # Ignore all warnings other than pymysql generated ones
            warnings.simplefilter("ignore")
            warnings.simplefilter("error", category=pymysql.Warning)

            # verify for both buffered and unbuffered cursor types
            for cursor_class in (cursors.Cursor, cursors.SSCursor):
                c = conn.cursor(cursor_class)
                try:
                    c.execute("SELECT CAST('124b' AS SIGNED)")
                    c.fetchall()
                except pymysql.Warning as e:
                    # Warnings should have errorcode and string message, just like exceptions
                    self.assertEqual(len(e.args), 2)
                    self.assertEqual(e.args[0], 1292)
                    self.assertTrue(isinstance(e.args[1], text_type))
                else:
                    self.fail("Should raise Warning")
                finally:
                    c.close() 
Example #8
Source File: test_load_local.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def test_load_warnings(self):
        """Test load local infile produces the appropriate warnings"""
        conn = self.connect()
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'data',
                                'load_local_warn_data.txt')
        try:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                c.execute(
                    ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
                     "test_load_local FIELDS TERMINATED BY ','").format(filename)
                )
                self.assertEqual(w[0].category, Warning)
                expected_message = "Incorrect integer value"
                if expected_message not in str(w[-1].message):
                    self.fail("%r not in %r" % (expected_message, w[-1].message))
        finally:
            c.execute("DROP TABLE test_load_local")
            c.close() 
Example #9
Source File: test_issues.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def test_issue_491(self):
        """ Test warning propagation """
        conn = pymysql.connect(charset="utf8", **self.databases[0])

        with warnings.catch_warnings():
            # Ignore all warnings other than pymysql generated ones
            warnings.simplefilter("ignore")
            warnings.simplefilter("error", category=pymysql.Warning)

            # verify for both buffered and unbuffered cursor types
            for cursor_class in (cursors.Cursor, cursors.SSCursor):
                c = conn.cursor(cursor_class)
                try:
                    c.execute("SELECT CAST('124b' AS SIGNED)")
                    c.fetchall()
                except pymysql.Warning as e:
                    # Warnings should have errorcode and string message, just like exceptions
                    self.assertEqual(len(e.args), 2)
                    self.assertEqual(e.args[0], 1292)
                    self.assertTrue(isinstance(e.args[1], text_type))
                else:
                    self.fail("Should raise Warning")
                finally:
                    c.close() 
Example #10
Source File: test_load_local.py    From planespotter with MIT License 6 votes vote down vote up
def test_load_warnings(self):
        """Test load local infile produces the appropriate warnings"""
        conn = self.connections[0]
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'data',
                                'load_local_warn_data.txt')
        try:
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always')
                c.execute(
                    ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
                     "test_load_local FIELDS TERMINATED BY ','").format(filename)
                )
                self.assertEqual(w[0].category, Warning)
                expected_message = "Incorrect integer value"
                if expected_message not in str(w[-1].message):
                    self.fail("%r not in %r" % (expected_message, w[-1].message))
        finally:
            c.execute("DROP TABLE test_load_local")
            c.close() 
Example #11
Source File: test_issues.py    From planespotter with MIT License 6 votes vote down vote up
def test_issue_491(self):
        """ Test warning propagation """
        conn = pymysql.connect(charset="utf8", **self.databases[0])

        with warnings.catch_warnings():
            # Ignore all warnings other than pymysql generated ones
            warnings.simplefilter("ignore")
            warnings.simplefilter("error", category=pymysql.Warning)

            # verify for both buffered and unbuffered cursor types
            for cursor_class in (cursors.Cursor, cursors.SSCursor):
                c = conn.cursor(cursor_class)
                try:
                    c.execute("SELECT CAST('124b' AS SIGNED)")
                    c.fetchall()
                except pymysql.Warning as e:
                    # Warnings should have errorcode and string message, just like exceptions
                    self.assertEqual(len(e.args), 2)
                    self.assertEqual(e.args[0], 1292)
                    self.assertTrue(isinstance(e.args[1], text_type))
                else:
                    self.fail("Should raise Warning")
                finally:
                    c.close() 
Example #12
Source File: test_issues.py    From ServerlessCrawler-VancouverRealState with MIT License 4 votes vote down vote up
def test_issue_363(self):
        """ Test binary / geometry types. """
        conn = pymysql.connect(charset="utf8", **self.databases[0])
        self.safe_create_table(
            conn, "issue363",
            "CREATE TABLE issue363 ( "
            "id INTEGER PRIMARY KEY, geom LINESTRING NOT NULL, "
            "SPATIAL KEY geom (geom)) "
            "ENGINE=MyISAM default charset=utf8")

        cur = conn.cursor()
        query = ("INSERT INTO issue363 (id, geom) VALUES"
                 "(1998, GeomFromText('LINESTRING(1.1 1.1,2.2 2.2)'))")
        # From MySQL 5.7, ST_GeomFromText is added and GeomFromText is deprecated.
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)

        # select WKT
        query = "SELECT AsText(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row, ("LINESTRING(1.1 1.1,2.2 2.2)", ))

        # select WKB
        query = "SELECT AsBinary(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row,
                         (b"\x01\x02\x00\x00\x00\x02\x00\x00\x00"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\x01@"
                          b"\x9a\x99\x99\x99\x99\x99\x01@", ))

        # select internal binary
        cur.execute("SELECT geom FROM issue363")
        row = cur.fetchone()
        # don't assert the exact internal binary value, as it could
        # vary across implementations
        self.assertTrue(isinstance(row[0], bytes)) 
Example #13
Source File: test_issues.py    From satori with Apache License 2.0 4 votes vote down vote up
def test_issue_363(self):
        """ Test binary / geometry types. """
        conn = pymysql.connect(charset="utf8", **self.databases[0])
        self.safe_create_table(
            conn, "issue363",
            "CREATE TABLE issue363 ( "
            "id INTEGER PRIMARY KEY, geom LINESTRING NOT NULL, "
            "SPATIAL KEY geom (geom)) "
            "ENGINE=MyISAM default charset=utf8")

        cur = conn.cursor()
        query = ("INSERT INTO issue363 (id, geom) VALUES"
                 "(1998, GeomFromText('LINESTRING(1.1 1.1,2.2 2.2)'))")
        # From MySQL 5.7, ST_GeomFromText is added and GeomFromText is deprecated.
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)

        # select WKT
        query = "SELECT AsText(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row, ("LINESTRING(1.1 1.1,2.2 2.2)", ))

        # select WKB
        query = "SELECT AsBinary(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row,
                         (b"\x01\x02\x00\x00\x00\x02\x00\x00\x00"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\x01@"
                          b"\x9a\x99\x99\x99\x99\x99\x01@", ))

        # select internal binary
        cur.execute("SELECT geom FROM issue363")
        row = cur.fetchone()
        # don't assert the exact internal binary value, as it could
        # vary across implementations
        self.assertTrue(isinstance(row[0], bytes)) 
Example #14
Source File: test_issues.py    From aws-servicebroker with Apache License 2.0 4 votes vote down vote up
def test_issue_363(self):
        """ Test binary / geometry types. """
        conn = pymysql.connect(charset="utf8", **self.databases[0])
        self.safe_create_table(
            conn, "issue363",
            "CREATE TABLE issue363 ( "
            "id INTEGER PRIMARY KEY, geom LINESTRING NOT NULL, "
            "SPATIAL KEY geom (geom)) "
            "ENGINE=MyISAM default charset=utf8")

        cur = conn.cursor()
        query = ("INSERT INTO issue363 (id, geom) VALUES"
                 "(1998, GeomFromText('LINESTRING(1.1 1.1,2.2 2.2)'))")
        # From MySQL 5.7, ST_GeomFromText is added and GeomFromText is deprecated.
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)

        # select WKT
        query = "SELECT AsText(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row, ("LINESTRING(1.1 1.1,2.2 2.2)", ))

        # select WKB
        query = "SELECT AsBinary(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row,
                         (b"\x01\x02\x00\x00\x00\x02\x00\x00\x00"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\x01@"
                          b"\x9a\x99\x99\x99\x99\x99\x01@", ))

        # select internal binary
        cur.execute("SELECT geom FROM issue363")
        row = cur.fetchone()
        # don't assert the exact internal binary value, as it could
        # vary across implementations
        self.assertTrue(isinstance(row[0], bytes)) 
Example #15
Source File: test_issues.py    From planespotter with MIT License 4 votes vote down vote up
def test_issue_363(self):
        """ Test binary / geometry types. """
        conn = pymysql.connect(charset="utf8", **self.databases[0])
        self.safe_create_table(
            conn, "issue363",
            "CREATE TABLE issue363 ( "
            "id INTEGER PRIMARY KEY, geom LINESTRING NOT NULL, "
            "SPATIAL KEY geom (geom)) "
            "ENGINE=MyISAM default charset=utf8")

        cur = conn.cursor()
        query = ("INSERT INTO issue363 (id, geom) VALUES"
                 "(1998, GeomFromText('LINESTRING(1.1 1.1,2.2 2.2)'))")
        # From MySQL 5.7, ST_GeomFromText is added and GeomFromText is deprecated.
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)

        # select WKT
        query = "SELECT AsText(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row, ("LINESTRING(1.1 1.1,2.2 2.2)", ))

        # select WKB
        query = "SELECT AsBinary(geom) FROM issue363"
        if self.mysql_server_is(conn, (5, 7, 0)):
            with self.assertWarns(pymysql.err.Warning) as cm:
                cur.execute(query)
        else:
            cur.execute(query)
        row = cur.fetchone()
        self.assertEqual(row,
                         (b"\x01\x02\x00\x00\x00\x02\x00\x00\x00"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\xf1?"
                          b"\x9a\x99\x99\x99\x99\x99\x01@"
                          b"\x9a\x99\x99\x99\x99\x99\x01@", ))

        # select internal binary
        cur.execute("SELECT geom FROM issue363")
        row = cur.fetchone()
        # don't assert the exact internal binary value, as it could
        # vary across implementations
        self.assertTrue(isinstance(row[0], bytes)) 
Example #16
Source File: conftest.py    From terracotta with MIT License 4 votes vote down vote up
def driver_path(provider, tmpdir, mysql_server):
    """Get a valid, uninitialized driver path for given provider"""
    import random
    import string

    from urllib.parse import urlparse

    def validate_con_info(con_info):
        return (con_info.scheme == 'mysql'
                and con_info.hostname
                and con_info.username
                and not con_info.path)

    def random_string(length):
        return ''.join(random.choices(string.ascii_uppercase, k=length))

    if provider == 'sqlite':
        dbfile = tmpdir.join('test.sqlite')
        yield str(dbfile)

    elif provider == 'mysql':
        if not mysql_server:
            return pytest.skip('mysql_server argument not given')

        if not mysql_server.startswith('mysql://'):
            mysql_server = f'mysql://{mysql_server}'

        con_info = urlparse(mysql_server)
        if not validate_con_info(con_info):
            raise ValueError('invalid value for mysql_server')

        dbpath = random_string(24)

        import pymysql
        try:
            with pymysql.connect(con_info.hostname, user=con_info.username,
                                 password=con_info.password) as con:
                pass
        except pymysql.OperationalError as exc:
            raise RuntimeError('error connecting to MySQL server') from exc

        try:
            yield f'{mysql_server}/{dbpath}'

        finally:  # cleanup
            with pymysql.connect(con_info.hostname, user=con_info.username,
                                 password=con_info.password) as con:
                try:
                    con.execute(f'DROP DATABASE IF EXISTS {dbpath}')
                except pymysql.Warning:
                    pass

    else:
        return NotImplementedError(f'unknown provider {provider}')