1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 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, 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())
33 async with apiobj.api._async_api.begin() as conn:
34 return await search.lookup(conn, details)
36 return apiobj.async_to_sync(run())
39 def test_postcode_only_search(apiobj):
40 apiobj.add_postcode(place_id=100, country_code='ch', postcode='12345')
41 apiobj.add_postcode(place_id=101, country_code='pl', postcode='12 345')
43 results = run_search(apiobj, 0.3, ['12345', '12 345'], [0.0, 0.1])
45 assert len(results) == 2
46 assert [r.place_id for r in results] == [100, 101]
49 def test_postcode_with_country(apiobj):
50 apiobj.add_postcode(place_id=100, country_code='ch', postcode='12345')
51 apiobj.add_postcode(place_id=101, country_code='pl', postcode='12 345')
53 results = run_search(apiobj, 0.3, ['12345', '12 345'], [0.0, 0.1],
56 assert len(results) == 1
57 assert results[0].place_id == 101
60 class TestPostcodeSearchWithAddress:
62 @pytest.fixture(autouse=True)
63 def fill_database(self, apiobj):
64 apiobj.add_postcode(place_id=100, country_code='ch',
65 parent_place_id=1000, postcode='12345',
66 geometry='POINT(17 5)')
67 apiobj.add_postcode(place_id=101, country_code='pl',
68 parent_place_id=2000, postcode='12345',
69 geometry='POINT(-45 7)')
70 apiobj.add_placex(place_id=1000, class_='place', type='village',
71 rank_search=22, rank_address=22,
73 apiobj.add_search_name(1000, names=[1,2,10,11],
74 search_rank=22, address_rank=22,
76 apiobj.add_placex(place_id=2000, class_='place', type='village',
77 rank_search=22, rank_address=22,
79 apiobj.add_search_name(2000, names=[1,2,20,21],
80 search_rank=22, address_rank=22,
84 def test_lookup_both(self, apiobj):
85 lookup = FieldLookup('name_vector', [1,2], 'restrict')
86 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
88 results = run_search(apiobj, 0.1, ['12345'], lookup=[lookup], ranking=[ranking])
90 assert [r.place_id for r in results] == [100, 101]
93 def test_restrict_by_name(self, apiobj):
94 lookup = FieldLookup('name_vector', [10], 'restrict')
96 results = run_search(apiobj, 0.1, ['12345'], lookup=[lookup])
98 assert [r.place_id for r in results] == [100]
101 @pytest.mark.parametrize('coord,place_id', [((16.5, 5), 100),
102 ((-45.1, 7.004), 101)])
103 def test_lookup_near(self, apiobj, coord, place_id):
104 lookup = FieldLookup('name_vector', [1,2], 'restrict')
105 ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
107 results = run_search(apiobj, 0.1, ['12345'],
108 lookup=[lookup], ranking=[ranking],
109 details=SearchDetails(near=napi.Point(*coord),
112 assert [r.place_id for r in results] == [place_id]
115 @pytest.mark.parametrize('geom', [napi.GeometryFormat.GEOJSON,
116 napi.GeometryFormat.KML,
117 napi.GeometryFormat.SVG,
118 napi.GeometryFormat.TEXT])
119 def test_return_geometries(self, apiobj, geom):
120 results = run_search(apiobj, 0.1, ['12345'],
121 details=SearchDetails(geometry_output=geom))
124 assert all(geom.name.lower() in r.geometry for r in results)
127 @pytest.mark.parametrize('viewbox, rids', [('-46,6,-44,8', [101,100]),
128 ('16,4,18,6', [100,101])])
129 def test_prefer_viewbox(self, apiobj, viewbox, rids):
130 results = run_search(apiobj, 0.1, ['12345'],
131 details=SearchDetails.from_kwargs({'viewbox': viewbox}))
133 assert [r.place_id for r in results] == rids
136 @pytest.mark.parametrize('viewbox, rid', [('-46,6,-44,8', 101),
138 def test_restrict_to_viewbox(self, apiobj, viewbox, rid):
139 results = run_search(apiobj, 0.1, ['12345'],
140 details=SearchDetails.from_kwargs({'viewbox': viewbox,
141 'bounded_viewbox': True}))
143 assert [r.place_id for r in results] == [rid]
146 @pytest.mark.parametrize('coord,rids', [((17.05, 5), [100, 101]),
147 ((-45, 7.1), [101, 100])])
148 def test_prefer_near(self, apiobj, coord, rids):
149 results = run_search(apiobj, 0.1, ['12345'],
150 details=SearchDetails(near=napi.Point(*coord)))
152 assert [r.place_id for r in results] == rids
155 @pytest.mark.parametrize('pid,rid', [(100, 101), (101, 100)])
156 def test_exclude(self, apiobj, pid, rid):
157 results = run_search(apiobj, 0.1, ['12345'],
158 details=SearchDetails(excluded=[pid]))
160 assert [r.place_id for r in results] == [rid]