Files
maptool/test/test_library.rb
T

159 lines
7.0 KiB
Ruby

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
def test_renaming_and_recoloring_a_track_and_a_layer
@store.save(folder: "", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx")))
library = open_library
layer = library.tree["children"].first["children"].first["path"]
track = library.import(folder: "t", filename: "ride.gpx", io: File.open(fixture_path("sample.gpx"))).first
library.update_node(track, name: "Morning ride", color: "#00ff00")
library.update_node(layer, color: "#0000ff")
nodes = library.tree["children"].flat_map { |f| f["children"].flat_map { |c| c["children"] || [c] } }
assert_includes nodes.map { |n| n.values_at("name", "color") }, ["Morning ride", "#00ff00"]
lines = JSON.parse(library.geojson(layer))["features"].reject { |f| f["geometry"]["type"] == "Point" }
assert_equal ["#0000ff"], lines.map { |f| f["properties"]["color"] }.uniq
library.update_node(track, color: "")
assert_nil JSON.parse(library.geojson(track))["features"].first["properties"]["color"]
assert_raises(Maptool::Library::InvalidValue) { library.update_node(track, color: "red") }
end
def test_splitting_a_layer_turns_it_into_items_and_keeps_note_ids
@store.append_feature(feature: note("Gate", "n1"), folder: "", name: "My Notes")
library = open_library
folder = library.tree["children"].first
key = folder["children"].first["path"]
keys = library.split_layer(key)
assert_equal ["Points (1)"], library.tree["children"].first["children"].map { |c| c["name"] }
assert_equal ["n1"], JSON.parse(library.geojson(keys.first))["features"].map { |f| f["properties"]["id"] }
assert_equal ["My Notes.geojson"], Dir.glob("*/*", base: File.join(@dir, "sources")).map { |f| File.basename(f) }
assert_raises(Maptool::Library::NotFound) { library.geojson(key) }
end
def test_renaming_and_moving_folders
library = open_library
key = library.add_note(note("Gate"), "a/b")
library.create_folder("c")
assert_equal "a/x", library.rename_folder("a/b", "x")
assert_equal "c/x", library.move_folder("a/x", "c")
assert_equal ["a", "c"], library.tree["children"].map { |f| f["name"] }
assert_equal ["Gate"], JSON.parse(library.geojson(key))["features"].map { |f| f["properties"]["name"] }
assert_raises(Maptool::Library::InvalidPath) { library.move_folder("c", "c/x") }
library.create_folder("a/x")
assert_raises(Maptool::Library::InvalidValue) { library.move_folder("c/x", "a") }
end
def test_a_saved_route_is_a_route_node_and_editing_replaces_it
route = { "type" => "Feature", "geometry" => { "type" => "LineString", "coordinates" => [[17.1, 48.1], [17.2, 48.2]] },
"properties" => { "name" => "To the lake", "kind" => "route", "via" => [[17.1, 48.1], [17.2, 48.2]] } }
library = open_library
key = library.add_note(route, "Routes")
node = library.tree["children"].first["children"].first
assert_equal [key, ["route"]], node.values_at("path", "kinds")
id = JSON.parse(library.geojson(key))["features"].first["properties"]["id"]
edited = route.merge("properties" => route["properties"].merge("name" => "Lake, scenic", "via" => [[17.1, 48.1], [17.15, 48.3], [17.2, 48.2]]))
library.update_feature(key, id, edited)
features = JSON.parse(library.geojson(key))["features"]
assert_equal [["Lake, scenic", 3]], features.map { |f| [f["properties"]["name"], f["properties"]["via"].size] }
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