Skip to content

Commit

Permalink
feat:cleanup(ex/test/factories): split factories into files
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Oct 1, 2024
1 parent d0cbb2b commit 61ebc55
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 324 deletions.
83 changes: 83 additions & 0 deletions test/support/factories/detour_factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule Skate.DetourFactory do
@moduledoc """
Defines ExMachina factory functions for `Skate.Factory` related to
`Skate.Detours`
"""

defmacro __using__(_opts) do
quote do
def missed_stops_result_factory do
%Skate.Detours.MissedStops.Result{
missed_stops: build_list(3, :gtfs_stop),
connection_stop_start: build(:gtfs_stop),
connection_stop_end: build(:gtfs_stop)
}
end

def detour_factory do
%Skate.Detours.Db.Detour{
author: build(:user),
state: build(:detour_snapshot)
}
end

def detour_snapshot_factory do
%{
"context" => %{
"uuid" => nil,
"route" => %{
"name" => sequence("detour_route_name:"),
"directionNames" => %{
"0" => "Outbound",
"1" => "Inbound"
}
},
"routePattern" => %{
"name" => sequence("detour_route_pattern_name:"),
"headsign" => sequence("detour_route_pattern_headsign:"),
"directionId" => sequence(:detour_route_pattern_direction, [0, 1])
}
},
"value" => %{}
}
end

def with_id(%Skate.Detours.Db.Detour{} = detour, id) do
%{detour | state: with_id(detour.state, id)}
end

def with_id(%{"context" => %{"uuid" => _}} = snapshot, id) do
put_in(snapshot["context"]["uuid"], id)
end

def activated(%Skate.Detours.Db.Detour{} = detour) do
%{detour | state: activated(detour.state)}
end

def activated(%{"value" => %{}} = state) do
put_in(state["value"], %{"Detour Drawing" => %{"Active" => "Reviewing"}})
end

def with_direction(%Skate.Detours.Db.Detour{} = detour, direction) do
%{
detour
| state: with_direction(detour.state, direction)
}
end

def with_direction(
%{"context" => %{"routePattern" => %{"directionId" => _}}} = state,
:inbound
) do
put_in(state["context"]["routePattern"]["directionId"], 1)
end

def with_direction(
%{"context" => %{"routePattern" => %{"directionId" => _}}} = state,
:outbound
) do
put_in(state["context"]["routePattern"]["directionId"], 0)
end
end
end
end
122 changes: 122 additions & 0 deletions test/support/factories/gtfs_factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
defmodule Skate.GtfsFactory do
@moduledoc """
Defines ExMachina factory functions for `Skate.Factory` related to GTFS objects
"""

defmacro __using__(_opts) do
quote do
def gtfs_realtime_enhanced_trip_descriptor_factory do
%{
"direction_id" => 0,
"overload_offset" => -6,
"route_id" => "Green-E",
"schedule_relationship" => "SCHEDULED",
"start_date" => "20180815",
"start_time" => nil,
"tm_trip_id" => "37165437-X"
}
end

def gtfs_realtime_enhanced_vehicle_position_factory do
%{
"block_id" => "Q238-135",
"capacity" => 18,
"congestion_level" => nil,
"current_status" => "STOPPED_AT",
"current_stop_sequence" => 670,
"load" => 12,
"location_source" => "samsara",
"occupancy_percentage" => 0.67,
"occupancy_status" => "FEW_SEATS_AVAILABLE",
"operator" => %{
"id" => build(:operator_id),
"logon_time" => 1_534_340_301,
"first_name" => build(:first_name),
"last_name" => "EVANS"
},
"position" => %{
"bearing" => 135,
"latitude" => 42.32951,
"longitude" => -71.11109,
"odometer" => 5.1,
"speed" => 2.9796
},
"run_id" => "128-1007",
"stop_id" => "70257",
"timestamp" => 1_534_340_406,
"trip" => build(:gtfs_realtime_enhanced_trip_descriptor),
"vehicle" => %{
"id" => "G-10098",
"label" => "3823-3605",
"license_plate" => nil
},
"revenue" => false
}
end

def gtfs_stoptime_factory do
%Schedule.Gtfs.StopTime{
stop_id: "stop1",
time: 150,
timepoint_id: "t1"
}
end

def gtfs_stop_factory do
stop_id = sequence("Schedule.Gtfs.Stop.id:")

%Schedule.Gtfs.Stop{
id: stop_id,
name: "Stop #{stop_id}",
location_type: :stop,
latitude: 42.01,
longitude: -71.01
}
end

def gtfs_route_factory do
%Schedule.Gtfs.Route{
id: "route",
description: "Key Bus",
direction_names: %{0 => "Outbound", 1 => "Inbound"},
name: "Point A - Point B",
garages: MapSet.new([])
}
end

def gtfs_route_pattern_factory do
route_pattern_id = sequence("Schedule.Gtfs.RoutePattern.id:")

%Schedule.Gtfs.RoutePattern{
id: route_pattern_id,
name: "",
route_id: "route",
direction_id: 0,
representative_trip_id: "#{route_pattern_id}_representative_trip_id",
time_desc: nil,
sort_order: 1
}
end

def gtfs_shape_point_factory do
%Schedule.Gtfs.Shape.Point{
shape_id: "shape1",
lat: 42.413560 + sequence(:shape_point_seq, &(&1 * 0.001)),
lon: -70.992110 + sequence(:shape_point_seq, &(&1 * 0.001)),
sequence: sequence(:shape_point_seq, & &1)
}
end

def gtfs_shape_factory(attrs) do
shape_id = Map.get(attrs, :id, "shape1")

shape = %Schedule.Gtfs.Shape{
id: shape_id,
points: build_list(10, :gtfs_shape_point, %{shape_id: shape_id})
}

merge_attributes(shape, attrs)
end
end
end
end
36 changes: 36 additions & 0 deletions test/support/factories/hastus_factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Skate.HastusFactory do
@moduledoc """
Defines ExMachina factory functions for `Skate.Factory` related to HASTUS data
"""

defmacro __using__(_opts) do
quote do
def hastus_trip_factory do
%Schedule.Hastus.Trip{
schedule_id: "schedule",
run_id: "run",
block_id: "block",
start_time: 100,
end_time: 102,
start_place: "place1",
end_place: "place2",
route_id: "route",
trip_id: "trip1"
}
end

def hastus_activity_factory do
%Schedule.Hastus.Activity{
schedule_id: "schedule",
run_id: "run",
start_time: 100,
end_time: 105,
start_place: "place1",
end_place: "place2",
activity_type: "Operator",
partial_block_id: "block"
}
end
end
end
end
48 changes: 48 additions & 0 deletions test/support/factories/open_route_service_factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule Skate.OpenRouteServiceFactory do
@moduledoc """
Defines ExMachina factory functions for `Skate.Factory` related to
open route service API return values
"""

defmacro __using__(_opts) do
quote do
def ors_directions_step_json_factory do
%{
"instruction" => sequence("ors_instruction_step"),
"name" => sequence("ors_instruction_name"),
"type" => sequence("ors_instruction_type", [0, 1, 2, 3, 4, 5, 6, 7, 12, 13])
}
end

def ors_directions_segment_json_factory do
%{
"steps" =>
build_list(
sequence("ors_segment_json_num_steps", [4, 1, 3, 2]),
:ors_directions_step_json
)
}
end

def ors_directions_json_factory(attrs) do
coordinates = Map.get(attrs, :coordinates, [[0, 0], [1, 1], [2, 2]])

segments =
Map.get_lazy(attrs, :segments, fn ->
build_list(3, :ors_directions_segment_json)
end)

%{
"features" => [
%{
"geometry" => %{"coordinates" => coordinates},
"properties" => %{
"segments" => segments
}
}
]
}
end
end
end
end
85 changes: 85 additions & 0 deletions test/support/factories/schedule_factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
defmodule Skate.ScheduleFactory do
@moduledoc """
Defines ExMachina factory functions for `Skate.Factory` related to `Schedule`
"""

defmacro __using__(_opts) do
quote do
def piece_factory do
%Schedule.Piece{
schedule_id: "schedule",
run_id: "run",
start_time: 50,
start_place: "garage",
trips: [
build(:trip),
build(:trip, %{id: "trip2", route_id: "route"})
],
end_time: 200,
end_place: "station"
}
end

def block_factory do
%Schedule.Block{
id: "block",
service_id: "service",
schedule_id: "schedule",
start_time: 0,
end_time: 1,
pieces: [build(:piece)]
}
end

def run_factory do
%Schedule.Run{
schedule_id: "schedule",
service_id: "service",
id: "run",
activities: [
build(:piece)
]
}
end

def as_directed_factory do
%Schedule.AsDirected{
kind: :wad,
start_time: 1,
end_time: 2,
start_place: "place1",
end_place: "place2"
}
end

def shape_with_stops_factory(attrs) do
shape = build(:gtfs_shape, Map.take(attrs, [:id, :points]))

shape_with_stops = %Schedule.ShapeWithStops{
id: shape.id,
points: shape.points,
stops: build_list(3, :gtfs_stop)
}

merge_attributes(shape_with_stops, attrs)
end

def trip_factory do
%Schedule.Trip{
id: "trip",
block_id: "block",
route_id: "route",
service_id: "service",
headsign: "headsign",
direction_id: 0,
run_id: "run",
stop_times: [
build(:gtfs_stoptime)
],
start_time: 100,
end_time: 200
}
end
end
end
end
Loading

0 comments on commit 61ebc55

Please sign in to comment.