1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for running the postcode searcher.
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
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)
24 penalty = global_penalty
25 postcodes = WeightedStrings(pcs, pc_penalties)
26 countries = WeightedStrings(ccodes, [0.0] * len(ccodes))
30 search = PostcodeSearch(0.0, MySearchData())
32 api = frontend(apiobj, options=['search'])
35 async with api._async_api.begin() as conn:
36 return await search.lookup(conn, details)
38 return api._loop.run_until_complete(run())
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')
45 results = run_search(apiobj, frontend, 0.3, ['12345', '12 345'], [0.0, 0.1])
47 assert len(results) == 2
48 assert [r.place_id for r in results] == [100, 101]
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')
55 results = run_search(apiobj, frontend, 0.3, ['12345', '12 345'], [0.0, 0.1],
58 assert len(results) == 1
59 assert results[0].place_id == 101
62 def test_postcode_area(apiobj, frontend):
63 apiobj.add_postcode(place_id=100, country_code='ch', postcode='12345')
64 apiobj.add_placex(place_id=200, country_code='ch', postcode='12345',
65 osm_type='R', osm_id=34, class_='boundary', type='postal_code',
66 geometry='POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))')
68 results = run_search(apiobj, frontend, 0.3, ['12345'], [0.0])
70 assert len(results) == 1
71 assert results[0].place_id == 200
72 assert results[0].bbox.area == 1
75 class TestPostcodeSearchWithAddress:
77 @pytest.fixture(autouse=True)
78 def fill_database(self, apiobj):
79 apiobj.add_postcode(place_id=100, country_code='ch',
80 parent_place_id=1000, postcode='12345',
81 geometry='POINT(17 5)')
82 apiobj.add_postcode(place_id=101, country_code='pl',
83 parent_place_id=2000, postcode='12345',
84 geometry='POINT(-45 7)')
85 apiobj.add_placex(place_id=1000, class_='place', type='village',
86 rank_search=22, rank_address=22,
88 apiobj.add_search_name(1000, names=[1,2,10,11],
89 search_rank=22, address_rank=22,
91 apiobj.add_placex(place_id=2000, class_='place', type='village',
92 rank_search=22, rank_address=22,
94 apiobj.add_search_name(2000, names=[1,2,20,21],
95 search_rank=22, address_rank=22,
99 def test_lookup_both(self, apiobj, frontend):
100 lookup = FieldLookup('name_vector', [1,2], 'restrict')
101 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
103 results = run_search(apiobj, frontend, 0.1, ['12345'], lookup=[lookup], ranking=[ranking])
105 assert [r.place_id for r in results] == [100, 101]
108 def test_restrict_by_name(self, apiobj, frontend):
109 lookup = FieldLookup('name_vector', [10], 'restrict')
111 results = run_search(apiobj, frontend, 0.1, ['12345'], lookup=[lookup])
113 assert [r.place_id for r in results] == [100]
116 @pytest.mark.parametrize('coord,place_id', [((16.5, 5), 100),
117 ((-45.1, 7.004), 101)])
118 def test_lookup_near(self, apiobj, frontend, coord, place_id):
119 lookup = FieldLookup('name_vector', [1,2], 'restrict')
120 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
122 results = run_search(apiobj, frontend, 0.1, ['12345'],
123 lookup=[lookup], ranking=[ranking],
124 details=SearchDetails(near=napi.Point(*coord),
127 assert [r.place_id for r in results] == [place_id]
130 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
131 napi.GeometryFormat.KML,
132 napi.GeometryFormat.SVG,
133 napi.GeometryFormat.TEXT])
134 def test_return_geometries(self, apiobj, frontend, geom):
135 results = run_search(apiobj, frontend, 0.1, ['12345'],
136 details=SearchDetails(geometry_output=geom))
139 assert all(geom.name.lower() in r.geometry for r in results)
142 @pytest.mark.parametrize('viewbox, rids', [('-46,6,-44,8', [101,100]),
143 ('16,4,18,6', [100,101])])
144 def test_prefer_viewbox(self, apiobj, frontend, viewbox, rids):
145 results = run_search(apiobj, frontend, 0.1, ['12345'],
146 details=SearchDetails.from_kwargs({'viewbox': viewbox}))
148 assert [r.place_id for r in results] == rids
151 @pytest.mark.parametrize('viewbox, rid', [('-46,6,-44,8', 101),
153 def test_restrict_to_viewbox(self, apiobj, frontend, viewbox, rid):
154 results = run_search(apiobj, frontend, 0.1, ['12345'],
155 details=SearchDetails.from_kwargs({'viewbox': viewbox,
156 'bounded_viewbox': True}))
158 assert [r.place_id for r in results] == [rid]
161 @pytest.mark.parametrize('coord,rids', [((17.05, 5), [100, 101]),
162 ((-45, 7.1), [101, 100])])
163 def test_prefer_near(self, apiobj, frontend, coord, rids):
164 results = run_search(apiobj, frontend, 0.1, ['12345'],
165 details=SearchDetails(near=napi.Point(*coord)))
167 assert [r.place_id for r in results] == rids
170 @pytest.mark.parametrize('pid,rid', [(100, 101), (101, 100)])
171 def test_exclude(self, apiobj, frontend, pid, rid):
172 results = run_search(apiobj, frontend, 0.1, ['12345'],
173 details=SearchDetails(excluded=[pid]))
175 assert [r.place_id for r in results] == [rid]