]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_helpers_v1.py
Merge pull request #3670 from lonvia/flake-for-tests
[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) 2025 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
15 @pytest.mark.parametrize('inp', ['',
16                                  'abc',
17                                  '12 23',
18                                  'abc -78.90, 12.456 def',
19                                  '40 N 60 W'])
20 def test_extract_coords_no_coords(inp):
21     query, x, y = helper.extract_coords_from_query(inp)
22
23     assert query == inp
24     assert x is None
25     assert y is None
26
27
28 def test_extract_coords_null_island():
29     assert ('', 0.0, 0.0) == helper.extract_coords_from_query('0.0 -0.0')
30
31
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')
34
35
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')
38
39
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)
43
44
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",
48                                  "40° 26.767'\n"
49                                  "    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'",
54
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"',
63
64                                  '40.446 -79.982',
65                                  '40.446,-79.982',
66                                  '40.446° N 79.982° W',
67                                  'N 40.446° W 79.982°',
68
69                                  '[40.446 -79.982]',
70                                  '[40.446,\v-79.982]',
71                                  '       40.446  ,   -79.982     ',
72                                  '       40.446  ,   -79.982     ',
73                                  '       40.446 ,   -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)
77
78     assert query == ''
79     assert pytest.approx(x, abs=0.001) == -79.982
80     assert pytest.approx(y, abs=0.001) == 40.446
81
82     query, x, y = helper.extract_coords_from_query('foo bar ' + inp)
83
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
87
88     query, x, y = helper.extract_coords_from_query(inp + ' x')
89
90     assert query == 'x'
91     assert pytest.approx(x, abs=0.001) == -79.982
92     assert pytest.approx(y, abs=0.001) == 40.446
93
94
95 def test_extract_coords_formats_southeast():
96     query, x, y = helper.extract_coords_from_query('S 40 26.767, E 79 58.933')
97
98     assert query == ''
99     assert pytest.approx(x, abs=0.001) == 79.982
100     assert pytest.approx(y, abs=0.001) == -40.446
101
102
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)
109
110     assert query == 'foo bar'
111     assert cls == 'shop'
112     assert typ == 'fish'
113
114
115 def test_extract_category_only():
116     assert helper.extract_category_from_query('[shop=market]') == ('', 'shop', 'market')
117
118
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)