diff options
| author | Teddy Wing | 2017-05-04 15:55:42 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-05-04 17:10:14 +0200 |
| commit | 8978ffb9e346d35176371ef7d7879c8e41e96a97 (patch) | |
| tree | dae30be32b4355b85935df22d046d4ad453cd7af | |
| parent | 4c12a6632907d5d24b654791db994bfb830c7e37 (diff) | |
| download | chouette-core-8978ffb9e346d35176371ef7d7879c8e41e96a97.tar.bz2 | |
VehicleJourney: Add #with_stops
A new method that does the same thing as the query in
`VehicleJourneysController#collection`
(4c12a6632907d5d24b654791db994bfb830c7e37). The goal is to replace the
controller query code with this method so that it can be unit tested.
Refs #3268
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 13 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 41 |
2 files changed, 54 insertions, 0 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 4d7d596d8..297e462f0 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -209,5 +209,18 @@ module Chouette end end + def self.with_stops + self + .joins(:journey_pattern) + .joins(' + LEFT JOIN "vehicle_journey_at_stops" + ON "vehicle_journey_at_stops"."vehicle_journey_id" = + "vehicle_journeys"."id" + AND "vehicle_journey_at_stops"."stop_point_id" = + "journey_patterns"."departure_stop_point_id" + ') + .order("vehicle_journey_at_stops.departure_time") + end + end end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 3d3a948bc..ace28bd70 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -221,6 +221,47 @@ describe Chouette::VehicleJourney, :type => :model do end end + describe ".with_stops" do + it "selects vehicle journeys including stops in order or earliest departure time" do + def initialize_stop_times(vehicle_journey, &block) + vehicle_journey + .vehicle_journey_at_stops + .each_with_index do |at_stop, index| + at_stop.update( + departure_time: at_stop.departure_time + block.call(index), + arrival_time: at_stop.arrival_time + block.call(index) + ) + end + end + + # Create later vehicle journey to give it a later id, such that it should + # appear last if the order in the query isn't right. + journey_late = create(:vehicle_journey) + journey_early = create( + :vehicle_journey, + route: journey_late.route, + journey_pattern: journey_late.journey_pattern + ) + + initialize_stop_times(journey_early) do |index| + (index + 5).minutes + end + initialize_stop_times(journey_late) do |index| + (index + 65).minutes + end + + expected_journey_order = [journey_early, journey_late] + + expect( + journey_late + .route + .vehicle_journeys + .with_stops + .to_a + ).to eq(expected_journey_order) + end + end + subject { create(:vehicle_journey_odd) } describe "in_relation_to_a_journey_pattern methods" do let!(:route) { create(:route)} |
