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 lookup API call.
 
  12 import nominatim.api as napi
 
  14 def test_lookup_empty_list(apiobj):
 
  15     assert apiobj.api.lookup([]) == []
 
  18 def test_lookup_non_existing(apiobj):
 
  19     assert apiobj.api.lookup((napi.PlaceID(332), napi.OsmID('W', 4),
 
  20                               napi.OsmID('W', 4, 'highway'))) == []
 
  23 @pytest.mark.parametrize('idobj', (napi.PlaceID(332), napi.OsmID('W', 4),
 
  24                                    napi.OsmID('W', 4, 'highway')))
 
  25 def test_lookup_single_placex(apiobj, idobj):
 
  26     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
 
  27                      class_='highway', type='residential',
 
  28                      name={'name': 'Road'}, address={'city': 'Barrow'},
 
  29                      extratags={'surface': 'paved'},
 
  30                      parent_place_id=34, linked_place_id=55,
 
  31                      admin_level=15, country_code='gb',
 
  33                      postcode='34425', wikipedia='en:Faa',
 
  34                      rank_search=27, rank_address=26,
 
  37                      geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
 
  39     result = apiobj.api.lookup([idobj])
 
  41     assert len(result) == 1
 
  45     assert result.source_table.name == 'PLACEX'
 
  46     assert result.category == ('highway', 'residential')
 
  47     assert result.centroid == (pytest.approx(23.0), pytest.approx(34.0))
 
  49     assert result.place_id == 332
 
  50     assert result.osm_object == ('W', 4)
 
  52     assert result.names == {'name': 'Road'}
 
  53     assert result.address == {'city': 'Barrow'}
 
  54     assert result.extratags == {'surface': 'paved'}
 
  56     assert result.housenumber == '4'
 
  57     assert result.postcode == '34425'
 
  58     assert result.wikipedia == 'en:Faa'
 
  60     assert result.rank_search == 27
 
  61     assert result.rank_address == 26
 
  62     assert result.importance == pytest.approx(0.01)
 
  64     assert result.country_code == 'gb'
 
  66     assert result.address_rows is None
 
  67     assert result.linked_rows is None
 
  68     assert result.parented_rows is None
 
  69     assert result.name_keywords is None
 
  70     assert result.address_keywords is None
 
  72     assert result.geometry == {}
 
  75 def test_lookup_multiple_places(apiobj):
 
  76     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
 
  77                      class_='highway', type='residential',
 
  78                      name={'name': 'Road'}, address={'city': 'Barrow'},
 
  79                      extratags={'surface': 'paved'},
 
  80                      parent_place_id=34, linked_place_id=55,
 
  81                      admin_level=15, country_code='gb',
 
  83                      postcode='34425', wikipedia='en:Faa',
 
  84                      rank_search=27, rank_address=26,
 
  87                      geometry='LINESTRING(23 34, 23.1 34, 23.1 34.1, 23 34)')
 
  88     apiobj.add_osmline(place_id=4924, osm_id=9928,
 
  90                        startnumber=1, endnumber=4, step=1,
 
  91                        country_code='gb', postcode='34425',
 
  92                        address={'city': 'Big'},
 
  93                        geometry='LINESTRING(23 34, 23 35)')
 
  96     result = apiobj.api.lookup((napi.OsmID('W', 1),
 
  98                                 napi.OsmID('W', 9928)))
 
 100     assert len(result) == 2
 
 102     assert set(r.place_id for r in result) == {332, 4924}
 
 105 @pytest.mark.parametrize('gtype', list(napi.GeometryFormat))
 
 106 def test_simple_place_with_geometry(apiobj, gtype):
 
 107     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
 
 108                      class_='highway', type='residential',
 
 109                      name={'name': 'Road'}, address={'city': 'Barrow'},
 
 110                      extratags={'surface': 'paved'},
 
 111                      parent_place_id=34, linked_place_id=55,
 
 112                      admin_level=15, country_code='gb',
 
 114                      postcode='34425', wikipedia='en:Faa',
 
 115                      rank_search=27, rank_address=26,
 
 118                      geometry='POLYGON((23 34, 23.1 34, 23.1 34.1, 23 34))')
 
 120     result = apiobj.api.lookup([napi.OsmID('W', 4)],
 
 121                                geometry_output=gtype)
 
 123     assert len(result) == 1
 
 124     assert result[0].place_id == 332
 
 126     if gtype == napi.GeometryFormat.NONE:
 
 127         assert list(result[0].geometry.keys()) == []
 
 129         assert list(result[0].geometry.keys()) == [gtype.name.lower()]
 
 132 def test_simple_place_with_geometry_simplified(apiobj):
 
 133     apiobj.add_placex(place_id=332, osm_type='W', osm_id=4,
 
 134                      class_='highway', type='residential',
 
 135                      name={'name': 'Road'}, address={'city': 'Barrow'},
 
 136                      extratags={'surface': 'paved'},
 
 137                      parent_place_id=34, linked_place_id=55,
 
 138                      admin_level=15, country_code='gb',
 
 140                      postcode='34425', wikipedia='en:Faa',
 
 141                      rank_search=27, rank_address=26,
 
 144                      geometry='POLYGON((23 34, 22.999 34, 23.1 34, 23.1 34.1, 23 34))')
 
 146     result = apiobj.api.lookup([napi.OsmID('W', 4)],
 
 147                                geometry_output=napi.GeometryFormat.TEXT,
 
 148                                geometry_simplification=0.1)
 
 150     assert len(result) == 1
 
 151     assert result[0].place_id == 332
 
 152     assert result[0].geometry == {'text': 'POLYGON((23 34,23.1 34,23.1 34.1,23 34))'}