Skip to content

Commit

Permalink
get stops info so that we know what zone a cr stop is in for fare cal…
Browse files Browse the repository at this point in the history
…culation; tests (#2132)
  • Loading branch information
anthonyshull authored Jul 23, 2024
1 parent 2071866 commit a9e240e
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 106 deletions.
12 changes: 5 additions & 7 deletions lib/trip_planner/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ defmodule Dotcom.TripPlanner.Parser do
MBTA system.
"""

alias TripPlan.{Itinerary, Leg, NamedPosition, PersonalDetail, TransitDetail}
alias Dotcom.TripPlanner.FarePasses
alias OpenTripPlannerClient.Schema
alias TripPlan.{Itinerary, Leg, NamedPosition, PersonalDetail, TransitDetail}

@stops_repo Application.compile_env!(:dotcom, :repo_modules)[:stops]

@spec parse(Schema.Itinerary.t()) :: Itinerary.t()
def parse(
Expand Down Expand Up @@ -166,15 +168,11 @@ defmodule Dotcom.TripPlanner.Parser do

defp build_stop(
%Schema.Stop{
gtfs_id: "mbta-ma-us:" <> gtfs_id,
name: name
gtfs_id: "mbta-ma-us:" <> gtfs_id
},
attributes
) do
%Stops.Stop{
id: gtfs_id,
name: name
}
@stops_repo.get(gtfs_id)
|> struct(attributes)
end

Expand Down
54 changes: 32 additions & 22 deletions test/dotcom/trip_plan/alerts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ defmodule Dotcom.TripPlan.AlertsTest do

import Dotcom.TripPlan.Alerts
import Mox
import Test.Support.Factories.TripPlanner.TripPlanner

alias Alerts.Alert
alias Alerts.InformedEntity, as: IE
alias Alerts.{Alert, InformedEntity}
alias Test.Support.Factories.{MBTA.Api, Stops.Stop, TripPlanner.TripPlanner}
alias TripPlan.Itinerary

setup :verify_on_exit!

setup do
leg = build(:transit_leg)
stub(Stops.Repo.Mock, :get, fn _ ->
Stop.build(:stop)
end)

leg = TripPlanner.build(:transit_leg)

itinerary =
build(:itinerary,
TripPlanner.build(:itinerary,
legs: [leg]
)

[route_id] = Itinerary.route_ids(itinerary)
[trip_id] = Itinerary.trip_ids(itinerary)

{:ok, %{itinerary: itinerary, route_id: route_id, trip_id: trip_id, route: leg.mode.route}}
end

Expand All @@ -29,37 +33,37 @@ defmodule Dotcom.TripPlan.AlertsTest do
expect(MBTA.Api.Mock, :get_json, fn "/trips/" <> id, [] ->
%JsonApi{
data: [
Test.Support.Factories.MBTA.Api.build(:trip_item, %{id: id})
Api.build(:trip_item, %{id: id})
]
}
end)

good_alert =
Alert.new(
active_period: [valid_active_period(itinerary)],
informed_entity: [%IE{route: route_id}]
informed_entity: [%InformedEntity{route: route_id}]
)

bad_alert = Alert.update(good_alert, informed_entity: [%IE{route: "not_valid"}])
bad_alert = Alert.update(good_alert, informed_entity: [%InformedEntity{route: "not_valid"}])
assert_only_good_alert(good_alert, bad_alert, itinerary)
end

test "returns an alert if it affects the trip", %{itinerary: itinerary, trip_id: trip_id} do
expect(MBTA.Api.Mock, :get_json, fn "/trips/" <> ^trip_id, [] ->
%JsonApi{
data: [
Test.Support.Factories.MBTA.Api.build(:trip_item, %{id: trip_id})
Api.build(:trip_item, %{id: trip_id})
]
}
end)

good_alert =
Alert.new(
active_period: [valid_active_period(itinerary)],
informed_entity: [%IE{trip: trip_id}]
informed_entity: [%InformedEntity{trip: trip_id}]
)

bad_alert = Alert.update(good_alert, informed_entity: [%IE{trip: "not_valid"}])
bad_alert = Alert.update(good_alert, informed_entity: [%InformedEntity{trip: "not_valid"}])
assert_only_good_alert(good_alert, bad_alert, itinerary)
end

Expand All @@ -70,7 +74,7 @@ defmodule Dotcom.TripPlan.AlertsTest do
expect(MBTA.Api.Mock, :get_json, fn "/trips/" <> id, [] ->
%JsonApi{
data: [
Test.Support.Factories.MBTA.Api.build(:trip_item, %{
Api.build(:trip_item, %{
id: id,
attributes: %{"direction_id" => 1}
})
Expand All @@ -81,11 +85,13 @@ defmodule Dotcom.TripPlan.AlertsTest do
good_alert =
Alert.new(
active_period: [valid_active_period(itinerary)],
informed_entity: [%IE{route: route_id, direction_id: 1}]
informed_entity: [%InformedEntity{route: route_id, direction_id: 1}]
)

bad_alert =
Alert.update(good_alert, informed_entity: [%IE{route: route_id, direction_id: 0}])
Alert.update(good_alert,
informed_entity: [%InformedEntity{route: route_id, direction_id: 0}]
)

assert_only_good_alert(good_alert, bad_alert, itinerary)
end
Expand All @@ -97,26 +103,28 @@ defmodule Dotcom.TripPlan.AlertsTest do
expect(MBTA.Api.Mock, :get_json, fn "/trips/" <> id, [] ->
%JsonApi{
data: [
Test.Support.Factories.MBTA.Api.build(:trip_item, %{id: id})
Api.build(:trip_item, %{id: id})
]
}
end)

good_alert =
Alert.new(
active_period: [valid_active_period(itinerary)],
informed_entity: [%IE{route_type: route.type}]
informed_entity: [%InformedEntity{route_type: route.type}]
)

bad_alert = Alert.update(good_alert, informed_entity: [%IE{route_type: route.type + 1}])
bad_alert =
Alert.update(good_alert, informed_entity: [%InformedEntity{route_type: route.type + 1}])

assert_only_good_alert(good_alert, bad_alert, itinerary)
end

test "returns an alert if it matches a transfer stop", %{itinerary: itinerary} do
expect(MBTA.Api.Mock, :get_json, fn "/trips/" <> id, [] ->
%JsonApi{
data: [
Test.Support.Factories.MBTA.Api.build(:trip_item, %{id: id})
Api.build(:trip_item, %{id: id})
]
}
end)
Expand All @@ -126,11 +134,13 @@ defmodule Dotcom.TripPlan.AlertsTest do
good_alert =
Alert.new(
active_period: [valid_active_period(itinerary)],
informed_entity: [%IE{stop: stop_id}]
informed_entity: [%InformedEntity{stop: stop_id}]
)

bad_alert =
Alert.update(good_alert, informed_entity: [%IE{stop: stop_id, route: "different route"}])
Alert.update(good_alert,
informed_entity: [%InformedEntity{stop: stop_id, route: "different route"}]
)

assert_only_good_alert(good_alert, bad_alert, itinerary)
end
Expand All @@ -139,15 +149,15 @@ defmodule Dotcom.TripPlan.AlertsTest do
expect(MBTA.Api.Mock, :get_json, fn "/trips/" <> id, [] ->
%JsonApi{
data: [
Test.Support.Factories.MBTA.Api.build(:trip_item, %{id: id})
Api.build(:trip_item, %{id: id})
]
}
end)

good_alert =
Alert.new(
active_period: [valid_active_period(itinerary)],
informed_entity: [%IE{route: route_id}]
informed_entity: [%InformedEntity{route: route_id}]
)

bad_alert = Alert.update(good_alert, active_period: [invalid_active_period(itinerary)])
Expand Down
26 changes: 17 additions & 9 deletions test/dotcom/trip_plan/itinerary_row_list_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ defmodule Dotcom.TripPlan.ItineraryRowListTest do

import Dotcom.TripPlan.ItineraryRowList
import Mox
import Test.Support.Factories.TripPlanner.TripPlanner

alias Test.Support.Factories.{Stops.Stop, TripPlanner.TripPlanner}

@date_time ~N[2017-06-27T11:43:00]

Expand All @@ -23,7 +24,11 @@ defmodule Dotcom.TripPlan.ItineraryRowListTest do
[]
end)

itinerary = build(:itinerary)
stub(Stops.Repo.Mock, :get, fn _ ->
Stop.build(:stop)
end)

itinerary = TripPlanner.build(:itinerary)

stub(MBTA.Api.Mock, :get_json, fn "/trips" <> _, _ ->
%JsonApi{data: [Test.Support.Factories.MBTA.Api.build(:trip_item)]}
Expand All @@ -33,14 +38,17 @@ defmodule Dotcom.TripPlan.ItineraryRowListTest do
end

test "ItineraryRow contains given stop name when no stop_id present" do
from = build(:stop_named_position, stop: nil)
to = build(:stop_named_position, stop: %Stops.Stop{id: "place-sstat"})
from = TripPlanner.build(:stop_named_position, stop: nil)
to = TripPlanner.build(:stop_named_position, stop: %Stops.Stop{id: "place-sstat"})
date_time = ~N[2017-06-27T11:43:00]

itinerary =
build(:itinerary,
TripPlanner.build(:itinerary,
start: date_time,
legs: [build(:transit_leg, from: from), build(:transit_leg, to: to)]
legs: [
TripPlanner.build(:transit_leg, from: from),
TripPlanner.build(:transit_leg, to: to)
]
)

itinerary_row_list = from_itinerary(itinerary)
Expand Down Expand Up @@ -105,7 +113,7 @@ defmodule Dotcom.TripPlan.ItineraryRowListTest do
end

test "Distance is given with personal steps", %{itinerary: itinerary} do
leg = build(:walking_leg)
leg = TripPlanner.build(:walking_leg)
personal_itinerary = %{itinerary | legs: [leg]}
row_list = from_itinerary(personal_itinerary)

Expand All @@ -132,12 +140,12 @@ defmodule Dotcom.TripPlan.ItineraryRowListTest do
test "Does not replace to stop_id" do
stop_id = Faker.Internet.slug()
stop_name = Faker.Address.city()
to = build(:stop_named_position, stop: %Stops.Stop{id: stop_id})
to = TripPlanner.build(:stop_named_position, stop: %Stops.Stop{id: stop_id})

itinerary = %TripPlan.Itinerary{
start: nil,
stop: nil,
legs: [build(:transit_leg, to: to)]
legs: [TripPlanner.build(:transit_leg, to: to)]
}

{name, id, _datetime, _alerts} =
Expand Down
23 changes: 17 additions & 6 deletions test/dotcom/trip_plan/itinerary_row_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ defmodule TripPlan.ItineraryRowTest do

import Dotcom.TripPlan.ItineraryRow
import Mox
import Test.Support.Factories.TripPlanner.TripPlanner

alias Alerts.{Alert, InformedEntity}
alias Dotcom.TripPlan.ItineraryRow
alias Routes.Route
alias Alerts.{Alert, InformedEntity}
alias Test.Support.Factories.MBTA.Api
alias Test.Support.Factories.{MBTA.Api, Stops.Stop, TripPlanner.TripPlanner}
alias TripPlan.{Leg, NamedPosition, PersonalDetail}

setup :verify_on_exit!

setup do
stub(Stops.Repo.Mock, :get, fn _ ->
Stop.build(:stop)
end)

:ok
end

describe "route_id/1" do
test "returns the route id when a route is present" do
row = %ItineraryRow{route: %Route{id: "route"}}
Expand Down Expand Up @@ -318,8 +325,12 @@ defmodule TripPlan.ItineraryRowTest do
end

describe "from_leg/3" do
@personal_leg build(:walking_leg)
@transit_leg build(:transit_leg)
stub(Stops.Repo.Mock, :get, fn _ ->
Stop.build(:stop)
end)

@personal_leg TripPlanner.build(:walking_leg)
@transit_leg TripPlanner.build(:transit_leg)

setup do
stub(MBTA.Api.Mock, :get_json, fn path, _ ->
Expand All @@ -339,7 +350,7 @@ defmodule TripPlan.ItineraryRowTest do
end

test "returns an itinerary row from a Leg" do
leg = build(:transit_leg)
leg = TripPlanner.build(:transit_leg)

stub(Stops.Repo.Mock, :get_parent, fn id ->
%Stops.Stop{id: id}
Expand Down
4 changes: 4 additions & 0 deletions test/dotcom/trip_plan/related_link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ defmodule Dotcom.TripPlan.RelatedLinkTest do
setup :verify_on_exit!

setup do
stub(Stops.Repo.Mock, :get, fn _ ->
Stop.build(:stop)
end)

itinerary =
build(:itinerary,
legs: [build(:transit_leg)]
Expand Down
6 changes: 5 additions & 1 deletion test/dotcom_web/views/trip_plan_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule DotcomWeb.TripPlanViewTest do

alias Fares.Fare
alias Dotcom.TripPlan.{IntermediateStop, ItineraryRow, Query}
alias Test.Support.Factories.TripPlanner.TripPlanner
alias Test.Support.Factories.{Stops.Stop, TripPlanner.TripPlanner}
alias TripPlan.{Itinerary, Leg, NamedPosition, TransitDetail}

@highest_one_way_fare %Fares.Fare{
Expand Down Expand Up @@ -77,6 +77,10 @@ defmodule DotcomWeb.TripPlanViewTest do
setup :verify_on_exit!

setup do
stub(Stops.Repo.Mock, :get, fn _ ->
Stop.build(:stop)
end)

stub(MBTA.Api.Mock, :get_json, fn "/schedules/", [route: "Red", date: "1970-01-01"] ->
{:error,
[
Expand Down
21 changes: 13 additions & 8 deletions test/trip_plan/itinerary_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ defmodule TripPlan.ItineraryTest do
use ExUnit.Case, async: true

import Mox
import Test.Support.Factories.TripPlanner.TripPlanner
import TripPlan.Itinerary

alias TripPlan.{TransitDetail, Leg, PersonalDetail, TransitDetail}
alias Test
alias Test.Support.Factories.{Stops.Stop, TripPlanner.TripPlanner}
alias TripPlan.{Leg, PersonalDetail, TransitDetail}

@from build(:stop_named_position)
@to build(:stop_named_position)
@transit_leg build(:transit_leg, from: @from, to: @to)
@from TripPlanner.build(:stop_named_position)
@to TripPlanner.build(:stop_named_position)

setup :verify_on_exit!

setup do
itinerary = build(:itinerary, legs: [@transit_leg])
stub(Stops.Repo.Mock, :get, fn _ ->
Stop.build(:stop)
end)

transit_leg = TripPlanner.build(:transit_leg, from: @from, to: @to)
itinerary = TripPlanner.build(:itinerary, legs: [transit_leg])
%{itinerary: itinerary}
end

Expand Down Expand Up @@ -75,8 +80,8 @@ defmodule TripPlan.ItineraryTest do
describe "positions/1" do
test "returns all named positions for the itinerary" do
itinerary =
build(:itinerary,
legs: build_list(3, :walking_leg, from: @from, to: @to)
TripPlanner.build(:itinerary,
legs: TripPlanner.build_list(3, :walking_leg, from: @from, to: @to)
)

[first, second, third] = itinerary.legs
Expand Down
Loading

0 comments on commit a9e240e

Please sign in to comment.