Skip to content

Commit 259a3dc

Browse files
authored
Merge pull request #2205 from c2corg/smart-origin/ffcam_livraison1
feat: Smart/Origin: ffcam livraison1
2 parents 6696cad + ebaab6d commit 259a3dc

6 files changed

Lines changed: 342 additions & 37 deletions

File tree

c2corg_api/tests/views/test_navitia.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from shapely.geometry import Polygon
33
import json
44
import requests
5-
from pyramid.httpexceptions import HTTPBadRequest
5+
from pyramid.httpexceptions import (HTTPBadRequest, HTTPNotFound,
6+
HTTPUnauthorized)
67
from unittest.mock import call
78
from c2corg_api.tests import BaseTestCase
89
from c2corg_api.views import to_json_dict
@@ -1207,7 +1208,7 @@ def test_handle_navitia_response_401(self):
12071208
mock_response = mock.Mock()
12081209
mock_response.status_code = 401
12091210

1210-
with self.assertRaises(HTTPInternalServerError):
1211+
with self.assertRaises(HTTPUnauthorized):
12111212
handle_navitia_response(mock_response)
12121213

12131214
def test_handle_navitia_response_400(self):
@@ -1220,18 +1221,17 @@ def test_handle_navitia_response_400(self):
12201221
def test_handle_navitia_response_404_no_journey(self):
12211222
mock_response = mock.Mock()
12221223
mock_response.status_code = 404
1223-
mock_response.json.return_value = {"error": {"id": "no_origin"}}
1224+
mock_response.json.return_value = {"error": {"id": "no_solution"}}
12241225

1225-
result = handle_navitia_response(mock_response)
1226-
self.assertIsNone(result)
1226+
with self.assertRaises(HTTPNotFound):
1227+
handle_navitia_response(mock_response)
12271228

12281229
def test_handle_navitia_response_404_other_error(self):
12291230
mock_response = mock.Mock()
12301231
mock_response.status_code = 404
1231-
mock_response.json.return_value = {"error": {"id": "some_other_error"}}
1232-
1233-
result = handle_navitia_response(mock_response)
1234-
self.assertIsNone(result)
1232+
mock_response.json.return_value = {"error": {"id": "unknown_object"}}
1233+
with self.assertRaises(HTTPNotFound):
1234+
handle_navitia_response(mock_response)
12351235

12361236
def test_handle_navitia_response_other_error(self):
12371237
mock_response = mock.Mock()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from c2corg_api.models.stoparea import Stoparea
2+
from c2corg_api.tests.views import BaseDocumentTestRest
3+
4+
5+
class TestStopareaRest(BaseDocumentTestRest):
6+
7+
def setUp(self): # noqa
8+
self.set_prefix_and_model(
9+
"/stopareas", "sa", Stoparea, None, None)
10+
BaseDocumentTestRest.setUp(self)
11+
self._add_test_data()
12+
13+
def _add_test_data(self):
14+
# Create a stoparea for testing
15+
stoparea = Stoparea(
16+
stoparea_id=1,
17+
navitia_id='nav1',
18+
stoparea_name='Stop Area 1',
19+
line='line1',
20+
operator='operator1',
21+
)
22+
23+
self.session.add(stoparea)
24+
self.session.flush()
25+
26+
def test_collection_get(self):
27+
"""Test getting list of stopareas."""
28+
response = self.app.get('/stopareas', status=200)
29+
result = response.json
30+
31+
assert result['total_results'] >= 0
32+
assert isinstance(result['documents'], list)
33+
34+
def test_get_stoparea_not_found(self):
35+
"""Test getting a stoparea that doesn't exist."""
36+
response = self.app.get('/stopareas/999999', status=404)
37+
result = response.json
38+
39+
assert result['error'] == 'Stoparea not found'
40+
41+
def test_get_stoparea_found(self):
42+
"""Test getting a stoparea that exists."""
43+
response = self.app.get('/stopareas/1', status=200)
44+
result = response.json
45+
46+
assert result['id'] == 1
47+
assert result['navitia_id'] == 'nav1'
48+
assert result['stoparea_name'] == 'Stop Area 1'
49+
assert result['line'] == 'line1'
50+
assert result['operator'] == 'operator1'
51+
52+
53+
class TestStopareaInfoRest(BaseDocumentTestRest):
54+
55+
def setUp(self): # noqa
56+
self.set_prefix_and_model(
57+
"/stopareas", "sa", Stoparea, None, None)
58+
BaseDocumentTestRest.setUp(self)
59+
self._add_test_data()
60+
61+
def _add_test_data(self):
62+
# Create a stoparea for testing
63+
stoparea = Stoparea(
64+
stoparea_id=1,
65+
navitia_id='nav1',
66+
stoparea_name='Stop Area 1',
67+
line='line1',
68+
operator='operator1'
69+
)
70+
71+
self.session.add(stoparea)
72+
self.session.flush()
73+
74+
def test_get_info_stoparea_not_found(self):
75+
"""Test getting info for a stoparea that doesn't exist."""
76+
response = self.app.get('/stopareas/999999/fr/info', status=404)
77+
result = response.json
78+
79+
assert result['error'] == 'Stoparea not found'
80+
81+
def test_get_info_stoparea_found(self):
82+
"""Test getting info for a stoparea that exists."""
83+
response = self.app.get('/stopareas/1/fr/info', status=200)
84+
result = response.json
85+
86+
assert result['stoparea_id'] == 1
87+
assert result['attributes']['navitia_id'] == 'nav1'
88+
assert result['attributes']['stoparea_name'] == 'Stop Area 1'
89+
assert result['attributes']['line'] == 'line1'
90+
assert result['attributes']['operator'] == 'operator1'
91+
92+
def test_get_info_stoparea_lang_not_found(self):
93+
"""Test getting info for a stoparea that doesn't exist."""
94+
response = self.app.get('/stopareas/1/invalid/info', status=400)
95+
result = response.json
96+
97+
assert result['errors'][0]['description'] == 'invalid lang'
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
from c2corg_api.models.waypoint import (
2+
Waypoint, WaypointLocale)
3+
from c2corg_api.models.stoparea import Stoparea
4+
from c2corg_api.models.waypoint_stoparea import WaypointStoparea
5+
6+
from c2corg_api.tests.views import BaseDocumentTestRest
7+
8+
9+
class TestWaypointStopareaRest(BaseDocumentTestRest):
10+
11+
def setUp(self): # noqa
12+
self.set_prefix_and_model(
13+
"/waypoints_stopareas", "ws", WaypointStoparea, None, None)
14+
BaseDocumentTestRest.setUp(self)
15+
self._add_test_data()
16+
17+
def _add_test_data(self):
18+
# Create waypoints for testing
19+
waypoint1 = Waypoint(
20+
waypoint_type='summit',
21+
elevation=3779
22+
)
23+
24+
locale_en = WaypointLocale(
25+
lang='en', title='Mont Pourri', access='y')
26+
waypoint1.locales.append(locale_en)
27+
28+
self.session.add(waypoint1)
29+
self.session.flush()
30+
self.waypoint1 = waypoint1
31+
32+
waypoint2 = Waypoint(
33+
waypoint_type='summit',
34+
elevation=3000
35+
)
36+
37+
locale_en2 = WaypointLocale(
38+
lang='en', title='Another Summit', access='y')
39+
waypoint2.locales.append(locale_en2)
40+
41+
self.session.add(waypoint2)
42+
self.session.flush()
43+
self.waypoint2 = waypoint2
44+
45+
# Create stopareas for testing
46+
stoparea1 = Stoparea(
47+
stoparea_id=1,
48+
navitia_id='nav1',
49+
stoparea_name='Stop Area 1',
50+
line='line1',
51+
operator='operator1'
52+
)
53+
54+
self.session.add(stoparea1)
55+
self.session.flush()
56+
self.stoparea1 = stoparea1
57+
58+
stoparea2 = Stoparea(
59+
stoparea_id=2,
60+
navitia_id='nav2',
61+
stoparea_name='Stop Area 2',
62+
line='line2',
63+
operator='operator2'
64+
)
65+
66+
self.session.add(stoparea2)
67+
self.session.flush()
68+
self.stoparea2 = stoparea2
69+
70+
# Create waypoint-stoparea associations
71+
waypoint_stoparea1 = WaypointStoparea(
72+
waypoint_stoparea_id=1,
73+
waypoint_id=waypoint1.document_id,
74+
stoparea_id=stoparea1.stoparea_id,
75+
distance=100.0
76+
)
77+
78+
self.session.add(waypoint_stoparea1)
79+
self.session.flush()
80+
self.waypoint_stoparea1 = waypoint_stoparea1
81+
82+
waypoint_stoparea2 = WaypointStoparea(
83+
waypoint_stoparea_id=2,
84+
waypoint_id=waypoint1.document_id,
85+
stoparea_id=stoparea2.stoparea_id,
86+
distance=200.0
87+
)
88+
89+
self.session.add(waypoint_stoparea2)
90+
self.session.flush()
91+
self.waypoint_stoparea2 = waypoint_stoparea2
92+
93+
def test_get_stopareas_by_waypoint(self):
94+
"""Test getting stopareas for a waypoint."""
95+
response = self.app.get('/waypoints/{}/stopareas'.format(
96+
self.waypoint1.document_id), status=200)
97+
result = response.json
98+
99+
self.assertEqual(result['waypoint_id'], self.waypoint1.document_id)
100+
self.assertEqual(len(result['stopareas']), 2)
101+
102+
stoparea1 = result['stopareas'][0]
103+
self.assertEqual(stoparea1['id'], 1)
104+
self.assertEqual(stoparea1['navitia_id'], 'nav1')
105+
self.assertEqual(stoparea1['stoparea_name'], 'Stop Area 1')
106+
self.assertEqual(stoparea1['line'], 'line1')
107+
self.assertEqual(stoparea1['operator'], 'operator1')
108+
self.assertEqual(stoparea1['distance'], 100.0)
109+
110+
stoparea2 = result['stopareas'][1]
111+
self.assertEqual(stoparea2['id'], 2)
112+
self.assertEqual(stoparea2['navitia_id'], 'nav2')
113+
self.assertEqual(stoparea2['stoparea_name'], 'Stop Area 2')
114+
self.assertEqual(stoparea2['line'], 'line2')
115+
self.assertEqual(stoparea2['operator'], 'operator2')
116+
self.assertEqual(stoparea2['distance'], 200.0)
117+
118+
def test_get_stopareas_by_waypoint_not_found(self):
119+
"""Test getting stopareas for a waypoint that doesn't exist."""
120+
# We'll test that it returns an empty array instead of 404
121+
response = self.app.get('/waypoints/999999/stopareas', status=200)
122+
result = response.json
123+
124+
self.assertEqual(result['waypoint_id'], 999999)
125+
self.assertEqual(result['stopareas'], [])
126+
127+
def test_get_is_reachable_true(self):
128+
"""Test checking if a waypoint is reachable (has stopareas)."""
129+
response = self.app.get('/waypoints/{}/isReachable'.format(
130+
self.waypoint1.document_id), status=200)
131+
result = response.json
132+
133+
self.assertTrue(result)
134+
135+
def test_get_is_reachable_false(self):
136+
"""Test checking if a waypoint is not reachable (no stopareas)."""
137+
response = self.app.get('/waypoints/{}/isReachable'.format(
138+
self.waypoint2.document_id), status=200)
139+
result = response.json
140+
141+
self.assertFalse(result)
142+
143+
def test_get_info(self):
144+
"""Test getting info for a waypoint-stoparea."""
145+
146+
response = self.app.get(
147+
'/waypoints_stopareas/1/en/info', status=200)
148+
result = response.json
149+
150+
self.assertEqual(
151+
result['waypoint_stoparea_id'],
152+
self.waypoint_stoparea1.waypoint_stoparea_id
153+
)
154+
self.assertEqual(
155+
result['attributes']['distance'],
156+
100.0
157+
)
158+
self.assertEqual(
159+
result['attributes']['waypoint_id'],
160+
self.waypoint1.document_id
161+
)
162+
self.assertEqual(result['attributes']['stoparea_id'], 1)
163+
164+
def test_get_info_not_found(self):
165+
"""Test getting info for a waypoint-stoparea that doesn't exist."""
166+
response = self.app.get(
167+
'/waypoints_stopareas/999999/en/info', status=404)
168+
self.assertEqual(
169+
response.json_body,
170+
{'error': 'Waypoint Stoparea not found'}
171+
)
172+
173+
def test_get_info_lang_not_found(self):
174+
"""Test getting info with an invalid lang."""
175+
response = self.app.get(
176+
'/waypoints_stopareas/1/invalid/info', status=400)
177+
178+
# Updated to match actual JSON response format
179+
self.assertEqual(
180+
response.json_body['errors'][0]['description'],
181+
"invalid lang"
182+
)

0 commit comments

Comments
 (0)