Add testing for read_postgis function

It also installs postgis and creates a simple database filled with the borough
data. A new requirements.test.txt file is created so that not everybody
installing geopandas is required to install the packages for testing, psycopg2
in this case.
This commit is contained in:
Jacob Wasserman
2013-10-05 02:39:33 -04:00
parent 3fd71dd472
commit 0a7508f111
3 changed files with 36 additions and 1 deletions
+24
View File
@@ -7,6 +7,7 @@ import urllib2
import numpy as np
from shapely.geometry import Point, Polygon
import psycopg2
from geopandas import GeoDataFrame
@@ -82,3 +83,26 @@ class TestDataFrame(unittest.TestCase):
lonlat = df2.to_crs(epsg=4326)
utm = lonlat.to_crs(epsg=26918)
self.assertTrue(all(df2['geometry'].almost_equals(utm['geometry'], decimal=2)))
def _validate_sql(self, df):
# Make sure all the columns are there and the geometries
# were properly loaded as MultiPolygons
columns = ('borocode', 'boroname', 'shape_leng', 'shape_area')
for col in columns:
self.assertTrue(col in df.columns, 'Column {} missing'.format(col))
self.assertTrue(all(df['geometry'].type == 'MultiPolygon'))
def test_read_postgis_default(self):
con = psycopg2.connect(dbname='test_geopandas')
sql = "SELECT * FROM nybb;"
df = GeoDataFrame.read_postgis(sql, con)
self._validate_sql(df)
def test_read_postgis_custom_geom_col(self):
con = psycopg2.connect(dbname='test_geopandas')
sql = """SELECT
borocode, boroname, shape_leng, shape_area,
geom AS __geometry__
FROM nybb;"""
df = GeoDataFrame.read_postgis(sql, con, geom_col='__geometry__')
self._validate_sql(df)