Nominatim/test/python/api/test_helpers_v1.py
2024-06-26 11:52:47 +02:00

117 lines
4.2 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of Nominatim. (https://nominatim.org)
#
# Copyright (C) 2024 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Tests for the helper functions for v1 API.
"""
import pytest
import nominatim_api.v1.helpers as helper
@pytest.mark.parametrize('inp', ['',
'abc',
'12 23',
'abc -78.90, 12.456 def',
'40 N 60 W'])
def test_extract_coords_no_coords(inp):
query, x, y = helper.extract_coords_from_query(inp)
assert query == inp
assert x is None
assert y is None
def test_extract_coords_null_island():
assert ('', 0.0, 0.0) == helper.extract_coords_from_query('0.0 -0.0')
def test_extract_coords_with_text_before():
assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('abc -78.90, 12.456')
def test_extract_coords_with_text_after():
assert ('abc', 12.456, -78.90) == helper.extract_coords_from_query('-78.90, 12.456 abc')
@pytest.mark.parametrize('inp', [' [12.456,-78.90] ', ' 12.456,-78.90 '])
def test_extract_coords_with_spaces(inp):
assert ('', -78.90, 12.456) == helper.extract_coords_from_query(inp)
@pytest.mark.parametrize('inp', ['40 26.767 N 79 58.933 W',
'40° 26.767 N 79° 58.933 W',
"40° 26.767' N 79° 58.933' W",
"40° 26.767'\n"
" N 79° 58.933' W",
'N 40 26.767, W 79 58.933',
'N 40°26.767, W 79°58.933',
' N 40°26.767, W 79°58.933',
"N 40°26.767', W 79°58.933'",
'40 26 46 N 79 58 56 W',
'40° 26 46″ N 79° 58 56″ W',
'40° 26 46.00″ N 79° 58 56.00″ W',
'40°2646″N 79°5856″W',
'N 40 26 46 W 79 58 56',
'N 40° 26 46″, W 79° 58 56″',
'N 40° 26\' 46", W 79° 58\' 56"',
'N 40° 26\' 46", W 79° 58\' 56"',
'40.446 -79.982',
'40.446,-79.982',
'40.446° N 79.982° W',
'N 40.446° W 79.982°',
'[40.446 -79.982]',
'[40.446, -79.982]',
' 40.446 , -79.982 ',
' 40.446 , -79.982 ',
' 40.446 , -79.982 ',
' 40.446 , -79.982 '])
def test_extract_coords_formats(inp):
query, x, y = helper.extract_coords_from_query(inp)
assert query == ''
assert pytest.approx(x, abs=0.001) == -79.982
assert pytest.approx(y, abs=0.001) == 40.446
query, x, y = helper.extract_coords_from_query('foo bar ' + inp)
assert query == 'foo bar'
assert pytest.approx(x, abs=0.001) == -79.982
assert pytest.approx(y, abs=0.001) == 40.446
query, x, y = helper.extract_coords_from_query(inp + ' x')
assert query == 'x'
assert pytest.approx(x, abs=0.001) == -79.982
assert pytest.approx(y, abs=0.001) == 40.446
def test_extract_coords_formats_southeast():
query, x, y = helper.extract_coords_from_query('S 40 26.767, E 79 58.933')
assert query == ''
assert pytest.approx(x, abs=0.001) == 79.982
assert pytest.approx(y, abs=0.001) == -40.446
@pytest.mark.parametrize('inp', ['[shop=fish] foo bar',
'foo [shop=fish] bar',
'foo [shop=fish]bar',
'foo bar [shop=fish]'])
def test_extract_category_good(inp):
query, cls, typ = helper.extract_category_from_query(inp)
assert query == 'foo bar'
assert cls == 'shop'
assert typ == 'fish'
def test_extract_category_only():
assert helper.extract_category_from_query('[shop=market]') == ('', 'shop', 'market')
@pytest.mark.parametrize('inp', ['house []', 'nothing', '[352]'])
def test_extract_category_no_match(inp):
assert helper.extract_category_from_query(inp) == (inp, None, None)