Skip to content

Commit

Permalink
rearrange
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshull committed Jan 21, 2025
1 parent 90de999 commit 92f2461
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions test/support/factories/trip_planner/trip_planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule Test.Support.Factories.TripPlanner.TripPlanner do
alias Dotcom.TripPlan.{NamedPosition, Parser}
alias OpenTripPlannerClient.Test.Support.Factory

# FACTORIES

def bus_leg_factory do
build(:otp_bus_leg)
|> Parser.parse()
Expand Down Expand Up @@ -41,33 +43,6 @@ defmodule Test.Support.Factories.TripPlanner.TripPlanner do
|> Parser.parse()
end

@doc """
Returns a list of itineraries that can be grouped.
You can pass in the number of groups you want and the number of itineraries in each group.
"""
def groupable_otp_itineraries(group_count \\ 2, itinerary_count \\ 1) do
Enum.map(1..group_count, fn _ ->
otp_itineraries(itinerary_count)
end)
|> List.flatten()
|> Enum.shuffle()
end

defp otp_itineraries(itinerary_count) do
[a, b, c] =
Enum.map(1..3, fn _ ->
Factory.build(:place, stop: Factory.build(:stop, gtfs_id: nil))
end)

leg_types = [:otp_bus_leg, :otp_ferry_leg, :otp_subway_leg]

a_b_leg = build(Faker.Util.pick(leg_types), from: a, to: b)
b_c_leg = build(Faker.Util.pick(leg_types), from: b, to: c)

build_list(itinerary_count, :otp_itinerary, legs: [a_b_leg, b_c_leg])
end

def itinerary_factory do
Factory.build(:itinerary)
|> limit_route_types()
Expand Down Expand Up @@ -193,6 +168,23 @@ defmodule Test.Support.Factories.TripPlanner.TripPlanner do
|> Parser.parse()
end

# NON-FACTORY FUNCTIONS

@doc """
Returns a list of itineraries that can be grouped.
You can pass in the number of groups you want and the number of itineraries in each group.
"""
def groupable_otp_itineraries(group_count \\ 2, itinerary_count \\ 1) do
Enum.map(1..group_count, fn _ ->
otp_itineraries(itinerary_count)
end)
|> List.flatten()
|> Enum.shuffle()
end

# PRIVATE FUNCTIONS

# OpenTripPlannerClient supports a greater number of route_type values than
# Dotcom does! Tweak that here.
defp limit_route_types(%OpenTripPlannerClient.Schema.Itinerary{legs: legs} = itinerary) do
Expand All @@ -211,4 +203,19 @@ defmodule Test.Support.Factories.TripPlanner.TripPlanner do
end

defp limit_route_types(leg), do: leg

# Create a number of otp itineraries with the same two random legs.
defp otp_itineraries(itinerary_count) do
[a, b, c] =
Enum.map(1..3, fn _ ->
Factory.build(:place, stop: Factory.build(:stop, gtfs_id: nil))
end)

leg_types = [:otp_bus_leg, :otp_ferry_leg, :otp_subway_leg]

a_b_leg = build(Faker.Util.pick(leg_types), from: a, to: b)
b_c_leg = build(Faker.Util.pick(leg_types), from: b, to: c)

build_list(itinerary_count, :otp_itinerary, legs: [a_b_leg, b_c_leg])
end
end

0 comments on commit 92f2461

Please sign in to comment.