2018-06-26 19:35:24 +08:00
|
|
|
#!/usr/local/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-01-25 18:00:42 +08:00
|
|
|
from unittest import TestCase
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
from nose.tools import assert_not_equal, assert_equal
|
|
|
|
from ..helpers.integration_test_helper import IntegrationTestHelper
|
|
|
|
|
|
|
|
|
2018-06-26 19:35:24 +08:00
|
|
|
class TestStreetFunctionsSetUp(TestCase):
|
2016-01-25 18:00:42 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.env_variables = IntegrationTestHelper.get_environment_variables()
|
2016-11-07 17:33:15 +08:00
|
|
|
self.sql_api_url = "{0}://{1}.{2}/api/v1/sql".format(
|
|
|
|
self.env_variables['schema'],
|
2016-01-25 18:00:42 +08:00
|
|
|
self.env_variables['username'],
|
|
|
|
self.env_variables['host'],
|
|
|
|
self.env_variables['api_key']
|
|
|
|
)
|
|
|
|
|
2018-06-26 19:35:24 +08:00
|
|
|
|
|
|
|
class TestStreetFunctions(TestStreetFunctionsSetUp):
|
|
|
|
|
2016-11-10 23:00:25 +08:00
|
|
|
def test_if_select_with_street_point_is_ok(self):
|
|
|
|
query = "SELECT cdb_geocode_street_point(street) " \
|
2016-01-25 18:00:42 +08:00
|
|
|
"as geometry FROM {0} LIMIT 1&api_key={1}".format(
|
2018-06-26 19:35:24 +08:00
|
|
|
self.env_variables['table_name'],
|
|
|
|
self.env_variables['api_key'])
|
2016-01-25 18:00:42 +08:00
|
|
|
geometry = IntegrationTestHelper.execute_query(self.sql_api_url, query)
|
2016-02-24 16:54:00 +08:00
|
|
|
assert_not_equal(geometry['geometry'], None)
|
2016-01-25 18:00:42 +08:00
|
|
|
|
|
|
|
def test_if_select_with_street_without_api_key_raise_error(self):
|
2016-01-26 22:53:18 +08:00
|
|
|
query = "SELECT cdb_geocode_street_point(street) " \
|
2018-06-26 19:35:24 +08:00
|
|
|
"as geometry FROM {0} LIMIT 1".format(
|
|
|
|
self.env_variables['table_name'])
|
2016-01-25 18:00:42 +08:00
|
|
|
try:
|
|
|
|
IntegrationTestHelper.execute_query(self.sql_api_url, query)
|
|
|
|
except Exception as e:
|
|
|
|
assert_equal(e.message[0], "The api_key must be provided")
|
2018-06-26 19:35:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestBulkStreetFunctions(TestStreetFunctionsSetUp):
|
|
|
|
|
|
|
|
def test_full_spec(self):
|
|
|
|
query = "select cartodb_id, st_x(the_geom), st_y(the_geom) " \
|
|
|
|
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point(" \
|
|
|
|
"'select 1 as cartodb_id, ''Spain'' as country, " \
|
|
|
|
"''Castilla y León'' as state, ''Valladolid'' as city, " \
|
|
|
|
"''Plaza Mayor'' as street " \
|
|
|
|
"UNION " \
|
|
|
|
"select 2 as cartodb_id, ''Spain'' as country, " \
|
|
|
|
"''Castilla y León'' as state, ''Valladolid'' as city, " \
|
|
|
|
"''Paseo Zorrilla'' as street' " \
|
2018-06-26 19:59:44 +08:00
|
|
|
", 'street', 'city', 'state', 'country')"
|
2018-06-26 19:35:24 +08:00
|
|
|
response = self._run_authenticated(query)
|
|
|
|
|
|
|
|
assert_equal(response['total_rows'], 2)
|
|
|
|
|
|
|
|
row_by_cartodb_id = self._row_by_cartodb_id(response)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[1], -3.7074009, 40.415511)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[2], -4.7404453, 41.6314339)
|
|
|
|
|
|
|
|
def test_empty_columns(self):
|
|
|
|
query = "select *, st_x(the_geom), st_y(the_geom) " \
|
|
|
|
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
|
|
|
|
"'select * from jsonb_to_recordset(''[" \
|
|
|
|
"{\"cartodb_id\": 1, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}" \
|
|
|
|
"]''::jsonb) as (cartodb_id integer, address text)', " \
|
2018-06-26 19:59:44 +08:00
|
|
|
"'address', '''''', '''''', '''''')"
|
2018-06-26 19:35:24 +08:00
|
|
|
response = self._run_authenticated(query)
|
|
|
|
|
|
|
|
assert_equal(response['total_rows'], 1)
|
|
|
|
|
|
|
|
row_by_cartodb_id = self._row_by_cartodb_id(response)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[1], -122.0885504, 37.4238657)
|
|
|
|
|
|
|
|
def test_null_columns(self):
|
|
|
|
query = "select *, st_x(the_geom), st_y(the_geom) " \
|
|
|
|
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
|
|
|
|
"'select * from jsonb_to_recordset(''[" \
|
|
|
|
"{\"cartodb_id\": 1, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}" \
|
|
|
|
"]''::jsonb) as (cartodb_id integer, address text)', " \
|
2018-06-26 19:59:44 +08:00
|
|
|
"'address')"
|
2018-06-26 19:35:24 +08:00
|
|
|
response = self._run_authenticated(query)
|
|
|
|
|
|
|
|
assert_equal(response['total_rows'], 1)
|
|
|
|
|
|
|
|
row_by_cartodb_id = self._row_by_cartodb_id(response)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[1], -122.0885504, 37.4238657)
|
|
|
|
|
|
|
|
def test_batching(self):
|
|
|
|
query = "select *, st_x(the_geom), st_y(the_geom) " \
|
|
|
|
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
|
|
|
|
"'select * from jsonb_to_recordset(''[" \
|
|
|
|
"{\"cartodb_id\": 1, \"address\": \"1900 amphitheatre parkway, mountain view, ca, us\"}," \
|
|
|
|
"{\"cartodb_id\": 2, \"address\": \"1901 amphitheatre parkway, mountain view, ca, us\"}," \
|
|
|
|
"{\"cartodb_id\": 3, \"address\": \"1902 amphitheatre parkway, mountain view, ca, us\"}" \
|
|
|
|
"]''::jsonb) as (cartodb_id integer, address text)', " \
|
2018-06-26 19:59:44 +08:00
|
|
|
"'address', null, null, null, 2)"
|
2018-06-26 19:35:24 +08:00
|
|
|
response = self._run_authenticated(query)
|
|
|
|
|
|
|
|
assert_equal(response['total_rows'], 3)
|
|
|
|
|
|
|
|
row_by_cartodb_id = self._row_by_cartodb_id(response)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[1], -122.0875324, 37.4227968)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[2], -122.0885504, 37.4238657)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[3], -122.0876674, 37.4235729)
|
|
|
|
|
2018-06-26 19:59:44 +08:00
|
|
|
def test_city_column_geocoding(self):
|
|
|
|
query = "select *, st_x(the_geom), st_y(the_geom) " \
|
|
|
|
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
|
|
|
|
"'select * from jsonb_to_recordset(''[" \
|
|
|
|
"{\"cartodb_id\": 1, \"city\": \"Valladolid\"}," \
|
|
|
|
"{\"cartodb_id\": 2, \"city\": \"Madrid\"}" \
|
|
|
|
"]''::jsonb) as (cartodb_id integer, city text)', " \
|
|
|
|
"'city')"
|
|
|
|
response = self._run_authenticated(query)
|
|
|
|
|
|
|
|
assert_equal(response['total_rows'], 2)
|
|
|
|
|
|
|
|
row_by_cartodb_id = self._row_by_cartodb_id(response)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[1], -4.7245321, 41.652251)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[2], -3.7037902, 40.4167754)
|
|
|
|
|
2018-06-26 22:52:41 +08:00
|
|
|
def test_free_text_geocoding(self):
|
|
|
|
query = "select *, st_x(the_geom), st_y(the_geom) " \
|
|
|
|
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
|
|
|
|
"'select * from (" \
|
|
|
|
"select 1 as cartodb_id, ''W 26th Street'' as address, " \
|
|
|
|
"null as city , null as state , null as country" \
|
|
|
|
")_x', " \
|
|
|
|
"'''Logroño, La Rioja, Spain''')"
|
|
|
|
response = self._run_authenticated(query)
|
|
|
|
|
|
|
|
assert_equal(response['total_rows'], 1)
|
|
|
|
|
|
|
|
row_by_cartodb_id = self._row_by_cartodb_id(response)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[1], -2.4449852, 42.4627195)
|
|
|
|
|
2018-06-27 14:51:40 +08:00
|
|
|
def test_templating_geocoding(self):
|
|
|
|
query = "SELECT cartodb_id, st_x(the_geom), st_y(the_geom) from " \
|
|
|
|
"cdb_dataservices_client.cdb_bulk_geocode_street_point(" \
|
|
|
|
"'select 1 as cartodb_id, ''Logroño'' as city', " \
|
|
|
|
"'city || '', '' || ''Spain''') " \
|
|
|
|
"UNION " \
|
|
|
|
"SELECT cartodb_id, st_x(the_geom), st_y(the_geom) from " \
|
|
|
|
"cdb_dataservices_client.cdb_bulk_geocode_street_point(" \
|
|
|
|
"'select 2 as cartodb_id, ''Logroño'' as city', " \
|
|
|
|
"'city || '', '' || ''Argentina''')"
|
|
|
|
response = self._run_authenticated(query)
|
|
|
|
# from nose.tools import set_trace; set_trace()
|
|
|
|
|
|
|
|
assert_equal(response['total_rows'], 2)
|
|
|
|
|
|
|
|
row_by_cartodb_id = self._row_by_cartodb_id(response)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[1], -2.4449852, 42.4627195)
|
|
|
|
self._assert_x_y(row_by_cartodb_id[2], -61.6961807, -29.5031057)
|
|
|
|
|
2018-06-26 19:59:44 +08:00
|
|
|
|
2018-06-26 19:35:24 +08:00
|
|
|
def _run_authenticated(self, query):
|
|
|
|
authenticated_query = "{}&api_key={}".format(query,
|
|
|
|
self.env_variables[
|
|
|
|
'api_key'])
|
|
|
|
return IntegrationTestHelper.execute_query_raw(self.sql_api_url,
|
|
|
|
authenticated_query)
|
|
|
|
|
|
|
|
def _row_by_cartodb_id(self, response):
|
|
|
|
return {r['cartodb_id']: r for r in response['rows']}
|
|
|
|
|
|
|
|
def _assert_x_y(self, row, expected_x, expected_y):
|
|
|
|
assert_equal(row['st_x'], expected_x)
|
|
|
|
assert_equal(row['st_y'], expected_y)
|
|
|
|
|