Skip to content

Commit 779c0ea

Browse files
committed
fixup! tests: adapt fuzzer for timetable v2
1 parent 27c37dd commit 779c0ea

File tree

4 files changed

+71
-53
lines changed

4 files changed

+71
-53
lines changed

tests/fuzzer/fuzzer.py

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
"""
2929
Generates random tests, running pathfinding + simulations on random paths.
30+
DEPRECATED: the requests follow the train schedule v1 format.
31+
To be deleted with the old version of the endpoints.
3032
"""
3133

3234

tests/fuzzer/fuzzer_v2.py

+50-47
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from enum import Enum, IntEnum
99
from functools import cache
1010
from pathlib import Path
11-
from typing import Dict, Iterable, List, Optional, Tuple
11+
from typing import Dict, Iterable, List, Optional, Tuple, TypeVar
1212

1313
import requests
1414
from osrd_schemas.switch_type import builtin_node_types
@@ -36,51 +36,6 @@
3636
"""
3737

3838

39-
class _FailedTest(Exception):
40-
pass
41-
42-
43-
class _ErrorType(str, Enum):
44-
SCHEDULE = "SCHEDULE"
45-
RESULT = "RESULT"
46-
STDCM = "STDCM"
47-
48-
49-
class _Endpoint(IntEnum):
50-
BEGIN = 0
51-
END = 1
52-
53-
def opposite(self):
54-
return _Endpoint.END if self == _Endpoint.BEGIN else _Endpoint.BEGIN
55-
56-
57-
@dataclass(eq=True, frozen=True)
58-
class _TrackEndpoint:
59-
track: str
60-
endpoint: _Endpoint
61-
62-
@staticmethod
63-
def from_dict(obj: Dict) -> "_TrackEndpoint":
64-
return _TrackEndpoint(obj["track"], _Endpoint._member_map_[obj["endpoint"]])
65-
66-
67-
@dataclass
68-
class _InfraGraph:
69-
RJSInfra: Dict
70-
tracks: Dict[str, Dict] = field(default_factory=dict)
71-
links: Dict[_TrackEndpoint, List[_TrackEndpoint]] = field(default_factory=lambda: defaultdict(list))
72-
73-
def link(self, a: _TrackEndpoint, b: _TrackEndpoint):
74-
self.links[a].append(b)
75-
self.links[b].append(a)
76-
77-
78-
@dataclass
79-
class _RollingStock:
80-
name: str
81-
id: int
82-
83-
8439
def run(
8540
editoast_url: str,
8641
scenario: Scenario,
@@ -186,6 +141,54 @@ def create_scenario(editoast_url: str, infra_id: int) -> Scenario:
186141
return Scenario(project_id, study_id, id, infra_id, timetable_id)
187142

188143

144+
class _FailedTest(Exception):
145+
pass
146+
147+
148+
class _ErrorType(str, Enum):
149+
SCHEDULE = "SCHEDULE"
150+
RESULT = "RESULT"
151+
STDCM = "STDCM"
152+
153+
154+
class _Endpoint(IntEnum):
155+
BEGIN = 0
156+
END = 1
157+
158+
def opposite(self):
159+
return _Endpoint.END if self == _Endpoint.BEGIN else _Endpoint.BEGIN
160+
161+
162+
@dataclass(eq=True, frozen=True)
163+
class _TrackEndpoint:
164+
track: str
165+
endpoint: _Endpoint
166+
167+
@staticmethod
168+
def from_dict(obj: Dict) -> "_TrackEndpoint":
169+
return _TrackEndpoint(obj["track"], _Endpoint._member_map_[obj["endpoint"]])
170+
171+
172+
@dataclass
173+
class _InfraGraph:
174+
RJSInfra: Dict
175+
tracks: Dict[str, Dict] = field(default_factory=dict)
176+
links: Dict[_TrackEndpoint, List[_TrackEndpoint]] = field(default_factory=lambda: defaultdict(list))
177+
178+
def link(self, a: _TrackEndpoint, b: _TrackEndpoint):
179+
self.links[a].append(b)
180+
self.links[b].append(a)
181+
182+
183+
@dataclass
184+
class _RollingStock:
185+
name: str
186+
id: int
187+
188+
189+
U = TypeVar("U")
190+
191+
189192
def _make_error(
190193
error_type: _ErrorType,
191194
response: Response,
@@ -373,7 +376,7 @@ def _make_graph(editoast_url: str, infra: int) -> _InfraGraph:
373376
return graph
374377

375378

376-
def _random_set_element(s: Iterable):
379+
def _random_set_element(s: Iterable[U]) -> U:
377380
"""
378381
Picks a random element in an iterable
379382
"""

tests/tests/test_regressions.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414
REGRESSION_TESTS_JSON_FILES = [
1515
str(json_file.relative_to(REGRESSION_TESTS_DATA_FOLDER))
1616
for json_file in REGRESSION_TESTS_DATA_FOLDER.rglob("*.json")
17-
if json.loads(json_file.read_bytes()).get("timetable_version") != 2
17+
if json.loads(json_file.read_text()).get("timetable_version") != 2
1818
]
1919

2020

21+
"""
22+
This file runs the regression tests generated by the fuzzer.
23+
DEPRECATED: the requests follow the train schedule v1 format.
24+
To be deleted with the old version of the endpoints,
25+
along with the old test files.
26+
"""
27+
28+
2129
def _load_infra(editoast_url: str, infra_id):
2230
"""
2331
Send a request to preload the infra, raise a RuntimeError on failure

tests/tests/test_regressions_v2.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from pathlib import Path
3-
from typing import List
3+
from typing import Dict, List, Optional
44

55
import pytest
66
import requests
@@ -14,11 +14,16 @@
1414
REGRESSION_TESTS_JSON_FILES = [
1515
str(json_file.relative_to(REGRESSION_TESTS_DATA_FOLDER))
1616
for json_file in REGRESSION_TESTS_DATA_FOLDER.rglob("*.json")
17-
if json.loads(json_file.read_bytes()).get("timetable_version") == 2
17+
if json.loads(json_file.read_text()).get("timetable_version") == 2
1818
]
1919

2020

21-
def _load_infra(editoast_url: str, infra_id):
21+
"""
22+
This file runs the regression tests generated by the fuzzer.
23+
"""
24+
25+
26+
def _load_infra(editoast_url: str, infra_id: int):
2227
"""
2328
Send a request to preload the infra, raise a RuntimeError on failure
2429
"""
@@ -29,7 +34,7 @@ def _load_infra(editoast_url: str, infra_id):
2934
raise RuntimeError(f"Infra load {r.status_code}: {r.content}")
3035

3136

32-
def _schedule_with_payload(editoast_url: str, payload, accept_400, scenario: Scenario):
37+
def _schedule_with_payload(editoast_url: str, payload: Dict, accept_400: bool, scenario: Scenario) -> Optional[int]:
3338
"""
3439
Send a schedule request with the given payload, raises an error if the request failed (unless we accept 400s).
3540
Returns the schedule id.
@@ -42,7 +47,7 @@ def _schedule_with_payload(editoast_url: str, payload, accept_400, scenario: Sce
4247
return r.json()[0]["id"]
4348

4449

45-
def _stdcm_with_payload(editoast_url: str, payload, scenario: Scenario):
50+
def _stdcm_with_payload(editoast_url: str, payload: Dict, scenario: Scenario):
4651
"""
4752
Send a stdcm request with the given payload, raises an error if the request failed.
4853
"""

0 commit comments

Comments
 (0)