Files
maptool/test/test_search.rb
T

69 lines
2.7 KiB
Ruby

require_relative "test_helper"
require "maptool/gpx_store"
require "maptool/library"
require "maptool/search"
require "stringio"
class SearchTest < Minitest::Test
def setup
@data_dir = Dir.mktmpdir
@library = Maptool::Library.new(File.join(@data_dir, "library.sqlite"),
gpx_store: Maptool::GpxStore.new(File.join(@data_dir, "gpx")),
sources_dir: File.join(@data_dir, "sources"))
camp = { "type" => "FeatureCollection", "features" => [
point("Kemp Košice", 21.26, 48.72, "pitná voda"),
{ "type" => "Feature", "properties" => { "kind" => "track" },
"geometry" => { "type" => "LineString", "coordinates" => [[17.0, 48.0, 150], [18.0, 49.0, 300]] } },
] }
@library.import(folder: "trips", filename: "camps.geojson", io: StringIO.new(JSON.generate(camp)))
trails = File.join(@data_dir, "hiking_trails")
FileUtils.mkdir_p(trails)
File.write(File.join(trails, "SK.geojson"),
JSON.generate("type" => "FeatureCollection", "features" => [point("Cesta hrdinov SNP", 19.0, 48.8)]))
end
def test_finds_across_sources_ignoring_case_and_diacritics_and_says_where
layer = search("kosice")
assert_equal [["Kemp Košice", "layers", "trips / Track"]], layer.map { |r| r.values_at(:name, :source, :source_label) }
assert_match(/\Aitem:/, layer.first[:ref])
trail = search("snp hrdinov").first
assert_equal ["hiking_trails", "SK", "Hiking trails SK"], trail.values_at(:source, :ref, :source_label)
end
def test_matches_description_and_falls_back_to_node_label_with_bbox
assert_equal ["Kemp Košice"], search("voda").map { |r| r[:name] }
track = search("trips track").first
assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox]
end
def test_with_a_map_center_results_come_nearest_first_with_their_distance
camps = { "type" => "FeatureCollection", "features" => [
point("Kemp far", 21.0, 48.7), point("Kemp near", 17.1, 48.15),
] }
@library.import(folder: "", filename: "more.geojson", io: StringIO.new(JSON.generate(camps)))
results = search("kemp", center: [48.14, 17.10])
assert_equal ["Kemp near", "Kemp far", "Kemp Košice"], results.map { |r| r[:name] }
assert_in_delta 1.3, results.first[:distance_km], 0.2
end
def test_source_filter_and_blank_query
assert_empty search("snp", sources: ["layers"])
assert_empty search(" ")
end
private
def search(text, **opts)
Maptool::Search.query(text, library: @library, data_dir: @data_dir, **opts)
end
def point(name, lng, lat, description = nil)
{ "type" => "Feature", "geometry" => { "type" => "Point", "coordinates" => [lng, lat] },
"properties" => { "name" => name, "description" => description }.compact }
end
end