Skip to content

Commit be32f01

Browse files
TST: sql skip tests at class level if database is not available
1 parent f7af818 commit be32f01

File tree

1 file changed

+90
-27
lines changed

1 file changed

+90
-27
lines changed

pandas/io/tests/test_sql.py

+90-27
Original file line numberDiff line numberDiff line change
@@ -868,24 +868,38 @@ class _TestSQLAlchemy(PandasSQLTest):
868868
"""
869869
flavor = None
870870

871+
@classmethod
872+
def setUpClass(cls):
873+
cls.setup_import()
874+
cls.setup_driver()
875+
876+
# test connection
877+
try:
878+
conn = cls.connect()
879+
conn.connect()
880+
except sqlalchemy.exc.OperationalError:
881+
msg = "{0} - can't connect to {1} server".format(cls, cls.flavor)
882+
raise nose.SkipTest(msg)
883+
871884
def setUp(self):
872-
self.setup_import()
873-
self.setup_driver()
874885
self.setup_connect()
875886

876887
self._load_iris_data()
877888
self._load_raw_sql()
878889
self._load_test1_data()
879890

880-
def setup_import(self):
891+
@classmethod
892+
def setup_import(cls):
881893
# Skip this test if SQLAlchemy not available
882894
if not SQLALCHEMY_INSTALLED:
883895
raise nose.SkipTest('SQLAlchemy not installed')
884896

885-
def setup_driver(self):
897+
@classmethod
898+
def setup_driver(cls):
886899
raise NotImplementedError()
887900

888-
def connect(self):
901+
@classmethod
902+
def connect(cls):
889903
raise NotImplementedError()
890904

891905
def setup_connect(self):
@@ -1221,12 +1235,14 @@ class TestSQLiteAlchemy(_TestSQLAlchemy):
12211235
"""
12221236
flavor = 'sqlite'
12231237

1224-
def connect(self):
1238+
@classmethod
1239+
def connect(cls):
12251240
return sqlalchemy.create_engine('sqlite:///:memory:')
12261241

1227-
def setup_driver(self):
1242+
@classmethod
1243+
def setup_driver(cls):
12281244
# sqlite3 is built-in
1229-
self.driver = None
1245+
cls.driver = None
12301246

12311247
def tearDown(self):
12321248
# in memory so tables should not be removed explicitly
@@ -1275,14 +1291,16 @@ class TestMySQLAlchemy(_TestSQLAlchemy):
12751291
"""
12761292
flavor = 'mysql'
12771293

1278-
def connect(self):
1279-
return sqlalchemy.create_engine(
1280-
'mysql+{driver}://root@localhost/pandas_nosetest'.format(driver=self.driver))
1294+
@classmethod
1295+
def connect(cls):
1296+
url = 'mysql+{driver}://root@localhost/pandas_nosetest'
1297+
return sqlalchemy.create_engine(url.format(driver=cls.driver))
12811298

1282-
def setup_driver(self):
1299+
@classmethod
1300+
def setup_driver(cls):
12831301
try:
12841302
import pymysql
1285-
self.driver = 'pymysql'
1303+
cls.driver = 'pymysql'
12861304
except ImportError:
12871305
raise nose.SkipTest('pymysql not installed')
12881306

@@ -1347,14 +1365,16 @@ class TestPostgreSQLAlchemy(_TestSQLAlchemy):
13471365
"""
13481366
flavor = 'postgresql'
13491367

1350-
def connect(self):
1368+
@classmethod
1369+
def connect(cls):
13511370
url = 'postgresql+{driver}://postgres@localhost/pandas_nosetest'
1352-
return sqlalchemy.create_engine(url.format(driver=self.driver))
1371+
return sqlalchemy.create_engine(url.format(driver=cls.driver))
13531372

1354-
def setup_driver(self):
1373+
@classmethod
1374+
def setup_driver(cls):
13551375
try:
13561376
import psycopg2
1357-
self.driver = 'psycopg2'
1377+
cls.driver = 'psycopg2'
13581378
except ImportError:
13591379
raise nose.SkipTest('psycopg2 not installed')
13601380

@@ -1431,7 +1451,8 @@ class TestSQLiteFallback(PandasSQLTest):
14311451
"""
14321452
flavor = 'sqlite'
14331453

1434-
def connect(self):
1454+
@classmethod
1455+
def connect(cls):
14351456
return sqlite3.connect(':memory:')
14361457

14371458
def drop_table(self, table_name):
@@ -1582,6 +1603,28 @@ class TestMySQLLegacy(TestSQLiteFallback):
15821603
"""
15831604
flavor = 'mysql'
15841605

1606+
@classmethod
1607+
def setUpClass(cls):
1608+
cls.setup_driver()
1609+
1610+
# test connection
1611+
try:
1612+
cls.connect()
1613+
except cls.driver.err.OperationalError:
1614+
raise nose.SkipTest("{0} - can't connect to MySQL server".format(cls))
1615+
1616+
@classmethod
1617+
def setup_driver(cls):
1618+
try:
1619+
import pymysql
1620+
cls.driver = pymysql
1621+
except ImportError:
1622+
raise nose.SkipTest('pymysql not installed')
1623+
1624+
@classmethod
1625+
def connect(cls):
1626+
return cls.driver.connect(host='127.0.0.1', user='root', passwd='', db='pandas_nosetest')
1627+
15851628
def drop_table(self, table_name):
15861629
cur = self.conn.cursor()
15871630
cur.execute("DROP TABLE IF EXISTS %s" % table_name)
@@ -1594,16 +1637,7 @@ def _count_rows(self, table_name):
15941637
rows = cur.fetchall()
15951638
return rows[0][0]
15961639

1597-
def connect(self):
1598-
return self.driver.connect(host='127.0.0.1', user='root', passwd='', db='pandas_nosetest')
1599-
16001640
def setUp(self):
1601-
try:
1602-
import pymysql
1603-
self.driver = pymysql
1604-
except ImportError:
1605-
raise nose.SkipTest('pymysql not installed')
1606-
16071641
try:
16081642
self.conn = self.connect()
16091643
except self.driver.err.OperationalError:
@@ -1941,6 +1975,35 @@ def clean_up(test_table_to_drop):
19411975

19421976
class TestXMySQL(tm.TestCase):
19431977

1978+
@classmethod
1979+
def setUpClass(cls):
1980+
_skip_if_no_pymysql()
1981+
1982+
# test connection
1983+
import pymysql
1984+
try:
1985+
# Try Travis defaults.
1986+
# No real user should allow root access with a blank password.
1987+
pymysql.connect(host='localhost', user='root', passwd='',
1988+
db='pandas_nosetest')
1989+
except:
1990+
pass
1991+
else:
1992+
return
1993+
try:
1994+
pymysql.connect(read_default_group='pandas')
1995+
except pymysql.ProgrammingError as e:
1996+
raise nose.SkipTest(
1997+
"Create a group of connection parameters under the heading "
1998+
"[pandas] in your system's mysql default file, "
1999+
"typically located at ~/.my.cnf or /etc/.my.cnf. ")
2000+
except pymysql.Error as e:
2001+
raise nose.SkipTest(
2002+
"Cannot connect to database. "
2003+
"Create a group of connection parameters under the heading "
2004+
"[pandas] in your system's mysql default file, "
2005+
"typically located at ~/.my.cnf or /etc/.my.cnf. ")
2006+
19442007
def setUp(self):
19452008
_skip_if_no_pymysql()
19462009
import pymysql

0 commit comments

Comments
 (0)