Skip to content

Commit

Permalink
100% test coverage for mbta api module
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshull committed Apr 22, 2024
1 parent fd70948 commit 19719fc
Show file tree
Hide file tree
Showing 20 changed files with 546 additions and 61 deletions.
4 changes: 2 additions & 2 deletions lib/mbta/api/route_patterns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule MBTA.Api.RoutePatterns do
end

@spec get(Route.id_t(), keyword()) :: api_response_t()
def get(id, opts \\ []) do
@mbta_api.get_json("/route_patterns/#{id}", opts)
def get(id, params \\ []) do
@mbta_api.get_json("/route_patterns/#{id}", params)
end
end
28 changes: 14 additions & 14 deletions lib/mbta/api/routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ defmodule MBTA.Api.Routes do
@type api_response_t() :: JsonApi.t() | {:error, any}

@spec all(keyword()) :: api_response_t()
def all(opts \\ []) do
@mbta_api.get_json("/routes/", opts)
def all(params \\ []) do
@mbta_api.get_json("/routes/", params)
end

@spec get(Route.id_t(), keyword()) :: api_response_t()
def get(id, opts \\ []) do
@mbta_api.get_json("/routes/#{id}", opts)
def get(id, params \\ []) do
@mbta_api.get_json("/routes/#{id}", params)
end

@spec by_type(Route.type_int(), keyword()) :: api_response_t()
def by_type(type, opts \\ []) do
opts = put_in(opts[:type], type)
def by_type(type, params \\ []) do
params = put_in(params[:type], type)

@mbta_api.get_json("/routes/", opts)
@mbta_api.get_json("/routes/", params)
end

@spec by_stop(Stop.id_t(), keyword()) :: api_response_t()
def by_stop(stop_id, opts \\ []) do
opts = put_in(opts[:stop], stop_id)
def by_stop(stop_id, params \\ []) do
params = put_in(params[:stop], stop_id)

@mbta_api.get_json("/routes/", opts)
@mbta_api.get_json("/routes/", params)
end

@spec by_stop_and_direction(Stop.id_t(), 0 | 1, keyword()) :: api_response_t()
def by_stop_and_direction(stop_id, direction_id, opts \\ []) do
opts = put_in(opts[:stop], stop_id)
opts = put_in(opts[:direction_id], direction_id)
def by_stop_and_direction(stop_id, direction_id, params \\ []) do
params = put_in(params[:stop], stop_id)
params = put_in(params[:direction_id], direction_id)

@mbta_api.get_json("/routes/", opts)
@mbta_api.get_json("/routes/", params)
end
end
7 changes: 6 additions & 1 deletion lib/mbta/api/stats.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
defmodule MBTA.Api.Stats do
@moduledoc """
This Agent attaches to telemetry events emitted by Finch and aggregates them by path and status.
When `dispatch_stats/0` is called, it sends the aggregated data to the `:mbta_api` telemetry event.
"""

use Agent

def start_link(initial_value \\ %{}) do
Expand Down Expand Up @@ -40,7 +46,6 @@ defmodule MBTA.Api.Stats do
durations
|> Enum.sum()
|> Kernel.div(count)
|> Kernel.div(1000)

:telemetry.execute([:mbta_api, :request], %{count: count, avg: avg}, %{
path: path,
Expand Down
12 changes: 5 additions & 7 deletions lib/mbta/api/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ defmodule MBTA.Api.Stream do
}
end

@spec event(String.t()) :: Event.event()
for atom <- ~w(reset add update remove)a do
str = Atom.to_string(atom)
defp event(unquote(str)), do: unquote(atom)
end

defp event("error"), do: :unknown
defp event("add"), do: :add
defp event("remove"), do: :remove
defp event("update"), do: :update
defp event("reset"), do: :reset
defp event(_), do: :unknown
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ defmodule DotCom.Mixfile do
{:telemetry_metrics, "0.6.2"},
{:telemetry_metrics_statsd, "0.7.0"},
{:telemetry_poller, "1.1.0"},
{:telemetry_test, "0.1.2", only: [:test]},
# latest version is 3.7.11; cannot upgrade because tests fail
{:timex, "3.1.24"},
{:unrooted_polytree, "0.1.1"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.2", "2caabe9344ec17eafe5403304771c3539f3b6e2f7fb6a6f602558c825d0d0bfb", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9b43db0dc33863930b9ef9d27137e78974756f5f198cae18409970ed6fa5b561"},
"telemetry_metrics_statsd": {:hex, :telemetry_metrics_statsd, "0.7.0", "92732fae63db31ef2508df6faee7d81401883e33f2976715a82f296a33a45cee", [:mix], [{:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "797e34a856376dfd4e96347da0f747fcff4e0cadf6e6f0f989598f563cad05ff"},
"telemetry_poller": {:hex, :telemetry_poller, "1.1.0", "58fa7c216257291caaf8d05678c8d01bd45f4bdbc1286838a28c4bb62ef32999", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9eb9d9cbfd81cbd7cdd24682f8711b6e2b691289a0de6826e58452f28c103c8f"},
"telemetry_test": {:hex, :telemetry_test, "0.1.2", "122d927567c563cf57773105fa8104ae4299718ec2cbdddcf6776562c7488072", [:mix], [{:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7bd41a49ecfd33ecd82d2c7edae19a5736f0d2150206d0ee290dcf3885d0e14d"},
"tesla": {:hex, :tesla, "1.8.0", "d511a4f5c5e42538d97eef7c40ec4f3e44effdc5068206f42ed859e09e51d1fd", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.13", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, ">= 1.0.0", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.2", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:msgpax, "~> 2.3", [hex: :msgpax, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "10501f360cd926a309501287470372af1a6e1cbed0f43949203a4c13300bc79f"},
"timex": {:hex, :timex, "3.1.24", "d198ae9783ac807721cca0c5535384ebdf99da4976be8cefb9665a9262a1e9e3", [:mix], [{:combine, "~> 0.7", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "ca852258d788542c263b12dbf55375fe2ccf5674e7b20995e3d84d2d4412bc0f"},
"tzdata": {:hex, :tzdata, "0.5.22", "f2ba9105117ee0360eae2eca389783ef7db36d533899b2e84559404dbc77ebb8", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "cd66c8a1e6a9e121d1f538b01bef459334bb4029a1ffb4eeeb5e4eae0337e7b6"},
Expand Down
25 changes: 25 additions & 0 deletions test/mbta/api/alerts_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule MBTA.Api.AlertsTest do
use ExUnit.Case, async: false

import Mox

alias MBTA.Api.{Alerts, Mock}

setup :set_mox_global
setup :verify_on_exit!

test "all/0 returns a list of alerts" do
# Setup
expect(Mock, :get_json, fn url, _ ->
assert url == "/alerts/"

[]
end)

# Exercise
alerts = Alerts.all()

# Verify
assert alerts == []
end
end
43 changes: 43 additions & 0 deletions test/mbta/api/facilities_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule MBTA.Api.FacilitiesTest do
use ExUnit.Case, async: false

import Mox

alias MBTA.Api.{Facilities, Mock}

setup :set_mox_global
setup :verify_on_exit!

test "all/0 returns a list of facilities" do
# Setup
expect(Mock, :get_json, fn url, _ ->
assert url == "/facilities/"

[]
end)

# Exercise
facilities = Facilities.all()

# Verify
assert facilities == []
end

test "filter_by/1 sends filters as query parameters" do
# Setup
expect(Mock, :get_json, fn url, params ->
assert url == "/facilities/"

assert Enum.sort(params) ==
Enum.sort([{"filter[stop_id]", "place-sstat"}, {"filter[route_type]", "0"}])

[]
end)

# Exercise
facilities = Facilities.filter_by(%{stop_id: "place-sstat", route_type: "0"})

# Verify
assert facilities == []
end
end
26 changes: 26 additions & 0 deletions test/mbta/api/predictions_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule MBTA.Api.PredictionsTest do
use ExUnit.Case, async: false

import Mox

alias MBTA.Api.{Predictions, Mock}

setup :set_mox_global
setup :verify_on_exit!

test "all/1 returns a list of predictions" do
# Setup
expect(Mock, :get_json, fn url, params ->
assert url == "/predictions/"
assert Enum.sort(params) == Enum.sort(foo: 1, bar: 2)

[]
end)

# Exercise
predictions = Predictions.all(foo: 1, bar: 2)

# Verify
assert predictions == []
end
end
42 changes: 42 additions & 0 deletions test/mbta/api/route_patterns_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule Mbta.Api.RoutePatternsTest do
use ExUnit.Case, async: false

import Mox

alias MBTA.Api.{Mock, RoutePatterns}

setup :set_mox_global
setup :verify_on_exit!

test "all/0 returns a list of route patterns" do
# Setup
expect(Mock, :get_json, fn url, _ ->
assert url == "/route_patterns/"

[]
end)

# Exercise
route_patterns = RoutePatterns.all()

# Verify
assert route_patterns == []
end

test "get/1 returns a route pattern" do
# Setup
id = 1

expect(Mock, :get_json, fn url, _ ->
assert url == "/route_patterns/#{id}"

%{}
end)

# Exercise
route_pattern = RoutePatterns.get(1)

# Verify
assert route_pattern == %{}
end
end
97 changes: 97 additions & 0 deletions test/mbta/api/routes_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
defmodule MBTA.Api.RoutesTest do
use ExUnit.Case, async: false

import Mox

alias MBTA.Api.{Mock, Routes}

setup :set_mox_global
setup :verify_on_exit!

test "all/0 returns a list of routes" do
# Setup
expect(Mock, :get_json, fn url, _ ->
assert url == "/routes/"

[]
end)

# Exercise
routes = Routes.all()

# Verify
assert routes == []
end

test "get/1 returns a route" do
# Setup
id = :rand.uniform(100)

expect(Mock, :get_json, fn url, _ ->
assert url == "/routes/#{id}"

%{}
end)

# Exercise
route = Routes.get(id)

# Verify
assert route == %{}
end

test "by_type/1 sends type as query parameter" do
# Setup
type = :rand.uniform(100)

expect(Mock, :get_json, fn url, params ->
assert url == "/routes/"
assert Enum.sort(params) == Enum.sort(type: type)

[]
end)

# Exercise
routes = Routes.by_type(type)

# Verify
assert routes == []
end

test "by_stop/1 sends stop as query parameter" do
# Setup
stop = Faker.Team.creature() |> String.downcase()

expect(Mock, :get_json, fn url, params ->
assert url == "/routes/"
assert Enum.sort(params) == Enum.sort(stop: stop)

[]
end)

# Exercise
routes = Routes.by_stop(stop)

# Verify
assert routes == []
end

test "by_stop_and_direction/2 sends stop and direction_id as query parameters" do
# Setup
stop = Faker.Team.creature() |> String.downcase()
direction_id = :rand.uniform(1)

expect(Mock, :get_json, fn url, params ->
assert url == "/routes/"
assert Enum.sort(params) == Enum.sort(stop: stop, direction_id: direction_id)

[]
end)

# Exercise
routes = Routes.by_stop_and_direction(stop, direction_id)

# Verify
assert routes == []
end
end
25 changes: 25 additions & 0 deletions test/mbta/api/schedules_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule MBTA.Api.SchedulesTest do
use ExUnit.Case, async: false

import Mox

alias MBTA.Api.{Mock, Schedules}

setup :set_mox_global
setup :verify_on_exit!

test "all/0 returns a list of schedules" do
# Setup
expect(Mock, :get_json, fn url, _ ->
assert url == "/schedules/"

[]
end)

# Exercise
schedules = Schedules.all()

# Verify
assert schedules == []
end
end
Loading

0 comments on commit 19719fc

Please sign in to comment.