@@ -1,91 +1,63 @@
|
||||
defmodule Mobilizon.Service.Geospatial.AddokTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mock
|
||||
import Mox
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Addok
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
setup do
|
||||
# Config.instance_user_agent/0 makes database calls so because of ownership connection
|
||||
# we need to define it like this instead of a constant
|
||||
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
|
||||
{:ok,
|
||||
httpoison_headers: [
|
||||
{"User-Agent", Config.instance_user_agent()}
|
||||
]}
|
||||
end
|
||||
|
||||
@endpoint get_in(Application.get_env(:mobilizon, Addok), [:endpoint])
|
||||
@fake_endpoint "https://domain.tld"
|
||||
alias Mobilizon.Service.HTTP.BaseClient.Mock
|
||||
|
||||
describe "search address" do
|
||||
test "produces a valid search address", %{httpoison_headers: httpoison_headers} do
|
||||
with_mock HTTPoison, get: fn _url, _headers, _options -> "{}" end do
|
||||
Addok.search("10 Rue Jangot")
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"#{@endpoint}/search/?q=10%20Rue%20Jangot&limit=10",
|
||||
httpoison_headers,
|
||||
@http_options
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
|
||||
with_mock HTTPoison, get: fn _url, _headers, _options -> "{}" end do
|
||||
Addok.search("10 Rue Jangot",
|
||||
endpoint: @fake_endpoint,
|
||||
limit: 5,
|
||||
coords: %{lat: 49, lon: 12}
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"#{@fake_endpoint}/search/?q=10%20Rue%20Jangot&limit=5&lat=49&lon=12",
|
||||
httpoison_headers,
|
||||
@http_options
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/addok/search" do
|
||||
assert %Address{
|
||||
country: "France",
|
||||
region: "69, Rhône, Auvergne-Rhône-Alpes",
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{coordinates: {4.842569, 45.751718}, properties: %{}, srid: 4326}
|
||||
} == Addok.search("10 rue Jangot") |> hd
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/addok/search.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url: "https://api-adresse.data.gouv.fr/search/?q=10%20rue%20Jangot&limit=10"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert %Address{
|
||||
country: "France",
|
||||
region: "69, Rhône, Auvergne-Rhône-Alpes",
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{coordinates: {4.842569, 45.751718}, properties: %{}, srid: 4326}
|
||||
} == Addok.search("10 rue Jangot") |> hd
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/addok/geocode" do
|
||||
assert %Address{
|
||||
country: "France",
|
||||
region: "69, Rhône, Auvergne-Rhône-Alpes",
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{coordinates: {4.842569, 45.751718}, properties: %{}, srid: 4326}
|
||||
} == Addok.geocode(4.842569, 45.751718) |> hd
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/addok/geocode.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url: "https://api-adresse.data.gouv.fr/reverse/?lon=4.842569&lat=45.751718&limit=10"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert %Address{
|
||||
country: "France",
|
||||
region: "69, Rhône, Auvergne-Rhône-Alpes",
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{coordinates: {4.842569, 45.751718}, properties: %{}, srid: 4326}
|
||||
} == Addok.geocode(4.842569, 45.751718) |> hd
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mock
|
||||
import Mox
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Service.Geospatial.GoogleMaps
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
alias Mobilizon.Service.HTTP.BaseClient.Mock
|
||||
|
||||
describe "search address" do
|
||||
test "without API Key triggers an error" do
|
||||
@@ -20,76 +14,117 @@ defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do
|
||||
end
|
||||
end
|
||||
|
||||
test "produces a valid search address with options" do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url, _headers, _options ->
|
||||
{:ok,
|
||||
%HTTPoison.Response{status_code: 200, body: "{\"status\": \"OK\", \"results\": []}"}}
|
||||
end do
|
||||
GoogleMaps.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr",
|
||||
api_key: "toto"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://maps.googleapis.com/maps/api/geocode/json?limit=5&key=toto&language=fr&address=10%20Rue%20Jangot",
|
||||
[],
|
||||
@http_options
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "triggers an error with an invalid API Key" do
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/google_maps/api_key_invalid.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://maps.googleapis.com/maps/api/geocode/json?limit=10&key=secret_key&language=en&address=10%20rue%20Jangot"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert_raise ArgumentError, "The provided API key is invalid.", fn ->
|
||||
GoogleMaps.search("10 rue Jangot", api_key: "secret_key")
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/google_maps/search" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8424032, 45.75164940000001},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "gm:ChIJtW0QikTq9EcRLI4Vy6bRx0U"
|
||||
} ==
|
||||
GoogleMaps.search("10 rue Jangot",
|
||||
api_key: "toto"
|
||||
)
|
||||
|> hd
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/google_maps/search.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
data_2 =
|
||||
File.read!("test/fixtures/geospatial/google_maps/search_2.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, 3, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://maps.googleapis.com/maps/api/geocode/json?limit=10&key=toto&language=en&address=10%20rue%20Jangot"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
|
||||
%{
|
||||
method: :get,
|
||||
url: _url
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data_2}}
|
||||
end)
|
||||
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8424032, 45.75164940000001},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "gm:ChIJtW0QikTq9EcRLI4Vy6bRx0U"
|
||||
} ==
|
||||
GoogleMaps.search("10 rue Jangot",
|
||||
api_key: "toto"
|
||||
)
|
||||
|> hd
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/google_maps/geocode" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10bis Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10bis Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8424966, 45.751725},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "gm:ChIJrW0QikTq9EcR96jk2OnO75w"
|
||||
} ==
|
||||
GoogleMaps.geocode(4.842569, 45.751718, api_key: "toto")
|
||||
|> hd
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/google_maps/geocode.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
data_2 =
|
||||
File.read!("test/fixtures/geospatial/google_maps/geocode_2.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, 3, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://maps.googleapis.com/maps/api/geocode/json?limit=10&key=toto&language=en&latlng=45.751718,4.842569&result_type=street_address"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
|
||||
%{
|
||||
method: :get,
|
||||
url: _url
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data_2}}
|
||||
end)
|
||||
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10bis Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10bis Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8424966, 45.751725},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "gm:ChIJrW0QikTq9EcR96jk2OnO75w"
|
||||
} ==
|
||||
GoogleMaps.geocode(4.842569, 45.751718, api_key: "toto")
|
||||
|> hd
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,11 @@
|
||||
defmodule Mobilizon.Service.Geospatial.MapQuestTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mock
|
||||
import Mox
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.MapQuest
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
setup do
|
||||
# Config.instance_user_agent/0 makes database calls so because of ownership connection
|
||||
# we need to define it like this instead of a constant
|
||||
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
|
||||
{:ok,
|
||||
httpoison_headers: [
|
||||
{"User-Agent", Config.instance_user_agent()}
|
||||
]}
|
||||
end
|
||||
alias Mobilizon.Service.HTTP.BaseClient.Mock
|
||||
|
||||
describe "search address" do
|
||||
test "without API Key triggers an error" do
|
||||
@@ -31,75 +14,88 @@ defmodule Mobilizon.Service.Geospatial.MapQuestTest do
|
||||
end
|
||||
end
|
||||
|
||||
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url, _headers, _options ->
|
||||
{:ok,
|
||||
%HTTPoison.Response{
|
||||
status_code: 200,
|
||||
body: "{\"info\": {\"statuscode\": 0}, \"results\": []}"
|
||||
}}
|
||||
end do
|
||||
MapQuest.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr",
|
||||
api_key: "toto"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://open.mapquestapi.com/geocoding/v1/address?key=toto&location=10%20Rue%20Jangot&maxResults=5",
|
||||
httpoison_headers,
|
||||
@http_options
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "triggers an error with an invalid API Key" do
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://open.mapquestapi.com/geocoding/v1/address?key=secret_key&location=10%20rue%20Jangot&maxResults=10"
|
||||
},
|
||||
_opts ->
|
||||
{:ok,
|
||||
%Tesla.Env{status: 403, body: "The AppKey submitted with this request is invalid."}}
|
||||
end)
|
||||
|
||||
assert_raise ArgumentError, "The AppKey submitted with this request is invalid.", fn ->
|
||||
MapQuest.search("10 rue Jangot", api_key: "secret_key")
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/map_quest/search" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "FR",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.842566, 45.751714},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} ==
|
||||
MapQuest.search("10 rue Jangot", api_key: "secret_key")
|
||||
|> hd
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/map_quest/search.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://open.mapquestapi.com/geocoding/v1/address?key=secret_key&location=10%20rue%20Jangot&maxResults=10"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "FR",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.842566, 45.751714},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} ==
|
||||
MapQuest.search("10 rue Jangot", api_key: "secret_key")
|
||||
|> hd
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/map_quest/geocode" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "FR",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.842569, 45.751718},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} ==
|
||||
MapQuest.geocode(4.842569, 45.751718, api_key: "secret_key")
|
||||
|> hd
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/map_quest/geocode.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://open.mapquestapi.com/geocoding/v1/reverse?key=secret_key&location=45.751718,4.842569&maxResults=10"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "FR",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.842569, 45.751718},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} ==
|
||||
MapQuest.geocode(4.842569, 45.751718, api_key: "secret_key")
|
||||
|> hd
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,91 +1,81 @@
|
||||
defmodule Mobilizon.Service.Geospatial.NominatimTest do
|
||||
use Mobilizon.DataCase, async: false
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
import Mock
|
||||
use Mobilizon.DataCase
|
||||
import Mox
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Nominatim
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
setup do
|
||||
# Config.instance_user_agent/0 makes database calls so because of ownership connection
|
||||
# we need to define it like this instead of a constant
|
||||
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
|
||||
{:ok,
|
||||
httpoison_headers: [
|
||||
{"User-Agent", Config.instance_user_agent()}
|
||||
]}
|
||||
end
|
||||
alias Mobilizon.Service.HTTP.BaseClient.Mock
|
||||
|
||||
describe "search address" do
|
||||
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url, _headers, _options ->
|
||||
{:ok, %HTTPoison.Response{status_code: 200, body: "[]"}}
|
||||
end do
|
||||
Nominatim.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://nominatim.openstreetmap.org/search?format=geocodejson&q=10%20Rue%20Jangot&limit=5&accept-language=fr&addressdetails=1&namedetails=1",
|
||||
httpoison_headers,
|
||||
@http_options
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/nominatim/search" do
|
||||
assert [
|
||||
%Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "nominatim:3078260611",
|
||||
type: "house"
|
||||
}
|
||||
] == Nominatim.search("10 rue Jangot")
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/nominatim/search.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://nominatim.openstreetmap.org/search?format=geocodejson&q=10%20rue%20Jangot&limit=10&accept-language=en&addressdetails=1&namedetails=1"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert [
|
||||
%Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "nominatim:3078260611",
|
||||
type: "house"
|
||||
}
|
||||
] == Nominatim.search("10 rue Jangot")
|
||||
end
|
||||
|
||||
test "returns a valid address from reverse geocode" do
|
||||
use_cassette "geospatial/nominatim/geocode" do
|
||||
assert [
|
||||
%Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "nominatim:3078260611",
|
||||
type: "house"
|
||||
}
|
||||
] ==
|
||||
Nominatim.geocode(4.842569, 45.751718)
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/nominatim/geocode.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url:
|
||||
"https://nominatim.openstreetmap.org/reverse?format=geocodejson&lat=45.751718&lon=4.842569&accept-language=en&addressdetails=1&namedetails=1"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert [
|
||||
%Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
},
|
||||
origin_id: "nominatim:3078260611",
|
||||
type: "house"
|
||||
}
|
||||
] ==
|
||||
Nominatim.geocode(4.842569, 45.751718)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,66 +1,41 @@
|
||||
defmodule Mobilizon.Service.Geospatial.PhotonTest do
|
||||
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
||||
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mock
|
||||
import Mox
|
||||
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Geospatial.Photon
|
||||
|
||||
@http_options [
|
||||
follow_redirect: true,
|
||||
ssl: [{:versions, [:"tlsv1.2"]}]
|
||||
]
|
||||
|
||||
setup do
|
||||
# Config.instance_user_agent/0 makes database calls so because of ownership connection
|
||||
# we need to define it like this instead of a constant
|
||||
# See https://hexdocs.pm/ecto_sql/Ecto.Adapters.SQL.Sandbox.html
|
||||
{:ok,
|
||||
httpoison_headers: [
|
||||
{"User-Agent", Config.instance_user_agent()}
|
||||
]}
|
||||
end
|
||||
alias Mobilizon.Service.HTTP.BaseClient.Mock
|
||||
|
||||
describe "search address" do
|
||||
test "produces a valid search address with options", %{httpoison_headers: httpoison_headers} do
|
||||
with_mock HTTPoison,
|
||||
get: fn _url, _headers, _options ->
|
||||
{:ok, %HTTPoison.Response{status_code: 200, body: "{\"features\": []"}}
|
||||
end do
|
||||
Photon.search("10 Rue Jangot",
|
||||
limit: 5,
|
||||
lang: "fr"
|
||||
)
|
||||
|
||||
assert_called(
|
||||
HTTPoison.get(
|
||||
"https://photon.komoot.de/api/?q=10%20Rue%20Jangot&lang=fr&limit=5",
|
||||
httpoison_headers,
|
||||
@http_options
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "returns a valid address from search" do
|
||||
use_cassette "geospatial/photon/search" do
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} == Photon.search("10 rue Jangot") |> hd
|
||||
end
|
||||
data =
|
||||
File.read!("test/fixtures/geospatial/photon/search.json")
|
||||
|> Jason.decode!()
|
||||
|
||||
Mock
|
||||
|> expect(:call, fn
|
||||
%{
|
||||
method: :get,
|
||||
url: "https://photon.komoot.de/api/?q=10%20rue%20Jangot&lang=en&limit=10"
|
||||
},
|
||||
_opts ->
|
||||
{:ok, %Tesla.Env{status: 200, body: data}}
|
||||
end)
|
||||
|
||||
assert %Address{
|
||||
locality: "Lyon",
|
||||
description: "10 Rue Jangot",
|
||||
region: "Auvergne-Rhône-Alpes",
|
||||
country: "France",
|
||||
postal_code: "69007",
|
||||
street: "10 Rue Jangot",
|
||||
geom: %Geo.Point{
|
||||
coordinates: {4.8425657, 45.7517141},
|
||||
properties: %{},
|
||||
srid: 4326
|
||||
}
|
||||
} == Photon.search("10 rue Jangot") |> hd
|
||||
end
|
||||
|
||||
# Photon returns something quite wrong, so no need to test this right now.
|
||||
|
||||
Reference in New Issue
Block a user