Skip to content

Commit ab1be21

Browse files
authored
Merge pull request #4306 from hove-io/fix_some_information_in_booking_rule
[jormun]: Fix some information in booking_url
2 parents 627a078 + bbd5785 commit ab1be21

File tree

4 files changed

+96
-80
lines changed

4 files changed

+96
-80
lines changed

source/jormungandr/jormungandr/scenarios/tests/helpers_tests.py

+2
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,14 @@ def get_odt_journey(booking_url):
653653
section.origin.embedded_type = type_pb2.STOP_POINT
654654
section.origin.stop_point.uri = 'stop_a'
655655
section.origin.stop_point.name = 'stop a name'
656+
section.origin.stop_point.label = 'stop a name (city)'
656657
section.origin.stop_point.coord.lon = 1.0
657658
section.origin.stop_point.coord.lat = 2.0
658659
section.destination.uri = 'stop_b'
659660
section.destination.embedded_type = type_pb2.STOP_POINT
660661
section.destination.stop_point.uri = 'stop_b'
661662
section.destination.stop_point.name = 'stop_b_name'
663+
section.destination.stop_point.label = 'stop_b_name (city)'
662664
section.destination.stop_point.coord.lon = 3.0
663665
section.destination.stop_point.coord.lat = 4.0
664666
booking_rule = section.booking_rule

source/jormungandr/jormungandr/scenarios/tests/new_default_tests.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
from jormungandr.scenarios.utils import switch_back_to_ridesharing
4545
from jormungandr.utils import make_origin_destination_key, str_to_time_stamp
4646
from werkzeug.exceptions import HTTPException
47+
import pytz
48+
from jormungandr import app
49+
from flask import g
4750
import pytest
4851
from pytest_mock import mocker
4952
from collections import defaultdict
@@ -835,25 +838,27 @@ def journey_with_disruptions_on_poi_test(mocker):
835838

836839

837840
def journey_with_booking_rule_test():
838-
booking_url = (
839-
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
840-
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
841-
"&from_coord_lon={from_coord_lon}&not_managed={not_managed}"
842-
)
843-
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
844-
assert len(response_journey_with_odt.journeys) == 1
845-
journey = response_journey_with_odt.journeys[0]
846-
assert len(journey.sections) == 3
847-
odt_section = journey.sections[1]
848-
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
849-
assert (
850-
odt_section.booking_rule.booking_url
851-
== "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}"
852-
)
841+
with app.app_context():
842+
g.timezone = pytz.timezone("Europe/Paris")
843+
booking_url = (
844+
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
845+
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
846+
"&from_coord_lon={from_coord_lon}&not_managed={not_managed}"
847+
)
848+
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
849+
assert len(response_journey_with_odt.journeys) == 1
850+
journey = response_journey_with_odt.journeys[0]
851+
assert len(journey.sections) == 3
852+
odt_section = journey.sections[1]
853+
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
854+
assert (
855+
odt_section.booking_rule.booking_url
856+
== "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}"
857+
)
853858

854-
update_booking_rule_url_in_response(response_journey_with_odt)
855-
odt_section = response_journey_with_odt.journeys[0].sections[1]
856-
assert (
857-
odt_section.booking_rule.booking_url
858-
== "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"
859-
)
859+
update_booking_rule_url_in_response(response_journey_with_odt)
860+
odt_section = response_journey_with_odt.journeys[0].sections[1]
861+
assert (
862+
odt_section.booking_rule.booking_url
863+
== "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"
864+
)

source/jormungandr/jormungandr/scenarios/tests/utils_tests.py

+59-56
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
fill_disruptions_on_places_nearby,
3535
update_booking_rule_url_in_section,
3636
)
37-
37+
import pytz
38+
from jormungandr import app
39+
from flask import g
3840
import pytest
3941
from pytest_mock import mocker
4042

@@ -102,58 +104,59 @@ def update_disruptions_on_pois_for_places_nearby_test(mocker):
102104

103105

104106
def journey_with_booking_url_in_booking_rule_test():
105-
instance = lambda: None
106-
# Get a response with a section of ODT having booking_rule.
107-
booking_url = (
108-
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
109-
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
110-
"&from_coord_lon={from_coord_lon}&to_coord_lat={to_coord_lat}&to_coord_lon={to_coord_lon}"
111-
)
112-
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
113-
assert len(response_journey_with_odt.journeys) == 1
114-
journey = response_journey_with_odt.journeys[0]
115-
assert len(journey.sections) == 3
116-
odt_section = journey.sections[1]
117-
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
118-
booking_rule = odt_section.booking_rule
119-
assert booking_rule.name == "odt_name_value"
120-
assert (
121-
booking_rule.booking_url
122-
== "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}"
123-
)
124-
assert booking_rule.info_url == "odt_url_value"
125-
assert booking_rule.message == "odt_conditions_value"
126-
assert booking_rule.phone_number == "odt_phone_value"
127-
update_booking_rule_url_in_section(odt_section)
128-
assert (
129-
booking_rule.booking_url
130-
== "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"
131-
)
132-
133-
# Use a booking_url with fewer placeholders
134-
booking_url = (
135-
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
136-
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}"
137-
)
138-
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
139-
odt_section = response_journey_with_odt.journeys[0].sections[1]
140-
update_booking_rule_url_in_section(odt_section)
141-
assert (
142-
odt_section.booking_rule.booking_url
143-
== "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"
144-
)
145-
146-
# Add a placeholder which is not predefined in the function to update url
147-
# This placeholder will not be replaced(updated)
148-
booking_url = (
149-
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
150-
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
151-
"&from_coord_lon={from_coord_lon}&toto={toto}"
152-
)
153-
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
154-
odt_section = response_journey_with_odt.journeys[0].sections[1]
155-
update_booking_rule_url_in_section(odt_section)
156-
assert (
157-
odt_section.booking_rule.booking_url
158-
== "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"
159-
)
107+
with app.app_context():
108+
g.timezone = pytz.timezone("Europe/Paris")
109+
# Get a response with a section of ODT having booking_rule.
110+
booking_url = (
111+
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
112+
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
113+
"&from_coord_lon={from_coord_lon}&to_coord_lat={to_coord_lat}&to_coord_lon={to_coord_lon}"
114+
)
115+
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
116+
assert len(response_journey_with_odt.journeys) == 1
117+
journey = response_journey_with_odt.journeys[0]
118+
assert len(journey.sections) == 3
119+
odt_section = journey.sections[1]
120+
assert odt_section.type == response_pb2.ON_DEMAND_TRANSPORT
121+
booking_rule = odt_section.booking_rule
122+
assert booking_rule.name == "odt_name_value"
123+
assert (
124+
booking_rule.booking_url
125+
== "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}"
126+
)
127+
assert booking_rule.info_url == "odt_url_value"
128+
assert booking_rule.message == "odt_conditions_value"
129+
assert booking_rule.phone_number == "odt_phone_value"
130+
update_booking_rule_url_in_section(odt_section)
131+
assert (
132+
booking_rule.booking_url
133+
== "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"
134+
)
135+
136+
# Use a booking_url with fewer placeholders
137+
booking_url = (
138+
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
139+
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}&from_coord_lon={from_coord_lon}"
140+
)
141+
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
142+
odt_section = response_journey_with_odt.journeys[0].sections[1]
143+
update_booking_rule_url_in_section(odt_section)
144+
assert (
145+
odt_section.booking_rule.booking_url
146+
== "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"
147+
)
148+
149+
# Add a placeholder which is not predefined in the function to update url
150+
# This placeholder will not be replaced(updated)
151+
booking_url = (
152+
"https://domaine/search?departure-address={from_name}&destination-address={to_name}"
153+
"&requested-departure-time={departure_datetime}&from_coord_lat={from_coord_lat}"
154+
"&from_coord_lon={from_coord_lon}&toto={toto}"
155+
)
156+
response_journey_with_odt = helpers_tests.get_odt_journey(booking_url=booking_url)
157+
odt_section = response_journey_with_odt.journeys[0].sections[1]
158+
update_booking_rule_url_in_section(odt_section)
159+
assert (
160+
odt_section.booking_rule.booking_url
161+
== "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"
162+
)

source/jormungandr/jormungandr/scenarios/utils.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import navitiacommon.request_pb2 as request_pb2
3434
from future.moves.itertools import zip_longest
3535
from jormungandr.fallback_modes import FallbackModes
36+
from jormungandr.utils import timestamp_to_date_str
37+
from jormungandr.timezone import get_timezone_or_paris
3638
import requests as requests
3739
import re
3840
from collections import defaultdict
@@ -566,10 +568,10 @@ def update_booking_rule_url_in_section(section):
566568
return
567569

568570
departure_datetime = section.begin_date_time
569-
from_name = section.origin.stop_point.name
571+
from_name = section.origin.stop_point.label
570572
from_coord_lat = section.origin.stop_point.coord.lat
571573
from_coord_lon = section.origin.stop_point.coord.lon
572-
to_name = section.destination.stop_point.name
574+
to_name = section.destination.stop_point.label
573575
to_coord_lat = section.destination.stop_point.coord.lat
574576
to_coord_lon = section.destination.stop_point.coord.lon
575577

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

585+
# Datetime formatting: "%Y-%m-%dT%H:%M:%S%z" -> 2024-09-24T09:25:45+0200
586+
date_format = "%Y-%m-%dT%H:%M:%S%z"
587+
timezone = get_timezone_or_paris()
588+
583589
for p in placeholders:
584590
if p == "departure_datetime":
585-
placeholder_dict[p] = departure_datetime
591+
placeholder_dict[p] = timestamp_to_date_str(departure_datetime, timezone, _format=date_format)
586592
elif p == "from_name":
587593
placeholder_dict[p] = from_name
588594
elif p == "from_coord_lat":

0 commit comments

Comments
 (0)