Library phase 1: SQLite item library with folders, migration of old files as layers, import split into items

This commit is contained in:
randy committed 2026-11-13 09:03:22 +01:00
1 parent 4216211a20
commit ad8b72c3d3
16 files changed
+897 -499

No files matched your search

+100 -167
View File
@@ -77,11 +77,11 @@ class AppTest < Minitest::Test
# --- auth guard ---
def test_write_routes_require_login
assert_equal 401, request.post("/api/gpx").status
assert_equal 401, request.post("/api/gpx/folder").status
assert_equal 401, request.delete("/api/gpx/file/x.gpx").status
assert_equal 401, request.delete("/api/gpx/folder/x").status
assert_equal 401, request.post("/api/gpx/move").status
assert_equal 401, request.post("/api/library/import").status
assert_equal 401, request.post("/api/library/folder").status
assert_equal 401, request.delete("/api/library/node/item:x").status
assert_equal 401, request.delete("/api/library/folder/x").status
assert_equal 401, request.post("/api/library/move").status
assert_equal 401, request.post("/api/plugins/test-plugin/run").status
assert_equal 401, request.post("/api/offline-tiles/build").status
assert_equal 401, request.post("/api/terrain-tiles/build").status
@@ -90,112 +90,83 @@ class AppTest < Minitest::Test
assert_equal 401, request.delete("/api/terrain-tiles").status
end
# --- GPX/FIT upload and the tree ---
# --- Tracks & Points library (lib/maptool/library.rb) ---
def test_gpx_upload_and_tree
content = File.read(fixture_path("sample.gpx"))
response = post_file("/api/gpx", filename: "ride.gpx", content: content, env: logged_in_env)
def test_gpx_import_makes_one_track_node_carrying_its_waypoint
response = upload("ride.gpx", File.read(fixture_path("sample.gpx")))
assert_equal 200, response.status
body = json_body(response)
assert_equal ["ride.gpx"], body["paths"]
assert_equal ["ride.gpx"], body["tree"]["children"].map { |c| c["name"] }
assert_equal 1, body["paths"].size
assert_match(/\Aitem:/, body["paths"].first)
node = body["tree"]["children"].first
assert_equal ["Morning Loop", %w[track waypoint]], node.values_at("name", "kinds")
features = json_body(request.get(node_url(node["path"])))["features"]
assert_equal %w[track waypoint], features.map { |f| f["properties"]["kind"] }
assert_operator features.first["properties"]["distance_meters"], :>, 0
assert(features.all? { |f| f["properties"]["id"] })
end
def test_gpx_file_returns_geojson_with_track_properties
content = File.read(fixture_path("sample.gpx"))
post_file("/api/gpx", filename: "ride.gpx", content: content, env: logged_in_env)
def test_importing_the_same_file_again_adds_nothing
first = json_body(upload("ride.gpx", File.read(fixture_path("sample.gpx"))))
second = json_body(upload("ride (1).gpx", File.read(fixture_path("sample.gpx"))))
response = request.get("/api/gpx/file/ride.gpx")
assert_equal 200, response.status
track = json_body(response)["features"].find { |f| f["properties"]["kind"] == "track" }
assert_operator track["properties"]["distance_meters"], :>, 0
assert_equal first["paths"], second["paths"]
assert_equal 1, second["tree"]["children"].size
end
def test_geojson_upload_is_served_back_unchanged
def test_geojson_points_land_in_the_folders_points_node
content = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"kind":"waypoint","name":"Spot"},"geometry":{"type":"Point","coordinates":[17.1,48.1]}}]}'
response = post_file("/api/gpx", filename: "spots.geojson", content: content, env: logged_in_env)
response = upload("spots.geojson", content, folder: "trips")
assert_equal 200, response.status
assert_equal ["spots.geojson"], json_body(response)["paths"]
file_response = request.get("/api/gpx/file/spots.geojson")
assert_equal 200, file_response.status
assert_equal "Spot", json_body(file_response)["features"][0]["properties"]["name"]
key = json_body(response)["paths"].first
assert_match(/\Apoints:/, key)
trips = json_body(response)["tree"]["children"].find { |c| c["name"] == "trips" }
assert_equal ["Points (1)"], trips["children"].map { |c| c["name"] }
assert_equal "Spot", json_body(request.get(node_url(key)))["features"][0]["properties"]["name"]
end
def test_download_of_a_converted_gpx_names_it_gpx_geojson
def test_downloads_need_login_and_serve_the_original_or_geojson
content = File.read(fixture_path("sample.gpx"))
post_file("/api/gpx", filename: "ride.gpx", content: content, env: logged_in_env)
key = json_body(upload("ride.gpx", content))["paths"].first
response = request.get("/api/gpx/file/ride.gpx?download=geojson", **logged_in_env)
assert_equal 200, response.status
assert_includes response.headers["Content-Disposition"], 'filename="ride.gpx.geojson"'
assert_equal 401, request.get("#{node_url(key)}?download=original").status
assert_equal 401, request.get("#{node_url(key)}?download=geojson").status
original = request.get("#{node_url(key)}?download=original", **logged_in_env)
assert_equal content, original.body
assert_includes original.headers["Content-Disposition"], 'filename="ride.gpx"'
geojson = request.get("#{node_url(key)}?download=geojson&name=Morning+Loop", **logged_in_env)
assert_includes geojson.headers["Content-Disposition"], 'filename="Morning Loop.geojson"'
end
def test_download_of_a_native_geojson_keeps_its_own_name
content = '{"type":"FeatureCollection","features":[]}'
post_file("/api/gpx", filename: "spots.geojson", content: content, env: logged_in_env)
response = request.get("/api/gpx/file/spots.geojson?download=geojson", **logged_in_env)
assert_equal 200, response.status
assert_includes response.headers["Content-Disposition"], 'filename="spots.geojson"'
end
def test_download_of_the_original_file_serves_it_unconverted
content = File.read(fixture_path("sample.gpx"))
post_file("/api/gpx", filename: "ride.gpx", content: content, env: logged_in_env)
response = request.get("/api/gpx/file/ride.gpx?download=original", **logged_in_env)
assert_equal 200, response.status
assert_includes response.headers["Content-Disposition"], 'filename="ride.gpx"'
assert_equal content, response.body
end
def test_download_requires_login
content = File.read(fixture_path("sample.gpx"))
post_file("/api/gpx", filename: "ride.gpx", content: content, env: logged_in_env)
assert_equal 401, request.get("/api/gpx/file/ride.gpx?download=geojson").status
assert_equal 401, request.get("/api/gpx/file/ride.gpx?download=original").status
# Plain viewing (no ?download) stays open to anyone.
assert_equal 200, request.get("/api/gpx/file/ride.gpx").status
end
def test_unparseable_gpx_returns_422
post_file("/api/gpx", filename: "bad.gpx", content: "<gpx><trk unclosed>", env: logged_in_env)
response = request.get("/api/gpx/file/bad.gpx")
assert_equal 422, response.status
def test_unparseable_gpx_is_rejected_at_import
assert_equal 422, upload("bad.gpx", "<gpx><trk unclosed>").status
assert_empty json_body(request.get("/api/library/tree"))["children"]
end
def test_fit_upload_is_parsed_the_same_as_gpx
fit_bytes = build_fit_file([[100_000_000, 200_000_000], [100_000_100, 200_000_100]])
response = post_file("/api/gpx", filename: "activity.fit", content: fit_bytes, env: logged_in_env)
key = json_body(upload("activity.fit", fit_bytes))["paths"].first
assert_equal 200, response.status
assert_equal ["activity.fit"], json_body(response)["paths"]
geojson = json_body(request.get("/api/gpx/file/activity.fit"))
geojson = json_body(request.get(node_url(key)))
assert_equal 2, geojson["features"][0]["geometry"]["coordinates"][0].size
end
def test_zip_upload_expands_into_its_track_entries_and_skips_the_rest
zip_bytes = build_zip(
"one.gpx" => File.read(fixture_path("sample.gpx")),
"two.fit" => build_fit_file([[100_000_000, 200_000_000]]),
"two.fit" => build_fit_file([[100_000_000, 200_000_000], [100_000_100, 200_000_100]]),
"readme.txt" => "not a track file",
)
response = post_file("/api/gpx?folder=imported", filename: "export.zip", content: zip_bytes,
content_type: "application/zip", env: logged_in_env)
response = upload("export.zip", zip_bytes, folder: "imported", content_type: "application/zip")
assert_equal 200, response.status
paths = json_body(response)["paths"]
assert_equal ["imported/one.gpx", "imported/two.fit"].sort, paths.sort
names = json_body(request.get("/api/gpx/tree"))["children"]
.find { |c| c["name"] == "imported" }["children"].map { |c| c["name"] }
refute_includes names, "readme.txt"
assert_equal 2, json_body(response)["paths"].size
imported = json_body(response)["tree"]["children"].find { |c| c["name"] == "imported" }
assert_equal 2, imported["children"].size
end
def test_zip_whose_contents_exceed_the_size_limit_is_rejected_before_saving_anything
@@ -203,10 +174,10 @@ class AppTest < Minitest::Test
original = Maptool::App::MAX_ZIP_UNCOMPRESSED
Maptool::App.send(:remove_const, :MAX_ZIP_UNCOMPRESSED)
Maptool::App.const_set(:MAX_ZIP_UNCOMPRESSED, 1024)
response = post_file("/api/gpx", filename: "bomb.zip", content: zip_bytes, content_type: "application/zip", env: logged_in_env)
response = upload("bomb.zip", zip_bytes, content_type: "application/zip")
assert_equal 413, response.status
assert_empty json_body(request.get("/api/gpx/tree"))["children"]
assert_empty json_body(request.get("/api/library/tree"))["children"]
ensure
Maptool::App.send(:remove_const, :MAX_ZIP_UNCOMPRESSED)
Maptool::App.const_set(:MAX_ZIP_UNCOMPRESSED, original)
@@ -226,29 +197,27 @@ class AppTest < Minitest::Test
end
def test_corrupt_zip_is_rejected
response = post_file("/api/gpx", filename: "bad.zip", content: "not a zip", content_type: "application/zip", env: logged_in_env)
assert_equal 400, response.status
assert_equal 400, upload("bad.zip", "not a zip", content_type: "application/zip").status
end
def test_folder_create_move_and_delete
content = File.read(fixture_path("sample.gpx"))
post_file("/api/gpx", filename: "ride.gpx", content: content, env: logged_in_env)
key = json_body(upload("ride.gpx", File.read(fixture_path("sample.gpx"))))["paths"].first
create = request.post("/api/gpx/folder?path=trips", **logged_in_env)
assert_equal 200, create.status
assert_equal 200, request.post("/api/library/folder?path=trips", **logged_in_env).status
move = request.post("/api/gpx/move?from=ride.gpx&to_folder=trips", **logged_in_env)
assert_equal 200, move.status
assert_equal "trips/ride.gpx", json_body(move)["path"]
move = request.post("/api/library/move", params: { key: key, to_folder: "trips" }, **logged_in_env)
assert_equal key, json_body(move)["path"]
trips = json_body(move)["tree"]["children"].find { |c| c["name"] == "trips" }
assert_equal ["Morning Loop"], trips["children"].map { |c| c["name"] }
delete = request.delete("/api/gpx/file/trips/ride.gpx", **logged_in_env)
delete = request.delete("/api/library/folder/trips", **logged_in_env)
assert_equal 200, delete.status
assert_empty json_body(delete)["tree"]["children"].find { |c| c["name"] == "trips" }["children"]
assert_empty json_body(delete)["tree"]["children"]
assert_equal 404, request.get(node_url(key)).status
end
def test_invalid_folder_path_is_rejected
response = request.post("/api/gpx/folder?path=..%2F..%2Fescape", **logged_in_env)
assert_equal 400, response.status
assert_equal 400, request.post("/api/library/folder?path=..%2F..%2Fescape", **logged_in_env).status
end
# --- Notes (hand-drawn points/areas, notes.js) ---
@@ -259,101 +228,56 @@ class AppTest < Minitest::Test
"properties" => { "name" => "Turning area", "category" => "Note" },
"geometry" => { "type" => "Polygon", "coordinates" => [[[17.1, 48.1], [17.2, 48.1], [17.2, 48.2], [17.1, 48.1]]] },
)
response = request.post("/api/gpx/notes", params: { feature: polygon, name: "Areas" }, **logged_in_env)
response = request.post("/api/library/notes", params: { feature: polygon, folder: "Areas" }, **logged_in_env)
assert_equal 200, response.status
geometry = json_body(request.get("/api/gpx/file/Areas.geojson"))["features"][0]["geometry"]
geometry = json_body(request.get(node_url(json_body(response)["path"])))["features"][0]["geometry"]
assert_equal "Polygon", geometry["type"]
end
def test_notes_requires_login
assert_equal 401, request.post("/api/gpx/notes", params: { feature: sample_note_feature, name: "Notes" }).status
assert_equal 401, request.post("/api/library/notes", params: { feature: sample_note_feature, folder: "Notes" }).status
end
def test_notes_creates_a_new_layer_file
response = request.post("/api/gpx/notes", params: { feature: sample_note_feature, name: "My Notes" }, **logged_in_env)
def test_notes_go_into_one_points_node_per_folder
first = json_body(request.post("/api/library/notes", params: { feature: sample_note_feature("Turning spot"), folder: "My Notes" }, **logged_in_env))
second = json_body(request.post("/api/library/notes", params: { feature: sample_note_feature("Narrow gate"), folder: "My Notes" }, **logged_in_env))
assert_equal 200, response.status
body = json_body(response)
assert_equal "My Notes.geojson", body["path"]
assert_equal ["My Notes.geojson"], body["tree"]["children"].map { |c| c["name"] }
assert_equal first["path"], second["path"]
features = json_body(request.get(node_url(first["path"])))["features"]
assert_equal ["Narrow gate", "Turning spot"], features.map { |f| f["properties"]["name"] }.sort
end
def test_notes_appends_to_an_existing_layer_file_instead_of_disambiguating
first = json_body(request.post("/api/gpx/notes", params: { feature: sample_note_feature("Turning spot"), name: "My Notes" }, **logged_in_env))
second = request.post("/api/gpx/notes", params: { feature: sample_note_feature("Narrow gate"), path: first["path"] }, **logged_in_env)
assert_equal 200, second.status
assert_equal first["path"], json_body(second)["path"]
features = json_body(request.get("/api/gpx/file/#{Rack::Utils.escape_path(first["path"])}"))["features"]
assert_equal ["Turning spot", "Narrow gate"], features.map { |f| f["properties"]["name"] }
def test_notes_rejects_malformed_or_geometryless_features
assert_equal 400, request.post("/api/library/notes", params: { feature: "not json" }, **logged_in_env).status
assert_equal 400, request.post("/api/library/notes", params: { feature: '{"type":"Feature","properties":{}}' }, **logged_in_env).status
end
def test_notes_rejects_malformed_feature_json
response = request.post("/api/gpx/notes", params: { feature: "not json", name: "Notes" }, **logged_in_env)
assert_equal 400, response.status
end
def test_notes_rejects_a_feature_with_no_geometry
response = request.post("/api/gpx/notes", params: { feature: '{"type":"Feature","properties":{}}', name: "Notes" }, **logged_in_env)
assert_equal 400, response.status
end
def test_notes_to_a_missing_path_is_404
response = request.post("/api/gpx/notes", params: { feature: sample_note_feature, path: "nope.geojson" }, **logged_in_env)
assert_equal 404, response.status
end
def test_notes_update_requires_login
assert_equal 401, request.put("/api/gpx/notes", params: { feature: sample_note_feature, path: "x.geojson", id: "a" }).status
def test_notes_update_and_delete_need_login
assert_equal 401, request.put("/api/library/notes", params: { feature: sample_note_feature, path: "points:root", id: "a" }).status
assert_equal 401, request.delete("/api/library/notes", params: { path: "points:root", id: "a" }).status
end
def test_notes_update_edits_the_matching_note
created = json_body(request.post("/api/gpx/notes", params: { feature: sample_note_feature("Original", id: "a"), name: "My Notes" }, **logged_in_env))
created = json_body(request.post("/api/library/notes", params: { feature: sample_note_feature("Original", id: "a") }, **logged_in_env))
response = request.put(
"/api/gpx/notes",
params: { feature: sample_note_feature("Edited", id: "a"), path: created["path"], id: "a" },
**logged_in_env,
)
response = request.put("/api/library/notes", params: { feature: sample_note_feature("Edited", id: "a"), path: created["path"], id: "a" }, **logged_in_env)
assert_equal 200, response.status
features = json_body(request.get(node_url(created["path"])))["features"]
assert_equal [%w[Edited a]], features.map { |f| f["properties"].values_at("name", "id") }
features = json_body(request.get("/api/gpx/file/#{Rack::Utils.escape_path(created["path"])}"))["features"]
assert_equal ["Edited"], features.map { |f| f["properties"]["name"] }
end
def test_notes_update_missing_note_is_404
created = json_body(request.post("/api/gpx/notes", params: { feature: sample_note_feature("Original", id: "a"), name: "My Notes" }, **logged_in_env))
response = request.put(
"/api/gpx/notes",
params: { feature: sample_note_feature("Edited", id: "nope"), path: created["path"], id: "nope" },
**logged_in_env,
)
assert_equal 404, response.status
end
def test_notes_delete_requires_login
assert_equal 401, request.delete("/api/gpx/notes", params: { path: "x.geojson", id: "a" }).status
missing = request.put("/api/library/notes", params: { feature: sample_note_feature, path: created["path"], id: "nope" }, **logged_in_env)
assert_equal 404, missing.status
end
def test_notes_delete_removes_the_matching_note
created = json_body(request.post("/api/gpx/notes", params: { feature: sample_note_feature("Turning spot", id: "a"), name: "My Notes" }, **logged_in_env))
request.post("/api/gpx/notes", params: { feature: sample_note_feature("Narrow gate", id: "b"), path: created["path"] }, **logged_in_env)
created = json_body(request.post("/api/library/notes", params: { feature: sample_note_feature("Turning spot", id: "a") }, **logged_in_env))
request.post("/api/library/notes", params: { feature: sample_note_feature("Narrow gate", id: "b") }, **logged_in_env)
response = request.delete("/api/gpx/notes", params: { path: created["path"], id: "a" }, **logged_in_env)
assert_equal 200, response.status
features = json_body(request.get("/api/gpx/file/#{Rack::Utils.escape_path(created["path"])}"))["features"]
assert_equal 200, request.delete("/api/library/notes", params: { path: created["path"], id: "a" }, **logged_in_env).status
features = json_body(request.get(node_url(created["path"])))["features"]
assert_equal ["Narrow gate"], features.map { |f| f["properties"]["name"] }
end
def test_notes_delete_missing_note_is_404
created = json_body(request.post("/api/gpx/notes", params: { feature: sample_note_feature("Turning spot", id: "a"), name: "My Notes" }, **logged_in_env))
response = request.delete("/api/gpx/notes", params: { path: created["path"], id: "nope" }, **logged_in_env)
assert_equal 404, response.status
assert_equal 404, request.delete("/api/library/notes", params: { path: created["path"], id: "a" }, **logged_in_env).status
end
# --- Import plugins (lib/maptool/plugins.rb) ---
@@ -364,7 +288,7 @@ class AppTest < Minitest::Test
assert_equal [{ "name" => "test-plugin", "label" => "Test plugin" }], json_body(response)
end
def test_plugins_run_saves_the_output_as_a_new_geojson_file
def test_plugins_run_imports_the_output_into_a_folder_of_its_own
response = request.post(
"/api/plugins/test-plugin/run",
params: { south: "48.0", west: "17.0", north: "48.1", east: "17.1" },
@@ -372,9 +296,9 @@ class AppTest < Minitest::Test
)
assert_equal 200, response.status
body = json_body(response)
assert_match(/\Atest-plugin_48\.05_17\.05_\d{6}_\d{4}\.geojson\z/, body["path"])
assert_match(/\Atest-plugin_48\.05_17\.05_\d{6}_\d{4}\z/, body["tree"]["children"].first["name"])
saved = json_body(request.get("/api/gpx/file/#{body["path"]}"))
saved = json_body(request.get(node_url(body["path"])))
assert_equal "Test POI", saved["features"][0]["properties"]["name"]
end
@@ -542,6 +466,15 @@ class AppTest < Minitest::Test
private
def upload(filename, content, folder: "", content_type: "application/octet-stream")
post_file("/api/library/import?#{URI.encode_www_form(folder: folder)}", filename: filename, content: content,
content_type: content_type, env: logged_in_env)
end
def node_url(key)
"/api/library/node/#{Rack::Utils.escape_path(key)}"
end
def sample_note_feature(name = "Turning spot", id: nil)
JSON.generate(
"type" => "Feature",
+1
View File
@@ -65,6 +65,7 @@ module AppTestHelper
# the next one, since minitest runs a class's tests in one process.
def teardown
FileUtils.rm_rf(Dir.glob(File.join(Maptool::App::DATA_DIR, "gpx", "*")))
Maptool::App::LIBRARY.send(:connect) { |db| db.execute_batch("DELETE FROM items; DELETE FROM folders") }
File.write(File.join(Maptool::App::DATA_DIR, "livetrack.json"), "[]")
FileUtils.rm_f(File.join(Maptool::App::DATA_DIR, "offline_regions.json"))
Maptool::PmtilesBuilder.reset!
+98
View File
@@ -0,0 +1,98 @@
require_relative "test_helper"
require "maptool/library"
require "stringio"
class LibraryTest < Minitest::Test
def setup
@dir = Dir.mktmpdir
@store = Maptool::GpxStore.new(File.join(@dir, "gpx"))
end
def teardown
FileUtils.remove_entry(@dir)
end
def test_a_fresh_library_migrates_each_old_file_into_a_subfolder_of_its_own
@store.save(folder: "logs", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx")))
@store.append_feature(feature: note("Gate", "n1"), folder: "", name: "My Notes")
library = open_library
tree = library.tree
assert_equal ["logs", "My Notes"], tree["children"].map { |c| c["name"] }
ride = tree["children"][0]["children"].first
assert_equal ["ride", "folder"], ride.values_at("name", "type")
layer = ride["children"].first
assert_equal ["ride.gpx", %w[waypoint track]], layer.values_at("name", "kinds")
assert_equal %w[waypoint track], JSON.parse(library.geojson(layer["path"]))["features"].map { |f| f["properties"]["kind"] }
# Running it again (or reopening) doesn't duplicate anything.
library.migrate
assert_equal tree, open_library.tree
end
def test_a_layers_notes_are_still_edited_in_its_old_file
@store.append_feature(feature: note("Gate", "n1"), folder: "", name: "My Notes")
library = open_library
key = library.tree["children"].first["children"].first["path"]
library.update_feature(key, "n1", note("Wide gate", "n1"))
assert_equal ["Wide gate"], JSON.parse(library.geojson(key))["features"].map { |f| f["properties"]["name"] }
library.delete_feature(key, "n1")
assert_empty JSON.parse(library.geojson(key))["features"]
end
def test_every_write_bumps_rev_and_deletes_are_tombstones
library = open_library
before = library.rev
key = library.add_note(note("Gate"), "Van")
id = JSON.parse(library.geojson(key))["features"].first["properties"]["id"]
library.delete_feature(key, id)
assert_operator library.rev, :>, before
row = library.send(:connect) { |db| db.get_first_row("SELECT deleted_at, rev FROM items WHERE id = ?", [id]) }
refute_nil row[0]
assert_equal library.rev, row[1]
assert_empty JSON.parse(library.geojson(key))["features"]
end
def test_the_same_feature_is_deduped_per_folder_only
library = open_library
library.add_note(note("Gate"), "a")
library.add_note(note("Gate"), "a")
library.add_note(note("Gate"), "b")
counts = library.tree["children"].map { |folder| folder["children"].first["name"] }
assert_equal ["Points (1)", "Points (1)"], counts
end
def test_moving_a_folders_points_returns_their_new_key
library = open_library
key = library.add_note(note("Gate"), "a")
moved = library.move_node(key, "b/c")
refute_equal key, moved
assert_equal ["Gate"], JSON.parse(library.geojson(moved))["features"].map { |f| f["properties"]["name"] }
assert_raises(Maptool::Library::NotFound) { library.move_node(key, "b") }
end
def test_deleting_a_folder_takes_its_subfolders_and_items
library = open_library
key = library.add_note(note("Gate"), "a/b")
library.delete_folder("a")
assert_empty library.tree["children"]
assert_empty JSON.parse(library.geojson(key))["features"]
assert_raises(Maptool::Library::InvalidPath) { library.create_folder("../x") }
end
private
def open_library
Maptool::Library.new(File.join(@dir, "library.sqlite"), gpx_store: @store, sources_dir: File.join(@dir, "sources"))
end
def note(name, id = nil)
{ "type" => "Feature", "properties" => { "name" => name, "category" => "Note", "id" => id }.compact,
"geometry" => { "type" => "Point", "coordinates" => [17.1, 48.1] } }
end
end
+11 -7
View File
@@ -1,18 +1,21 @@
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
@store = Maptool::GpxStore.new(File.join(@data_dir, "gpx"))
@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]] } },
] }
@store.save(folder: "trips", filename: "camps.geojson", io: StringIO.new(JSON.generate(camp)))
@library.import(folder: "trips", filename: "camps.geojson", io: StringIO.new(JSON.generate(camp)))
trails = File.join(@data_dir, "hiking_trails")
FileUtils.mkdir_p(trails)
@@ -22,16 +25,17 @@ class SearchTest < Minitest::Test
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) }
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_file_name_with_bbox
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("camps").first
track = search("trips track").first
assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox]
end
@@ -39,7 +43,7 @@ class SearchTest < Minitest::Test
camps = { "type" => "FeatureCollection", "features" => [
point("Kemp far", 21.0, 48.7), point("Kemp near", 17.1, 48.15),
] }
@store.save(folder: "", filename: "more.geojson", io: StringIO.new(JSON.generate(camps)))
@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] }
@@ -54,7 +58,7 @@ class SearchTest < Minitest::Test
private
def search(text, **opts)
Maptool::Search.query(text, gpx_store: @store, data_dir: @data_dir, **opts)
Maptool::Search.query(text, library: @library, data_dir: @data_dir, **opts)
end
def point(name, lng, lat, description = nil)