Skip to content

Commit 3db486c

Browse files
committed
Flag to run tests with static ids
1 parent 0174d51 commit 3db486c

File tree

7 files changed

+62
-38
lines changed

7 files changed

+62
-38
lines changed

tests/conftest.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Pytest configuration for test suite."""
2+
import uuid
3+
import pytest
4+
5+
6+
def pytest_addoption(parser):
7+
"""Add custom command-line options to pytest."""
8+
parser.addoption(
9+
"--fixed-ids",
10+
action="store_true",
11+
default=False,
12+
help="Use fixed car IDs instead of random UUIDs (useful for frontend testing)",
13+
)
14+
15+
16+
@pytest.fixture(scope="session")
17+
def use_fixed_ids(request):
18+
"""Fixture that returns whether to use fixed car IDs."""
19+
return request.config.getoption("--fixed-ids")
20+
21+
22+
@pytest.fixture
23+
def get_car_id(use_fixed_ids):
24+
"""Fixture that returns a function to generate car IDs."""
25+
def _get_car_id(base_name: str) -> str:
26+
"""Generate car ID: random UUID by default, or fixed name with --fixed-ids flag."""
27+
if use_fixed_ids:
28+
return base_name
29+
return f"{base_name}-{str(uuid.uuid4())[:8]}"
30+
31+
return _get_car_id

tests/test_accident.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ def collect_alerts(alert_queue: queue.Queue, car_ids: list[str], timeout: float
9191
return alerts
9292

9393

94-
def test_accident_directional_notification():
95-
random_id = str(uuid.uuid4())[:8]
96-
accident_car = f"accident-car-{random_id}"
97-
car_behind = f"car-behind-{random_id}"
98-
car_ahead = f"car-ahead-{random_id}"
94+
def test_accident_directional_notification(get_car_id):
95+
accident_car = get_car_id("accident-car")
96+
car_behind = get_car_id("car-behind")
97+
car_ahead = get_car_id("car-ahead")
9998

10099
ensure_car_exists(accident_car)
101100
ensure_car_exists(car_behind)
@@ -185,6 +184,17 @@ def other_cars_thread():
185184
time.sleep(2)
186185
alerts = collect_alerts(alert_queue, [car_behind, car_ahead], timeout=ALERT_TIMEOUT)
187186

187+
# Remove car device files to avoid interference with next test
188+
accident_car_file = SIM_DIR / "devices" / f"{accident_car}.json"
189+
car_behind_file = SIM_DIR / "devices" / f"{car_behind}.json"
190+
car_ahead_file = SIM_DIR / "devices" / f"{car_ahead}.json"
191+
if accident_car_file.exists():
192+
accident_car_file.unlink()
193+
if car_behind_file.exists():
194+
car_behind_file.unlink()
195+
if car_ahead_file.exists():
196+
car_ahead_file.unlink()
197+
188198
# Assertions
189199
assert len(alerts[car_behind]) > 0, (
190200
f"Car BEHIND should receive accident alerts. "
@@ -197,17 +207,6 @@ def other_cars_thread():
197207
f"Got {len(alerts[car_ahead])} alerts."
198208
)
199209

200-
# Remove car device files to avoid interference with next test
201-
accident_car_file = SIM_DIR / "devices" / f"{accident_car}.json"
202-
car_behind_file = SIM_DIR / "devices" / f"{car_behind}.json"
203-
car_ahead_file = SIM_DIR / "devices" / f"{car_ahead}.json"
204-
if accident_car_file.exists():
205-
accident_car_file.unlink()
206-
if car_behind_file.exists():
207-
car_behind_file.unlink()
208-
if car_ahead_file.exists():
209-
car_ahead_file.unlink()
210-
211210

212211
if __name__ == "__main__":
213212
test_accident_directional_notification()

tests/test_curved_route.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ def send_position(car_name: str, lat: float, lon: float) -> None:
3838
], check=True)
3939

4040

41-
def test_curved_route():
41+
def test_curved_route(get_car_id):
4242
"""Test vehicle navigation on a curved route with complex trajectory."""
43-
random_id = str(uuid.uuid4())[:8]
44-
car = f"curved-route-car-{random_id}"
43+
car = get_car_id("curved-route-car")
4544
ensure_car_exists(car)
4645

4746
# subscribe to position updates topic

tests/test_emergency_vehicle.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ def send_position(car_name: str, lat: float, lon: float) -> None:
3434
)
3535

3636

37-
def test_emergency_vehicle():
38-
random_id = str(uuid.uuid4())[:8]
39-
car_regular = f"ev-test-regular-{random_id}" # regular car (victim)
40-
car_emergency = f"ev-test-emergency-{random_id}" # emergency vehicle
37+
def test_emergency_vehicle(get_car_id):
38+
car_regular = get_car_id("ev-test-regular") # regular car (victim)
39+
car_emergency = get_car_id("ev-test-emergency") # emergency vehicle
4140

4241
ensure_car_exists(car_regular, emergency=False)
4342
ensure_car_exists(car_emergency, emergency=True)

tests/test_highway_entry.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ def on_highway_entry_alert(client, userdata, msg):
2626
ALERTS.append(json.loads(msg.payload.decode()))
2727

2828

29-
def test_highway_entry_unsafe():
29+
def test_highway_entry_unsafe(get_car_id):
3030
ALERTS.clear() # Clear alerts at the start
3131

32-
random_id = str(uuid.uuid4())[:8]
33-
highway_car = f"highway-car-{random_id}"
34-
entering_car = f"entering-car-{random_id}"
32+
highway_car = get_car_id("highway-car")
33+
entering_car = get_car_id("entering-car")
3534
ensure_car_exists(highway_car)
3635
ensure_car_exists(entering_car)
3736

@@ -103,12 +102,11 @@ def test_highway_entry_unsafe():
103102
assert len(unsafe_alerts) > 0, f"Expected unsafe alert but got: {ALERTS}"
104103

105104

106-
def test_highway_entry_safe():
105+
def test_highway_entry_safe(get_car_id):
107106
ALERTS.clear()
108107

109-
random_id = str(uuid.uuid4())[:8]
110-
highway_car = f"highway-car-{random_id}"
111-
entering_car = f"entering-car-{random_id}"
108+
highway_car = get_car_id("highway-car-2")
109+
entering_car = get_car_id("entering-car-2")
112110

113111
ensure_car_exists(highway_car)
114112
ensure_car_exists(entering_car)

tests/test_overtaking.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ def send_position(car_name: str, lat: float, lon: float) -> None:
2525
car_name, str(lat), str(lon)
2626
], check=True)
2727

28-
def test_overtaking():
29-
random_id = str(uuid.uuid4())[:8]
30-
car_slow = f"overtaking-car-front-{random_id}" # victim
31-
car_fast = f"overtaking-car-behind-{random_id}" # overtaker
28+
def test_overtaking(get_car_id):
29+
car_slow = get_car_id("overtaking-car-front") # victim
30+
car_fast = get_car_id("overtaking-car-behind") # overtaker
3231

3332
ensure_car_exists(car_slow)
3433
ensure_car_exists(car_fast)

tests/test_speeding.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ def on_speed_alert(client, userdata, msg):
2525
ALERTS.append(json.loads(msg.payload.decode()))
2626

2727

28-
def test_speeding():
29-
random_id = str(uuid.uuid4())[:8]
30-
car = f"speed-car-{random_id}"
28+
def test_speeding(get_car_id):
29+
car = get_car_id("speed-car")
3130
ensure_car_exists(car)
3231

3332
# subscribe to speed alerts

0 commit comments

Comments
 (0)