Make safe_create_table() is usable from many tests.

This commit is contained in:
INADA Naoki
2015-02-04 15:59:52 +09:00
parent 1a6cddb6ec
commit a4a460c051
3 changed files with 16 additions and 22 deletions

View File

@@ -50,23 +50,20 @@ class PyMySQLTestCase(unittest2.TestCase):
for connection in self.connections:
connection.close()
def safe_create_table(self, connection, tablename, ddl, cleanup=False):
def safe_create_table(self, connection, tablename, ddl, cleanup=True):
"""create a table.
Ensures any existing version of that table
is first dropped.
Ensures any existing version of that table is first dropped.
Also adds a cleanup rule to drop the table after the test
completes.
"""
cursor = connection.cursor()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
cursor.execute("drop table if exists test")
cursor.execute("create table test (data varchar(10))")
cursor.execute("drop table if exists `%s`" % (tablename,))
cursor.execute(ddl)
cursor.close()
if cleanup:
self.addCleanup(self.drop_table, connection, tablename)
@@ -75,7 +72,7 @@ class PyMySQLTestCase(unittest2.TestCase):
cursor = connection.cursor()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
cursor.execute("drop table if exists %s" % tablename)
cursor.execute("drop table if exists `%s`" % (tablename,))
cursor.close()
def safe_gc_collect(self):

View File

@@ -227,14 +227,14 @@ class TestCursor(base.PyMySQLTestCase):
""" test a single tuple """
conn = self.connections[0]
c = conn.cursor()
try:
c.execute("create table mystuff (id integer primary key)")
c.execute("insert into mystuff (id) values (1)")
c.execute("insert into mystuff (id) values (2)")
c.execute("select id from mystuff where id in %s", ((1,),))
self.assertEqual([(1,)], list(c.fetchall()))
finally:
c.execute("drop table mystuff")
self.safe_create_table(
conn, 'mystuff',
"create table mystuff (id integer primary key)")
c.execute("insert into mystuff (id) values (1)")
c.execute("insert into mystuff (id) values (2)")
c.execute("select id from mystuff where id in %s", ((1,),))
self.assertEqual([(1,)], list(c.fetchall()))
c.close()
class TestBulkInserts(base.PyMySQLTestCase):
@@ -247,11 +247,8 @@ class TestBulkInserts(base.PyMySQLTestCase):
c = conn.cursor(self.cursor_type)
# create a table ane some data to query
with warnings.catch_warnings():
warnings.simplefilter("ignore")
c.execute("drop table if exists bulkinsert")
c.execute(
"""CREATE TABLE bulkinsert
self.safe_create_table(conn, 'bulkinsert', """\
CREATE TABLE bulkinsert
(
id int(11),
name char(20),

View File

@@ -11,7 +11,7 @@ class CursorTest(base.PyMySQLTestCase):
self.safe_create_table(
conn,
"test", "create table test (data varchar(10))",
cleanup=True)
)
cursor = conn.cursor()
cursor.execute(
"insert into test (data) values "