Skip to content

Commit

Permalink
test(Repository): Test schedules actually cached
Browse files Browse the repository at this point in the history
  • Loading branch information
KaylaBrady committed Nov 7, 2024
1 parent 43f4bb9 commit b487e91
Showing 1 changed file with 149 additions and 2 deletions.
151 changes: 149 additions & 2 deletions test/mbta_v3_api/repository_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ defmodule MBTAV3API.RepositoryTest do

import Mox

alias MBTAV3API.Route
alias MBTAV3API.{Alert, Repository, RoutePattern, Stop}
alias MBTAV3API.JsonApi
alias MBTAV3API.JsonApi.Object
alias MBTAV3API.{Alert, Repository, Route, RoutePattern, Schedule, Stop}
import Test.Support.Sigils

setup :verify_on_exit!
Expand Down Expand Up @@ -433,4 +434,150 @@ defmodule MBTAV3API.RepositoryTest do
]
}} = Repository.stops([])
end

describe "schedules/2" do
test "returns cached response when given same request twice" do
expect(
MobileAppBackend.HTTPMock,
:request,
# Only called once because first response is cached
1,
fn %Req.Request{url: %URI{path: "/schedules"}, options: %{params: _params}} ->
{:ok,
Req.Response.json(%{
data: [
%{
"attributes" => %{
"arrival_time" => "2024-03-13T01:07:00-04:00",
"departure_time" => "2024-03-13T01:07:00-04:00",
"drop_off_type" => 0,
"id" => "schedule-60565179-70159-90",
"pickup_type" => 0,
"route_id" => "Green-B",
"stop_id" => "70159",
"stop_sequence" => 90,
"trip_id" => "trip_1"
},
"id" => "sched_1",
"relationships" => %{
"trip" => %{
"data" => %{
"id" => "trip_1",
"type" => "trip"
}
}
},
"type" => "schedule"
}
],
included: [
%{
"attributes" => %{
"headsign" => "Headsign",
"direction_id" => 1
},
"id" => "trip_1",
"type" => "trip"
}
]
})}
end
)

assert {:ok,
%{
data: [
%Schedule{
id: "sched_1"
}
],
included: %{trips: %{"trip_1" => %{id: "trip_1"}}}
}} = Repository.schedules([])

assert {:ok,
%{
data: [
%Schedule{
id: "sched_1"
}
],
included: %{trips: %{"trip_1" => %{id: "trip_1"}}}
}} = Repository.schedules([])
end

test "makes new request when new params passed" do
expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{url: %URI{path: "/schedules"}, options: %{params: _params}} ->
{:ok,
Req.Response.json(%{
data: [
%{
"attributes" => %{
"arrival_time" => "2024-03-13T01:07:00-04:00",
"departure_time" => "2024-03-13T01:07:00-04:00",
"drop_off_type" => 0,
"id" => "schedule-60565179-70159-90",
"pickup_type" => 0,
"route_id" => "Green-B",
"stop_id" => "70159",
"stop_sequence" => 90,
"trip_id" => "trip_1"
},
"id" => "sched_1",
"relationships" => %{
"trip" => %{
"data" => %{
"id" => "trip_1",
"type" => "trip"
}
}
},
"type" => "schedule"
}
],
included: [
%{
"attributes" => %{
"headsign" => "Headsign",
"direction_id" => 1
},
"id" => "trip_1",
"type" => "trip"
}
]
})}
end
)

expect(
MobileAppBackend.HTTPMock,
:request,
fn %Req.Request{url: %URI{path: "/schedules"}, options: %{params: _params}} ->
{:ok,
Req.Response.json(%{
data: [],
included: []
})}
end
)

assert {:ok,
%{
data: [
%Schedule{
id: "sched_1"
}
],
included: %{trips: %{"trip_1" => %{id: "trip_1"}}}
}} = Repository.schedules([])

assert {:ok,
%JsonApi.Response{
data: [],
included: Object.to_full_map([])
}} == Repository.schedules(filter: [stop: "fake_stop"])
end
end
end

0 comments on commit b487e91

Please sign in to comment.