defmodule CartosnapApp.GeocoderTest do
# Not async: one test swaps the geocoder's config.
use ExUnit.Case, async: false
alias CartosnapApp.Geocoder
alias CartosnapApp.Geocoder.Place
defp feature(props, [lon, lat]) do
%{
"type" => "Feature",
"properties" => props,
"geometry" => %{"type" => "Point", "coordinates" => [lon, lat]}
}
end
defp fenway,
do:
feature(
%{
"osm_type" => "R",
"osm_id" => 11_188_464,
"osm_key" => "leisure",
"osm_value" => "stadium",
"name" => "Fenway Park",
"city" => "Boston",
"state" => "Massachusetts",
"country" => "United States"
},
[-71.0972, 42.3467]
)
defp street,
do:
feature(
%{
"osm_type" => "W",
"osm_id" => 1,
"osm_key" => "highway",
"name" => "Fenway",
"city" => "Boston"
},
[-71.09, 42.34]
)
test "finds places, drops roads, and reads them in lat/lon" do
Req.Test.stub(Geocoder, fn conn ->
assert conn.request_path == "/geocode/api"
assert conn.query_params["q"] == "fenway"
Req.Test.json(conn, %{"features" => [street(), fenway(), fenway()]})
end)
assert {:ok, [%Place{} = place]} = Geocoder.search("fenway")
assert {place.lat, place.lon} == {42.3467, -71.0972}
assert Place.detail(place) == "Stadium · Boston, Massachusetts, United States"
assert Place.label(place) == "Fenway Park, Boston"
end
test "biases results toward a position when given one" do
Req.Test.stub(Geocoder, fn conn ->
assert conn.query_params["lat"] == "42.0"
assert conn.query_params["lon"] == "-71.0"
Req.Test.json(conn, %{"features" => []})
end)
assert {:ok, []} = Geocoder.search("pier", near: {42.0, -71.0})
end
test "short queries don't reach the server" do
Req.Test.stub(Geocoder, fn _conn -> flunk("should not be asked") end)
assert {:ok, []} = Geocoder.search(" ab ")
end
test "reports a failing server" do
Req.Test.stub(Geocoder, &Plug.Conn.send_resp(&1, 502, "bad gateway"))
assert {:error, {:http, 502}} = Geocoder.search("anywhere")
end
test "asks the fallback when the primary finds nothing" do
previous = Application.get_env(:cartosnap_app, :geocoder)
Application.put_env(
:cartosnap_app,
:geocoder,
Keyword.put(previous, :fallback_url, "https://fallback.test")
)
on_exit(fn -> Application.put_env(:cartosnap_app, :geocoder, previous) end)
Req.Test.stub(Geocoder, fn
%{host: "fallback.test"} = conn -> Req.Test.json(conn, %{"features" => [fenway()]})
conn -> Req.Test.json(conn, %{"features" => []})
end)
assert {:ok, [%Place{name: "Fenway Park"}]} = Geocoder.search("fenway")
end
describe "reverse/3" do
test "names the town, or the neighbourhood when asked" do
Req.Test.stub(Geocoder, fn conn ->
assert conn.request_path == "/geocode/reverse"
Req.Test.json(conn, %{
"features" => [
feature(
%{"name" => "7 Main St", "district" => "Gion", "city" => "Kyoto"},
[135.77, 35.0]
)
]
})
end)
assert Geocoder.reverse(35.0, 135.77) == {:ok, "Kyoto"}
assert Geocoder.reverse(35.0, 135.77, scale: :neighbourhood) == {:ok, "Gion"}
end
test "is nil where nothing's known, and an error when the server fails" do
Req.Test.stub(Geocoder, &Req.Test.json(&1, %{"features" => []}))
assert Geocoder.reverse(0.0, 0.0) == {:ok, nil}
Req.Test.stub(Geocoder, &Plug.Conn.send_resp(&1, 503, ""))
assert {:error, {:http, 503}} = Geocoder.reverse(0.0, 0.0)
end
end
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
3.7 KiB · text
History
44bc4f7