def test_fetchmany(self): """MySQLCursor object fetchmany()-method""" cur = self._get_cursor(self.cnx) self.assertEqual([], cur.fetchmany()) tbl = 'myconnpy_fetch' self.setup_table(self.cnx, tbl) stmt_insert = ( "INSERT INTO {table} (col1,col2) " "VALUES (%s,%s)".format(table=tbl)) stmt_select = ( "SELECT col1,col2 FROM {table} " "ORDER BY col1 DESC".format(table=tbl)) cur = self.cnx.cursor() nrrows = 10 data = [(i, str(i * 100)) for i in range(1, nrrows+1)] cur.executemany(stmt_insert, data) cur.execute(stmt_select) exp = [(10, '1000'), (9, '900'), (8, '800'), (7, '700')] rows = cur.fetchmany(4) self.assertTrue(tests.cmp_result(exp, rows), "Fetching first 4 rows test failed.") exp = [(6, '600'), (5, '500'), (4, '400')] rows = cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") exp = [(3, '300'), (2, '200'), (1, '100')] rows = cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") self.assertEqual([], cur.fetchmany()) cur.close()
def test_fetchmany(self): """MySQLCursor object fetchmany()-method""" self.check_method(self.cur, 'fetchmany') self.assertEqual([], self.cur.fetchmany()) self.cnx = connection.MySQLConnection(**tests.get_mysql_config()) tbl = 'myconnpy_fetch' self._test_execute_setup(self.cnx, tbl) stmt_insert = ("INSERT INTO {table} (col1,col2) " "VALUES (%s,%s)".format(table=tbl)) stmt_select = ("SELECT col1,col2 FROM {table} " "ORDER BY col1 DESC".format(table=tbl)) self.cur = self.cnx.cursor() nrrows = 10 data = [(i, str(i * 100)) for i in range(0, nrrows)] self.cur.executemany(stmt_insert, data) self.cur.execute(stmt_select) exp = [(9, '900'), (8, '800'), (7, '700'), (6, '600')] rows = self.cur.fetchmany(4) self.assertTrue(tests.cmp_result(exp, rows), "Fetching first 4 rows test failed.") exp = [(5, '500'), (4, '400'), (3, '300')] rows = self.cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") exp = [(2, '200'), (1, '100'), (0, '0')] rows = self.cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") self.assertEqual([], self.cur.fetchmany()) self._test_execute_cleanup(self.cnx, tbl) self.cur.close()
def test_fetchmany(self): """MySQLCursor object fetchmany()-method""" cur = self._get_cursor(self.cnx) self.assertEqual([], cur.fetchmany()) tbl = 'myconnpy_fetch' self.setup_table(self.cnx, tbl) stmt_insert = ("INSERT INTO {table} (col1,col2) " "VALUES (%s,%s)".format(table=tbl)) stmt_select = ("SELECT col1,col2 FROM {table} " "ORDER BY col1 DESC".format(table=tbl)) cur = self.cnx.cursor() nrrows = 10 data = [(i, str(i * 100)) for i in range(1, nrrows + 1)] cur.executemany(stmt_insert, data) cur.execute(stmt_select) exp = [(10, '1000'), (9, '900'), (8, '800'), (7, '700')] rows = cur.fetchmany(4) self.assertTrue(tests.cmp_result(exp, rows), "Fetching first 4 rows test failed.") exp = [(6, '600'), (5, '500'), (4, '400')] rows = cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") exp = [(3, '300'), (2, '200'), (1, '100')] rows = cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") self.assertEqual([], cur.fetchmany()) cur.close()
def test_fetchall(self): """MySQLCursor object fetchall()-method""" self.check_method(self.cur, 'fetchall') self.assertRaises(errors.InterfaceError, self.cur.fetchall) self.cnx = connection.MySQLConnection(**tests.get_mysql_config()) tbl = 'myconnpy_fetch' self._test_execute_setup(self.cnx, tbl) stmt_insert = ("INSERT INTO {table} (col1,col2) " "VALUES (%s,%s)".format(table=tbl)) stmt_select = ("SELECT col1,col2 FROM {table} " "ORDER BY col1 ASC".format(table=tbl)) self.cur = self.cnx.cursor() self.cur.execute("SELECT * FROM {table}".format(table=tbl)) self.assertEqual([], self.cur.fetchall(), "fetchall() with empty result should return []") nrrows = 10 data = [(i, str(i * 100)) for i in range(0, nrrows)] self.cur.executemany(stmt_insert, data) self.cur.execute(stmt_select) self.assertTrue(tests.cmp_result(data, self.cur.fetchall()), "Fetching all rows failed.") self.assertEqual(None, self.cur.fetchone()) self._test_execute_cleanup(self.cnx, tbl) self.cur.close()
def test_fetchall(self): """MySQLCursor object fetchall()-method""" self.check_method(self.cur, 'fetchall') self.assertRaises(errors.InterfaceError, self.cur.fetchall) self.connection = connection.MySQLConnection(**tests.get_mysql_config()) tbl = 'myconnpy_fetch' self._test_execute_setup(self.connection, tbl) stmt_insert = ( "INSERT INTO {table} (col1,col2) " "VALUES (%s,%s)".format(table=tbl)) stmt_select = ( "SELECT col1,col2 FROM {table} " "ORDER BY col1 ASC".format(table=tbl)) self.cur = self.connection.cursor() self.cur.execute("SELECT * FROM {table}".format(table=tbl)) self.assertEqual([], self.cur.fetchall(), "fetchall() with empty result should return []") nrrows = 10 data = [(i, str(i * 100)) for i in range(0, nrrows)] self.cur.executemany(stmt_insert, data) self.cur.execute(stmt_select) self.assertTrue(tests.cmp_result(data, self.cur.fetchall()), "Fetching all rows failed.") self.assertEqual(None, self.cur.fetchone()) self._test_execute_cleanup(self.connection, tbl) self.cur.close()
def test_fetchall(self): cur = self._get_cursor(self.cnx) self.assertRaises(errors.InterfaceError, cur.fetchall) tbl = 'myconnpy_fetch' self.setup_table(self.cnx, tbl) stmt_insert = ( "INSERT INTO {table} (col1,col2) " "VALUES (%s,%s)".format(table=tbl)) stmt_select = ( "SELECT col1,col2 FROM {table} " "ORDER BY col1 ASC".format(table=tbl)) cur = self.cnx.cursor() cur.execute("SELECT * FROM {table}".format(table=tbl)) self.assertEqual([], cur.fetchall(), "fetchall() with empty result should return []") nrrows = 10 data = [(i, str(i * 100)) for i in range(1, nrrows+1)] cur.executemany(stmt_insert, data) cur.execute(stmt_select) self.assertTrue(tests.cmp_result(data, cur.fetchall()), "Fetching all rows failed.") self.assertEqual(None, cur.fetchone()) cur.close()
def test_execute(self): """MySQLCursor object execute()-method""" self.check_method(self.cur, 'execute') self.assertEqual(None, self.cur.execute(None, None)) config = tests.get_mysql_config() config['get_warnings'] = True self.connection = connection.MySQLConnection(**config) self.cur = self.connection.cursor() self.assertRaises(errors.ProgrammingError, self.cur.execute, 'SELECT %s,%s,%s', ( 'foo', 'bar', )) self.assertRaises(errors.ProgrammingError, self.cur.execute, 'SELECT %s,%s', ('foo', 'bar', 'foobar')) self.cur.execute("SELECT 'a' + 'b'") self.cur.fetchone() exp = [(u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'"), (u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'b'")] self.assertTrue(tests.cmp_result(exp, self.cur._warnings)) self.cur.execute("SELECT BINARY 'myconnpy'") exp = [(u'myconnpy', )] self.assertEqual(exp, self.cur.fetchall()) self.cur.close() tbl = 'myconnpy_cursor' self._test_execute_setup(self.connection, tbl) stmt_insert = "INSERT INTO {0} (col1,col2) VALUES (%s,%s)".format(tbl) self.cur = self.connection.cursor() res = self.cur.execute(stmt_insert, (1, 100)) self.assertEqual(None, res, "Return value of execute() is wrong.") stmt_select = "SELECT col1,col2 FROM {0} ORDER BY col1".format(tbl) self.cur.execute(stmt_select) self.assertEqual([(1, u'100')], self.cur.fetchall(), "Insert test failed") data = {'id': 2} stmt = "SELECT * FROM {0} WHERE col1 <= %(id)s".format(tbl) self.cur.execute(stmt, data) self.assertEqual([(1, u'100')], self.cur.fetchall()) self._test_execute_cleanup(self.connection, tbl) self.cur.close()
def test_fetchmany(self): """MySQLCursor object fetchmany()-method""" self.check_method(self.cur, 'fetchmany') self.assertEqual([], self.cur.fetchmany()) self.connection = connection.MySQLConnection(**tests.get_mysql_config()) tbl = 'myconnpy_fetch' self._test_execute_setup(self.connection, tbl) stmt_insert = ( "INSERT INTO {table} (col1,col2) " "VALUES (%s,%s)".format(table=tbl)) stmt_select = ( "SELECT col1,col2 FROM {table} " "ORDER BY col1 DESC".format(table=tbl)) self.cur = self.connection.cursor() nrrows = 10 data = [(i, str(i * 100)) for i in range(0, nrrows)] self.cur.executemany(stmt_insert, data) self.cur.execute(stmt_select) exp = [(9, u'900'), (8, u'800'), (7, u'700'), (6, u'600')] rows = self.cur.fetchmany(4) self.assertTrue(tests.cmp_result(exp, rows), "Fetching first 4 rows test failed.") exp = [(5, u'500'), (4, u'400'), (3, u'300')] rows = self.cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") exp = [(2, u'200'), (1, u'100'), (0, u'0')] rows = self.cur.fetchmany(3) self.assertTrue(tests.cmp_result(exp, rows), "Fetching next 3 rows test failed.") self.assertEqual([], self.cur.fetchmany()) self._test_execute_cleanup(self.connection, tbl) self.cur.close()
def test_execute(self): """MySQLCursor object execute()-method""" self.check_method(self.cur, 'execute') self.assertEqual(None, self.cur.execute(None, None)) config = tests.get_mysql_config() config['get_warnings'] = True self.connection = connection.MySQLConnection(**config) self.cur = self.connection.cursor() self.assertRaises(errors.ProgrammingError, self.cur.execute, 'SELECT %s,%s,%s', ('foo', 'bar',)) self.assertRaises(errors.ProgrammingError, self.cur.execute, 'SELECT %s,%s', ('foo', 'bar', 'foobar')) self.cur.execute("SELECT 'a' + 'b'") self.cur.fetchone() exp = [ (u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'"), (u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'b'") ] self.assertTrue(tests.cmp_result(exp, self.cur._warnings)) self.cur.execute("SELECT BINARY 'myconnpy'") exp = [(u'myconnpy',)] self.assertEqual(exp, self.cur.fetchall()) self.cur.close() tbl = 'myconnpy_cursor' self._test_execute_setup(self.connection, tbl) stmt_insert = "INSERT INTO {0} (col1,col2) VALUES (%s,%s)".format(tbl) self.cur = self.connection.cursor() res = self.cur.execute(stmt_insert, (1, 100)) self.assertEqual(None, res, "Return value of execute() is wrong.") stmt_select = "SELECT col1,col2 FROM {0} ORDER BY col1".format(tbl) self.cur.execute(stmt_select) self.assertEqual([(1, u'100')], self.cur.fetchall(), "Insert test failed") data = {'id': 2} stmt = "SELECT * FROM {0} WHERE col1 <= %(id)s".format(tbl) self.cur.execute(stmt, data) self.assertEqual([(1, u'100')], self.cur.fetchall()) self._test_execute_cleanup(self.connection, tbl) self.cur.close()
def test__fetch_warnings(self): """MySQLCursor object _fetch_warnings()-method""" self.check_method(self.cur, '_fetch_warnings') self.assertRaises(errors.InterfaceError, self.cur._fetch_warnings) config = tests.get_mysql_config() config['get_warnings'] = True self.cnx = connection.MySQLConnection(**config) self.cur = self.cnx.cursor() self.cur.execute("SELECT 'a' + 'b'") self.cur.fetchone() exp = [('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'"), ('Warning', 1292, "Truncated incorrect DOUBLE value: 'b'")] self.assertTrue(tests.cmp_result(exp, self.cur._fetch_warnings())) self.assertEqual(len(exp), self.cur._warning_count)
def test__fetch_warnings(self): self.cnx.get_warnings = True cur = self._get_cursor(self.cnx) cur._cnx = None self.assertRaises(errors.InterfaceError, cur._fetch_warnings) cur = self._get_cursor(self.cnx) cur.execute("SELECT 'a' + 'b'") cur.fetchall() exp = [('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'"), ('Warning', 1292, "Truncated incorrect DOUBLE value: 'b'")] res = cur._fetch_warnings() self.assertTrue(tests.cmp_result(exp, res)) self.assertEqual(len(exp), cur._warning_count)
def test__fetch_warnings(self): """MySQLCursor object _fetch_warnings()-method""" self.check_method(self.cur, '_fetch_warnings') self.assertRaises(errors.InterfaceError, self.cur._fetch_warnings) config = tests.get_mysql_config() config['get_warnings'] = True self.connection = connection.MySQLConnection(**config) self.cur = self.connection.cursor() self.cur.execute("SELECT 'a' + 'b'") self.cur.fetchone() exp = [ (u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'"), (u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'b'") ] self.assertTrue(tests.cmp_result(exp, self.cur._fetch_warnings())) self.assertEqual(len(exp), self.cur._warning_count)
def test__fetch_warnings(self): self.cnx.get_warnings = True cur = self._get_cursor(self.cnx) cur._cnx = None self.assertRaises(errors.InterfaceError, cur._fetch_warnings) cur = self._get_cursor(self.cnx) cur.execute("SELECT 'a' + 'b'") cur.fetchall() exp = [ ('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'"), ('Warning', 1292, "Truncated incorrect DOUBLE value: 'b'") ] res = cur._fetch_warnings() self.assertTrue(tests.cmp_result(exp, res)) self.assertEqual(len(exp), cur._warning_count)
def test_execute(self): self.cnx.get_warnings = True cur = self._get_cursor(self.cnx) self.assertEqual(None, cur.execute(None)) self.assertRaises(errors.ProgrammingError, cur.execute, 'SELECT %s,%s,%s', ('foo', 'bar',)) cur.execute("SELECT 'a' + 'b'") cur.fetchall() exp = [ ('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'"), ('Warning', 1292, "Truncated incorrect DOUBLE value: 'b'") ] self.assertTrue(tests.cmp_result(exp, cur._warnings)) self.cnx.get_warnings = False cur.execute("SELECT BINARY 'ham'") exp = [('ham',)] self.assertEqual(exp, cur.fetchall()) cur.close() tbl = 'myconnpy_cursor' self.setup_table(self.cnx, tbl) cur = self._get_cursor(self.cnx) stmt_insert = "INSERT INTO {0} (col1,col2) VALUES (%s,%s)".format(tbl) res = cur.execute(stmt_insert, (1, 100)) self.assertEqual(None, res, "Return value of execute() is wrong.") stmt_select = "SELECT col1,col2 FROM {0} ORDER BY col1".format(tbl) cur.execute(stmt_select) self.assertEqual([(1, '100')], cur.fetchall(), "Insert test failed") data = {'id': 2} stmt = "SELECT col1,col2 FROM {0} WHERE col1 <= %(id)s".format(tbl) cur.execute(stmt, data) self.assertEqual([(1, '100')], cur.fetchall()) cur.close()
def test_execute(self): self.cnx.get_warnings = True cur = self._get_cursor(self.cnx) self.assertEqual(None, cur.execute(None)) self.assertRaises(errors.ProgrammingError, cur.execute, 'SELECT %s,%s,%s', ('foo', 'bar',)) cur.execute("SELECT 'a' + 'b'") cur.fetchall() exp = [ ('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'"), ('Warning', 1292, "Truncated incorrect DOUBLE value: 'b'") ] self.assertTrue(tests.cmp_result(exp, cur._warnings)) self.cnx.get_warnings = False cur.execute("SELECT BINARY 'ham'") exp = [(b'ham',)] self.assertEqual(exp, cur.fetchall()) cur.close() tbl = 'myconnpy_cursor' self.setup_table(self.cnx, tbl) cur = self._get_cursor(self.cnx) stmt_insert = "INSERT INTO {0} (col1,col2) VALUES (%s,%s)".format(tbl) res = cur.execute(stmt_insert, (1, 100)) self.assertEqual(None, res, "Return value of execute() is wrong.") stmt_select = "SELECT col1,col2 FROM {0} ORDER BY col1".format(tbl) cur.execute(stmt_select) self.assertEqual([(1, '100')], cur.fetchall(), "Insert test failed") data = {'id': 2} stmt = "SELECT col1,col2 FROM {0} WHERE col1 <= %(id)s".format(tbl) cur.execute(stmt, data) self.assertEqual([(1, '100')], cur.fetchall()) cur.close()