Skip to content

Commit

Permalink
wip more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen committed May 6, 2024
1 parent 864b4d9 commit 897218f
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 12 deletions.
86 changes: 74 additions & 12 deletions test/stops/repo_test.exs
Original file line number Diff line number Diff line change
@@ -1,56 +1,118 @@
defmodule Stops.RepoTest do
use ExUnit.Case
@moduletag :external
use ExUnit.Case, async: true

import Mox
import Stops.Repo
import Test.Support.Factory.MbtaApi
alias Dotcom.Cache.KeyGenerator
alias Stops.Stop

@parent_stop_item build(:stop_item, %{id: "parent-stop"})

setup :verify_on_exit!

setup do
cache = Application.get_env(:dotcom, :cache)
cache.flush()

stub(MBTA.Api.Mock, :get_json, fn "/stops/" <> id, _ ->
%JsonApi{
data: [
build(:stop_item, %{id: id})
]
}
end)

%{cache: cache}
end

describe "get/1" do
test "returns nil if the stop doesn't exist" do
expect(MBTA.Api.Mock, :get_json, fn _, _ ->
{:error, ""}
end)

assert get("get test: stop doesn't exist") == nil
end

test "returns a stop" do
assert %Stop{} = get("place-pktrm")
id = Faker.Internet.slug()
assert %Stop{id: ^id} = get(id)
end
end

describe "get!/1" do
test "raises a Stops.NotFoundError if the stop isn't found" do
expect(MBTA.Api.Mock, :get_json, fn _, _ ->
{:error, ""}
end)

assert_raise Stops.NotFoundError, fn ->
get!("get! test: stop doesn't exist")
end
end

test "returns a stop" do
assert %Stop{} = get!("place-pktrm")
id = Faker.Internet.slug()
assert %Stop{id: ^id} = get!(id)
end
end

describe "get_parent/1" do
test "returns the parent stop for a child stop" do
north_station_cr = get("BNT-0000-01")
assert north_station_cr.is_child? == true
assert north_station_cr.parent_id == "place-north"
assert %Stop{id: "place-north"} = get_parent(north_station_cr)
MBTA.Api.Mock
|> expect(:get_json, fn _, _ ->
%JsonApi{
data: [
build(:stop_item, %{
relationships: %{
"parent_station" => [@parent_stop_item]
}
})
]
}
end)
|> expect(:get_json, fn "/stops/parent-stop", _ ->
%JsonApi{
data: [@parent_stop_item]
}
end)

stop = get("has-parent")
assert stop.is_child? == true
assert stop.parent_id == "parent-stop"
assert %Stop{id: "parent-stop"} = get_parent(stop)
end

test "returns the same stop for a parent stop" do
north_station = get("place-north")
assert get_parent(north_station) == north_station
parent_stop = %Stop{is_child?: false, parent_id: nil}
assert get_parent(parent_stop) == parent_stop
end

test "takes ids" do
assert %Stop{id: "place-north"} = get_parent("BNT-0000-01")
assert %Stop{id: "place-north"} = get_parent("place-north")
id = Faker.Internet.slug()

MBTA.Api.Mock
|> expect(:get_json, fn path, _ ->
assert path == "/stops/" <> id

%JsonApi{
data: [
build(:stop_item, %{
relationships: %{
"parent_station" => [@parent_stop_item]
}
})
]
}
end)
|> expect(:get_json, fn "/stops/parent-stop", _ ->
%JsonApi{
data: [@parent_stop_item]
}
end)

assert %Stop{id: "parent-stop"} = get_parent(id)
end
end

Expand Down
52 changes: 52 additions & 0 deletions test/support/factory/mbta_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,56 @@ defmodule Test.Support.Factory.MbtaApi do
)
)
end

def stop_item_factory(attrs) do
build(
:item,
Map.merge(
%{
attributes: %{
"address" => Faker.Address.street_address(),
"description" => nil,
"latitude" => Faker.Address.latitude(),
"location_type" => Faker.Util.pick([0, 1, 2, 3]),
"longitude" => Faker.Address.longitude(),
"municipality" => "",
"name" => Faker.App.name(),
"platform_code" => nil,
"platform_name" => nil,
"sort_order" => 0,
"time_desc" => nil,
"typicality" => 1,
"wheelchair_boarding" => ""
},
relationships: %{
"child_stops" => [],
"facilities" => [
build(:item, %{
attributes: %{
"id" => "",
"latitude" => Faker.Address.latitude(),
"longitude" => Faker.Address.longitude(),
"long_name" => "",
"properties" => "",
"type" => Faker.Util.pick(~w(
fare_vending_retailer
fare_vending_machine
fare_media_assistant
fare_media_assistance_facility
ticket_window
)a)
}
})
],
"parent_station" => [],
"zone" => []
}
},
attrs,
fn _k, v1, v2 ->
Map.merge(v1, v2)
end
)
)
end
end

0 comments on commit 897218f

Please sign in to comment.