Vehicle profiles (Car, Camper, ...), avoid tolls/motorways, per-country vignette and toll km for a route
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-14 21:20:23 +01:00
1 parent 58ca871662
commit 69aa284e36
8 files changed
+328 -80

No files matched your search

+46 -16
View File
@@ -94,11 +94,22 @@ class RoutingTest < Minitest::Test
refute File.exist?(File.join(@routing, "build-request.json"))
end
def test_the_vehicle_is_validated_and_stored
assert_equal Maptool::Routing::DEFAULT_VEHICLE, Maptool::Routing.vehicle(@data)
Maptool::Routing.save_vehicle(@data, "height" => "3.4", "width" => "2.4", "length" => "8", "weight" => "4.2")
assert_equal 4.2, Maptool::Routing.vehicle(@data)[:weight]
assert_raises(ArgumentError) { Maptool::Routing.save_vehicle(@data, "height" => "30", "width" => "2", "length" => "8", "weight" => "3") }
def test_vehicle_profiles_are_validated_and_stored
assert_equal %w[car camper], Maptool::Routing.vehicles(@data).map { |v| v[:id] }
assert_equal "Car", Maptool::Routing.vehicle(@data, "nope")[:name]
Maptool::Routing.save_vehicles(@data, [{ "id" => "car", "name" => "Car", "height" => "" },
{ "id" => "van", "name" => "Van", "height" => "3.4", "weight" => "4.2" }])
assert_equal({ id: "van", name: "Van", height: 3.4, weight: 4.2 }, Maptool::Routing.vehicle(@data, "van"))
assert_equal({ id: "car", name: "Car" }, Maptool::Routing.vehicle(@data, "car"))
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, [{ "id" => "x", "name" => "X", "height" => "30" }]) }
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, [{ "id" => "x", "name" => "" }]) }
assert_raises(ArgumentError) { Maptool::Routing.save_vehicles(@data, []) }
end
def test_the_old_single_vehicle_becomes_the_camper
File.write(File.join(@data, "vehicle.json"), JSON.generate(height: 3.3, width: 2.3, length: 7, weight: 3.5))
assert_equal 3.3, Maptool::Routing.vehicle(@data, "camper")[:height]
end
def test_decode_polyline_reads_valhallas_precision_6
@@ -110,24 +121,40 @@ class RoutingTest < Minitest::Test
shape = "wdg|xA{ubdQc@w@"
reply = JSON.generate(trip: { summary: { length: 1.5, time: 90 },
legs: [{ shape: shape, maneuvers: [{ instruction: "Drive north.", length: 1.5 }] }] })
request = serve_once("HTTP/1.1 200 OK\r\nContent-Length: #{reply.bytesize}\r\n\r\n#{reply}") do |url|
route = Maptool::Routing.route(url, [[9.52, 47.14], [9.53, 47.15]], Maptool::Routing::DEFAULT_VEHICLE)
# The per-country lookup failing (400) leaves the route as it is.
request = serve_once("HTTP/1.1 200 OK\r\nContent-Length: #{reply.bytesize}\r\n\r\n#{reply}", "HTTP/1.1 400 Bad Request\r\nContent-Length: 2\r\n\r\n{}") do |url|
route = Maptool::Routing.route(url, [[9.52, 47.14], [9.53, 47.15]], Maptool::Routing::DEFAULT_VEHICLES[1])
assert_equal [2, "auto", "Drive north."], [route[:geometry][:coordinates].size, route[:properties][:costing], route[:properties][:maneuvers][0][:text]]
end
assert_includes request, %("costing_options":{"auto":{"height":3.0,"width":2.3}})
request = serve_once("HTTP/1.1 400 Bad Request\r\nContent-Length: 20\r\n\r\n{\"error\":\"no route\"}") do |url|
error = assert_raises(Maptool::Routing::RouteError) { Maptool::Routing.route(url, [[1, 2], [3, 4]], Maptool::Routing::DEFAULT_VEHICLE.merge(weight: 7.5)) }
error = assert_raises(Maptool::Routing::RouteError) { Maptool::Routing.route(url, [[1, 2], [3, 4]], Maptool::Routing::DEFAULT_VEHICLES[1].merge(weight: 7.5)) }
assert_equal "no route", error.message
end
assert_includes request, %("costing":"truck")
assert_raises(Maptool::Routing::Unavailable) { Maptool::Routing.route("", [[1, 2], [3, 4]], {}) }
end
def test_a_route_sums_motorway_and_toll_km_per_country_and_can_avoid_tolls
route = JSON.generate(trip: { summary: { length: 3, time: 90 }, legs: [{ shape: "wdg|xA{ubdQc@w@", maneuvers: [] }] })
attributes = JSON.generate(admins: [{ country_code: "AT" }, { country_code: "SK" }],
edges: [{ length: 1.0, road_class: "motorway", toll: false, end_node: { admin_index: 0 } },
{ length: 0.5, road_class: "primary", toll: false, end_node: { admin_index: 0 } },
{ length: 1.5, road_class: "motorway", toll: true, end_node: { admin_index: 1 } }])
ok = ->(body) { "HTTP/1.1 200 OK\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}" }
request = serve_once(ok.(route), ok.(attributes)) do |url|
countries = Maptool::Routing.route(url, [[1, 2], [3, 4]], { id: "car" }, { "tolls" => true })[:properties][:countries]
assert_equal [{ code: "AT", km: 1.5, motorway_km: 1.0, toll_km: 0.0 }, { code: "SK", km: 1.5, motorway_km: 1.5, toll_km: 1.5 }], countries
end
assert_includes request, %("costing_options":{"auto":{"use_tolls":0.0}})
assert_includes request, "/trace_attributes"
end
def test_isochrone_asks_valhalla_for_each_time_and_checks_them
reply = JSON.generate(type: "FeatureCollection", features: [])
request = serve_once("HTTP/1.1 200 OK\r\nContent-Length: #{reply.bytesize}\r\n\r\n#{reply}") do |url|
assert_equal "FeatureCollection", Maptool::Routing.isochrone(url, [17.1, 48.1], [60, 30, 60], Maptool::Routing::DEFAULT_VEHICLE)["type"]
assert_equal "FeatureCollection", Maptool::Routing.isochrone(url, [17.1, 48.1], [60, 30, 60], Maptool::Routing::DEFAULT_VEHICLES[1])["type"]
end
assert_includes request, %("contours":[{"time":30},{"time":60}])
assert_raises(Maptool::Routing::RouteError) { Maptool::Routing.isochrone("http://x", [17.1, 48.1], [600], {}) }
@@ -148,16 +175,19 @@ class RoutingTest < Minitest::Test
ENV.delete("OSMIUM_BIN")
end
# Answers one HTTP request with `response`; returns what was received.
def serve_once(response)
# Answers one HTTP request per response, in order; returns what was received.
def serve_once(*responses)
server = TCPServer.new("127.0.0.1", 0)
received = +""
thread = Thread.new do
client = server.accept
received << client.readpartial(65_536)
received << client.readpartial(65_536) unless received.include?("{")
client.write(response)
client.close
responses.each do |response|
client = server.accept
request = client.readpartial(65_536)
request << client.readpartial(65_536) unless request.include?("{")
received << request
client.write(response)
client.close
end
end
yield "http://127.0.0.1:#{server.addr[1]}"
thread.join