]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_results.py
These days the OSM wikipedia tab no longer contains URLs
[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 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         for k, v in kwargs.items():
27             setattr(self, k, v)
28         self._mapping = kwargs
29
30
31 def test_minimal_detailed_result():
32     res = DetailedResult(SourceTable.PLACEX,
33                          ('amenity', 'post_box'),
34                          Point(23.1, 0.5))
35
36     assert res.lon == 23.1
37     assert res.lat == 0.5
38     assert res.calculated_importance() == pytest.approx(0.0000001)
39
40 def test_detailed_result_custom_importance():
41     res = DetailedResult(SourceTable.PLACEX,
42                          ('amenity', 'post_box'),
43                          Point(23.1, 0.5),
44                          importance=0.4563)
45
46     assert res.calculated_importance() == 0.4563
47
48
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
55
56
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))
63
64     res = func(row, DetailedResult)
65
66     assert res.housenumber == '4'
67     assert res.extratags is None
68     assert res.category == ('place', 'house')
69
70
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))
78
79     res = func(row, DetailedResult)
80
81     assert res.housenumber is None
82     assert res.extratags == {'startnumber': '1', 'endnumber': '11', 'step': '2'}
83     assert res.category == ('place', 'houses')