Skip to content

TST: skip sql tests if connection to server fails #6651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 57 additions & 50 deletions pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,14 @@ def setUp(self):
try:
import pymysql
self.driver = pymysql

except ImportError:
raise nose.SkipTest
raise nose.SkipTest('pymysql not installed')

try:
self.conn = self.connect()
except self.driver.err.OperationalError:
raise nose.SkipTest("Can't connect to MySQL server")

self.conn = self.connect()
self.pandasSQL = sql.PandasSQLLegacy(self.conn, 'mysql')

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


class TestMySQLAlchemy(_TestSQLAlchemy):
flavor = 'mysql'

def connect(self):
return sqlalchemy.create_engine(
'mysql+{driver}://root@localhost/pandas_nosetest'.format(driver=self.driver))
flavor = 'mysql'

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

try:
import pymysql
self.driver = 'pymysql'
def setUp(self):
if not SQLALCHEMY_INSTALLED:
raise nose.SkipTest('SQLAlchemy not installed')

except ImportError:
raise nose.SkipTest
try:
import pymysql
self.driver = 'pymysql'
except ImportError:
raise nose.SkipTest('pymysql not installed')

try:
self.conn = self.connect()
self.pandasSQL = sql.PandasSQLAlchemy(self.conn)
except sqlalchemy.exc.OperationalError:
raise nose.SkipTest("Can't connect to MySQL server")

self._load_iris_data()
self._load_raw_sql()
self._load_iris_data()
self._load_raw_sql()

self._load_test1_data()
self._load_test1_data()

def tearDown(self):
c = self.conn.execute('SHOW TABLES')
for table in c.fetchall():
self.conn.execute('DROP TABLE %s' % table[0])

def tearDown(self):
c = self.conn.execute('SHOW TABLES')
for table in c.fetchall():
self.conn.execute('DROP TABLE %s' % table[0])
def test_default_type_conversion(self):
df = sql.read_table("types_test_data", self.conn)

def test_default_type_conversion(self):
df = sql.read_table("types_test_data", self.conn)

self.assertTrue(issubclass(df.FloatCol.dtype.type, np.floating),
"FloatCol loaded with incorrect type")
self.assertTrue(issubclass(df.IntCol.dtype.type, np.integer),
"IntCol loaded with incorrect type")
# MySQL has no real BOOL type (it's an alias for TINYINT)
self.assertTrue(issubclass(df.BoolCol.dtype.type, np.integer),
"BoolCol loaded with incorrect type")

# Int column with NA values stays as float
self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating),
"IntColWithNull loaded with incorrect type")
# Bool column with NA = int column with NA values => becomes float
self.assertTrue(issubclass(df.BoolColWithNull.dtype.type, np.floating),
"BoolColWithNull loaded with incorrect type")
self.assertTrue(issubclass(df.FloatCol.dtype.type, np.floating),
"FloatCol loaded with incorrect type")
self.assertTrue(issubclass(df.IntCol.dtype.type, np.integer),
"IntCol loaded with incorrect type")
# MySQL has no real BOOL type (it's an alias for TINYINT)
self.assertTrue(issubclass(df.BoolCol.dtype.type, np.integer),
"BoolCol loaded with incorrect type")

# Int column with NA values stays as float
self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating),
"IntColWithNull loaded with incorrect type")
# Bool column with NA = int column with NA values => becomes float
self.assertTrue(issubclass(df.BoolColWithNull.dtype.type, np.floating),
"BoolColWithNull loaded with incorrect type")


class TestPostgreSQLAlchemy(_TestSQLAlchemy):
Expand All @@ -780,26 +785,28 @@ class TestPostgreSQLAlchemy(_TestSQLAlchemy):
def connect(self):
return sqlalchemy.create_engine(
'postgresql+{driver}://postgres@localhost/pandas_nosetest'.format(driver=self.driver))

def setUp(self):
if not SQLALCHEMY_INSTALLED:
raise nose.SkipTest('SQLAlchemy not installed')

try:
import psycopg2
self.driver = 'psycopg2'

except ImportError:
raise nose.SkipTest

self.conn = self.connect()
self.pandasSQL = sql.PandasSQLAlchemy(self.conn)

raise nose.SkipTest('psycopg2 not installed')

try:
self.conn = self.connect()
self.pandasSQL = sql.PandasSQLAlchemy(self.conn)
except sqlalchemy.exc.OperationalError:
raise nose.SkipTest("Can't connect to PostgreSQL server")

self._load_iris_data()
self._load_raw_sql()

self._load_test1_data()

def tearDown(self):
c = self.conn.execute(
"SELECT table_name FROM information_schema.tables"
Expand Down