From ae3933586653c00ea92d788380fc81d3586e4d42 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 16 Apr 2018 12:15:01 +0200 Subject: WayCostsFromRouteCostsPopulator: Handle missing route costs If a `Route#cost` is missing the method shouldn't fail. Instead, it should just pass the `WayCost` along without distance & time cost values. This way we end up with a list of all `WayCost`s for all stop areas, even the new ones that haven't been run through the TomTom calculator yet. Refs #6097 --- lib/way_costs_from_route_costs_populator.rb | 6 ++-- .../way_costs_from_route_costs_populator_spec.rb | 39 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/way_costs_from_route_costs_populator.rb b/lib/way_costs_from_route_costs_populator.rb index 618e80366..e179df11f 100644 --- a/lib/way_costs_from_route_costs_populator.rb +++ b/lib/way_costs_from_route_costs_populator.rb @@ -8,8 +8,10 @@ class WayCostsFromRouteCostsPopulator @way_costs.map do |way_cost| route_cost = @route_costs[way_cost.id] - way_cost.distance = route_cost['distance'] - way_cost.time = route_cost['time'] + if route_cost + way_cost.distance = route_cost['distance'] + way_cost.time = route_cost['time'] + end way_cost end diff --git a/spec/lib/way_costs_from_route_costs_populator_spec.rb b/spec/lib/way_costs_from_route_costs_populator_spec.rb index b07988f38..eb9096c2c 100644 --- a/spec/lib/way_costs_from_route_costs_populator_spec.rb +++ b/spec/lib/way_costs_from_route_costs_populator_spec.rb @@ -44,5 +44,44 @@ RSpec.describe WayCostsFromRouteCostsPopulator do ) ]) end + + it "doesn't fail when `route_costs` doesn't include a WayCost" do + way_costs = [ + WayCost.new( + departure: Geokit::LatLng.new(48.85086, 2.36143), + arrival: Geokit::LatLng.new(47.91231, 1.87606), + id: '1-2' + ), + WayCost.new( + departure: Geokit::LatLng.new(47.91231, 1.87606), + arrival: Geokit::LatLng.new(52.50867, 13.42879), + id: '2-3' + ) + ] + + route_costs = { + '1-2' => { + 'distance' => 1234, + 'time' => 99 + } + } + + WayCostsFromRouteCostsPopulator.new(way_costs, route_costs).populate! + + expect(way_costs).to eq([ + WayCost.new( + departure: Geokit::LatLng.new(48.85086, 2.36143), + arrival: Geokit::LatLng.new(47.91231, 1.87606), + distance: 1234, + time: 99, + id: '1-2' + ), + WayCost.new( + departure: Geokit::LatLng.new(47.91231, 1.87606), + arrival: Geokit::LatLng.new(52.50867, 13.42879), + id: '2-3' + ) + ]) + end end end -- cgit v1.2.3