Files
maptool/test/test_trip.rb
T

39 lines
1.9 KiB
Ruby

require_relative "test_helper"
require "tmpdir"
require_relative "../lib/maptool/trip"
class TripTest < Minitest::Test
def test_an_update_routes_bumps_the_version_and_survives_a_restart
Dir.mktmpdir do |dir|
path = File.join(dir, "trip.json")
store = Maptool::TripStore.new(path)
assert_equal 0, store.get[:version]
points = [{ lngLat: [17.1, 48.1], name: "Home" }, { lngLat: [17.2, 48.2] }]
calls = []
router = ->(locations, vehicle, avoid) { calls << [locations, vehicle, avoid]; { type: "Feature", km: 1 } }
trip = store.update(points: points, vehicle: "camper", avoid: { tolls: true }, editing: nil, &router)
assert_equal [1, { type: "Feature", km: 1 }], trip.values_at(:version, :route)
assert_equal [[[[17.1, 48.1], [17.2, 48.2]], "camper", { tolls: true }]], calls
# only a name/nav changed: no routing, the route stays
points[1] = { lngLat: [17.2, 48.2], name: "Camp", nav: true }
trip = store.update(points: points, vehicle: "camper", avoid: { tolls: true }, editing: nil, &router)
assert_equal [2, 1, "Camp"], [trip[:version], calls.size, trip[:points][1][:name]]
reloaded = Maptool::TripStore.new(path).get
assert_equal [2, { type: "Feature", km: 1 }, "Camp"], [reloaded[:version], reloaded[:route], reloaded[:points][1][:name]]
end
end
def test_a_routing_error_is_kept_with_the_points_and_tried_again_next_time
Dir.mktmpdir do |dir|
store = Maptool::TripStore.new(File.join(dir, "trip.json"))
points = [{ lngLat: [1, 2] }, { lngLat: [3, 4] }]
trip = store.update(points: points, vehicle: nil, avoid: {}, editing: nil) { raise Maptool::Routing::RouteError, "no route" }
assert_equal [nil, "no route", 2], [trip[:route], trip[:error], trip[:points].size]
trip = store.update(points: points, vehicle: nil, avoid: {}, editing: nil) { :routed }
assert_equal [:routed, nil], trip.values_at(:route, :error)
end
end
end