-module(logic_geo_tests). -include_lib("eunit/include/eunit.hrl"). setup() -> catch ets:delete(eventhub_geo_cache), catch ets:delete(eventhub_geo_rl), logic_geo:ensure(), application:unset_env(eventhub, geo_http_get), os:putenv("PHOTON_URL", "http://photon.test"), ok. cleanup(_) -> application:unset_env(eventhub, geo_http_get), os:unsetenv("PHOTON_URL"), catch ets:delete(eventhub_geo_cache), catch ets:delete(eventhub_geo_rl), ok. logic_geo_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"Suggest too short", fun test_suggest_short/0}, {"Suggest parses photon geojson", fun test_suggest_ok/0}, {"Geocode empty features is not_found", fun test_geocode_not_found/0}, {"Unavailable without photon url", fun test_unavailable/0}, {"Reverse fallback address", fun test_reverse_empty/0}, {"Format address from properties", fun test_format_address/0} ]}. sample_geojson() -> <<"{ \"features\": [{ \"geometry\": {\"coordinates\": [37.62, 55.75]}, \"properties\": { \"name\": \"Red Square\", \"city\": \"Moscow\", \"country\": \"Russia\" } }] }">>. test_suggest_short() -> ?assertEqual({error, invalid_query}, logic_geo:suggest(<<"ab">>, <<"ru">>, <<"ip">>)). test_suggest_ok() -> application:set_env(eventhub, geo_http_get, fun(_) -> {ok, sample_geojson()} end), {ok, Hits} = logic_geo:suggest(<<"красная площадь">>, <<"ru">>, <<"ip1">>), ?assertMatch([#{<<"lat">> := 55.75, <<"lon">> := 37.62}], Hits), [#{<<"address">> := Addr}] = Hits, ?assertNotEqual(<<>>, Addr). test_geocode_not_found() -> application:set_env(eventhub, geo_http_get, fun(_) -> {ok, <<"{\"features\":[]}">>} end), ?assertEqual({error, not_found}, logic_geo:geocode(<<"nowherexyz">>, <<"ru">>, <<"u1">>)). test_unavailable() -> os:unsetenv("PHOTON_URL"), application:unset_env(eventhub, geo_http_get), ?assertEqual({error, unavailable}, logic_geo:suggest(<<"москва центр">>, <<"ru">>, <<"ip2">>)). test_reverse_empty() -> application:set_env(eventhub, geo_http_get, fun(_) -> {ok, <<"{\"features\":[]}">>} end), {ok, Hit} = logic_geo:reverse(55.75, 37.62, <<"ru">>, <<"u2">>), ?assertEqual(<<"photon">>, maps:get(<<"source">>, Hit)), ?assertEqual(55.75, maps:get(<<"lat">>, Hit)). test_format_address() -> Addr = logic_geo:format_address(#{ <<"housenumber">> => <<"1">>, <<"street">> => <<"Tverskaya">>, <<"city">> => <<"Moscow">>, <<"country">> => <<"Russia">> }), ?assertEqual(<<"1 Tverskaya, Moscow, Russia">>, Addr).