observatory-extension/scripts/autotest.py

101 lines
3.5 KiB
Python
Raw Normal View History

2016-05-13 04:36:05 +08:00
from nose.tools import assert_equal, assert_is_not_none
2016-05-13 02:21:31 +08:00
from nose_parameterized import parameterized
import os
import re
import requests
HOSTNAME = os.environ['OBS_HOSTNAME']
API_KEY = os.environ['OBS_API_KEY']
2016-05-16 23:52:41 +08:00
META_HOSTNAME = os.environ.get('OBS_META_HOSTNAME', HOSTNAME)
META_API_KEY = os.environ.get('OBS_META_API_KEY', API_KEY)
USE_SCHEMA = 'OBS_USE_SCHEMA' in os.environ
2016-05-13 02:21:31 +08:00
2016-05-16 23:52:41 +08:00
def query(q, is_meta=False, **options):
2016-05-13 02:21:31 +08:00
'''
Query the account. Returned is the response, wrapped by the requests
library.
'''
2016-05-16 23:52:41 +08:00
url = 'https://{hostname}/api/v2/sql'.format(
hostname=META_HOSTNAME if is_meta else HOSTNAME)
2016-05-13 02:21:31 +08:00
params = options.copy()
params['q'] = re.sub(r'\s+', ' ', q)
2016-05-16 23:52:41 +08:00
params['api_key'] = META_API_KEY if is_meta else API_KEY
2016-05-13 02:21:31 +08:00
return requests.get(url, params=params)
MEASURE_COLUMNS = [(r['id'], ) for r in query('''
SELECT id FROM obs_column
2016-05-13 02:21:31 +08:00
WHERE type ILIKE 'numeric'
AND weight > 0
2016-05-16 23:52:41 +08:00
''', is_meta=True).json()['rows']]
2016-05-13 02:21:31 +08:00
CATEGORY_COLUMNS = [(r['id'], ) for r in query('''
SELECT id FROM obs_column
2016-05-13 02:21:31 +08:00
WHERE type ILIKE 'text'
AND weight > 0
2016-05-16 23:52:41 +08:00
''', is_meta=True).json()['rows']]
2016-05-13 02:21:31 +08:00
BOUNDARY_COLUMNS = [(r['id'], ) for r in query('''
SELECT id FROM obs_column
2016-05-13 02:21:31 +08:00
WHERE type ILIKE 'geometry'
AND weight > 0
2016-05-16 23:52:41 +08:00
''', is_meta=True).json()['rows']]
2016-05-13 02:21:31 +08:00
def default_point(column_id):
'''
Returns default test point for the column_id.
'''
2016-05-16 23:28:27 +08:00
if column_id == 'whosonfirst.wof_disputed_geom':
return 'CDB_LatLng(33.78, 76.57)'
elif column_id == 'whosonfirst.wof_marinearea_geom':
return 'CDB_LatLng(43.33, -68.47)'
elif column_id in ('us.census.tiger.school_district_elementary',
'us.census.tiger.school_district_secondary',
'us.census.tiger.school_district_elementary_clipped',
'us.census.tiger.school_district_secondary_clipped'):
2016-05-16 23:28:27 +08:00
return 'CDB_LatLng(40.7025, -73.7067)'
elif column_id.startswith('es.ine'):
return 'CDB_LatLng(42.8226119029222, -2.51141249535454)'
elif column_id.startswith('us.zillow'):
2016-05-16 23:28:27 +08:00
return 'CDB_LatLng(28.3305906291771, -81.3544048197256)'
else:
return 'CDB_LatLng(40.7, -73.9)'
2016-05-13 02:21:31 +08:00
@parameterized(MEASURE_COLUMNS)
def test_measure_points(column_id):
resp = query('''
2016-05-16 23:52:41 +08:00
SELECT * FROM {schema}OBS_GetMeasure({point}, '{column_id}')
'''.format(column_id=column_id,
schema='cdb_observatory.' if USE_SCHEMA else '',
point=default_point(column_id)))
2016-05-13 02:21:31 +08:00
assert_equal(resp.status_code, 200)
2016-05-13 04:36:05 +08:00
rows = resp.json()['rows']
assert_equal(1, len(rows))
assert_is_not_none(rows[0].values()[0])
2016-05-13 02:21:31 +08:00
2016-05-23 23:10:29 +08:00
@parameterized(CATEGORY_COLUMNS)
def test_category_points(column_id):
resp = query('''
SELECT * FROM {schema}OBS_GetCategory({point}, '{column_id}')
'''.format(column_id=column_id,
schema='cdb_observatory.' if USE_SCHEMA else '',
point=default_point(column_id)))
assert_equal(resp.status_code, 200)
rows = resp.json()['rows']
assert_equal(1, len(rows))
assert_is_not_none(rows[0].values()[0])
@parameterized(BOUNDARY_COLUMNS)
def test_boundary_points(column_id):
resp = query('''
SELECT * FROM {schema}OBS_GetBoundary({point}, '{column_id}')
'''.format(column_id=column_id,
schema='cdb_observatory.' if USE_SCHEMA else '',
point=default_point(column_id)))
assert_equal(resp.status_code, 200)
rows = resp.json()['rows']
assert_equal(1, len(rows))
assert_is_not_none(rows[0].values()[0])