Skip to content

Commit 20d6b64

Browse files
committed
tests: avoid failing if fast_rolling_stock already exists
1 parent 4931c4b commit 20d6b64

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

tests/conftest.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from pathlib import Path
3-
from typing import Any, Iterable, List
3+
from typing import Any, Iterable, List, Optional
44

55
import pytest
66
import requests
@@ -80,13 +80,36 @@ def small_scenario(small_infra: Infra, foo_project_id: int, foo_study_id: int) -
8080
yield Scenario(foo_project_id, foo_study_id, scenario_id, small_infra.id, timetable_id)
8181

8282

83-
def _create_fast_rolling_stocks(test_rolling_stocks: List[TestRollingStock] = None):
83+
def get_rolling_stock(editoast_url: str, rolling_stock_name: str) -> int:
84+
"""
85+
Returns the ID corresponding to the rolling stock name, if available.
86+
:param editoast_url: Api url
87+
:param rolling_stock_name: name of the rolling stock
88+
:return: ID the rolling stock
89+
"""
90+
page = 1
91+
while page is not None:
92+
# TODO: feel free to reduce page_size when https://github.com/osrd-project/osrd/issues/5350 is fixed
93+
r = requests.get(editoast_url + "light_rolling_stock/", params={"page": page, "page_size": 1_000})
94+
if r.status_code // 100 != 2:
95+
raise RuntimeError(f"Rolling stock error {r.status_code}: {r.content}")
96+
rjson = r.json()
97+
for rolling_stock in rjson["results"]:
98+
if rolling_stock["name"] == rolling_stock_name:
99+
return rolling_stock["id"]
100+
page = rjson.get("next")
101+
raise ValueError(f"Unable to find rolling stock {rolling_stock_name}")
102+
103+
104+
def create_fast_rolling_stocks(test_rolling_stocks: Optional[List[TestRollingStock]] = None):
84105
if test_rolling_stocks is None:
85106
payload = json.loads(FAST_ROLLING_STOCK_JSON_PATH.read_text())
86-
response = requests.post(f"{EDITOAST_URL}rolling_stock/", json=payload).json()
87-
# TODO: if the fast_rolling_stock already exists, we should probably fetch it
88-
assert "id" in response, f"Failed to create rolling stock: {response}"
89-
return [response["id"]]
107+
response = requests.post(f"{EDITOAST_URL}rolling_stock/", json=payload)
108+
rjson = response.json()
109+
if response.status_code // 100 == 4 and "NameAlreadyUsed" in rjson["type"]:
110+
return [get_rolling_stock(EDITOAST_URL, rjson["context"]["name"])]
111+
assert "id" in rjson, f"Failed to create rolling stock: {rjson}"
112+
return [rjson["id"]]
90113
ids = []
91114
for rs in test_rolling_stocks:
92115
payload = json.loads(rs.base_path.read_text())
@@ -98,15 +121,15 @@ def _create_fast_rolling_stocks(test_rolling_stocks: List[TestRollingStock] = No
98121

99122
@pytest.fixture
100123
def fast_rolling_stocks(request: Any) -> Iterable[int]:
101-
ids = _create_fast_rolling_stocks(request.node.get_closest_marker("names_and_metadata").args[0])
124+
ids = create_fast_rolling_stocks(request.node.get_closest_marker("names_and_metadata").args[0])
102125
yield ids
103126
for id in ids:
104127
requests.delete(f"{EDITOAST_URL}rolling_stock/{id}?force=true")
105128

106129

107130
@pytest.fixture
108131
def fast_rolling_stock() -> int:
109-
id = _create_fast_rolling_stocks()[0]
132+
id = create_fast_rolling_stocks()[0]
110133
yield id
111134
requests.delete(f"{EDITOAST_URL}rolling_stock/{id}?force=true")
112135

tests/fuzzer/fuzzer.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import requests
1313
from requests import Response, Timeout
1414

15+
# TODO: we may want to use more qualified imports
16+
import conftest
17+
1518
TIMEOUT = 15
1619
# TODO: since infra ids are not stable, we may want to change to an infra name
1720
INFRA_ID = 1
@@ -303,18 +306,7 @@ def get_rolling_stock(editoast_url: str, rolling_stock_name: str) -> int:
303306
:param rolling_stock_name: name of the rolling stock
304307
:return: ID the rolling stock
305308
"""
306-
page = 1
307-
while page is not None:
308-
# TODO: feel free to reduce page_size when https://github.com/osrd-project/osrd/issues/5350 is fixed
309-
r = get_with_timeout(editoast_url + "light_rolling_stock/", params={"page": page, "page_size": 1_000})
310-
if r.status_code // 100 != 2:
311-
raise RuntimeError(f"Rolling stock error {r.status_code}: {r.content}")
312-
rjson = r.json()
313-
for rolling_stock in rjson["results"]:
314-
if rolling_stock["name"] == rolling_stock_name:
315-
return rolling_stock["id"]
316-
page = rjson.get("next")
317-
raise ValueError(f"Unable to find rolling stock {rolling_stock_name}")
309+
return conftest.get_rolling_stock(editoast_url, rolling_stock_name)
318310

319311

320312
def get_random_rolling_stock(editoast_url: str) -> int:
@@ -675,21 +667,13 @@ def get_infra_name(editoast_url: str, infra_id: int):
675667
return r.json()["name"]
676668

677669

678-
# TODO: duplicated in tests/conftest.py
679-
def create_fast_rolling_stock():
680-
path = Path(__file__).parents[2] / "editoast" / "src" / "tests" / "example_rolling_stock_1.json"
681-
payload = json.loads(path.read_text())
682-
response = requests.post(f"{EDITOAST_URL}rolling_stock/", json=payload).json()
683-
assert "id" in response, f"Failed to create rolling stock: {response}"
684-
685-
686670
if __name__ == "__main__":
687671
new_scenario = create_scenario(EDITOAST_URL, INFRA_ID)
688672
if ROLLING_STOCK_NAME == "fast_rolling_stock":
689673
try:
690674
get_rolling_stock(EDITOAST_URL, ROLLING_STOCK_NAME)
691675
except ValueError:
692-
create_fast_rolling_stock()
676+
conftest.create_fast_rolling_stocks(test_rolling_stocks=None)
693677
run(
694678
EDITOAST_URL,
695679
new_scenario,

0 commit comments

Comments
 (0)