mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-15 10:57:58 +00:00
split SearchResult type
Use adapted types for the different result types. This makes it easier to have adapted output formatting and means there are only result fields that are filled.
This commit is contained in:
@@ -59,14 +59,14 @@ def test_status_format_json_full():
|
||||
assert result == '{"status":0,"message":"OK","data_updated":"2010-02-07T20:20:03+00:00","software_version":"%s","database_version":"5.6"}' % (NOMINATIM_VERSION, )
|
||||
|
||||
|
||||
# SearchResult
|
||||
# DetailedResult
|
||||
|
||||
def test_search_details_minimal():
|
||||
search = napi.SearchResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0))
|
||||
search = napi.DetailedResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0))
|
||||
|
||||
result = api_impl.format_result(search, 'details-json', {})
|
||||
result = api_impl.format_result(search, 'json', {})
|
||||
|
||||
assert json.loads(result) == \
|
||||
{'category': 'place',
|
||||
@@ -83,8 +83,8 @@ def test_search_details_minimal():
|
||||
|
||||
|
||||
def test_search_details_full():
|
||||
import_date = dt.datetime(2010, 2, 7, 20, 20, 3, 0)
|
||||
search = napi.SearchResult(
|
||||
import_date = dt.datetime(2010, 2, 7, 20, 20, 3, 0, tzinfo=dt.timezone.utc)
|
||||
search = napi.DetailedResult(
|
||||
source_table=napi.SourceTable.PLACEX,
|
||||
category=('amenity', 'bank'),
|
||||
centroid=napi.Point(56.947, -87.44),
|
||||
@@ -106,7 +106,7 @@ def test_search_details_full():
|
||||
indexed_date = import_date
|
||||
)
|
||||
|
||||
result = api_impl.format_result(search, 'details-json', {})
|
||||
result = api_impl.format_result(search, 'json', {})
|
||||
|
||||
assert json.loads(result) == \
|
||||
{'place_id': 37563,
|
||||
@@ -140,12 +140,12 @@ def test_search_details_full():
|
||||
('ST_Polygon', True),
|
||||
('ST_MultiPolygon', True)])
|
||||
def test_search_details_no_geometry(gtype, isarea):
|
||||
search = napi.SearchResult(napi.SourceTable.PLACEX,
|
||||
search = napi.DetailedResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0),
|
||||
geometry={'type': gtype})
|
||||
|
||||
result = api_impl.format_result(search, 'details-json', {})
|
||||
result = api_impl.format_result(search, 'json', {})
|
||||
js = json.loads(result)
|
||||
|
||||
assert js['geometry'] == {'type': 'Point', 'coordinates': [1.0, 2.0]}
|
||||
@@ -153,12 +153,12 @@ def test_search_details_no_geometry(gtype, isarea):
|
||||
|
||||
|
||||
def test_search_details_with_geometry():
|
||||
search = napi.SearchResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0),
|
||||
geometry={'geojson': '{"type":"Point","coordinates":[56.947,-87.44]}'})
|
||||
search = napi.DetailedResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0),
|
||||
geometry={'geojson': '{"type":"Point","coordinates":[56.947,-87.44]}'})
|
||||
|
||||
result = api_impl.format_result(search, 'details-json', {})
|
||||
result = api_impl.format_result(search, 'json', {})
|
||||
js = json.loads(result)
|
||||
|
||||
assert js['geometry'] == {'type': 'Point', 'coordinates': [56.947, -87.44]}
|
||||
@@ -166,10 +166,10 @@ def test_search_details_with_geometry():
|
||||
|
||||
|
||||
def test_search_details_with_address_minimal():
|
||||
search = napi.SearchResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0),
|
||||
address_rows=[
|
||||
search = napi.DetailedResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0),
|
||||
address_rows=[
|
||||
napi.AddressLine(place_id=None,
|
||||
osm_object=None,
|
||||
category=('bnd', 'note'),
|
||||
@@ -180,9 +180,9 @@ def test_search_details_with_address_minimal():
|
||||
isaddress=False,
|
||||
rank_address=10,
|
||||
distance=0.0)
|
||||
])
|
||||
])
|
||||
|
||||
result = api_impl.format_result(search, 'details-json', {})
|
||||
result = api_impl.format_result(search, 'json', {})
|
||||
js = json.loads(result)
|
||||
|
||||
assert js['address'] == [{'localname': '',
|
||||
@@ -194,10 +194,10 @@ def test_search_details_with_address_minimal():
|
||||
|
||||
|
||||
def test_search_details_with_address_full():
|
||||
search = napi.SearchResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0),
|
||||
address_rows=[
|
||||
search = napi.DetailedResult(napi.SourceTable.PLACEX,
|
||||
('place', 'thing'),
|
||||
napi.Point(1.0, 2.0),
|
||||
address_rows=[
|
||||
napi.AddressLine(place_id=3498,
|
||||
osm_object=('R', 442),
|
||||
category=('bnd', 'note'),
|
||||
@@ -209,9 +209,9 @@ def test_search_details_with_address_full():
|
||||
isaddress=True,
|
||||
rank_address=10,
|
||||
distance=0.034)
|
||||
])
|
||||
])
|
||||
|
||||
result = api_impl.format_result(search, 'details-json', {})
|
||||
result = api_impl.format_result(search, 'json', {})
|
||||
js = json.loads(result)
|
||||
|
||||
assert js['address'] == [{'localname': 'Trespass',
|
||||
|
||||
84
test/python/api/test_results.py
Normal file
84
test/python/api/test_results.py
Normal file
@@ -0,0 +1,84 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#
|
||||
# This file is part of Nominatim. (https://nominatim.org)
|
||||
#
|
||||
# Copyright (C) 2023 by the Nominatim developer community.
|
||||
# For a full list of authors see the git log.
|
||||
"""
|
||||
Tests for result datatype helper functions.
|
||||
"""
|
||||
import struct
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
from nominatim.api import SourceTable, DetailedResult, Point
|
||||
import nominatim.api.results as nresults
|
||||
|
||||
class FakeCentroid:
|
||||
def __init__(self, x, y):
|
||||
self.data = struct.pack("=biidd", 1, 0x20000001, 4326,
|
||||
x, y)
|
||||
|
||||
class FakeRow:
|
||||
def __init__(self, **kwargs):
|
||||
for k, v in kwargs.items():
|
||||
setattr(self, k, v)
|
||||
self._mapping = kwargs
|
||||
|
||||
|
||||
def test_minimal_detailed_result():
|
||||
res = DetailedResult(SourceTable.PLACEX,
|
||||
('amenity', 'post_box'),
|
||||
Point(23.1, 0.5))
|
||||
|
||||
assert res.lon == 23.1
|
||||
assert res.lat == 0.5
|
||||
assert res.calculated_importance() == pytest.approx(0.0000001)
|
||||
|
||||
def test_detailed_result_custom_importance():
|
||||
res = DetailedResult(SourceTable.PLACEX,
|
||||
('amenity', 'post_box'),
|
||||
Point(23.1, 0.5),
|
||||
importance=0.4563)
|
||||
|
||||
assert res.calculated_importance() == 0.4563
|
||||
|
||||
|
||||
@pytest.mark.parametrize('func', (nresults.create_from_placex_row,
|
||||
nresults.create_from_osmline_row,
|
||||
nresults.create_from_tiger_row,
|
||||
nresults.create_from_postcode_row))
|
||||
def test_create_row_none(func):
|
||||
assert func(None, DetailedResult) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
|
||||
nresults.create_from_tiger_row))
|
||||
def test_create_row_with_housenumber(func):
|
||||
row = FakeRow(place_id = 2345, osm_id = 111, housenumber = 4,
|
||||
address = None, postcode = '99900', country_code = 'xd',
|
||||
centroid = FakeCentroid(0, 0))
|
||||
|
||||
res = func(row, DetailedResult)
|
||||
|
||||
assert res.housenumber == '4'
|
||||
assert res.extratags is None
|
||||
assert res.category == ('place', 'house')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('func', (nresults.create_from_osmline_row,
|
||||
nresults.create_from_tiger_row))
|
||||
def test_create_row_without_housenumber(func):
|
||||
row = FakeRow(place_id=2345, osm_id=111,
|
||||
startnumber=1, endnumber=11, step=2,
|
||||
address=None, postcode='99900', country_code='xd',
|
||||
centroid=FakeCentroid(0, 0))
|
||||
|
||||
res = func(row, DetailedResult)
|
||||
|
||||
assert res.housenumber is None
|
||||
assert res.extratags == {'startnumber': '1', 'endnumber': '11', 'step': '2'}
|
||||
assert res.category == ('place', 'houses')
|
||||
@@ -79,8 +79,8 @@ class TestCliDetailsCall:
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_status_mock(self, monkeypatch):
|
||||
result = napi.SearchResult(napi.SourceTable.PLACEX, ('place', 'thing'),
|
||||
napi.Point(1.0, -3.0))
|
||||
result = napi.DetailedResult(napi.SourceTable.PLACEX, ('place', 'thing'),
|
||||
napi.Point(1.0, -3.0))
|
||||
|
||||
monkeypatch.setattr(napi.NominatimAPI, 'lookup',
|
||||
lambda *args: result)
|
||||
|
||||
Reference in New Issue
Block a user