Skip to content

Commit 429f0f5

Browse files
Merge pull request pandas-dev#6651 from jorisvandenbossche/sql-skiptests
TST: skip sql tests if connection to server fails
2 parents ff2c5f2 + 6f945d3 commit 429f0f5

File tree

1 file changed

+57
-50
lines changed

1 file changed

+57
-50
lines changed

pandas/io/tests/test_sql.py

+57-50
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,14 @@ def setUp(self):
705705
try:
706706
import pymysql
707707
self.driver = pymysql
708-
709708
except ImportError:
710-
raise nose.SkipTest
709+
raise nose.SkipTest('pymysql not installed')
710+
711+
try:
712+
self.conn = self.connect()
713+
except self.driver.err.OperationalError:
714+
raise nose.SkipTest("Can't connect to MySQL server")
711715

712-
self.conn = self.connect()
713716
self.pandasSQL = sql.PandasSQLLegacy(self.conn, 'mysql')
714717

715718
self._load_iris_data()
@@ -725,53 +728,55 @@ def tearDown(self):
725728

726729

727730
class TestMySQLAlchemy(_TestSQLAlchemy):
728-
flavor = 'mysql'
729-
730-
def connect(self):
731-
return sqlalchemy.create_engine(
732-
'mysql+{driver}://root@localhost/pandas_nosetest'.format(driver=self.driver))
731+
flavor = 'mysql'
733732

734-
def setUp(self):
735-
if not SQLALCHEMY_INSTALLED:
736-
raise nose.SkipTest('SQLAlchemy not installed')
733+
def connect(self):
734+
return sqlalchemy.create_engine(
735+
'mysql+{driver}://root@localhost/pandas_nosetest'.format(driver=self.driver))
737736

738-
try:
739-
import pymysql
740-
self.driver = 'pymysql'
737+
def setUp(self):
738+
if not SQLALCHEMY_INSTALLED:
739+
raise nose.SkipTest('SQLAlchemy not installed')
741740

742-
except ImportError:
743-
raise nose.SkipTest
741+
try:
742+
import pymysql
743+
self.driver = 'pymysql'
744+
except ImportError:
745+
raise nose.SkipTest('pymysql not installed')
744746

747+
try:
745748
self.conn = self.connect()
746749
self.pandasSQL = sql.PandasSQLAlchemy(self.conn)
750+
except sqlalchemy.exc.OperationalError:
751+
raise nose.SkipTest("Can't connect to MySQL server")
747752

748-
self._load_iris_data()
749-
self._load_raw_sql()
753+
self._load_iris_data()
754+
self._load_raw_sql()
750755

751-
self._load_test1_data()
756+
self._load_test1_data()
757+
758+
def tearDown(self):
759+
c = self.conn.execute('SHOW TABLES')
760+
for table in c.fetchall():
761+
self.conn.execute('DROP TABLE %s' % table[0])
752762

753-
def tearDown(self):
754-
c = self.conn.execute('SHOW TABLES')
755-
for table in c.fetchall():
756-
self.conn.execute('DROP TABLE %s' % table[0])
763+
def test_default_type_conversion(self):
764+
df = sql.read_table("types_test_data", self.conn)
757765

758-
def test_default_type_conversion(self):
759-
df = sql.read_table("types_test_data", self.conn)
760-
761-
self.assertTrue(issubclass(df.FloatCol.dtype.type, np.floating),
762-
"FloatCol loaded with incorrect type")
763-
self.assertTrue(issubclass(df.IntCol.dtype.type, np.integer),
764-
"IntCol loaded with incorrect type")
765-
# MySQL has no real BOOL type (it's an alias for TINYINT)
766-
self.assertTrue(issubclass(df.BoolCol.dtype.type, np.integer),
767-
"BoolCol loaded with incorrect type")
768-
769-
# Int column with NA values stays as float
770-
self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating),
771-
"IntColWithNull loaded with incorrect type")
772-
# Bool column with NA = int column with NA values => becomes float
773-
self.assertTrue(issubclass(df.BoolColWithNull.dtype.type, np.floating),
774-
"BoolColWithNull loaded with incorrect type")
766+
self.assertTrue(issubclass(df.FloatCol.dtype.type, np.floating),
767+
"FloatCol loaded with incorrect type")
768+
self.assertTrue(issubclass(df.IntCol.dtype.type, np.integer),
769+
"IntCol loaded with incorrect type")
770+
# MySQL has no real BOOL type (it's an alias for TINYINT)
771+
self.assertTrue(issubclass(df.BoolCol.dtype.type, np.integer),
772+
"BoolCol loaded with incorrect type")
773+
774+
# Int column with NA values stays as float
775+
self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating),
776+
"IntColWithNull loaded with incorrect type")
777+
# Bool column with NA = int column with NA values => becomes float
778+
self.assertTrue(issubclass(df.BoolColWithNull.dtype.type, np.floating),
779+
"BoolColWithNull loaded with incorrect type")
775780

776781

777782
class TestPostgreSQLAlchemy(_TestSQLAlchemy):
@@ -780,26 +785,28 @@ class TestPostgreSQLAlchemy(_TestSQLAlchemy):
780785
def connect(self):
781786
return sqlalchemy.create_engine(
782787
'postgresql+{driver}://postgres@localhost/pandas_nosetest'.format(driver=self.driver))
783-
788+
784789
def setUp(self):
785790
if not SQLALCHEMY_INSTALLED:
786791
raise nose.SkipTest('SQLAlchemy not installed')
787-
792+
788793
try:
789794
import psycopg2
790795
self.driver = 'psycopg2'
791-
792796
except ImportError:
793-
raise nose.SkipTest
794-
795-
self.conn = self.connect()
796-
self.pandasSQL = sql.PandasSQLAlchemy(self.conn)
797-
797+
raise nose.SkipTest('psycopg2 not installed')
798+
799+
try:
800+
self.conn = self.connect()
801+
self.pandasSQL = sql.PandasSQLAlchemy(self.conn)
802+
except sqlalchemy.exc.OperationalError:
803+
raise nose.SkipTest("Can't connect to PostgreSQL server")
804+
798805
self._load_iris_data()
799806
self._load_raw_sql()
800-
807+
801808
self._load_test1_data()
802-
809+
803810
def tearDown(self):
804811
c = self.conn.execute(
805812
"SELECT table_name FROM information_schema.tables"

0 commit comments

Comments
 (0)