]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_postcode.py
enable all API tests for sqlite and port missing features
[nominatim.git] / test / python / api / search / test_search_postcode.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 postcode 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 PostcodeSearch
15 from nominatim.api.search.db_search_fields import WeightedStrings, FieldLookup, \
16                                                   FieldRanking, RankedTokens
17
18 def run_search(apiobj, frontend, global_penalty, pcs, pc_penalties=None,
19                ccodes=[], lookup=[], ranking=[], details=SearchDetails()):
20     if pc_penalties is None:
21         pc_penalties = [0.0] * len(pcs)
22
23     class MySearchData:
24         penalty = global_penalty
25         postcodes = WeightedStrings(pcs, pc_penalties)
26         countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
27         lookups = lookup
28         rankings = ranking
29
30     search = PostcodeSearch(0.0, MySearchData())
31
32     api = frontend(apiobj, options=['search'])
33
34     async def run():
35         async with api._async_api.begin() as conn:
36             return await search.lookup(conn, details)
37
38     return api._loop.run_until_complete(run())
39
40
41 def test_postcode_only_search(apiobj, frontend):
42     apiobj.add_postcode(place_id=100, country_code='ch', postcode='12345')
43     apiobj.add_postcode(place_id=101, country_code='pl', postcode='12 345')
44
45     results = run_search(apiobj, frontend, 0.3, ['12345', '12 345'], [0.0, 0.1])
46
47     assert len(results) == 2
48     assert [r.place_id for r in results] == [100, 101]
49
50
51 def test_postcode_with_country(apiobj, frontend):
52     apiobj.add_postcode(place_id=100, country_code='ch', postcode='12345')
53     apiobj.add_postcode(place_id=101, country_code='pl', postcode='12 345')
54
55     results = run_search(apiobj, frontend, 0.3, ['12345', '12 345'], [0.0, 0.1],
56                          ccodes=['de', 'pl'])
57
58     assert len(results) == 1
59     assert results[0].place_id == 101
60
61
62 class TestPostcodeSearchWithAddress:
63
64     @pytest.fixture(autouse=True)
65     def fill_database(self, apiobj):
66         apiobj.add_postcode(place_id=100, country_code='ch',
67                             parent_place_id=1000, postcode='12345',
68                             geometry='POINT(17 5)')
69         apiobj.add_postcode(place_id=101, country_code='pl',
70                             parent_place_id=2000, postcode='12345',
71                             geometry='POINT(-45 7)')
72         apiobj.add_placex(place_id=1000, class_='place', type='village',
73                           rank_search=22, rank_address=22,
74                           country_code='ch')
75         apiobj.add_search_name(1000, names=[1,2,10,11],
76                                search_rank=22, address_rank=22,
77                                country_code='ch')
78         apiobj.add_placex(place_id=2000, class_='place', type='village',
79                           rank_search=22, rank_address=22,
80                           country_code='pl')
81         apiobj.add_search_name(2000, names=[1,2,20,21],
82                                search_rank=22, address_rank=22,
83                                country_code='pl')
84
85
86     def test_lookup_both(self, apiobj, frontend):
87         lookup = FieldLookup('name_vector', [1,2], 'restrict')
88         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
89
90         results = run_search(apiobj, frontend, 0.1, ['12345'], lookup=[lookup], ranking=[ranking])
91
92         assert [r.place_id for r in results] == [100, 101]
93
94
95     def test_restrict_by_name(self, apiobj, frontend):
96         lookup = FieldLookup('name_vector', [10], 'restrict')
97
98         results = run_search(apiobj, frontend, 0.1, ['12345'], lookup=[lookup])
99
100         assert [r.place_id for r in results] == [100]
101
102
103     @pytest.mark.parametrize('coord,place_id', [((16.5, 5), 100),
104                                                 ((-45.1, 7.004), 101)])
105     def test_lookup_near(self, apiobj, frontend, coord, place_id):
106         lookup = FieldLookup('name_vector', [1,2], 'restrict')
107         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
108
109         results = run_search(apiobj, frontend, 0.1, ['12345'],
110                              lookup=[lookup], ranking=[ranking],
111                              details=SearchDetails(near=napi.Point(*coord),
112                                                    near_radius=0.6))
113
114         assert [r.place_id for r in results] == [place_id]
115
116
117     @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
118                                       napi.GeometryFormat.KML,
119                                       napi.GeometryFormat.SVG,
120                                       napi.GeometryFormat.TEXT])
121     def test_return_geometries(self, apiobj, frontend, geom):
122         results = run_search(apiobj, frontend, 0.1, ['12345'],
123                              details=SearchDetails(geometry_output=geom))
124
125         assert results
126         assert all(geom.name.lower() in r.geometry for r in results)
127
128
129     @pytest.mark.parametrize('viewbox, rids', [('-46,6,-44,8', [101,100]),
130                                                ('16,4,18,6', [100,101])])
131     def test_prefer_viewbox(self, apiobj, frontend, viewbox, rids):
132         results = run_search(apiobj, frontend, 0.1, ['12345'],
133                              details=SearchDetails.from_kwargs({'viewbox': viewbox}))
134
135         assert [r.place_id for r in results] == rids
136
137
138     @pytest.mark.parametrize('viewbox, rid', [('-46,6,-44,8', 101),
139                                                ('16,4,18,6', 100)])
140     def test_restrict_to_viewbox(self, apiobj, frontend, viewbox, rid):
141         results = run_search(apiobj, frontend, 0.1, ['12345'],
142                              details=SearchDetails.from_kwargs({'viewbox': viewbox,
143                                                                 'bounded_viewbox': True}))
144
145         assert [r.place_id for r in results] == [rid]
146
147
148     @pytest.mark.parametrize('coord,rids', [((17.05, 5), [100, 101]),
149                                             ((-45, 7.1), [101, 100])])
150     def test_prefer_near(self, apiobj, frontend, coord, rids):
151         results = run_search(apiobj, frontend, 0.1, ['12345'],
152                              details=SearchDetails(near=napi.Point(*coord)))
153
154         assert [r.place_id for r in results] == rids
155
156
157     @pytest.mark.parametrize('pid,rid', [(100, 101), (101, 100)])
158     def test_exclude(self, apiobj, frontend, pid, rid):
159         results = run_search(apiobj, frontend, 0.1, ['12345'],
160                              details=SearchDetails(excluded=[pid]))
161
162         assert [r.place_id for r in results] == [rid]