]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/search/test_search_postcode.py
Merge pull request #3045 from biswajit-k/taginfo
[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         apiobj.add_postcode(place_id=101, country_code='pl',
67                             parent_place_id=2000, postcode='12345')
68         apiobj.add_placex(place_id=1000, class_='place', type='village',
69                           rank_search=22, rank_address=22,
70                           country_code='ch')
71         apiobj.add_search_name(1000, names=[1,2,10,11],
72                                search_rank=22, address_rank=22,
73                                country_code='ch')
74         apiobj.add_placex(place_id=2000, class_='place', type='village',
75                           rank_search=22, rank_address=22,
76                           country_code='pl')
77         apiobj.add_search_name(2000, names=[1,2,20,21],
78                                search_rank=22, address_rank=22,
79                                country_code='pl')
80
81
82     def test_lookup_both(self, apiobj):
83         lookup = FieldLookup('name_vector', [1,2], 'restrict')
84         ranking = FieldRanking('name_vector', 0.3, [RankedTokens(0.0, [10])])
85
86         results = run_search(apiobj, 0.1, ['12345'], lookup=[lookup], ranking=[ranking])
87
88         assert [r.place_id for r in results] == [100, 101]
89
90
91     def test_restrict_by_name(self, apiobj):
92         lookup = FieldLookup('name_vector', [10], 'restrict')
93
94         results = run_search(apiobj, 0.1, ['12345'], lookup=[lookup])
95
96         assert [r.place_id for r in results] == [100]
97