Add function cdb_routing_with_waypoints

Add routing with waypoints functions to client and server. Includes
signature checks tests for the Postgresql functions and unit and
integration tests for the Python library.

Add client v0.6.0 and server v0.9.0
This commit is contained in:
Carla Iriberri
2016-05-23 17:26:43 +02:00
parent f46305de6f
commit 21aac960a6
29 changed files with 3769 additions and 67 deletions

View File

@@ -33,11 +33,11 @@ class MapzenRouting:
self._url = base_url
@qps_retry
def calculate_route_point_to_point(self, origin, destination, mode,
def calculate_route_point_to_point(self, waypoints, mode,
options=[], units=METRICS_UNITS):
parsed_options = self.__parse_options(options)
mode_param = self.__parse_mode_param(mode, parsed_options)
directions = self.__parse_directions(origin, destination)
directions = self.__parse_directions(waypoints)
json_request_params = self.__parse_json_parameters(directions,
mode_param,
units)
@@ -67,11 +67,16 @@ class MapzenRouting:
return json.dumps(json_options)
def __parse_directions(self, origin, destination):
return {"locations": [
{"lon": origin.longitude, "lat": origin.latitude},
{"lon": destination.longitude, "lat": destination.latitude}
]}
def __parse_directions(self, waypoints):
path = []
for idx, point in enumerate(waypoints):
if idx == 0 or idx == len(waypoints) - 1:
point_type = 'break'
else:
point_type = 'through'
path.append({"lon": str(point.longitude), "lat": str(point.latitude), "type": point_type})
return {"locations": path}
def __parse_routing_response(self, response):
try:

View File

@@ -10,7 +10,7 @@ from setuptools import setup, find_packages
setup(
name='cartodb_services',
version='0.6.1',
version='0.6.2',
description='CartoDB Services API Python Library',

View File

@@ -17,9 +17,9 @@ requests_mock.Mocker.TEST_PREFIX = 'test_'
@requests_mock.Mocker()
class MapzenRoutingTestCase(unittest.TestCase):
GOOD_SHAPE = [(38.5, -120.2), (43.2, -126.4)]
GOOD_SHAPE_SIMPLE = [(38.5, -120.2), (43.2, -126.4)]
GOOD_RESPONSE = """{{
GOOD_RESPONSE_SIMPLE = """{{
"id": "ethervoid-route",
"trip": {{
"status": 0,
@@ -51,7 +51,50 @@ class MapzenRoutingTestCase(unittest.TestCase):
}}
]
}}
}}""".format(GOOD_SHAPE)
}}""".format(GOOD_SHAPE_SIMPLE)
GOOD_SHAPE_MULTI = [(40.4, -3.7), (40.1, -3.4), (40.6, -3.9)]
GOOD_RESPONSE_MULTI = """{{
"id": "ethervoid-route",
"trip": {{
"language":"en-US",
"summary":{{
"length": 1.261,
"time": 913
}},
"locations":[
{{
"side_of_street":"right",
"lon": -3.7,
"lat": 40.4,
"type":"break"
}},
{{
"lon": -3.4,
"lat": 40.1,
"type": "through"
}},
{{
"lon": -3.9,
"lat": 40.6,
"type": "break"
}}
],
"units":"kilometers",
"legs":[
{{
"shape": "_squF~sqU~qy@_ry@_t`B~s`B",
"summary": {{
"length":1.261,
"time":913
}}
}}
],
"status_message": "Found route between points",
"status": 0
}}
}}""".format(GOOD_SHAPE_MULTI)
MALFORMED_RESPONSE = """{"manolo": "escobar"}"""
@@ -61,23 +104,39 @@ class MapzenRoutingTestCase(unittest.TestCase):
def test_calculate_simple_routing_with_valid_params(self, req_mock):
req_mock.register_uri('GET', requests_mock.ANY,
text=self.GOOD_RESPONSE)
text=self.GOOD_RESPONSE_SIMPLE)
origin = Coordinate('-120.2', '38.5')
destination = Coordinate('-126.4', '43.2')
response = self.routing.calculate_route_point_to_point(origin,
destination,
waypoints = [origin, destination]
response = self.routing.calculate_route_point_to_point(waypoints,
'car')
self.assertEqual(response.shape, self.GOOD_SHAPE)
self.assertEqual(response.shape, self.GOOD_SHAPE_SIMPLE)
self.assertEqual(response.length, 444.59)
self.assertEqual(response.duration, 16969)
def test_uknown_mode_raise_exception(self, req_mock):
req_mock.register_uri('GET', requests_mock.ANY,
text=self.GOOD_RESPONSE)
text=self.GOOD_RESPONSE_SIMPLE)
origin = Coordinate('-120.2', '38.5')
destination = Coordinate('-126.4', '43.2')
waypoints = [origin, destination]
assert_raises(WrongParams,
self.routing.calculate_route_point_to_point,
origin, destination, 'unknown')
waypoints, 'unknown')
def test_calculate_routing_waypoints_with_valid_params(self, req_mock):
req_mock.register_uri('GET', requests_mock.ANY,
text=self.GOOD_RESPONSE_MULTI)
origin = Coordinate('-3.7', '40.4')
pass_through = Coordinate('-3.4', '40.1')
destination = Coordinate('-3.9', '40.6')
waypoints = [origin, pass_through, destination]
response = self.routing.calculate_route_point_to_point(waypoints,
'walk')
self.assertEqual(response.length, 1.261)
self.assertEqual(response.duration, 913)
self.assertEqual(response.shape, self.GOOD_SHAPE_MULTI)