Skip to content

Commit

Permalink
refactor factories: just do the thing
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen committed May 6, 2024
1 parent 5e692fb commit 3a1f56a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 99 deletions.
85 changes: 16 additions & 69 deletions test/support/factory/mbta_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,34 @@ defmodule Test.Support.Factory.MbtaApi do
alias JsonApi.Item

def item_factory(attrs) do
item = %Item{
id: attrs[:id] || sequence(:id, &"item-#{&1}"),
attributes: attrs[:attributes] || %{},
relationships: attrs[:relationships] || %{}
}

merge_attributes(item, attrs)
merge_attributes(%Item{}, attrs)
end

@doc """
MBTA V3 API route patterns return a subset of canonical, direction_id, name, sort_order, time_desc, and typicality attributes, with representative_trip and route relationships.
MBTA V3 API route patterns return a subset of canonical, direction_id, name,
sort_order, time_desc, and typicality attributes, with representative_trip and
route relationships.
"""
def route_pattern_item_factory(attrs) do
item = %Item{
id: attrs[:id] || sequence(:id, &"item-#{&1}"),
attributes:
attrs[:attributes] ||
%{
build(
:item,
Map.merge(
%{
attributes: %{
"canonical" => false,
"direction_id" => 0,
"name" => Faker.App.name(),
"sort_order" => 0,
"time_desc" => nil,
"typicality" => 1
},
relationships:
attrs[:relationships] ||
%{
"representative_trip" => build_list(1, :trip_item),
"route" => build_list(1, :item, id: sequence(:route, &"route-#{&1}"))
relationships: %{
"representative_trip" => [build(:item)],
"route" => [build(:item)]
}
}

merge_attributes(item, attrs)
end

@doc """
MBTA V3 API trips return a subset of headsign, and optionally service, stops, and shape relationships.
"""
def trip_item_factory(attrs) do
item = %Item{
id: attrs[:id] || sequence(:trip, &"trip-#{&1}"),
attributes:
attrs[:attributes] ||
%{
"direction_id" => Faker.Util.pick([0, 1]),
"headsign" => Faker.Address.street_name(),
"name" => Faker.Address.street_name()
},
relationships: %{
"service" =>
Faker.Util.pick([
build_list(1, :item, id: sequence(:service, &"service-#{&1}")),
[]
]),
"stops" =>
Faker.Util.pick([
build_list(3, :item, id: sequence(:stop, &"stop-#{&1}")),
[]
]),
"shape" =>
Faker.Util.pick([
build_list(1, :shape_item),
[]
])
}
}

merge_attributes(item, attrs)
end

@doc """
MBTA V3 API shapes return a polyline attribute.
"""
def shape_item_factory(attrs) do
item = %Item{
id: attrs[:id] || sequence(:shape, &"shape-#{&1}"),
attributes: attrs[:attributes] || %{"polyline" => Faker.Lorem.characters()},
relationships: %{}
}

merge_attributes(item, attrs)
},
attrs
)
)
end
end
31 changes: 1 addition & 30 deletions test/support/factory/route_pattern.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,6 @@ defmodule Test.Support.Factory.RoutePattern do
alias RoutePatterns.RoutePattern

def route_pattern_factory(attrs) do
direction_id = Map.get(attrs, :direction_id, Faker.Util.pick([0, 1]))
headsign = attrs[:headsign] || Faker.Address.street_name()
id = attrs[:id] || sequence(:id, &"route-pattern-#{&1}")
name = attrs[:name] || "#{Faker.Address.street_name()}-#{Faker.Address.street_name()}"

representative_trip_id =
attrs[:representative_trip_id] || sequence(:representative_trip_id, &"trip-#{&1}")

representative_trip_polyline =
attrs[:representative_trip_polyline] || Faker.Lorem.characters()

route_id = attrs[:route_id] || sequence(:route_id, &"route-#{&1}")
service_id = attrs[:service_id] || sequence(:service_id, &"service-#{&1}")
shape_id = attrs[:shape_id] || sequence(:shape_id, &"shape-#{&1}")
typicality = attrs[:typicality] || Faker.Util.pick([0, 1, 2, 3, 4, 5])

route_pattern = %RoutePattern{
direction_id: direction_id,
headsign: headsign,
id: id,
name: name,
representative_trip_id: representative_trip_id,
representative_trip_polyline: representative_trip_polyline,
route_id: route_id,
service_id: service_id,
shape_id: shape_id,
typicality: typicality
}

merge_attributes(route_pattern, attrs)
merge_attributes(%RoutePattern{}, attrs)
end
end

0 comments on commit 3a1f56a

Please sign in to comment.