From a4a460c051df88174c9c510b01925fb147baae56 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 4 Feb 2015 15:59:52 +0900 Subject: [PATCH] Make safe_create_table() is usable from many tests. --- pymysql/tests/base.py | 13 +++++-------- pymysql/tests/test_basic.py | 23 ++++++++++------------- pymysql/tests/test_cursor.py | 2 +- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/pymysql/tests/base.py b/pymysql/tests/base.py index ce836a4..740157b 100644 --- a/pymysql/tests/base.py +++ b/pymysql/tests/base.py @@ -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): diff --git a/pymysql/tests/test_basic.py b/pymysql/tests/test_basic.py index e0787d2..f9838df 100644 --- a/pymysql/tests/test_basic.py +++ b/pymysql/tests/test_basic.py @@ -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), diff --git a/pymysql/tests/test_cursor.py b/pymysql/tests/test_cursor.py index f590b9a..f900774 100644 --- a/pymysql/tests/test_cursor.py +++ b/pymysql/tests/test_cursor.py @@ -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 "