Add test for find_close_events/4

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-12-04 12:06:34 +01:00
parent c6e6a61000
commit ace427c223
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 19 additions and 4 deletions

View File

@ -69,15 +69,20 @@ defmodule Mobilizon.Events do
import Geo.PostGIS import Geo.PostGIS
# 50 000 meters -> 50 kms @doc """
def find_close_events(lon, lat, radius \\ 50_000) do Find close events to coordinates
with {:ok, ip_point} <- Geo.WKT.decode("SRID=4326;POINT(#{lon} #{lat})") do
Radius is in meters and defaults to 50km.
"""
@spec find_close_events(number(), number(), number(), number()) :: list(Event.t())
def find_close_events(lon, lat, radius \\ 50_000, srid \\ 4326) do
with {:ok, point} <- Geo.WKT.decode("SRID=#{srid};POINT(#{lon} #{lat})") do
Repo.all( Repo.all(
from( from(
e in Event, e in Event,
join: a in Address, join: a in Address,
on: a.id == e.physical_address_id, on: a.id == e.physical_address_id,
where: st_dwithin_in_meters(^ip_point, a.geom, ^radius), where: st_dwithin_in_meters(^point, a.geom, ^radius),
preload: :organizer_actor preload: :organizer_actor
) )
) )

View File

@ -65,6 +65,16 @@ defmodule Mobilizon.EventsTest do
assert title2 == hd(tl(Events.find_events_by_name(""))).title assert title2 == hd(tl(Events.find_events_by_name(""))).title
end end
test "find_close_events/3 returns events in the area" do
assert [] == Events.find_close_events(0, 0)
geom = %Geo.Point{coordinates: {47.2330724, -1.55068}, srid: 4326}
address = insert(:address, geom: geom)
event = insert(:event, physical_address: address)
assert [event.id] == Events.find_close_events(47.2330724, -1.55068) |> Enum.map(& &1.id)
end
test "create_event/1 with valid data creates a event" do test "create_event/1 with valid data creates a event" do
actor = insert(:actor) actor = insert(:actor)
category = insert(:category) category = insert(:category)