]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_helpers_v1.py
Merge branch 'osm-search:master' into check-database-on-frozen-database
[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) 2023 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', ['', '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)
17
18     assert query == inp
19     assert x is None
20     assert y is None
21
22
23 def test_extract_coords_null_island():
24     assert ('', 0.0, 0.0) == helper.extract_coords_from_query('0.0 -0.0')
25
26
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')
29
30
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')
33
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)
37
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",
41                      "40° 26.767'\n"
42                      "    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'",
47  
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"',
56  
57                      '40.446 -79.982',
58                      '40.446,-79.982',
59                      '40.446° N 79.982° W',
60                      'N 40.446° W 79.982°',
61  
62                      '[40.446 -79.982]',
63                      '[40.446,\v-79.982]',
64                      '       40.446  ,   -79.982     ',
65                      '       40.446  ,   -79.982     ',
66                      '       40.446     ,   -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)
70
71     assert query == ''
72     assert pytest.approx(x, abs=0.001) == -79.982
73     assert pytest.approx(y, abs=0.001) == 40.446
74
75     query, x, y = helper.extract_coords_from_query('foo bar ' + inp)
76
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
80
81     query, x, y = helper.extract_coords_from_query(inp + ' x')
82
83     assert query == 'x'
84     assert pytest.approx(x, abs=0.001) == -79.982
85     assert pytest.approx(y, abs=0.001) == 40.446
86
87
88 def test_extract_coords_formats_southeast():
89     query, x, y = helper.extract_coords_from_query('S 40 26.767, E 79 58.933')
90
91     assert query == ''
92     assert pytest.approx(x, abs=0.001) == 79.982
93     assert pytest.approx(y, abs=0.001) == -40.446
94
95
96 @pytest.mark.parametrize('inp', ['[shop=fish] foo bar',
97                                  'foo [shop=fish] bar',
98                                  '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)
102
103     assert query == 'foo bar'
104     assert cls == 'shop'
105     assert typ == 'fish'
106
107 def test_extract_category_only():
108     assert helper.extract_category_from_query('[shop=market]') == ('', 'shop', 'market')
109
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)