Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jormun]: Fix some information in booking_url #4306

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,14 @@ def get_odt_journey(booking_url):
section.origin.embedded_type = type_pb2.STOP_POINT
section.origin.stop_point.uri = 'stop_a'
section.origin.stop_point.name = 'stop a name'
section.origin.stop_point.label = 'stop a name (city)'
section.origin.stop_point.coord.lon = 1.0
section.origin.stop_point.coord.lat = 2.0
section.destination.uri = 'stop_b'
section.destination.embedded_type = type_pb2.STOP_POINT
section.destination.stop_point.uri = 'stop_b'
section.destination.stop_point.name = 'stop_b_name'
section.destination.stop_point.label = 'stop_b_name (city)'
section.destination.stop_point.coord.lon = 3.0
section.destination.stop_point.coord.lat = 4.0
booking_rule = section.booking_rule
Expand Down
47 changes: 26 additions & 21 deletions source/jormungandr/jormungandr/scenarios/tests/new_default_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
from jormungandr.scenarios.utils import switch_back_to_ridesharing
from jormungandr.utils import make_origin_destination_key, str_to_time_stamp
from werkzeug.exceptions import HTTPException
import pytz
from jormungandr import app
from flask import g
import pytest
from pytest_mock import mocker
from collections import defaultdict
Expand Down Expand Up @@ -835,25 +838,27 @@ def journey_with_disruptions_on_poi_test(mocker):


def journey_with_booking_rule_test():
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
"&from_coord_lon={from_coord_lon}&not_managed={not_managed}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
assert len(response_journey_with_odt.journeys) == 1
journey = response_journey_with_odt.journeys[0]
assert len(journey.sections) == 3
odt_section = journey.sections[1]
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address={from_name}&destination-address={to_name}&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}&not_managed={not_managed}"
)
with app.app_context():
g.timezone = pytz.timezone("Europe/Paris")
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
"&from_coord_lon={from_coord_lon}&not_managed={not_managed}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
assert len(response_journey_with_odt.journeys) == 1
journey = response_journey_with_odt.journeys[0]
assert len(journey.sections) == 3
odt_section = journey.sections[1]
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address={from_name}&destination-address={to_name}&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}&not_managed={not_managed}"
)

update_booking_rule_url_in_response(response_journey_with_odt)
odt_section = response_journey_with_odt.journeys[0].sections[1]
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name&destination-address=stop_b_name&requested-departure-time=1722924300&from_coord_lat=2.0&from_coord_lon=1.0&not_managed=N/A"
)
update_booking_rule_url_in_response(response_journey_with_odt)
odt_section = response_journey_with_odt.journeys[0].sections[1]
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name%20(city)&destination-address=stop_b_name%20(city)&requested-departure-time=2024-08-06T08:05:00+0200&from_coord_lat=2.0&from_coord_lon=1.0&not_managed=N/A"
)
115 changes: 59 additions & 56 deletions source/jormungandr/jormungandr/scenarios/tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
fill_disruptions_on_places_nearby,
update_booking_rule_url_in_section,
)

import pytz
from jormungandr import app
from flask import g
import pytest
from pytest_mock import mocker

Expand Down Expand Up @@ -102,58 +104,59 @@ def update_disruptions_on_pois_for_places_nearby_test(mocker):


def journey_with_booking_url_in_booking_rule_test():
instance = lambda: None
# Get a response with a section of ODT having booking_rule.
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
"&from_coord_lon={from_coord_lon}&to_coord_lat={to_coord_lat}&to_coord_lon={to_coord_lon}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
assert len(response_journey_with_odt.journeys) == 1
journey = response_journey_with_odt.journeys[0]
assert len(journey.sections) == 3
odt_section = journey.sections[1]
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
booking_rule = odt_section.booking_rule
assert booking_rule.name == "odt_name_value"
assert (
booking_rule.booking_url
== "https://domaine/search?departure-address={from_name}&destination-address={to_name}&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}&to_coord_lat={to_coord_lat}&to_coord_lon={to_coord_lon}"
)
assert booking_rule.info_url == "odt_url_value"
assert booking_rule.message == "odt_conditions_value"
assert booking_rule.phone_number == "odt_phone_value"
update_booking_rule_url_in_section(odt_section)
assert (
booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name&destination-address=stop_b_name&requested-departure-time=1722924300&from_coord_lat=2.0&from_coord_lon=1.0&to_coord_lat=4.0&to_coord_lon=3.0"
)

# Use a booking_url with fewer placeholders
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
odt_section = response_journey_with_odt.journeys[0].sections[1]
update_booking_rule_url_in_section(odt_section)
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name&destination-address=stop_b_name&requested-departure-time=1722924300&from_coord_lat=2.0&from_coord_lon=1.0"
)

# Add a placeholder which is not predefined in the function to update url
# This placeholder will not be replaced(updated)
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
"&from_coord_lon={from_coord_lon}&toto={toto}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
odt_section = response_journey_with_odt.journeys[0].sections[1]
update_booking_rule_url_in_section(odt_section)
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name&destination-address=stop_b_name&requested-departure-time=1722924300&from_coord_lat=2.0&from_coord_lon=1.0&toto=N/A"
)
with app.app_context():
g.timezone = pytz.timezone("Europe/Paris")
# Get a response with a section of ODT having booking_rule.
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
"&from_coord_lon={from_coord_lon}&to_coord_lat={to_coord_lat}&to_coord_lon={to_coord_lon}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
assert len(response_journey_with_odt.journeys) == 1
journey = response_journey_with_odt.journeys[0]
assert len(journey.sections) == 3
odt_section = journey.sections[1]
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
booking_rule = odt_section.booking_rule
assert booking_rule.name == "odt_name_value"
assert (
booking_rule.booking_url
== "https://domaine/search?departure-address={from_name}&destination-address={to_name}&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}&to_coord_lat={to_coord_lat}&to_coord_lon={to_coord_lon}"
)
assert booking_rule.info_url == "odt_url_value"
assert booking_rule.message == "odt_conditions_value"
assert booking_rule.phone_number == "odt_phone_value"
update_booking_rule_url_in_section(odt_section)
assert (
booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name%20(city)&destination-address=stop_b_name%20(city)&requested-departure-time=2024-08-06T08:05:00+0200&from_coord_lat=2.0&from_coord_lon=1.0&to_coord_lat=4.0&to_coord_lon=3.0"
)

# Use a booking_url with fewer placeholders
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
odt_section = response_journey_with_odt.journeys[0].sections[1]
update_booking_rule_url_in_section(odt_section)
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name%20(city)&destination-address=stop_b_name%20(city)&requested-departure-time=2024-08-06T08:05:00+0200&from_coord_lat=2.0&from_coord_lon=1.0"
)

# Add a placeholder which is not predefined in the function to update url
# This placeholder will not be replaced(updated)
booking_url = (
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
"&from_coord_lon={from_coord_lon}&toto={toto}"
)
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
odt_section = response_journey_with_odt.journeys[0].sections[1]
update_booking_rule_url_in_section(odt_section)
assert (
odt_section.booking_rule.booking_url
== "https://domaine/search?departure-address=stop%20a%20name%20(city)&destination-address=stop_b_name%20(city)&requested-departure-time=2024-08-06T08:05:00+0200&from_coord_lat=2.0&from_coord_lon=1.0&toto=N/A"
)
12 changes: 9 additions & 3 deletions source/jormungandr/jormungandr/scenarios/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import navitiacommon.request_pb2 as request_pb2
from future.moves.itertools import zip_longest
from jormungandr.fallback_modes import FallbackModes
from jormungandr.utils import timestamp_to_date_str
from jormungandr.timezone import get_timezone_or_paris
import requests as requests
import re
from collections import defaultdict
Expand Down Expand Up @@ -566,10 +568,10 @@ def update_booking_rule_url_in_section(section):
return

departure_datetime = section.begin_date_time
from_name = section.origin.stop_point.name
from_name = section.origin.stop_point.label
from_coord_lat = section.origin.stop_point.coord.lat
from_coord_lon = section.origin.stop_point.coord.lon
to_name = section.destination.stop_point.name
to_name = section.destination.stop_point.label
to_coord_lat = section.destination.stop_point.coord.lat
to_coord_lon = section.destination.stop_point.coord.lon

Expand All @@ -580,9 +582,13 @@ def update_booking_rule_url_in_section(section):
placeholder_dict = defaultdict(lambda: 'N/A')
fmtr = Formatter()

# Datetime formatting: "%Y-%m-%dT%H:%M:%S%z" -> 2024-09-24T09:25:45+0200
date_format = "%Y-%m-%dT%H:%M:%S%z"
timezone = get_timezone_or_paris()

for p in placeholders:
if p == "departure_datetime":
placeholder_dict[p] = departure_datetime
placeholder_dict[p] = timestamp_to_date_str(departure_datetime, timezone, _format=date_format)
elif p == "from_name":
placeholder_dict[p] = from_name
elif p == "from_coord_lat":
Expand Down
Loading