]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_postcode.py
simplify weigh_search() function
[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, 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     async def run():
33         async with apiobj.api._async_api.begin() as conn:
34             return await search.lookup(conn, details)
35
36     return apiobj.async_to_sync(run())
37
38
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')
42
43     results = run_search(apiobj, 0.3, ['12345', '12 345'], [0.0, 0.1])
44
45     assert len(results) == 2
46     assert [r.place_id for r in results] == [100, 101]
47
48
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')
52
53     results = run_search(apiobj, 0.3, ['12345', '12 345'], [0.0, 0.1],
54                          ccodes=['de', 'pl'])
55
56     assert len(results) == 1
57     assert results[0].place_id == 101
58
59
60 class TestPostcodeSearchWithAddress:
61
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,
72                           country_code='ch')
73         apiobj.add_search_name(1000, names=[1,2,10,11],
74                                search_rank=22, address_rank=22,
75                                country_code='ch')
76         apiobj.add_placex(place_id=2000, class_='place', type='village',
77                           rank_search=22, rank_address=22,
78                           country_code='pl')
79         apiobj.add_search_name(2000, names=[1,2,20,21],
80                                search_rank=22, address_rank=22,
81                                country_code='pl')
82
83
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])])
87
88         results = run_search(apiobj, 0.1, ['12345'], lookup=[lookup], ranking=[ranking])
89
90         assert [r.place_id for r in results] == [100, 101]
91
92
93     def test_restrict_by_name(self, apiobj):
94         lookup = FieldLookup('name_vector', [10], 'restrict')
95
96         results = run_search(apiobj, 0.1, ['12345'], lookup=[lookup])
97
98         assert [r.place_id for r in results] == [100]
99
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])])
106
107         results = run_search(apiobj, 0.1, ['12345'],
108                              lookup=[lookup], ranking=[ranking],
109                              details=SearchDetails(near=napi.Point(*coord),
110                                                    near_radius=0.6))
111
112         assert [r.place_id for r in results] == [place_id]
113
114
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))
122
123         assert results
124         assert all(geom.name.lower() in r.geometry for r in results)
125
126
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}))
132
133         assert [r.place_id for r in results] == rids
134
135
136     @pytest.mark.parametrize('viewbox, rid', [('-46,6,-44,8', 101),
137                                                ('16,4,18,6', 100)])
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}))
142
143         assert [r.place_id for r in results] == [rid]
144
145
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)))
151
152         assert [r.place_id for r in results] == rids
153
154
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]))
159
160         assert [r.place_id for r in results] == [rid]