]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_helpers_v1.py
enable CI tests for Ubuntu 24
[nominatim.git] / test / python / api / test_helpers_v1.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for the helper functions for v1 API.
9 """
10 import pytest
11
12 import nominatim_api.v1.helpers as helper
13
14 @pytest.mark.parametrize('inp', ['',
15                                  'abc',
16                                  '12 23',
17                                  'abc -78.90, 12.456 def',
18                                  '40 N 60 W'])
19 def test_extract_coords_no_coords(inp):
20     query, x, y = helper.extract_coords_from_query(inp)
21
22     assert query == inp
23     assert x is None
24     assert y is None
25
26
27 def test_extract_coords_null_island():
28     assert ('', 0.0, 0.0) == helper.extract_coords_from_query('0.0 -0.0')
29
30
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')
33
34
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')
37
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)
41
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",
45                      "40° 26.767'\n"
46                      "    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'",
51  
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"',
60  
61                      '40.446 -79.982',
62                      '40.446,-79.982',
63                      '40.446° N 79.982° W',
64                      'N 40.446° W 79.982°',
65  
66                      '[40.446 -79.982]',
67                      '[40.446,\v-79.982]',
68                      '       40.446  ,   -79.982     ',
69                      '       40.446  ,   -79.982     ',
70                      '       40.446     ,   -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)
74
75     assert query == ''
76     assert pytest.approx(x, abs=0.001) == -79.982
77     assert pytest.approx(y, abs=0.001) == 40.446
78
79     query, x, y = helper.extract_coords_from_query('foo bar ' + inp)
80
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
84
85     query, x, y = helper.extract_coords_from_query(inp + ' x')
86
87     assert query == 'x'
88     assert pytest.approx(x, abs=0.001) == -79.982
89     assert pytest.approx(y, abs=0.001) == 40.446
90
91
92 def test_extract_coords_formats_southeast():
93     query, x, y = helper.extract_coords_from_query('S 40 26.767, E 79 58.933')
94
95     assert query == ''
96     assert pytest.approx(x, abs=0.001) == 79.982
97     assert pytest.approx(y, abs=0.001) == -40.446
98
99
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)
106
107     assert query == 'foo bar'
108     assert cls == 'shop'
109     assert typ == 'fish'
110
111 def test_extract_category_only():
112     assert helper.extract_category_from_query('[shop=market]') == ('', 'shop', 'market')
113
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)