]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_results.py
enable CI tests for Ubuntu 24
[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) 2024 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 import pytest_asyncio
15 import sqlalchemy as sa
16
17
18 from nominatim_api import SourceTable, DetailedResult, Point
19 import nominatim_api.results as nresults
20
21 def mkpoint(x, y):
22     return hexlify(struct.pack("=biidd", 1, 0x20000001, 4326, x, y)).decode('utf-8')
23
24 class FakeRow:
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():
29             setattr(self, k, v)
30         self._mapping = kwargs
31
32
33 def test_minimal_detailed_result():
34     res = DetailedResult(SourceTable.PLACEX,
35                          ('amenity', 'post_box'),
36                          Point(23.1, 0.5))
37
38     assert res.lon == 23.1
39     assert res.lat == 0.5
40     assert res.calculated_importance() == pytest.approx(0.00001)
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_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
57
58
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))
65
66     res = func(row, DetailedResult)
67
68     assert res.housenumber == '4'
69     assert res.extratags is None
70     assert res.category == ('place', 'house')
71
72
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))
80
81     res = func(row, DetailedResult)
82
83     assert res.housenumber is None
84     assert res.extratags == {'startnumber': '1', 'endnumber': '11', 'step': '2'}
85     assert res.category == ('place', 'houses')