1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2023 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', ['', 'abc', '12 23', 'abc -78.90, 12.456 def'])
 
  15 def test_extract_coords_no_coords(inp):
 
  16     query, x, y = helper.extract_coords_from_query(inp)
 
  23 def test_extract_coords_null_island():
 
  24     assert ('', 0.0, 0.0) == helper.extract_coords_from_query('0.0 -0.0')
 
  27 def test_extract_coords_with_text_before():
 
  28     assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('abc  -78.90, 12.456')
 
  31 def test_extract_coords_with_text_after():
 
  32     assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('-78.90, 12.456   abc')
 
  34 @pytest.mark.parametrize('inp', [' [12.456,-78.90] ', ' 12.456,-78.90 '])
 
  35 def test_extract_coords_with_spaces(inp):
 
  36     assert ('', -78.90, 12.456) == helper.extract_coords_from_query(inp)
 
  38 @pytest.mark.parametrize('inp', ['40 26.767 N 79 58.933 W',
 
  39                      '40° 26.767′ N 79° 58.933′ W',
 
  40                      "40° 26.767' N 79° 58.933' W",
 
  43                      'N 40 26.767, W 79 58.933',
 
  44                      'N 40°26.767′, W 79°58.933′',
 
  45                      '  N 40°26.767′, W 79°58.933′',
 
  46                      "N 40°26.767', W 79°58.933'",
 
  48                      '40 26 46 N 79 58 56 W',
 
  49                      '40° 26′ 46″ N 79° 58′ 56″ W',
 
  50                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
 
  51                      '40°26′46″N 79°58′56″W',
 
  52                      'N 40 26 46 W 79 58 56',
 
  53                      'N 40° 26′ 46″, W 79° 58′ 56″',
 
  54                      'N 40° 26\' 46", W 79° 58\' 56"',
 
  55                      'N 40° 26\' 46", W 79° 58\' 56"',
 
  59                      '40.446° N 79.982° W',
 
  60                      'N 40.446° W 79.982°',
 
  67                      '       40.446
\v,   -79.982 '])
 
  68 def test_extract_coords_formats(inp):
 
  69     query, x, y = helper.extract_coords_from_query(inp)
 
  72     assert pytest.approx(x, abs=0.001) == -79.982
 
  73     assert pytest.approx(y, abs=0.001) == 40.446
 
  75     query, x, y = helper.extract_coords_from_query('foo bar ' + inp)
 
  77     assert query == 'foo bar'
 
  78     assert pytest.approx(x, abs=0.001) == -79.982
 
  79     assert pytest.approx(y, abs=0.001) == 40.446
 
  81     query, x, y = helper.extract_coords_from_query(inp + ' x')
 
  84     assert pytest.approx(x, abs=0.001) == -79.982
 
  85     assert pytest.approx(y, abs=0.001) == 40.446
 
  88 def test_extract_coords_formats_southeast():
 
  89     query, x, y = helper.extract_coords_from_query('S 40 26.767, E 79 58.933')
 
  92     assert pytest.approx(x, abs=0.001) == 79.982
 
  93     assert pytest.approx(y, abs=0.001) == -40.446
 
  96 @pytest.mark.parametrize('inp', ['[shop=fish] foo bar',
 
  97                                  'foo [shop=fish] bar',
 
  99                                  'foo bar [shop=fish]'])
 
 100 def test_extract_category_good(inp):
 
 101     query, cls, typ = helper.extract_category_from_query(inp)
 
 103     assert query == 'foo bar'
 
 107 def test_extract_category_only():
 
 108     assert helper.extract_category_from_query('[shop=market]') == ('', 'shop', 'market')
 
 110 @pytest.mark.parametrize('inp', ['house []', 'nothing', '[352]'])
 
 111 def  test_extract_category_no_match(inp):
 
 112     assert helper.extract_category_from_query(inp) == (inp, None, None)