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 for k, v in kwargs.items():
28 self._mapping = kwargs
31 def test_minimal_detailed_result():
32 res = DetailedResult(SourceTable.PLACEX,
33 ('amenity', 'post_box'),
36 assert res.lon == 23.1
38 assert res.calculated_importance() == pytest.approx(0.0000001)
40 def test_detailed_result_custom_importance():
41 res = DetailedResult(SourceTable.PLACEX,
42 ('amenity', 'post_box'),
46 assert res.calculated_importance() == 0.4563
49 @pytest.mark.parametrize('func', (nresults.create_from_placex_row,
50 nresults.create_from_osmline_row,
51 nresults.create_from_tiger_row,
52 nresults.create_from_postcode_row))
53 def test_create_row_none(func):
54 assert func(None, DetailedResult) is None
57 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
58 nresults.create_from_tiger_row))
59 def test_create_row_with_housenumber(func):
60 row = FakeRow(place_id=2345, osm_type='W', osm_id=111, housenumber=4,
61 address=None, postcode='99900', country_code='xd',
62 centroid=mkpoint(0, 0))
64 res = func(row, DetailedResult)
66 assert res.housenumber == '4'
67 assert res.extratags is None
68 assert res.category == ('place', 'house')
71 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
72 nresults.create_from_tiger_row))
73 def test_create_row_without_housenumber(func):
74 row = FakeRow(place_id=2345, osm_type='W', osm_id=111,
75 startnumber=1, endnumber=11, step=2,
76 address=None, postcode='99900', country_code='xd',
77 centroid=mkpoint(0, 0))
79 res = func(row, DetailedResult)
81 assert res.housenumber is None
82 assert res.extratags == {'startnumber': '1', 'endnumber': '11', 'step': '2'}
83 assert res.category == ('place', 'houses')