<commit_before>from nose.tools import assert_equal, assert_is_not_none
from nose_parameterized import parameterized

import os
import re
import requests

HOSTNAME = os.environ['OBS_HOSTNAME']
API_KEY = os.environ['OBS_API_KEY']
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


def query(q, is_meta=False, **options):
    '''
    Query the account.  Returned is the response, wrapped by the requests
    library.
    '''
    url = 'https://{hostname}/api/v2/sql'.format(
        hostname=META_HOSTNAME if is_meta else HOSTNAME)
    params = options.copy()
    params['q'] = re.sub(r'\s+', ' ', q)
    params['api_key'] = META_API_KEY if is_meta else API_KEY
    return requests.get(url, params=params)

MEASURE_COLUMNS = [(r['id'], r['point_only'], ) for r in query('''
SELECT id, aggregate NOT ILIKE 'sum' as point_only
FROM obs_column
WHERE type ILIKE 'numeric'
AND weight > 0
''', is_meta=True).json()['rows']]

CATEGORY_COLUMNS = [(r['id'], ) for r in query('''
SELECT id FROM obs_column
WHERE type ILIKE 'text'
AND weight > 0
''', is_meta=True).json()['rows']]

BOUNDARY_COLUMNS = [(r['id'], ) for r in query('''
SELECT id FROM obs_column
WHERE type ILIKE 'geometry'
AND weight > 0
''', is_meta=True).json()['rows']]

US_CENSUS_MEASURE_COLUMNS = [(r['name'], ) for r in query('''
SELECT c.name FROM obs_column c, obs_column_tag ct
WHERE type ILIKE 'numeric'
AND c.id = ct.column_id
AND ct.tag_id LIKE 'us.census%'
AND weight > 0
''', is_meta=True).json()['rows']]


def default_geometry_id(column_id):
    '''
    Returns default test point for the column_id.
    '''
    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'):
        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'):
        return 'CDB_LatLng(28.3305906291771, -81.3544048197256)'
    else:
        return 'CDB_LatLng(40.7, -73.9)'


def default_point(column_id):
    '''
    Returns default test point for the column_id.
    '''
    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'):
        return 'CDB_LatLng(40.7025, -73.7067)'
    elif column_id.startswith('uk'):
        if 'WA' in column_id:
            return 'CDB_LatLng(51.46844551219723, -3.184833526611328)'
        else:
            return 'CDB_LatLng(51.51461834694225, -0.08883476257324219)'
    elif column_id.startswith('es'):
        return 'CDB_LatLng(42.8226119029222, -2.51141249535454)'
    elif column_id.startswith('us.zillow'):
        return 'CDB_LatLng(28.3305906291771, -81.3544048197256)'
    else:
        return 'CDB_LatLng(40.7, -73.9)'


def default_area(column_id):
    '''
    Returns default test area for the column_id
    '''
    point = default_point(column_id)
    area = 'ST_Transform(ST_Buffer(ST_Transform({point}, 3857), 1000), 4326)'.format(<commit_msg>test location for MX<commit_after>from nose.tools import assert_equal, assert_is_not_none
from nose_parameterized import parameterized

import os
import re
import requests

HOSTNAME = os.environ['OBS_HOSTNAME']
API_KEY = os.environ['OBS_API_KEY']
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


def query(q, is_meta=False, **options):
    '''
    Query the account.  Returned is the response, wrapped by the requests
    library.
    '''
    url = 'https://{hostname}/api/v2/sql'.format(
        hostname=META_HOSTNAME if is_meta else HOSTNAME)
    params = options.copy()
    params['q'] = re.sub(r'\s+', ' ', q)
    params['api_key'] = META_API_KEY if is_meta else API_KEY
    return requests.get(url, params=params)

MEASURE_COLUMNS = [(r['id'], r['point_only'], ) for r in query('''
SELECT id, aggregate NOT ILIKE 'sum' as point_only
FROM obs_column
WHERE type ILIKE 'numeric'
AND weight > 0
''', is_meta=True).json()['rows']]

CATEGORY_COLUMNS = [(r['id'], ) for r in query('''
SELECT id FROM obs_column
WHERE type ILIKE 'text'
AND weight > 0
''', is_meta=True).json()['rows']]

BOUNDARY_COLUMNS = [(r['id'], ) for r in query('''
SELECT id FROM obs_column
WHERE type ILIKE 'geometry'
AND weight > 0
''', is_meta=True).json()['rows']]

US_CENSUS_MEASURE_COLUMNS = [(r['name'], ) for r in query('''
SELECT c.name FROM obs_column c, obs_column_tag ct
WHERE type ILIKE 'numeric'
AND c.id = ct.column_id
AND ct.tag_id LIKE 'us.census%'
AND weight > 0
''', is_meta=True).json()['rows']]


def default_geometry_id(column_id):
    '''
    Returns default test point for the column_id.
    '''
    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'):
        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'):
        return 'CDB_LatLng(28.3305906291771, -81.3544048197256)'
    else:
        return 'CDB_LatLng(40.7, -73.9)'


def default_point(column_id):
    '''
    Returns default test point for the column_id.
    '''
    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'):
        return 'CDB_LatLng(40.7025, -73.7067)'
    elif column_id.startswith('uk'):
        if 'WA' in column_id:
            return 'CDB_LatLng(51.46844551219723, -3.184833526611328)'
        else:
            return 'CDB_LatLng(51.51461834694225, -0.08883476257324219)'
    elif column_id.startswith('es'):
        return 'CDB_LatLng(42.8226119029222, -2.51141249535454)'
    elif column_id.startswith('us.zillow'):
        return 'CDB_LatLng(28.3305906291771, -81.3544048197256)'
    elif column_id.startswith('mx.'):
        return 'CDB_LatLng(19.41347699386547, -99.17019367218018)'
    else:
        return 'CDB_LatLng(40.7, -73.9)'


def default_area(column_id):
    '''
    Returns default test area for the column_id
    '''
    point = default_point(column_id)
    area = 'ST_Transform(ST_Buffer(ST_Transform({point}, 3857), 1000), 4326)'.format(
{'fraction_non_alphanumeric': 0.1329650565262076, 'fraction_numerical': 0.0737410071942446, 'mean_word_length': 3.5, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2928678', 'n_tokens_mistral': 3255, 'n_tokens_neox': 2882, 'n_words': 555}