Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kotva006 committed Nov 14, 2023
1 parent 365d3bb commit dfcc0d9
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 118 deletions.
7 changes: 4 additions & 3 deletions apps/trip_plan/lib/trip_plan/api/open_trip_planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule TripPlan.Api.OpenTripPlanner do
@moduledoc "Fetches data from the OpenTripPlanner API."
@behaviour TripPlan.Api
require Logger
import __MODULE__.Builder, only: [build_query_params: 3]
import __MODULE__.Builder, only: [build_params: 3]
import __MODULE__.Parser, only: [parse_ql: 1]

def plan(from, to, connection_opts, opts, _parent) do
Expand All @@ -11,7 +11,7 @@ defmodule TripPlan.Api.OpenTripPlanner do

@impl true
def plan(from, to, connection_opts, opts) do
with {:ok, params} <- build_query_params(from, to, opts) do
with {:ok, params} <- build_params(from, to, opts) do
param_string = Enum.map_join(params, "\n", fn {key, val} -> ~s{#{key}: #{val}} end)

graph_ql_query = """
Expand Down Expand Up @@ -116,7 +116,8 @@ defmodule TripPlan.Api.OpenTripPlanner do
end

defp status_text({:ok, %{status: code, body: body}}) do
"status=#{code} content_length=#{byte_size(body)}"
string_body = Poison.encode!(body)
"status=#{code} content_length=#{byte_size(string_body)}"
end

defp status_text({:error, error}) do
Expand Down
21 changes: 7 additions & 14 deletions apps/trip_plan/lib/trip_plan/api/open_trip_planner/builder.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule TripPlan.Api.OpenTripPlanner.Builder do
alias TripPlan.Api.OpenTripPlanner, as: OTP
alias TripPlan.NamedPosition
alias Util.{Polygon, Position}
@ten_miles 16_093
alias Util.Position

@spec build_query_params(Position.t(), Position.t(), TripPlan.Api.plan_opts()) ::
@doc "Convert general planning options into query params for OTP"
@spec build_params(Position.t(), Position.t(), TripPlan.Api.plan_opts()) ::
{:ok, %{String.t() => String.t()}} | {:error, any}
def build_query_params(from, to, opts) do
def build_params(from, to, opts) do
from_string = location(from)
to_string = location(to)
default_mode_string = "[{mode: WALK}, {mode: TRANSIT}]"
Expand All @@ -28,13 +28,6 @@ defmodule TripPlan.Api.OpenTripPlanner.Builder do
"\"#{Position.latitude(position)},#{Position.longitude(position)}\""
end

@doc "Convert general planning options into query params for OTP"
@spec build_params(Position.t(), Position.t(), TripPlan.Api.plan_opts()) ::
{:ok, %{String.t() => String.t()}} | {:error, any}
def build_params(from, to, opts) do
do_build_params(opts, %{"mode" => "TRANSIT,WALK", "walkReluctance" => 5})
end

defp do_build_params([], acc) do
{:ok, acc}
end
Expand All @@ -51,12 +44,12 @@ defmodule TripPlan.Api.OpenTripPlanner.Builder do
end

defp do_build_params([{:depart_at, %DateTime{} = datetime} | rest], acc) do
acc = do_date_time(false, datetime, acc)
acc = do_date_time("false", datetime, acc)
do_build_params(rest, acc)
end

defp do_build_params([{:arrive_by, %DateTime{} = datetime} | rest], acc) do
acc = do_date_time(true, datetime, acc)
acc = do_date_time("true", datetime, acc)
do_build_params(rest, acc)
end

Expand All @@ -66,7 +59,7 @@ defmodule TripPlan.Api.OpenTripPlanner.Builder do

defp do_build_params([{:mode, [_ | _] = modes} | rest], acc) do
all_modes = Enum.map(modes, fn m -> "{mode: #{m}}" end)
joined_modes = "[#{Enum.join(all_modes, ",")}, {mode: WALK}]"
joined_modes = "[#{Enum.join(all_modes, ", ")}, {mode: WALK}]"
do_build_params(rest, Map.put(acc, "transportModes", joined_modes))
end

Expand Down
90 changes: 38 additions & 52 deletions apps/trip_plan/test/api/open_trip_planner/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,24 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
longitude: -71.060920
}

@from_outside %TripPlan.NamedPosition{
latitude: 42.314299,
longitude: -71.308373
}

@to_inside %TripPlan.NamedPosition{
latitude: 42.3636617,
longitude: -71.0832908
}

@to_outside %TripPlan.NamedPosition{
latitude: 42.4185923,
longitude: -71.2184401
}

describe "build_params/1" do
test "depart_at sets date/time options" do
expected =
{:ok,
%{
"date" => "2017-05-22",
"time" => "12:04pm",
"date" => "\"2017-05-22\"",
"time" => "\"12:04pm\"",
"arriveBy" => "false",
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK"
"transportModes" => "[{mode: WALK}, {mode: TRANSIT}]",
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual =
Expand All @@ -48,11 +41,14 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
expected =
{:ok,
%{
"date" => "2017-05-22",
"time" => "12:04pm",
"date" => "\"2017-05-22\"",
"time" => "\"12:04pm\"",
"arriveBy" => "true",
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK"
"transportModes" => "[{mode: WALK}, {mode: TRANSIT}]",
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual =
Expand All @@ -71,7 +67,10 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
%{
"wheelchair" => "true",
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK"
"transportModes" => "[{mode: WALK}, {mode: TRANSIT}]",
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual = build_params(@from_inside, @to_inside, wheelchair_accessible?: true)
Expand All @@ -81,35 +80,10 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
{:ok,
%{
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK"
}}

actual = build_params(@from_inside, @to_inside, wheelchair_accessible?: false)
assert expected == actual
end

test "maxWalkDistance is increased when searching from outside of subway service area" do
expected =
{:ok,
%{
"maxWalkDistance" => "16093",
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK"
}}

actual = build_params(@from_outside, @to_inside, wheelchair_accessible?: false)
assert expected == actual

actual = build_params(@from_inside, @to_outside, wheelchair_accessible?: false)
assert expected == actual
end

test "maxWalkDistance is not increased when searching from inside of subway service area" do
expected =
{:ok,
%{
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK"
"transportModes" => "[{mode: WALK}, {mode: TRANSIT}]",
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual = build_params(@from_inside, @to_inside, wheelchair_accessible?: false)
Expand All @@ -121,7 +95,10 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
{:ok,
%{
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK"
"transportModes" => "[{mode: WALK}, {mode: TRANSIT}]",
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual = build_params(@from_inside, @to_inside, mode: [])
Expand All @@ -133,7 +110,10 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
{:ok,
%{
"walkReluctance" => 5,
"mode" => "BUS,SUBWAY,TRAM,WALK"
"transportModes" => "[{mode: BUS}, {mode: SUBWAY}, {mode: TRAM}, {mode: WALK}]",
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual = build_params(@from_inside, @to_inside, mode: ["BUS", "SUBWAY", "TRAM"])
Expand All @@ -144,8 +124,11 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
expected =
{:ok,
%{
"mode" => "TRANSIT,WALK",
"walkReluctance" => 17
"transportModes" => "[{mode: WALK}, {mode: TRANSIT}]",
"walkReluctance" => 17,
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual = build_params(@from_inside, @to_inside, optimize_for: :less_walking)
Expand All @@ -157,8 +140,11 @@ defmodule TripPlan.Api.OpenTripPlanner.BuilderTest do
{:ok,
%{
"walkReluctance" => 5,
"mode" => "TRANSIT,WALK",
"transferPenalty" => 100
"transportModes" => "[{mode: WALK}, {mode: TRANSIT}]",
"transferPenalty" => 100,
"fromPlace" => "\"::42.356365,-71.06092\"",
"locale" => "\"en\"",
"toPlace" => "\"::42.3636617,-71.0832908\""
}}

actual = build_params(@from_inside, @to_inside, optimize_for: :fewest_transfers)
Expand Down
1 change: 0 additions & 1 deletion apps/trip_plan/test/api/open_trip_planner/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ defmodule TripPlan.Api.OpenTripPlanner.ParserTest do
intermediate_stop_ids: ~w(70024 70022 70020 70018)s
} = subway_leg.mode

assert "Orange Line" = subway_leg.name
assert "Orange Line" = subway_leg.long_name
assert "1" = subway_leg.type
assert "http://www.mbta.com" = subway_leg.url
Expand Down
27 changes: 0 additions & 27 deletions apps/trip_plan/test/api/open_trip_planner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,5 @@ defmodule TripPlan.Api.OpenTripPlannerTest do
actual = plan({1, 1}, {2, 2}, connection_opts, bad: :arg)
assert expected == actual
end

test "does not normally add wiremock headers", %{bypass: bypass, url: url} do
Bypass.expect(bypass, fn conn ->
assert List.keyfind(conn.req_headers, "x-wm-proxy-url", 0) == nil
send_resp(conn, 404, ~s({"body": {}}))
end)

connection_opts = [user_id: 1, force_otp1: false, force_otp2: false]
from = %NamedPosition{name: "a", latitude: "42.13", longitude: "12.12313"}
to = %NamedPosition{name: "b", latitude: "42.13", longitude: "12.12313"}
plan(from, to, connection_opts, root_url: url)
end

test "adds headers when WIREMOCK_PROXY=true", %{bypass: bypass, url: url} do
System.put_env("WIREMOCK_PROXY", "true")

Bypass.expect(bypass, fn conn ->
assert List.keyfind(conn.req_headers, "x-wm-proxy-url", 0) != nil
send_resp(conn, 404, ~s({"body": {}}))
end)

connection_opts = [user_id: 1, force_otp1: false, force_otp2: false]
from = %NamedPosition{name: "a", latitude: "42.13", longitude: "12.12313"}
to = %NamedPosition{name: "b", latitude: "42.13", longitude: "12.12313"}
plan(from, to, connection_opts, root_url: url)
System.delete_env("WIREMOCK_PROXY")
end
end
end
Loading

0 comments on commit dfcc0d9

Please sign in to comment.