Skip to content

Commit

Permalink
Merge pull request #4306 from hove-io/fix_some_information_in_booking…
Browse files Browse the repository at this point in the history
…_rule

[jormun]: Fix some information in booking_url
  • Loading branch information
kadhikari authored Sep 26, 2024
2 parents 627a078 + bbd5785 commit ab1be21
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 80 deletions.
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

0 comments on commit ab1be21

Please sign in to comment.