aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib
diff options
context:
space:
mode:
authorTeddy Wing2018-03-13 11:43:14 +0100
committerTeddy Wing2018-03-14 16:38:40 +0100
commit87947a7b6da52305029954341c6dc40b8cde6f86 (patch)
tree006391e653e92cf82b93cff37d9136e984f7fc2a /spec/lib
parent33c2dad6ba2483a4c71d7248979bf9d82a65f234 (diff)
downloadchouette-core-87947a7b6da52305029954341c6dc40b8cde6f86.tar.bz2
Add `StopAreasToWayCostsConverter`
A new class that converts a list of `StopArea`s to `WayCost`s. This will be called from `Chouette::Route` to get `WayCost`s from its stops, which will then be JSON serialised and stored in `Route#costs`. Update `WayCost`: * Remove comment about calculating the ID automatically. It actually needs to be the same as the `JourneyPattern#cost` ID (`key`), which is a string with the IDs of the departure and arrival stops. * Make `#==` check that `id`s are the same, which necessitates making `id` a reader. Refs #6095
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/stop_areas_to_way_costs_converter_spec.rb70
1 files changed, 70 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