Skip to content

Commit

Permalink
feat(ScheduleController): log empty schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen committed Nov 6, 2023
1 parent 5092596 commit f6b7e82
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion apps/site/lib/site_web/controllers/schedule_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ defmodule SiteWeb.ScheduleController do
|> future_departures(conn, params)
|> omit_last_stop_departures(params)

json(conn, schedules)
case schedules do
[%Schedule{} | _] ->
json(conn, schedules)

_ ->
Logger.info(
"module=#{__MODULE__} fun=schedules_for_stop stop=#{stop_id} date_time=#{DateTime.to_string(date_time(conn.assigns))} no_schedules_returned"
)

json(conn, [])
end
end
end

Expand Down
37 changes: 37 additions & 0 deletions apps/site/test/site_web/controllers/schedule_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,43 @@ defmodule SiteWeb.ScheduleControllerTest do
end
end

test "logs when no schedules returned", %{conn: conn} do
with_mock(Schedules.Repo, [:passthrough],
schedules_for_stop: fn "TEST 1234", [] ->
# will get filtered out
[
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2019-05-18 22:25:06.098765Z],
last_stop?: true
}
]
end
) do
log =
ExUnit.CaptureLog.capture_log(fn ->
old_level = Logger.level()

on_exit(fn ->
Logger.configure(level: old_level)
end)

Logger.configure(level: :info)

conn =
ScheduleController.schedules_for_stop(conn, %{
"stop_id" => "TEST 1234",
"future_departures" => "true",
"last_stop_departures" => "false"
})

assert [] = json_response(conn, 200)
end)

assert log =~ "[info] module=Elixir.SiteWeb.ScheduleController"
assert log =~ "fun=schedules_for_stop stop=TEST 1234"
assert log =~ "no_schedules_returned"
end
end
end
Expand Down

0 comments on commit f6b7e82

Please sign in to comment.