]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_country.py
Merge pull request #3262 from lonvia/fix-category-search
[nominatim.git] / test / python / api / search / test_search_country.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 running the country searcher.
9 """
10 import pytest
11
12 import nominatim.api as napi
13 from nominatim.api.types import SearchDetails
14 from nominatim.api.search.db_searches import CountrySearch
15 from nominatim.api.search.db_search_fields import WeightedStrings
16
17
18 def run_search(apiobj, global_penalty, ccodes,
19                country_penalties=None, details=SearchDetails()):
20     if country_penalties is None:
21         country_penalties = [0.0] * len(ccodes)
22
23     class MySearchData:
24         penalty = global_penalty
25         countries = WeightedStrings(ccodes, country_penalties)
26
27     search = CountrySearch(MySearchData())
28
29     async def run():
30         async with apiobj.api._async_api.begin() as conn:
31             return await search.lookup(conn, details)
32
33     return apiobj.async_to_sync(run())
34
35
36 def test_find_from_placex(apiobj):
37     apiobj.add_placex(place_id=55, class_='boundary', type='administrative',
38                       rank_search=4, rank_address=4,
39                       name={'name': 'Lolaland'},
40                       country_code='yw',
41                       centroid=(10, 10),
42                       geometry='POLYGON((9.5 9.5, 9.5 10.5, 10.5 10.5, 10.5 9.5, 9.5 9.5))')
43
44     results = run_search(apiobj, 0.5, ['de', 'yw'], [0.0, 0.3])
45
46     assert len(results) == 1
47     assert results[0].place_id == 55
48     assert results[0].accuracy == 0.8
49
50 def test_find_from_fallback_countries(apiobj):
51     apiobj.add_country('ro', 'POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')
52     apiobj.add_country_name('ro', {'name': 'România'})
53
54     results = run_search(apiobj, 0.0, ['ro'])
55
56     assert len(results) == 1
57     assert results[0].names == {'name': 'România'}
58
59
60 def test_find_none(apiobj):
61     assert len(run_search(apiobj, 0.0, ['xx'])) == 0
62
63
64 @pytest.mark.parametrize('coord,numres', [((0.5, 1), 1), ((10, 10), 0)])
65 def test_find_near(apiobj, coord, numres):
66     apiobj.add_country('ro', 'POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')
67     apiobj.add_country_name('ro', {'name': 'România'})
68
69     results = run_search(apiobj, 0.0, ['ro'],
70                          details=SearchDetails(near=napi.Point(*coord),
71                                                near_radius=0.1))
72
73     assert len(results) == numres
74
75
76 class TestCountryParameters:
77
78     @pytest.fixture(autouse=True)
79     def fill_database(self, apiobj):
80         apiobj.add_placex(place_id=55, class_='boundary', type='administrative',
81                           rank_search=4, rank_address=4,
82                           name={'name': 'Lolaland'},
83                           country_code='yw',
84                           centroid=(10, 10),
85                           geometry='POLYGON((9.5 9.5, 9.5 10.5, 10.5 10.5, 10.5 9.5, 9.5 9.5))')
86         apiobj.add_country('ro', 'POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')
87         apiobj.add_country_name('ro', {'name': 'România'})
88
89
90     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
91                                       napi.GeometryFormat.KML,
92                                       napi.GeometryFormat.SVG,
93                                       napi.GeometryFormat.TEXT])
94     @pytest.mark.parametrize('cc', ['yw', 'ro'])
95     def test_return_geometries(self, apiobj, geom, cc):
96         results = run_search(apiobj, 0.5, [cc],
97                              details=SearchDetails(geometry_output=geom))
98
99         assert len(results) == 1
100         assert geom.name.lower() in results[0].geometry
101
102
103     @pytest.mark.parametrize('pid,rids', [(76, [55]), (55, [])])
104     def test_exclude_place_id(self, apiobj, pid, rids):
105         results = run_search(apiobj, 0.5, ['yw', 'ro'],
106                              details=SearchDetails(excluded=[pid]))
107
108         assert [r.place_id for r in results] == rids
109
110
111     @pytest.mark.parametrize('viewbox,rids', [((9, 9, 11, 11), [55]),
112                                               ((-10, -10, -3, -3), [])])
113     def test_bounded_viewbox_in_placex(self, apiobj, viewbox, rids):
114         results = run_search(apiobj, 0.5, ['yw'],
115                              details=SearchDetails.from_kwargs({'viewbox': viewbox,
116                                                                 'bounded_viewbox': True}))
117
118         assert [r.place_id for r in results] == rids
119
120
121     @pytest.mark.parametrize('viewbox,numres', [((0, 0, 1, 1), 1),
122                                               ((-10, -10, -3, -3), 0)])
123     def test_bounded_viewbox_in_fallback(self, apiobj, viewbox, numres):
124         results = run_search(apiobj, 0.5, ['ro'],
125                              details=SearchDetails.from_kwargs({'viewbox': viewbox,
126                                                                 'bounded_viewbox': True}))
127
128         assert len(results) == numres