From ace427c2235d09094158bbadcd4c086ceb1a7837 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 4 Dec 2018 12:06:34 +0100 Subject: [PATCH] Add test for find_close_events/4 Signed-off-by: Thomas Citharel --- lib/mobilizon/events/events.ex | 13 +++++++++---- test/mobilizon/events/events_test.exs | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 5bf220078..7d0aa45e2 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -69,15 +69,20 @@ defmodule Mobilizon.Events do import Geo.PostGIS - # 50 000 meters -> 50 kms - def find_close_events(lon, lat, radius \\ 50_000) do - with {:ok, ip_point} <- Geo.WKT.decode("SRID=4326;POINT(#{lon} #{lat})") do + @doc """ + Find close events to coordinates + + 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( from( e in Event, join: a in Address, 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 ) ) diff --git a/test/mobilizon/events/events_test.exs b/test/mobilizon/events/events_test.exs index c6fd79e19..890572f04 100644 --- a/test/mobilizon/events/events_test.exs +++ b/test/mobilizon/events/events_test.exs @@ -65,6 +65,16 @@ defmodule Mobilizon.EventsTest do assert title2 == hd(tl(Events.find_events_by_name(""))).title 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 actor = insert(:actor) category = insert(:category)