def test_caching(self): def durations(origin, destinations): return [1] * len(destinations) # Fill cache with mock.patch.object(travel.vendors, 'backend', return_value=mock.Mock(durations=durations)): durations1 = travel.durations(places.vallouise, [places.paris], 'car') # If there was no cache, this would raise a ZeroDivisionError with mock.patch.object( travel.vendors, 'backend', return_value=mock.Mock(side_effect=ZeroDivisionError)): durations2 = travel.durations(places.vallouise, [places.paris], 'car') self.assertEqual([1], durations1) self.assertEqual(durations1, durations2)
def durations(): request_data = request.get_json(force=True) or {} try: travel_mode = request_data['travel_mode'] origin = parse_coordinates(request_data['origin']) destinations = [ parse_coordinates(dst) for dst in request_data['destinations'] ] except KeyError as e: return 'Missing argument: {}'.format(e.message), 400 except ValueError: return 'Invalid coordinates: proper format is "latitude,longitude"', 400 travel_durations = travel.durations(origin, destinations, mode=travel_mode) return json.dumps(travel_durations)
def test_invalid_destination(self, backend): invalid = (95, 0) durations = travel.durations(places.vallouise, [invalid], 'car') self.assertEqual([None], durations)
def test_durations(self, backend): durations = travel.durations(places.vallouise, [places.paris, places.metz], 'car') self.assertEqual([7000, 5000], durations)