1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 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
 
  14 @pytest.mark.parametrize('inp', ['',
 
  17                                  'abc -78.90, 12.456 def',
 
  19 def test_extract_coords_no_coords(inp):
 
  20     query, x, y = helper.extract_coords_from_query(inp)
 
  27 def test_extract_coords_null_island():
 
  28     assert ('', 0.0, 0.0) == helper.extract_coords_from_query('0.0 -0.0')
 
  31 def test_extract_coords_with_text_before():
 
  32     assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('abc  -78.90, 12.456')
 
  35 def test_extract_coords_with_text_after():
 
  36     assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('-78.90, 12.456   abc')
 
  38 @pytest.mark.parametrize('inp', [' [12.456,-78.90] ', ' 12.456,-78.90 '])
 
  39 def test_extract_coords_with_spaces(inp):
 
  40     assert ('', -78.90, 12.456) == helper.extract_coords_from_query(inp)
 
  42 @pytest.mark.parametrize('inp', ['40 26.767 N 79 58.933 W',
 
  43                      '40° 26.767′ N 79° 58.933′ W',
 
  44                      "40° 26.767' N 79° 58.933' W",
 
  47                      'N 40 26.767, W 79 58.933',
 
  48                      'N 40°26.767′, W 79°58.933′',
 
  49                      '  N 40°26.767′, W 79°58.933′',
 
  50                      "N 40°26.767', W 79°58.933'",
 
  52                      '40 26 46 N 79 58 56 W',
 
  53                      '40° 26′ 46″ N 79° 58′ 56″ W',
 
  54                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
 
  55                      '40°26′46″N 79°58′56″W',
 
  56                      'N 40 26 46 W 79 58 56',
 
  57                      'N 40° 26′ 46″, W 79° 58′ 56″',
 
  58                      'N 40° 26\' 46", W 79° 58\' 56"',
 
  59                      'N 40° 26\' 46", W 79° 58\' 56"',
 
  63                      '40.446° N 79.982° W',
 
  64                      'N 40.446° W 79.982°',
 
  71                      '       40.446
\v,   -79.982 '])
 
  72 def test_extract_coords_formats(inp):
 
  73     query, x, y = helper.extract_coords_from_query(inp)
 
  76     assert pytest.approx(x, abs=0.001) == -79.982
 
  77     assert pytest.approx(y, abs=0.001) == 40.446
 
  79     query, x, y = helper.extract_coords_from_query('foo bar ' + inp)
 
  81     assert query == 'foo bar'
 
  82     assert pytest.approx(x, abs=0.001) == -79.982
 
  83     assert pytest.approx(y, abs=0.001) == 40.446
 
  85     query, x, y = helper.extract_coords_from_query(inp + ' x')
 
  88     assert pytest.approx(x, abs=0.001) == -79.982
 
  89     assert pytest.approx(y, abs=0.001) == 40.446
 
  92 def test_extract_coords_formats_southeast():
 
  93     query, x, y = helper.extract_coords_from_query('S 40 26.767, E 79 58.933')
 
  96     assert pytest.approx(x, abs=0.001) == 79.982
 
  97     assert pytest.approx(y, abs=0.001) == -40.446
 
 100 @pytest.mark.parametrize('inp', ['[shop=fish] foo bar',
 
 101                                  'foo [shop=fish] bar',
 
 102                                  'foo [shop=fish]bar',
 
 103                                  'foo bar [shop=fish]'])
 
 104 def test_extract_category_good(inp):
 
 105     query, cls, typ = helper.extract_category_from_query(inp)
 
 107     assert query == 'foo bar'
 
 111 def test_extract_category_only():
 
 112     assert helper.extract_category_from_query('[shop=market]') == ('', 'shop', 'market')
 
 114 @pytest.mark.parametrize('inp', ['house []', 'nothing', '[352]'])
 
 115 def  test_extract_category_no_match(inp):
 
 116     assert helper.extract_category_from_query(inp) == (inp, None, None)