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 result datatype helper functions.
11 from binascii import hexlify
15 import sqlalchemy as sa
18 from nominatim.api import SourceTable, DetailedResult, Point
19 import nominatim.api.results as nresults
22 return hexlify(struct.pack("=biidd", 1, 0x20000001, 4326, x, y)).decode('utf-8')
25 def __init__(self, **kwargs):
26 if 'parent_place_id' not in kwargs:
27 kwargs['parent_place_id'] = None
28 for k, v in kwargs.items():
30 self._mapping = kwargs
33 def test_minimal_detailed_result():
34 res = DetailedResult(SourceTable.PLACEX,
35 ('amenity', 'post_box'),
38 assert res.lon == 23.1
40 assert res.calculated_importance() == pytest.approx(0.0000001)
42 def test_detailed_result_custom_importance():
43 res = DetailedResult(SourceTable.PLACEX,
44 ('amenity', 'post_box'),
48 assert res.calculated_importance() == 0.4563
51 @pytest.mark.parametrize('func', (nresults.create_from_placex_row,
52 nresults.create_from_osmline_row,
53 nresults.create_from_tiger_row,
54 nresults.create_from_postcode_row))
55 def test_create_row_none(func):
56 assert func(None, DetailedResult) is None
59 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
60 nresults.create_from_tiger_row))
61 def test_create_row_with_housenumber(func):
62 row = FakeRow(place_id=2345, osm_type='W', osm_id=111, housenumber=4,
63 address=None, postcode='99900', country_code='xd',
64 centroid=mkpoint(0, 0))
66 res = func(row, DetailedResult)
68 assert res.housenumber == '4'
69 assert res.extratags is None
70 assert res.category == ('place', 'house')
73 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
74 nresults.create_from_tiger_row))
75 def test_create_row_without_housenumber(func):
76 row = FakeRow(place_id=2345, osm_type='W', osm_id=111,
77 startnumber=1, endnumber=11, step=2,
78 address=None, postcode='99900', country_code='xd',
79 centroid=mkpoint(0, 0))
81 res = func(row, DetailedResult)
83 assert res.housenumber is None
84 assert res.extratags == {'startnumber': '1', 'endnumber': '11', 'step': '2'}
85 assert res.category == ('place', 'houses')