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