aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
authorRobert2017-06-27 14:25:35 +0200
committerRobert2017-06-27 14:25:35 +0200
commitf20df3c08dfec0e3dda68401204f7d49470119a7 (patch)
tree4f27a34130d4ff044d48729f16d2dff154047ce5 /spec/models
parent970954938043d8d73c4457ee2d91e22c0e422e65 (diff)
parenteabd56ff9f2c7979192e54b4ae11673f1cc778c1 (diff)
downloadchouette-core-f20df3c08dfec0e3dda68401204f7d49470119a7.tar.bz2
conflicts resolved
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/calendar_spec.rb10
-rw-r--r--spec/models/chouette/journey_pattern_spec.rb83
-rw-r--r--spec/models/chouette/vehicle_journey_spec.rb31
3 files changed, 114 insertions, 10 deletions
diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb
index 6a2b24011..a3da95aca 100644
--- a/spec/models/calendar_spec.rb
+++ b/spec/models/calendar_spec.rb
@@ -109,15 +109,11 @@ RSpec.describe Calendar, :type => :model do
let(:calendar) { create(:calendar, date_ranges: []) }
it 'shoud fill date_ranges with date ranges' do
- expected_ranges = [
- Range.new(Date.today, Date.tomorrow)
- ]
- expected_ranges.each_with_index do |range, index|
- calendar.date_ranges << Calendar::Period.from_range(index, range)
- end
+ expected_range = Date.today..Date.tomorrow
+ calendar.date_ranges << expected_range
calendar.valid?
- expect(calendar.date_ranges.map { |period| period.begin..period.end }).to eq(expected_ranges)
+ expect(calendar.date_ranges.map { |period| period.begin..period.end }).to eq([expected_range])
end
end
diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb
index 19b5060d2..aaf9a694f 100644
--- a/spec/models/chouette/journey_pattern_spec.rb
+++ b/spec/models/chouette/journey_pattern_spec.rb
@@ -2,6 +2,26 @@ require 'spec_helper'
describe Chouette::JourneyPattern, :type => :model do
+ context 'validate minimum stop_points size' do
+ let(:journey_pattern) { create :journey_pattern }
+ let(:stop_points) { journey_pattern.stop_points }
+
+ it 'should be valid if it has at least two sp' do
+ journey_pattern.stop_points.first(stop_points.size - 2).each do |sp|
+ journey_pattern.stop_points.delete(sp)
+ end
+ expect(journey_pattern).to be_valid
+ end
+
+ it 'should not be valid if it has less then two sp' do
+ journey_pattern.stop_points.first(stop_points.size - 1).each do |sp|
+ journey_pattern.stop_points.delete(sp)
+ end
+ expect(journey_pattern).to_not be_valid
+ expect(journey_pattern.errors).to have_key(:stop_points)
+ end
+ end
+
describe "state_update" do
def journey_pattern_to_state jp
jp.attributes.slice('name', 'published_name', 'registration_number').tap do |item|
@@ -24,12 +44,14 @@ describe Chouette::JourneyPattern, :type => :model do
end
it 'should attach checked stop_points' do
- state['stop_points'].each{|sp| sp['checked'] = true}
# Make sure journey_pattern has no stop_points
- journey_pattern.stop_points.delete_all
+ state['stop_points'].each{|sp| sp['checked'] = false}
+ journey_pattern.state_stop_points_update(state)
expect(journey_pattern.reload.stop_points).to be_empty
+ state['stop_points'].each{|sp| sp['checked'] = true}
journey_pattern.state_stop_points_update(state)
+
expect(journey_pattern.reload.stop_points.count).to eq(5)
end
@@ -89,6 +111,63 @@ describe Chouette::JourneyPattern, :type => :model do
expect(collection.first).to_not have_key('object_id')
end
+
+ it 'should create journey_pattern' do
+ new_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route))
+ Chouette::JourneyPattern.state_create_instance route, new_state
+ expect(new_state['object_id']).to be_truthy
+ expect(new_state['new_record']).to be_truthy
+ end
+
+ it 'should delete journey_pattern' do
+ state['deletable'] = true
+ collection = [state]
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to change{Chouette::JourneyPattern.count}.from(1).to(0)
+
+ expect(collection).to be_empty
+ end
+
+ it 'should delete multiple journey_pattern' do
+ collection = 5.times.collect{journey_pattern_to_state(create(:journey_pattern, route: route))}
+ collection.map{|i| i['deletable'] = true}
+
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to change{Chouette::JourneyPattern.count}.from(5).to(0)
+ end
+
+ it 'should validate journey_pattern on update' do
+ journey_pattern.name = ''
+ collection = [state]
+ Chouette::JourneyPattern.state_update route, collection
+ expect(collection.first['errors']).to have_key(:name)
+ end
+
+ it 'should validate journey_pattern on create' do
+ new_state = journey_pattern_to_state(build(:journey_pattern, name: '', objectid: nil, route: route))
+ collection = [new_state]
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to_not change{Chouette::JourneyPattern.count}
+
+ expect(collection.first['errors']).to have_key(:name)
+ expect(collection.first).to_not have_key('object_id')
+ end
+
+ it 'should not save any journey_pattern of collection if one is invalid' do
+ journey_pattern.name = ''
+ valid_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route))
+ invalid_state = journey_pattern_to_state(journey_pattern)
+ collection = [valid_state, invalid_state]
+
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to_not change{Chouette::JourneyPattern.count}
+
+ expect(collection.first).to_not have_key('object_id')
+ end
end
describe "#stop_point_ids" do
diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb
index 8f9080b99..c78ef5b33 100644
--- a/spec/models/chouette/vehicle_journey_spec.rb
+++ b/spec/models/chouette/vehicle_journey_spec.rb
@@ -351,7 +351,7 @@ describe Chouette::VehicleJourney, :type => :model do
end
end
- describe ".departure_time_between" do
+ describe ".where_departure_time_between" do
it "selects vehicle journeys whose departure times are between the
specified range" do
journey_early = create(
@@ -404,6 +404,35 @@ describe Chouette::VehicleJourney, :type => :model do
.to_a
).to eq([journey])
end
+
+ it "uses an inclusive range" do
+ journey_early = create(
+ :vehicle_journey,
+ stop_departure_time: '03:00:00'
+ )
+
+ route = journey_early.route
+ journey_pattern = journey_early.journey_pattern
+
+ journey_late = create(
+ :vehicle_journey,
+ route: route,
+ journey_pattern: journey_pattern,
+ stop_departure_time: '04:00:00'
+ )
+
+ expect(route
+ .vehicle_journeys
+ .select('DISTINCT "vehicle_journeys".*')
+ .joins('
+ LEFT JOIN "vehicle_journey_at_stops"
+ ON "vehicle_journey_at_stops"."vehicle_journey_id" =
+ "vehicle_journeys"."id"
+ ')
+ .where_departure_time_between('03:00', '04:00', allow_empty: true)
+ .to_a
+ ).to match_array([journey_early, journey_late])
+ end
end
describe ".without_time_tables" do