Skip to content

Commit 35d61ae

Browse files
TEST: add basic postgresql tests
TEST: add postgresql to travis
1 parent 5c9a2fd commit 35d61ae

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ install:
7373

7474
before_script:
7575
- mysql -e 'create database pandas_nosetest;'
76+
- psql -c 'create database pandas_nosetest;' -U postgres
7677

7778
script:
7879
- echo "script"

ci/requirements-2.7.txt

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ beautifulsoup4==4.2.1
1919
statsmodels==0.5.0
2020
bigquery==2.0.17
2121
sqlalchemy==0.8.1
22+
psycopg2==2.5.2

ci/requirements-3.3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ scipy==0.12.0
1515
beautifulsoup4==4.2.1
1616
statsmodels==0.4.3
1717
sqlalchemy==0.9.1
18+
psycopg2==2.5.2

pandas/io/tests/test_sql.py

+67-3
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@
3636
`PetalLength` DOUBLE,
3737
`PetalWidth` DOUBLE,
3838
`Name` VARCHAR(200)
39+
)""",
40+
'postgresql': """CREATE TABLE iris (
41+
"SepalLength" DOUBLE PRECISION,
42+
"SepalWidth" DOUBLE PRECISION,
43+
"PetalLength" DOUBLE PRECISION,
44+
"PetalWidth" DOUBLE PRECISION,
45+
"Name" VARCHAR(200)
3946
)"""
4047
},
4148
'insert_iris': {
4249
'sqlite': """INSERT INTO iris VALUES(?, ?, ?, ?, ?)""",
43-
'mysql': """INSERT INTO iris VALUES(%s, %s, %s, %s, "%s");"""
50+
'mysql': """INSERT INTO iris VALUES(%s, %s, %s, %s, "%s");""",
51+
'postgresql': """INSERT INTO iris VALUES(%s, %s, %s, %s, %s);"""
4452
},
4553
'create_test_types': {
4654
'sqlite': """CREATE TABLE types_test_data (
@@ -62,6 +70,16 @@
6270
`BoolCol` BOOLEAN,
6371
`IntColWithNull` INTEGER,
6472
`BoolColWithNull` BOOLEAN
73+
)""",
74+
'postgresql': """CREATE TABLE types_test_data (
75+
"TextCol" TEXT,
76+
"DateCol" TIMESTAMP,
77+
"IntDateCol" INTEGER,
78+
"FloatCol" DOUBLE PRECISION,
79+
"IntCol" INTEGER,
80+
"BoolCol" BOOLEAN,
81+
"IntColWithNull" INTEGER,
82+
"BoolColWithNull" BOOLEAN
6583
)"""
6684
},
6785
'insert_test_types': {
@@ -72,6 +90,10 @@
7290
'mysql': """
7391
INSERT INTO types_test_data
7492
VALUES("%s", %s, %s, %s, %s, %s, %s, %s)
93+
""",
94+
'postgresql': """
95+
INSERT INTO types_test_data
96+
VALUES(%s, %s, %s, %s, %s, %s, %s, %s)
7597
"""
7698
}
7799
}
@@ -504,8 +526,8 @@ def test_default_type_convertion(self):
504526
self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating),
505527
"IntColWithNull loaded with incorrect type")
506528
# Non-native Bool column with NA values stays as float
507-
self.assertTrue(
508-
issubclass(df.BoolColWithNull.dtype.type, np.floating), "BoolCol loaded with incorrect type")
529+
self.assertTrue(issubclass(df.BoolColWithNull.dtype.type, np.floating),
530+
"BoolColWithNull loaded with incorrect type")
509531

510532
def test_default_date_load(self):
511533
df = sql.read_table("types_test_data", self.conn)
@@ -699,6 +721,48 @@ def test_default_date_load(self):
699721
self.assertTrue(
700722
issubclass(df.DateCol.dtype.type, np.datetime64), "DateCol loaded with incorrect type")
701723

724+
725+
class TestPostgreSQLAlchemy(TestSQLAlchemy):
726+
flavor = 'postgresql'
727+
728+
def connect(self):
729+
return sqlalchemy.create_engine(
730+
'postgresql+{driver}://postgres@localhost/pandas_nosetest'.format(driver=self.driver))
731+
732+
def setUp(self):
733+
if not SQLALCHEMY_INSTALLED:
734+
raise nose.SkipTest('SQLAlchemy not installed')
735+
736+
try:
737+
import psycopg2
738+
self.driver = 'psycopg2'
739+
740+
except ImportError:
741+
raise nose.SkipTest
742+
743+
self.conn = self.connect()
744+
self.pandasSQL = sql.PandasSQLAlchemy(self.conn)
745+
746+
self._load_iris_data()
747+
self._load_raw_sql()
748+
749+
self._load_test1_data()
750+
751+
def tearDown(self):
752+
c = self.conn.execute(
753+
"SELECT table_name FROM information_schema.tables"
754+
" WHERE table_schema = 'public'")
755+
for table in c.fetchall():
756+
self.conn.execute("DROP TABLE %s" % table[0])
757+
758+
def test_default_date_load(self):
759+
df = sql.read_table("types_test_data", self.conn)
760+
761+
# IMPORTANT - sqlite has no native date type, so shouldn't parse,
762+
# but PostgreSQL SHOULD be converted.
763+
self.assertTrue(issubclass(df.DateCol.dtype.type, np.datetime64),
764+
"DateCol loaded with incorrect type")
765+
702766
if __name__ == '__main__':
703767
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
704768
exit=False)

0 commit comments

Comments
 (0)