]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_results.py
Merge pull request #3828 from lonvia/code-cleanup
[nominatim.git] / test / python / api / test_results.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) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for result datatype helper functions.
9 """
10 import struct
11 from binascii import hexlify
12
13 import pytest
14
15 from nominatim_api import SourceTable, DetailedResult, Point
16 import nominatim_api.results as nresults
17
18
19 def mkpoint(x, y):
20     return hexlify(struct.pack("=biidd", 1, 0x20000001, 4326, x, y)).decode('utf-8')
21
22
23 class FakeRow:
24     def __init__(self, **kwargs):
25         if 'parent_place_id' not in kwargs:
26             kwargs['parent_place_id'] = None
27         for k, v in kwargs.items():
28             setattr(self, k, v)
29         self._mapping = kwargs
30
31
32 def test_minimal_detailed_result():
33     res = DetailedResult(SourceTable.PLACEX,
34                          ('amenity', 'post_box'),
35                          Point(23.1, 0.5))
36
37     assert res.lon == 23.1
38     assert res.lat == 0.5
39     assert res.calculated_importance() == pytest.approx(0.00001)
40
41
42 def test_detailed_result_custom_importance():
43     res = DetailedResult(SourceTable.PLACEX,
44                          ('amenity', 'post_box'),
45                          Point(23.1, 0.5),
46                          importance=0.4563)
47
48     assert res.calculated_importance() == 0.4563
49
50
51 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
52                                   nresults.create_from_tiger_row))
53 def test_create_row_with_housenumber(func):
54     row = FakeRow(place_id=2345, osm_type='W', osm_id=111, housenumber=4,
55                   address=None, postcode='99900', country_code='xd',
56                   centroid=mkpoint(0, 0))
57
58     res = func(row, DetailedResult)
59
60     assert res.housenumber == '4'
61     assert res.extratags is None
62     assert res.category == ('place', 'house')
63
64
65 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
66                                   nresults.create_from_tiger_row))
67 def test_create_row_without_housenumber(func):
68     row = FakeRow(place_id=2345, osm_type='W', osm_id=111,
69                   startnumber=1, endnumber=11, step=2,
70                   address=None, postcode='99900', country_code='xd',
71                   centroid=mkpoint(0, 0))
72
73     res = func(row, DetailedResult)
74
75     assert res.housenumber is None
76     assert res.extratags == {'startnumber': '1', 'endnumber': '11', 'step': '2'}
77     assert res.category == ('place', 'houses')