]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_results.py
Merge pull request #3063 from lonvia/variable-parameters
[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) 2023 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
12 import pytest
13 import pytest_asyncio
14 import sqlalchemy as sa
15
16
17 from nominatim.api import SourceTable, DetailedResult, Point
18 import nominatim.api.results as nresults
19
20 class FakeCentroid:
21     def __init__(self, x, y):
22         self.data = struct.pack("=biidd", 1, 0x20000001, 4326,
23                                         x, y)
24
25 class FakeRow:
26     def __init__(self, **kwargs):
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.0000001)
40
41 def test_detailed_result_custom_importance():
42     res = DetailedResult(SourceTable.PLACEX,
43                          ('amenity', 'post_box'),
44                          Point(23.1, 0.5),
45                          importance=0.4563)
46
47     assert res.calculated_importance() == 0.4563
48
49
50 @pytest.mark.parametrize('func', (nresults.create_from_placex_row,
51                                   nresults.create_from_osmline_row,
52                                   nresults.create_from_tiger_row,
53                                   nresults.create_from_postcode_row))
54 def test_create_row_none(func):
55     assert func(None, DetailedResult) is None
56
57
58 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
59                                   nresults.create_from_tiger_row))
60 def test_create_row_with_housenumber(func):
61     row = FakeRow(place_id=2345, osm_type='W', osm_id=111, housenumber=4,
62                   address=None, postcode='99900', country_code='xd',
63                   centroid=FakeCentroid(0, 0))
64
65     res = func(row, DetailedResult)
66
67     assert res.housenumber == '4'
68     assert res.extratags is None
69     assert res.category == ('place', 'house')
70
71
72 @pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
73                                   nresults.create_from_tiger_row))
74 def test_create_row_without_housenumber(func):
75     row = FakeRow(place_id=2345, osm_type='W', osm_id=111,
76                   startnumber=1, endnumber=11, step=2,
77                   address=None, postcode='99900', country_code='xd',
78                   centroid=FakeCentroid(0, 0))
79
80     res = func(row, DetailedResult)
81
82     assert res.housenumber is None
83     assert res.extratags == {'startnumber': '1', 'endnumber': '11', 'step': '2'}
84     assert res.category == ('place', 'houses')