Named buildings and land-use areas are skipped unless they're also a shop/amenity/etc. A second, unshown FTS column holds the name without apostrophes and the brand tag, so "mcdonalds" finds McDonald's and "lidl" finds "Lidl Petržalka". The index records a schema version; rake places:index rebuilds older ones by itself.
95 lines
4.6 KiB
Ruby
95 lines
4.6 KiB
Ruby
require_relative "test_helper"
|
|
require "maptool/places"
|
|
require "maptool/search"
|
|
require "stringio"
|
|
|
|
# No real osmium — import is fed canned `osmium export -f geojsonseq`
|
|
# lines (same split HikingTrailsTest makes around parse_opl).
|
|
class PlacesTest < Minitest::Test
|
|
def setup
|
|
@dir = Dir.mktmpdir
|
|
@db = File.join(@dir, "SK.sqlite")
|
|
lines = [
|
|
feature("Point", [17.107, 48.148], "name" => "Bratislava", "place" => "city"),
|
|
feature("Point", [17.20, 48.30], "name" => "Bratislavská reštaurácia", "amenity" => "restaurant"),
|
|
feature("Point", [21.26, 48.72], "name" => "Hlavná", "place" => "square"),
|
|
# two segments of one street a few hundred metres apart -> one row
|
|
feature("LineString", [[17.10, 48.14], [17.101, 48.141], [17.102, 48.142]], "name" => "Hlavná", "highway" => "residential"),
|
|
feature("LineString", [[17.103, 48.143], [17.104, 48.144]], "name" => "Hlavná", "highway" => "residential"),
|
|
feature("Polygon", [[[19.0, 48.0], [19.2, 48.0], [19.2, 48.2], [19.0, 48.0]]], "name" => "Košické jazero", "natural" => "water"),
|
|
feature("Point", [18.0, 48.0], "highway" => "crossing"), # no name -> skipped
|
|
feature("Polygon", [[[17.0, 48.0], [17.01, 48.0], [17.01, 48.01], [17.0, 48.0]]], "name" => "Bytovka Lidl", "building" => "yes"),
|
|
feature("Polygon", [[[17.0, 48.0], [17.01, 48.0], [17.01, 48.01], [17.0, 48.0]]], "name" => "Družstvo", "landuse" => "farmland"),
|
|
feature("Polygon", [[[17.2, 48.1], [17.21, 48.1], [17.21, 48.11], [17.2, 48.1]]],
|
|
"name" => "Lidl Petržalka", "brand" => "Lidl", "shop" => "supermarket", "building" => "retail"),
|
|
feature("Point", [17.15, 48.15], "name" => "McDonald's", "amenity" => "fast_food", "brand" => "McDonald's"),
|
|
"not json",
|
|
]
|
|
Maptool::Places.import(StringIO.new(lines.join("\n")), @db)
|
|
end
|
|
|
|
def teardown
|
|
FileUtils.remove_entry(@dir)
|
|
end
|
|
|
|
def test_prefix_and_diacritics_insensitive_with_more_important_kinds_first
|
|
results = search("bratisl")
|
|
assert_equal ["Bratislava", "Bratislavská reštaurácia"], results.map { |r| r[:name] }
|
|
assert_equal ["city", "restaurant"], results.map { |r| r[:kind] }
|
|
|
|
assert_equal ["Košické jazero"], search("kosicke").map { |r| r[:name] }
|
|
assert_equal [19.0, 48.0, 19.2, 48.2], search("jazero").first[:bbox]
|
|
end
|
|
|
|
def test_street_segments_are_merged_and_nearer_matches_come_first
|
|
assert_equal ["square", "street"], search("hlavna", center: [48.7, 21.2]).map { |r| r[:kind] }
|
|
assert_equal ["street", "square"], search("hlavna", center: [48.14, 17.1]).map { |r| r[:kind] }
|
|
street = search("hlavna").find { |r| r[:kind] == "street" }
|
|
assert_equal [17.10, 48.14, 17.102, 48.142], street[:bbox]
|
|
assert_equal 2, search("hlavna").size
|
|
end
|
|
|
|
def test_a_city_counts_as_closer_than_it_is_but_a_shop_next_door_still_wins
|
|
# restaurant ~18 km from Bratislava's center point; from right next to
|
|
# the restaurant it wins, from halfway the city does (weighted x0.01)
|
|
assert_equal "Bratislavská reštaurácia", search("bratisl", center: [48.30, 17.20]).first[:name]
|
|
assert_equal "Bratislava", search("bratisl", center: [48.22, 17.15]).first[:name]
|
|
end
|
|
|
|
def test_all_words_must_match_and_unnamed_or_broken_lines_are_skipped
|
|
assert_equal ["Bratislavská reštaurácia"], search("bratislavska restau").map { |r| r[:name] }
|
|
assert_empty search("crossing")
|
|
end
|
|
|
|
def test_buildings_and_land_use_are_dropped_but_a_shop_in_a_building_is_kept
|
|
assert_empty search("bytovka")
|
|
assert_empty search("druzstvo")
|
|
assert_equal [["Lidl Petržalka", "supermarket"]], search("lidl").map { |r| r.values_at(:name, :kind) }
|
|
end
|
|
|
|
def test_apostrophes_and_brand_are_searchable
|
|
assert_equal ["McDonald's"], search("mcdonalds").map { |r| r[:name] }
|
|
assert_equal ["McDonald's"], search("McDonald's").map { |r| r[:name] }
|
|
assert_equal ["McDonald's"], search("mcdonald").map { |r| r[:name] }
|
|
assert_equal ["Lidl Petržalka"], search("lidl").map { |r| r[:name] } # also via brand
|
|
end
|
|
|
|
def test_an_index_from_an_older_schema_is_not_current
|
|
assert Maptool::Places.current?(@db)
|
|
SQLite3::Database.new(@db) { |db| db.execute("PRAGMA user_version = 1") }
|
|
refute Maptool::Places.current?(@db)
|
|
refute Maptool::Places.current?(File.join(@dir, "missing.sqlite"))
|
|
end
|
|
|
|
private
|
|
|
|
def search(text, **opts)
|
|
words = Maptool::Search.normalize(text).split
|
|
Maptool::Places.search(@db, words, **opts).map { |r| r.except(:rank) }
|
|
end
|
|
|
|
def feature(type, coordinates, tags)
|
|
JSON.generate("type" => "Feature", "geometry" => { "type" => type, "coordinates" => coordinates }, "properties" => tags)
|
|
end
|
|
end
|