Skip to content

Testing helpers

Rendered at build time from the docstrings and signatures of continuous_gtfs.testing. A member with no description has no docstring yet; the fix is in the code, never on this page.

Fixtures and assertions for unit-testing a step in isolation. The Testing Transforms guide walks through the pattern.

Public test-helper API for unit-testing transform steps.

A transform step — a continuous_gtfs builtin instance like RemoveRows or UpdateFields, or a custom @step function — exposes .apply(ctx), which mutates a :class:~continuous_gtfs.PipelineContext in place. To unit-test one step in isolation:

  1. Build a PipelineContext whose .output holds the GTFS table(s) or GTFS-RT feed(s) the step reads — these helpers do that.
  2. Call step.apply(ctx).
  3. Assert on the mutated ctx.output.

No orchestrator, no network, no real feed — just the step's logic against a tiny in-memory fixture.

Example::

from continuous_gtfs.testing import gtfs_df, schedule_context

def test_remove_inactive_calendars():
    calendar = gtfs_df(
        """
        service_id,monday,tuesday
        DEAD,0,0
        LIVE,1,0
        """
    )
    ctx = schedule_context(**{"calendar.txt": calendar})
    remove_inactive_calendars.apply(ctx)
    assert ctx.output["calendar.txt"]["service_id"].to_list() == ["LIVE"]

gtfs_df

gtfs_df(csv: str) -> pl.DataFrame

Parse a CSV string into an all-text polars DataFrame.

GTFS columns are text — a route_type of 3 and a stop_id of S1 are both strings. :class:~continuous_gtfs.builtins.schedule.MatchCondition compares against string values. Forcing every column to Utf8 (infer_schema_length=0) keeps fixtures faithful to how the worker parses a real feed — otherwise polars would infer 0/1 service-day flags as integers and conditions like MatchCondition("monday", value="0") would silently never match.

Leading indentation is stripped via :func:textwrap.dedent, so the CSV block can be indented to match the surrounding test code without that indentation leaking into column names or values::

calendar = gtfs_df(
    """
    service_id,monday
    DEAD,0
    LIVE,1
    """
)
# → polars.DataFrame with Utf8 columns; "DEAD" and "LIVE" as service_id

Parameters:

Name Type Description Default
csv str

A CSV-formatted string, optionally indented.

required

Returns:

Type Description
DataFrame

A polars DataFrame with all columns typed Utf8.

schedule_context

schedule_context(**tables: DataFrame) -> PipelineContext

Return a :class:~continuous_gtfs.PipelineContext seeded for schedule testing.

ctx.output is populated with the supplied GTFS tables; keys should be GTFS filenames. Schedule steps read and mutate tables in ctx.output.

Prefer the explicit-filename dict form so the keys match what steps expect::

ctx = schedule_context(**{"stops.txt": stops_df, "routes.txt": routes_df})

Keyword arguments whose names don't end in .txt still work (the dict is passed through as-is), but the step will only find them under the key you provide, so be deliberate about the naming.

Parameters:

Name Type Description Default
**tables DataFrame

Keyword arguments mapping GTFS filenames to polars DataFrames.

{}

Returns:

Name Type Description
A PipelineContext

class:~continuous_gtfs.PipelineContext ready to pass to

PipelineContext

step.apply(ctx).

realtime_context

realtime_context(**feeds: FeedMessage) -> PipelineContext

Return a :class:~continuous_gtfs.PipelineContext seeded for realtime testing.

ctx.output is populated with the supplied GTFS-RT feeds; keys are feed names ("trip_updates", "vehicle_positions", etc.). Realtime steps iterate every :class:~google.transit.gtfs_realtime_pb2.FeedMessage in ctx.output::

feed = trip_updates_feed(trip_id="T1", stop_ids=["S1", "S2"])
ctx = realtime_context(trip_updates=feed)
some_rt_step.apply(ctx)

Parameters:

Name Type Description Default
**feeds FeedMessage

Keyword arguments mapping feed names to FeedMessage objects.

{}

Returns:

Name Type Description
A PipelineContext

class:~continuous_gtfs.PipelineContext ready to pass to

PipelineContext

step.apply(ctx).

trip_updates_feed

trip_updates_feed(
    *, trip_id: str, stop_ids: list[str]
) -> gtfs_rt.FeedMessage

Return a minimal GTFS-RT TripUpdate feed.

Creates a :class:~google.transit.gtfs_realtime_pb2.FeedMessage with a single TripUpdate entity containing one StopTimeUpdate per entry in stop_ids::

feed = trip_updates_feed(trip_id="trip_42", stop_ids=["S1", "S2", "S3"])
ctx = realtime_context(trip_updates=feed)
some_step.apply(ctx)
assert len(ctx.output["trip_updates"].entity) == 1

Parameters:

Name Type Description Default
trip_id str

The trip identifier for the single TripUpdate entity.

required
stop_ids list[str]

Ordered list of stop identifiers. Each becomes a StopTimeUpdate with stop_sequence starting at 1.

required

Returns:

Type Description
FeedMessage

A populated :class:~google.transit.gtfs_realtime_pb2.FeedMessage.

vehicle_positions_feed

vehicle_positions_feed(
    *,
    vehicle_ids: list[str],
    trip_ids: list[str] | None = None,
    stop_ids: list[str] | None = None,
) -> gtfs_rt.FeedMessage

Return a minimal GTFS-RT VehiclePositions feed.

Creates a :class:~google.transit.gtfs_realtime_pb2.FeedMessage with one VehiclePosition entity per entry in vehicle_ids::

feed = vehicle_positions_feed(
    vehicle_ids=["v1", "v2"],
    trip_ids=["T1", "T2"],
    stop_ids=["S10", "S20"],
)
ctx = realtime_context(vehicle_positions=feed)
some_step.apply(ctx)

When trip_ids or stop_ids are omitted, defaults are derived from the vehicle ID: trip_id becomes "trip_{vehicle_id}" and stop_id becomes "S0".

Parameters:

Name Type Description Default
vehicle_ids list[str]

One entry per vehicle; each becomes a separate entity.

required
trip_ids list[str] | None

Optional; zipped with vehicle_ids. Defaults to "trip_{vehicle_id}" per vehicle when omitted.

None
stop_ids list[str] | None

Optional; zipped with vehicle_ids. Defaults to "S0" per vehicle when omitted.

None

Returns:

Type Description
FeedMessage

A populated :class:~google.transit.gtfs_realtime_pb2.FeedMessage.

assert_unchanged

assert_unchanged(
    before: DataFrame, after: DataFrame, *, excluding: Expr
) -> None

Assert that rows not matched by excluding are unchanged.

Checks that before and after are identical for all rows outside excluding. Useful for verifying that a transform modifies only the rows it was intended to touch. The complement check — that unintended rows are untouched — is easy to miss when writing assertions, and this helper makes it explicit::

before = ctx.output["stops.txt"].clone()
remove_expansion_stops.apply(ctx)
assert_unchanged(
    before,
    ctx.output["stops.txt"],
    excluding=pl.col("stop_id").is_in(["S100", "S107"]),
)

Both frames are filtered to ~excluding and sorted by all columns before comparison, so row-order differences within the unchanged set do not produce false failures.

Raises :exc:AssertionError with a human-readable message on failure. The message includes the before/after row counts and a sample of up to 5 changed rows when the counts match but values differ.

Precondition: before and after must share the same schema (same columns and types). Steps that add or remove columns are not compatible with this helper for the affected columns — use direct polars assertions instead.

Parameters:

Name Type Description Default
before DataFrame

Snapshot of the DataFrame before the step ran (use .clone() to capture it before calling step.apply(ctx)).

required
after DataFrame

The DataFrame from ctx.output after the step ran.

required
excluding Expr

A polars expression that identifies the rows the step was expected to modify. All other rows are asserted unchanged.

required

Raises:

Type Description
AssertionError

If any row not matched by excluding differs between before and after.