1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2025 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Tests for the helper functions for v1 API.
 
  12 import nominatim_api.v1.helpers as helper
 
  15 @pytest.mark.parametrize('inp', ['',
 
  18                                  'abc -78.90, 12.456 def',
 
  20 def test_extract_coords_no_coords(inp):
 
  21     query, x, y = helper.extract_coords_from_query(inp)
 
  28 def test_extract_coords_null_island():
 
  29     assert ('', 0.0, 0.0) == helper.extract_coords_from_query('0.0 -0.0')
 
  32 def test_extract_coords_with_text_before():
 
  33     assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('abc  -78.90, 12.456')
 
  36 def test_extract_coords_with_text_after():
 
  37     assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('-78.90, 12.456   abc')
 
  40 @pytest.mark.parametrize('inp', [' [12.456,-78.90] ', ' 12.456,-78.90 '])
 
  41 def test_extract_coords_with_spaces(inp):
 
  42     assert ('', -78.90, 12.456) == helper.extract_coords_from_query(inp)
 
  45 @pytest.mark.parametrize('inp', ['40 26.767 N 79 58.933 W',
 
  46                                  '40° 26.767′ N 79° 58.933′ W',
 
  47                                  "40° 26.767' N 79° 58.933' W",
 
  50                                  'N 40 26.767, W 79 58.933',
 
  51                                  'N 40°26.767′, W 79°58.933′',
 
  52                                  '      N 40°26.767′, W 79°58.933′',
 
  53                                  "N 40°26.767', W 79°58.933'",
 
  55                                  '40 26 46 N 79 58 56 W',
 
  56                                  '40° 26′ 46″ N 79° 58′ 56″ W',
 
  57                                  '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
 
  58                                  '40°26′46″N 79°58′56″W',
 
  59                                  'N 40 26 46 W 79 58 56',
 
  60                                  'N 40° 26′ 46″, W 79° 58′ 56″',
 
  61                                  'N 40° 26\' 46", W 79° 58\' 56"',
 
  62                                  'N 40° 26\' 46", W 79° 58\' 56"',
 
  66                                  '40.446° N 79.982° W',
 
  67                                  'N 40.446° W 79.982°',
 
  74                                  '       40.446
\v,   -79.982     '])
 
  75 def test_extract_coords_formats(inp):
 
  76     query, x, y = helper.extract_coords_from_query(inp)
 
  79     assert pytest.approx(x, abs=0.001) == -79.982
 
  80     assert pytest.approx(y, abs=0.001) == 40.446
 
  82     query, x, y = helper.extract_coords_from_query('foo bar ' + inp)
 
  84     assert query == 'foo bar'
 
  85     assert pytest.approx(x, abs=0.001) == -79.982
 
  86     assert pytest.approx(y, abs=0.001) == 40.446
 
  88     query, x, y = helper.extract_coords_from_query(inp + ' x')
 
  91     assert pytest.approx(x, abs=0.001) == -79.982
 
  92     assert pytest.approx(y, abs=0.001) == 40.446
 
  95 def test_extract_coords_formats_southeast():
 
  96     query, x, y = helper.extract_coords_from_query('S 40 26.767, E 79 58.933')
 
  99     assert pytest.approx(x, abs=0.001) == 79.982
 
 100     assert pytest.approx(y, abs=0.001) == -40.446
 
 103 @pytest.mark.parametrize('inp', ['[shop=fish] foo bar',
 
 104                                  'foo [shop=fish] bar',
 
 105                                  'foo [shop=fish]bar',
 
 106                                  'foo bar [shop=fish]'])
 
 107 def test_extract_category_good(inp):
 
 108     query, cls, typ = helper.extract_category_from_query(inp)
 
 110     assert query == 'foo bar'
 
 115 def test_extract_category_only():
 
 116     assert helper.extract_category_from_query('[shop=market]') == ('', 'shop', 'market')
 
 119 @pytest.mark.parametrize('inp', ['house []', 'nothing', '[352]'])
 
 120 def test_extract_category_no_match(inp):
 
 121     assert helper.extract_category_from_query(inp) == (inp, None, None)