Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dependencies = [
"httpx==0.28.1",
"pydantic-settings==2.14.1",
"gtfs-realtime-bindings==2.0.0",
"pandas==3.0.3",
]

[project.optional-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/translink/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def resolve_static_schedule(cache: StaticScheduleCache, service_date: date) -> l
async def get_static_schedule(
db_session: DBSession, service_date: date | None = None
) -> tuple[date, list[StaticScheduleEntry]]:
"""Read the weekly cache and resolve it for a date without network or pandas work."""
"""Read the weekly cache and resolve it for a date without network or bulk parsing work."""
target_date = service_date or datetime.now(tz=TZ_INFO).date()
try:
cached = await db_session.scalar(
Expand Down
34 changes: 17 additions & 17 deletions tests/unit/test_translink.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import csv
import io
import zipfile
from datetime import date, datetime, timedelta
from unittest.mock import AsyncMock, MagicMock, patch

import httpx
import pandas as pd
import pytest
from fastapi import status
from google.transit import gtfs_realtime_pb2
Expand Down Expand Up @@ -37,8 +37,12 @@
# ---------------------------------------------------------------------------


def _current_day_name() -> str:
return datetime.now(tz=TZ_INFO).strftime("%A").lower()
def rows_to_csv(rows: list[dict[str, str]]) -> str:
output = io.StringIO(newline="")
writer = csv.DictWriter(output, fieldnames=list(rows[0]))
writer.writeheader()
writer.writerows(rows)
return output.getvalue()


def make_gtfs_zip(departure_time: str = "23:00:00", active_weekdays: set[int] | None = None) -> bytes:
Expand All @@ -57,7 +61,7 @@ def make_gtfs_zip(departure_time: str = "23:00:00", active_weekdays: set[int] |
with zipfile.ZipFile(buf, "w") as z:
cal_row = {day: ("1" if index in active_weekdays else "0") for index, day in enumerate(all_days)}
cal_row.update({"service_id": "SVC1", "start_date": "20240101", "end_date": "20991231"})
z.writestr("calendar.txt", pd.DataFrame([cal_row]).to_csv(index=False))
z.writestr("calendar.txt", rows_to_csv([cal_row]))

# calendar_dates.txt - no exceptions
z.writestr("calendar_dates.txt", "date,service_id,exception_type\n")
Expand All @@ -67,14 +71,14 @@ def make_gtfs_zip(departure_time: str = "23:00:00", active_weekdays: set[int] |
{"trip_id": f"trip_{num}", "route_id": rid, "service_id": "SVC1", "direction_id": str(did)}
for rid, (did, _sid, num) in BUS_DATA.items()
]
z.writestr("trips.txt", pd.DataFrame(trips_rows).to_csv(index=False))
z.writestr("trips.txt", rows_to_csv(trips_rows))

# stop_times.txt - one stop per trip at the correct SFU bus loop stop
stop_rows = [
{"trip_id": f"trip_{num}", "stop_id": sid, "departure_time": departure_time}
for _rid, (_, sid, num) in BUS_DATA.items()
]
z.writestr("stop_times.txt", pd.DataFrame(stop_rows).to_csv(index=False))
z.writestr("stop_times.txt", rows_to_csv(stop_rows))

return buf.getvalue()

Expand Down Expand Up @@ -283,27 +287,23 @@ async def test__weekly_cache_resolves_multiple_weekdays_without_refetching():
async def test__fetch_static_schedule_excludes_wrong_direction():
"""Trips are direction-filtered; a wrong-direction trip should not appear."""
buf = io.BytesIO()
day = _current_day_name()
all_days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
current_weekday = datetime.now(tz=TZ_INFO).weekday()

with zipfile.ZipFile(buf, "w") as z:
cal_row = {d: ("1" if d == day else "0") for d in all_days}
cal_row = {day: ("1" if index == current_weekday else "0") for index, day in enumerate(all_days)}
cal_row.update({"service_id": "SVC1", "start_date": "20240101", "end_date": "20991231"})
z.writestr("calendar.txt", pd.DataFrame([cal_row]).to_csv(index=False))
z.writestr("calendar.txt", rows_to_csv([cal_row]))
z.writestr("calendar_dates.txt", "date,service_id,exception_type\n")

# Route 6656 expects direction_id=0; give it direction_id=1
trips_df = pd.DataFrame(
[
{"trip_id": "wrong_dir", "route_id": "6656", "service_id": "SVC1", "direction_id": "1"},
]
z.writestr(
"trips.txt",
rows_to_csv([{"trip_id": "wrong_dir", "route_id": "6656", "service_id": "SVC1", "direction_id": "1"}]),
)
z.writestr("trips.txt", trips_df.to_csv(index=False))
z.writestr(
"stop_times.txt",
pd.DataFrame([{"trip_id": "wrong_dir", "stop_id": "2836", "departure_time": "23:00:00"}]).to_csv(
index=False
),
rows_to_csv([{"trip_id": "wrong_dir", "stop_id": "2836", "departure_time": "23:00:00"}]),
)

client = mock_http_client(buf.getvalue())
Expand Down
Loading
Loading