require_relative "test_helper" require "maptool/gpx_store" require "maptool/search" require "stringio" class SearchTest < Minitest::Test def setup @data_dir = Dir.mktmpdir @store = Maptool::GpxStore.new(File.join(@data_dir, "gpx")) 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]] } }, ] } @store.save(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/camps.geojson"]], layer.map { |r| r.values_at(:name, :source, :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_file_name_with_bbox assert_equal ["Kemp Košice"], search("voda").map { |r| r[:name] } track = search("camps").first assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox] 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, gpx_store: @store, 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