diff options
Diffstat (limited to 'spec/lib')
| -rw-r--r-- | spec/lib/stop_areas_to_way_costs_converter_spec.rb | 70 | ||||
| -rw-r--r-- | spec/lib/tom_tom/batch_spec.rb | 52 | ||||
| -rw-r--r-- | spec/lib/tom_tom_spec.rb | 15 | ||||
| -rw-r--r-- | spec/lib/way_cost_collection_json_serializer_spec.rb | 37 |
4 files changed, 174 insertions, 0 deletions
diff --git a/spec/lib/stop_areas_to_way_costs_converter_spec.rb b/spec/lib/stop_areas_to_way_costs_converter_spec.rb new file mode 100644 index 000000000..7e09ede2e --- /dev/null +++ b/spec/lib/stop_areas_to_way_costs_converter_spec.rb @@ -0,0 +1,70 @@ +RSpec.describe StopAreasToWayCostsConverter do + describe "#convert" do + it "converts a StopArea collection to WayCosts" do + route = create(:route_common) + coordinates = [ + [1.5, 1.8], + [2.2, 2.1], + [3.0, 3.6], + [4.9, 4.3] + ] + + stop_areas = coordinates.map do |latlng| + stop_area = create( + :stop_area, + area_type: 'zdep', + latitude: latlng[0], + longitude: latlng[1] + ) + + create( + :stop_point, + route: route, + stop_area: stop_area + ) + + stop_area + end + + way_costs = [ + WayCost.new( + departure: Geokit::LatLng.new( + stop_areas[0].latitude, + stop_areas[0].longitude + ), + arrival: Geokit::LatLng.new( + stop_areas[1].latitude, + stop_areas[1].longitude + ), + id: "#{stop_areas[0].id}-#{stop_areas[1].id}" + ), + WayCost.new( + departure: Geokit::LatLng.new( + stop_areas[1].latitude, + stop_areas[1].longitude + ), + arrival: Geokit::LatLng.new( + stop_areas[2].latitude, + stop_areas[2].longitude + ), + id: "#{stop_areas[1].id}-#{stop_areas[2].id}" + ), + WayCost.new( + departure: Geokit::LatLng.new( + stop_areas[2].latitude, + stop_areas[2].longitude + ), + arrival: Geokit::LatLng.new( + stop_areas[3].latitude, + stop_areas[3].longitude + ), + id: "#{stop_areas[2].id}-#{stop_areas[3].id}" + ) + ] + + expect( + StopAreasToWayCostsConverter.new(stop_areas).convert + ).to eq(way_costs) + end + end +end diff --git a/spec/lib/tom_tom/batch_spec.rb b/spec/lib/tom_tom/batch_spec.rb new file mode 100644 index 000000000..c3ab46a4b --- /dev/null +++ b/spec/lib/tom_tom/batch_spec.rb @@ -0,0 +1,52 @@ +RSpec.describe TomTom::Batch do + let(:batch) { TomTom::Batch.new(nil) } + + describe "#extract_costs_to_way_costs" do + it "puts distance & time costs in way_costs" do + way_costs = [ + WayCost.new( + departure: Geokit::LatLng.new(48.85086, 2.36143), + arrival: Geokit::LatLng.new(47.91231, 1.87606) + ), + WayCost.new( + departure: Geokit::LatLng.new(47.91231, 1.87606), + arrival: Geokit::LatLng.new(52.50867, 13.42879) + ) + ] + + expected_way_costs = way_costs.deep_dup + expected_way_costs[0].distance = 117947 + expected_way_costs[0].time = 7969 + expected_way_costs[1].distance = 1114379 + expected_way_costs[1].time = 71010 + + batch_response = JSON.parse(read_fixture('tom_tom_batch.json')) + + expect( + batch.extract_costs_to_way_costs!(way_costs, batch_response) + ).to match_array(expected_way_costs) + end + end + + describe "#convert_way_costs" do + it "turns WayCost points into a collection of colon-separated strings" do + way_costs = [ + WayCost.new( + departure: Geokit::LatLng.new(2, 48), + arrival: Geokit::LatLng.new(3, 46) + ), + WayCost.new( + departure: Geokit::LatLng.new(-71, 42), + arrival: Geokit::LatLng.new(-71.5, 42.9) + ) + ] + + expect( + batch.convert_way_costs(way_costs) + ).to eq([ + '2,48:3,46', + '-71,42:-71.5,42.9' + ]) + end + end +end diff --git a/spec/lib/tom_tom_spec.rb b/spec/lib/tom_tom_spec.rb new file mode 100644 index 000000000..71584e242 --- /dev/null +++ b/spec/lib/tom_tom_spec.rb @@ -0,0 +1,15 @@ +RSpec.describe TomTom do + describe ".enabled?" do + it "returns true when API key is set" do + TomTom.instance_variable_set(:@api_key, 'fake key') + + expect(TomTom.enabled?).to be true + end + + it "returns false without an API key" do + TomTom.instance_variable_set(:@api_key, '') + + expect(TomTom.enabled?).to be false + end + end +end diff --git a/spec/lib/way_cost_collection_json_serializer_spec.rb b/spec/lib/way_cost_collection_json_serializer_spec.rb new file mode 100644 index 000000000..22faf88d3 --- /dev/null +++ b/spec/lib/way_cost_collection_json_serializer_spec.rb @@ -0,0 +1,37 @@ +RSpec.describe WayCostCollectionJSONSerializer do + describe ".dump" do + it "creates a JSON hash of hashed WayCost attributes" do + way_costs = [ + 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), + distance: 5678, + time: 999, + id: '2-3' + ) + ] + + expect( + WayCostCollectionJSONSerializer.dump(way_costs) + ).to eq(<<-JSON.delete(" \n")) + { + "1-2": { + "distance": 1234, + "time": 99 + }, + "2-3": { + "distance": 5678, + "time": 999 + } + } + JSON + end + end +end |
