aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/calendar_spec.rb122
-rw-r--r--spec/models/chouette/access_point_spec.rb2
-rw-r--r--spec/models/chouette/area_type_spec.rb43
-rw-r--r--spec/models/chouette/footnote_spec.rb12
-rw-r--r--spec/models/chouette/journey_pattern_spec.rb50
-rw-r--r--spec/models/chouette/purchase_window_spec.rb27
-rw-r--r--spec/models/chouette/route/route_base_spec.rb4
-rw-r--r--spec/models/chouette/route/route_duplication_spec.rb11
-rw-r--r--spec/models/chouette/routing_constraint_zone_spec.rb9
-rw-r--r--spec/models/chouette/stop_area_spec.rb80
-rw-r--r--spec/models/chouette/time_table_period_spec.rb2
-rw-r--r--spec/models/chouette/time_table_spec.rb134
-rw-r--r--spec/models/chouette/vehicle_journey_at_stop_spec.rb39
-rw-r--r--spec/models/chouette/vehicle_journey_spec.rb256
-rw-r--r--spec/models/compliance_check_spec.rb32
-rw-r--r--spec/models/compliance_control_class_level_defaults/generic_attribute_control/pattern_cccld_spec.rb3
-rw-r--r--spec/models/compliance_control_class_level_defaults/generic_attribute_control/uniqueness_cccld_spec.rb1
-rw-r--r--spec/models/compliance_control_spec.rb10
-rw-r--r--spec/models/compliance_control_validations/genric_attribute_validation/min_max_validation_spec.rb1
-rw-r--r--spec/models/custom_field_spec.rb35
-rw-r--r--spec/models/import_spec.rb57
-rw-r--r--spec/models/merge_spec.rb58
-rw-r--r--spec/models/organisation_spec.rb37
-rw-r--r--spec/models/referential/referential_lock_during_creation_spec.rb198
-rw-r--r--spec/models/referential_cloning_spec.rb73
-rw-r--r--spec/models/referential_spec.rb43
-rw-r--r--spec/models/workbench_spec.rb3
-rw-r--r--spec/models/workgroup_spec.rb30
28 files changed, 1319 insertions, 53 deletions
diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb
index e71c2b081..a5c0a7471 100644
--- a/spec/models/calendar_spec.rb
+++ b/spec/models/calendar_spec.rb
@@ -9,20 +9,34 @@ RSpec.describe Calendar, :type => :model do
it { is_expected.to be_versioned }
describe '#to_time_table' do
- let(:calendar) { create(:calendar, date_ranges: [Date.today...(Date.today + 1.month)]) }
+ let(:calendar) { create(:calendar, int_day_types: Calendar::MONDAY | Calendar::SUNDAY, date_ranges: [Date.today...(Date.today + 1.month)]) }
it 'should convert calendar to an instance of Chouette::TimeTable' do
time_table = calendar.convert_to_time_table
expect(time_table).to be_an_instance_of(Chouette::TimeTable)
+ expect(time_table.int_day_types).to eq calendar.int_day_types
expect(time_table.periods[0].period_start).to eq(calendar.periods[0].begin)
expect(time_table.periods[0].period_end).to eq(calendar.periods[0].end)
expect(time_table.dates.map(&:date)).to match_array(calendar.dates)
end
end
+ describe 'application days' do
+ let(:calendar) { create(:calendar) }
+ it "should default to all days" do
+ %w(monday tuesday wednesday thursday friday saturday sunday).each do |day|
+ expect(calendar.send(day)).to be_truthy
+ end
+ end
+ end
+
describe 'validations' do
it 'validates that dates and date_ranges do not overlap' do
- expect(build(:calendar, dates: [Date.today], date_ranges: [Date.today..Date.tomorrow])).to_not be_valid
+ expect(build(:calendar, dates: [Date.today.beginning_of_week], date_ranges: [Date.today.beginning_of_week..Date.today])).to_not be_valid
+ end
+
+ it 'validates that dates and date_ranges do not overlap but allow for days not in the list' do
+ expect(build(:calendar, dates: [Date.today.beginning_of_week - 1.week], date_ranges: [(Date.today.beginning_of_week - 1.week)..Date.today], int_day_types: Calendar::THURSDAY)).to be_valid
end
it 'validates that there are no duplicates in dates' do
@@ -42,4 +56,108 @@ RSpec.describe Calendar, :type => :model do
end
end
+ describe "Update state" do
+ def calendar_to_state calendar
+ calendar.slice('id').tap do |item|
+ item['comment'] = calendar.name
+ item['day_types'] = "Di,Lu,Ma,Me,Je,Ve,Sa"
+ item['current_month'] = calendar.month_inspect(Date.today.beginning_of_month)
+ item['current_periode_range'] = Date.today.beginning_of_month.to_s
+ item['time_table_periods'] = calendar.periods.map{|p| {'id': p.id, 'period_start': p.period_start.to_s, 'period_end': p.period_end.to_s}}
+ end
+ end
+
+ subject(:calendar){ create :calendar }
+ let(:state) { calendar_to_state subject }
+
+ it 'should update time table periods association' do
+ period = state['time_table_periods'].first
+ period['period_start'] = (Date.today - 1.month).to_s
+ period['period_end'] = (Date.today + 1.month).to_s
+
+ subject.state_update_periods state['time_table_periods']
+ ['period_end', 'period_start'].each do |prop|
+ expect(subject.reload.periods.first.send(prop).to_s).to eq(period[prop])
+ end
+ end
+
+ it 'should create time table periods association' do
+ state['time_table_periods'] << {
+ 'id' => false,
+ 'period_start' => (Date.today + 1.year).to_s,
+ 'period_end' => (Date.today + 2.year).to_s
+ }
+
+ expect {
+ subject.state_update_periods state['time_table_periods']
+ }.to change {subject.periods.count}.by(1)
+ expect(state['time_table_periods'].last['id']).to eq subject.reload.periods.last.id
+ end
+
+ it 'should delete time table periods association' do
+ state['time_table_periods'].first['deleted'] = true
+ expect {
+ subject.state_update_periods state['time_table_periods']
+ }.to change {subject.periods.count}.by(-1)
+ end
+
+ it 'should update name' do
+ state['comment'] = "Edited timetable name"
+ subject.state_update state
+ expect(subject.reload.name).to eq state['comment']
+ end
+
+ it 'should update day_types' do
+ state['day_types'] = "Di,Lu,Je,Ma"
+ subject.state_update state
+ expect(subject.reload.valid_days).to include(7, 1, 4, 2)
+ expect(subject.reload.valid_days).not_to include(3, 5, 6)
+ end
+
+ it 'should delete date if date is set to neither include or excluded date' do
+ updated = state['current_month'].map do |day|
+ day['include_date'] = false if day['include_date']
+ end
+
+ expect {
+ subject.state_update state
+ }.to change {subject.dates.count}.by(-updated.compact.count)
+ end
+
+ it 'should update date if date is set to excluded date' do
+ updated = state['current_month'].map do |day|
+ next unless day['include_date']
+ day['include_date'] = false
+ day['excluded_date'] = true
+ end
+
+ subject.state_update state
+ expect(subject.reload.excluded_days.count).to eq (updated.compact.count)
+ end
+
+ it 'should create new include date' do
+ day = state['current_month'].find{|d| !d['excluded_date'] && !d['include_date'] }
+ date = Date.parse(day['date'])
+ day['include_date'] = true
+ expect(subject.included_days).not_to include(date)
+
+ expect {
+ subject.state_update state
+ }.to change {subject.dates.count}.by(1)
+ expect(subject.reload.included_days).to include(date)
+ end
+
+ it 'should create new exclude date' do
+ day = state['current_month'].find{|d| !d['excluded_date'] && !d['include_date']}
+ date = Date.parse(day['date'])
+ day['excluded_date'] = true
+ expect(subject.excluded_days).not_to include(date)
+
+ expect {
+ subject.state_update state
+ }.to change {subject.all_dates.count}.by(1)
+ expect(subject.reload.excluded_days).to include(date)
+ end
+ end
+
end
diff --git a/spec/models/chouette/access_point_spec.rb b/spec/models/chouette/access_point_spec.rb
index c734ecedf..2184c6ec2 100644
--- a/spec/models/chouette/access_point_spec.rb
+++ b/spec/models/chouette/access_point_spec.rb
@@ -136,7 +136,7 @@ describe Chouette::AccessPoint, :type => :model do
describe "#generic_access_link_matrix" do
it "should have 2 generic_access_links in matrix" do
- stop_place = create :stop_area, :area_type => "zdlp"
+ stop_place = create :stop_area, :area_type => "gdl"
commercial_stop_point = create :stop_area, :area_type => "lda" ,:parent => stop_place
subject = create :access_point, :stop_area => stop_place
expect(subject.generic_access_link_matrix.size).to eq(2)
diff --git a/spec/models/chouette/area_type_spec.rb b/spec/models/chouette/area_type_spec.rb
new file mode 100644
index 000000000..28325dd0a
--- /dev/null
+++ b/spec/models/chouette/area_type_spec.rb
@@ -0,0 +1,43 @@
+require "rails_helper"
+
+RSpec.describe Chouette::AreaType do
+
+ describe "::ALL" do
+ it "includes all supported types" do
+ expect(Chouette::AreaType::ALL).to match_array( %i(zdep zder zdlp zdlr lda gdl deposit border service_area relief other) )
+ expect(Chouette::AreaType::COMMERCIAL).to match_array( %i(zdep zder zdlp zdlr lda gdl) )
+ expect(Chouette::AreaType::NON_COMMERCIAL).to match_array( %i( deposit border service_area relief other) )
+ end
+ end
+
+ describe ".find" do
+ it "returns nil if the given code is nil" do
+ expect(Chouette::AreaType.find(nil)).to be_nil
+ end
+
+ it "returns nil if the given code is unknown" do
+ expect(Chouette::AreaType.find('dummy')).to be_nil
+ end
+
+ it "returns an AreaType associated to the code" do
+ expect(Chouette::AreaType.find('zdep').code).to eq :zdep
+ end
+ end
+
+ describe ".options" do
+ before do
+ Chouette::AreaType.reset_caches!
+ end
+
+ it "returns an array with label and code for each type" do
+ allow(Chouette::AreaType).to receive(:all).and_return(%i{zdep lda})
+
+ expected_options = [
+ [Chouette::AreaType.find('zdep').label, :zdep],
+ [Chouette::AreaType.find('lda').label, :lda]
+ ]
+ expect(Chouette::AreaType.options).to eq(expected_options)
+ end
+ end
+
+end
diff --git a/spec/models/chouette/footnote_spec.rb b/spec/models/chouette/footnote_spec.rb
index fc5e5f306..05f55c2f0 100644
--- a/spec/models/chouette/footnote_spec.rb
+++ b/spec/models/chouette/footnote_spec.rb
@@ -1,12 +1,12 @@
require 'spec_helper'
describe Chouette::Footnote, type: :model do
- let(:footnote) { create(:footnote) }
+ subject { create(:footnote) }
it { should validate_presence_of :line }
describe 'data_source_ref' do
it 'should set default if omitted' do
- expect(footnote.data_source_ref).to eq "DATASOURCEREF_EDITION_BOIV"
+ expect(subject.data_source_ref).to eq "DATASOURCEREF_EDITION_BOIV"
end
it 'should not set default if not omitted' do
@@ -18,16 +18,16 @@ describe Chouette::Footnote, type: :model do
end
describe 'checksum' do
- it_behaves_like 'checksum support', :footnote
+ it_behaves_like 'checksum support'
context '#checksum_attributes' do
it 'should return code and label' do
- expected = [footnote.code, footnote.label]
- expect(footnote.checksum_attributes).to include(*expected)
+ expected = [subject.code, subject.label]
+ expect(subject.checksum_attributes).to include(*expected)
end
it 'should not return other atrributes' do
- expect(footnote.checksum_attributes).to_not include(footnote.updated_at)
+ expect(subject.checksum_attributes).to_not include(subject.updated_at)
end
end
end
diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb
index ea7c2a2e9..19a74a0e7 100644
--- a/spec/models/chouette/journey_pattern_spec.rb
+++ b/spec/models/chouette/journey_pattern_spec.rb
@@ -2,9 +2,10 @@ require 'spec_helper'
describe Chouette::JourneyPattern, :type => :model do
it { is_expected.to be_versioned }
+ subject { create(:journey_pattern) }
describe 'checksum' do
- it_behaves_like 'checksum support', :journey_pattern
+ it_behaves_like 'checksum support'
end
# context 'validate minimum stop_points size' do
@@ -32,6 +33,44 @@ describe Chouette::JourneyPattern, :type => :model do
# end
# end
+ describe "full_schedule?" do
+ let(:journey_pattern) { create :journey_pattern }
+ subject{ journey_pattern.full_schedule? }
+ context "when no time is set" do
+ it { should be_falsy }
+ end
+
+ context "when the costs are incomplete" do
+ context "with a missing distance" do
+ before(:each){
+ journey_pattern.costs = generate_journey_pattern_costs(->(i){i == 1 ? nil : 10}, 10)
+ }
+ it { should be_falsy }
+ end
+
+ context "with a missing time" do
+ before(:each){
+ journey_pattern.costs = generate_journey_pattern_costs(10, ->(i){i == 1 ? nil : 10})
+ }
+ it { should be_falsy }
+ end
+ end
+
+ context "with a zeroed cost" do
+ before(:each){
+ journey_pattern.costs = generate_journey_pattern_costs(->(i){i == 1 ? 0 : 10}, 10)
+ }
+ it { should be_falsy }
+ end
+
+ context "when all the times are set" do
+ before(:each){
+ journey_pattern.costs = generate_journey_pattern_costs(10, 10)
+ }
+ it { should be_truthy }
+ end
+ end
+
describe "state_update" do
def journey_pattern_to_state jp
jp.attributes.slice('name', 'published_name', 'registration_number').tap do |item|
@@ -39,6 +78,7 @@ describe Chouette::JourneyPattern, :type => :model do
item['stop_points'] = jp.stop_points.map do |sp|
{ 'id' => sp.stop_area_id }
end
+ item['costs'] = jp.costs
end
end
@@ -72,6 +112,14 @@ describe Chouette::JourneyPattern, :type => :model do
expect(new_state['new_record']).to be_truthy
end
+ it 'should create journey_pattern with state_update' do
+ new_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route))
+ collection = [new_state]
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to change{Chouette::JourneyPattern.count}.by(1)
+ end
+
it 'should delete journey_pattern' do
state['deletable'] = true
collection = [state]
diff --git a/spec/models/chouette/purchase_window_spec.rb b/spec/models/chouette/purchase_window_spec.rb
new file mode 100644
index 000000000..702a44eeb
--- /dev/null
+++ b/spec/models/chouette/purchase_window_spec.rb
@@ -0,0 +1,27 @@
+RSpec.describe Chouette::PurchaseWindow, :type => :model do
+ let(:referential) {create(:referential)}
+ subject { create(:purchase_window, referential: referential) }
+
+ it { should belong_to(:referential) }
+ it { is_expected.to validate_presence_of(:name) }
+
+ describe 'validations' do
+ it 'validates and date_ranges do not overlap' do
+ expect(build(:purchase_window, referential: referential,date_ranges: [Date.today..Date.today + 10.day, Date.yesterday..Date.tomorrow])).to_not be_valid
+ # expect(build(periods: [Date.today..Date.today + 10.day, Date.yesterday..Date.tomorrow ])).to_not be_valid
+ end
+ end
+
+ describe 'before_validation' do
+ let(:purchase_window) { build(:purchase_window, referential: referential, date_ranges: []) }
+
+ it 'shoud fill date_ranges with date ranges' do
+ expected_range = Date.today..Date.tomorrow
+ purchase_window.date_ranges << expected_range
+ purchase_window.valid?
+
+ expect(purchase_window.date_ranges.map { |period| period.begin..period.end }).to eq([expected_range])
+ end
+ end
+
+end
diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb
index 26f57eae5..98cb3e358 100644
--- a/spec/models/chouette/route/route_base_spec.rb
+++ b/spec/models/chouette/route/route_base_spec.rb
@@ -1,8 +1,8 @@
RSpec.describe Chouette::Route, :type => :model do
-
subject { create(:route) }
+
describe 'checksum' do
- it_behaves_like 'checksum support', :route
+ it_behaves_like 'checksum support'
end
it { is_expected.to enumerize(:direction).in(:straight_forward, :backward, :clockwise, :counter_clockwise, :north, :north_west, :west, :south_west, :south, :south_east, :east, :north_east) }
diff --git a/spec/models/chouette/route/route_duplication_spec.rb b/spec/models/chouette/route/route_duplication_spec.rb
index ee45b5005..8b3a948a2 100644
--- a/spec/models/chouette/route/route_duplication_spec.rb
+++ b/spec/models/chouette/route/route_duplication_spec.rb
@@ -2,7 +2,7 @@ RSpec.describe Chouette::Route do
let!( :route ){ create :route }
- context '#duplicate' do
+ context '#duplicate' do
describe 'properties' do
it 'same attribute values' do
route.duplicate
@@ -23,7 +23,12 @@ RSpec.describe Chouette::Route do
it 'duplicates its stop points' do
expect{ route.duplicate }.to change{Chouette::StopPoint.count}.by(route.stop_points.count)
end
- it 'does bot duplicate the stop areas' do
+
+ it 'duplicates its stop points in the same order' do
+ expect(route.duplicate.stop_points.order(:position).map(&:stop_area_id)).to eq route.stop_points.order(:position).map(&:stop_area_id)
+ end
+
+ it 'does not duplicate the stop areas' do
expect{ route.duplicate }.not_to change{Chouette::StopArea.count}
end
end
@@ -34,7 +39,7 @@ RSpec.describe Chouette::Route do
it 'the required attributes' do
expect( values_for_create(first_duplicate, except: %w{objectid name checksum checksum_source}) ).to eq( values_for_create( second_duplicate, except: %w{objectid name checksum checksum_source} ) )
- end
+ end
it 'the stop areas' do
expect( first_duplicate.stop_areas.pluck(:id) ).to eq( route.stop_areas.pluck(:id) )
diff --git a/spec/models/chouette/routing_constraint_zone_spec.rb b/spec/models/chouette/routing_constraint_zone_spec.rb
index 8ebd8695c..bda6bb04a 100644
--- a/spec/models/chouette/routing_constraint_zone_spec.rb
+++ b/spec/models/chouette/routing_constraint_zone_spec.rb
@@ -5,21 +5,16 @@ describe Chouette::RoutingConstraintZone, type: :model do
subject { create(:routing_constraint_zone) }
it { is_expected.to validate_presence_of :name }
+ it { is_expected.to validate_presence_of :route_id }
# shoulda matcher to validate length of array ?
xit { is_expected.to validate_length_of(:stop_point_ids).is_at_least(2) }
it { is_expected.to be_versioned }
describe 'checksum' do
- it_behaves_like 'checksum support', :routing_constraint_zone
+ it_behaves_like 'checksum support'
end
describe 'validations' do
- it 'validates the presence of route_id' do
- expect {
- subject.update!(route_id: nil)
- }.to raise_error(NoMethodError)
- end
-
it 'validates the presence of stop_point_ids' do
expect {
subject.update!(stop_point_ids: [])
diff --git a/spec/models/chouette/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb
index c6aeafaf8..32ee5a3a6 100644
--- a/spec/models/chouette/stop_area_spec.rb
+++ b/spec/models/chouette/stop_area_spec.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
require 'spec_helper'
describe Chouette::StopArea, :type => :model do
@@ -9,10 +10,20 @@ describe Chouette::StopArea, :type => :model do
it { should belong_to(:stop_area_referential) }
it { should validate_presence_of :name }
+ it { should validate_presence_of :kind }
it { should validate_numericality_of :latitude }
it { should validate_numericality_of :longitude }
it { is_expected.to be_versioned }
+ describe "#area_type" do
+ it "should validate the value is correct regarding to the kind" do
+ expect(build(:stop_area, kind: :commercial, area_type: :gdl)).to be_valid
+ expect(build(:stop_area, kind: :non_commercial, area_type: :relief)).to be_valid
+ expect(build(:stop_area, kind: :commercial, area_type: :relief)).to_not be_valid
+ expect(build(:stop_area, kind: :non_commercial, area_type: :gdl)).to_not be_valid
+ end
+ end
+
# describe ".latitude" do
# it "should accept -90 value" do
# subject = create :stop_area, :area_type => "BoardingPosition"
@@ -426,5 +437,74 @@ describe Chouette::StopArea, :type => :model do
# end
# end
+ describe "#parent" do
+
+ let(:stop_area) { FactoryGirl.build :stop_area, parent: FactoryGirl.build(:stop_area) }
+
+ it "is valid when parent has an 'higher' type" do
+ stop_area.area_type = 'zdep'
+ stop_area.parent.area_type = 'zdlp'
+
+ stop_area.valid?
+ expect(stop_area.errors).to_not have_key(:parent_id)
+ end
+
+ it "is valid when parent is undefined" do
+ stop_area.parent = nil
+
+ stop_area.valid?
+ expect(stop_area.errors).to_not have_key(:parent_id)
+ end
+
+ it "isn't valid when parent has the same type" do
+ stop_area.parent.area_type = stop_area.area_type = 'zdep'
+
+ stop_area.valid?
+ expect(stop_area.errors).to have_key(:parent_id)
+ end
+
+ it "isn't valid when parent has a lower type" do
+ stop_area.area_type = 'lda'
+ stop_area.parent.area_type = 'zdep'
+
+ stop_area.valid?
+ expect(stop_area.errors).to have_key(:parent_id)
+ end
+
+ it "use parent area type label in validation error message" do
+ stop_area.area_type = 'zdep'
+ stop_area.parent.area_type = 'zdep'
+
+ stop_area.valid?
+ expect(stop_area.errors[:parent_id].first).to include(Chouette::AreaType.find(stop_area.parent.area_type).label)
+ end
+
+ end
+
+ describe '#waiting_time' do
+
+ let(:stop_area) { FactoryGirl.build :stop_area }
+
+ it 'can be nil' do
+ stop_area.waiting_time = nil
+ expect(stop_area).to be_valid
+ end
+
+ it 'can be zero' do
+ stop_area.waiting_time = 0
+ expect(stop_area).to be_valid
+ end
+
+ it 'can be positive' do
+ stop_area.waiting_time = 120
+ expect(stop_area).to be_valid
+ end
+
+ it "can't be negative" do
+ stop_area.waiting_time = -1
+ expect(stop_area).to_not be_valid
+ end
+
+ end
end
diff --git a/spec/models/chouette/time_table_period_spec.rb b/spec/models/chouette/time_table_period_spec.rb
index cc1a3ae09..e14d38ade 100644
--- a/spec/models/chouette/time_table_period_spec.rb
+++ b/spec/models/chouette/time_table_period_spec.rb
@@ -10,7 +10,7 @@ describe Chouette::TimeTablePeriod, :type => :model do
it { is_expected.to validate_presence_of :period_end }
describe 'checksum' do
- it_behaves_like 'checksum support', :time_table_period
+ it_behaves_like 'checksum support'
end
describe "#overlap" do
diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb
index 677308fc8..bb88877b9 100644
--- a/spec/models/chouette/time_table_spec.rb
+++ b/spec/models/chouette/time_table_spec.rb
@@ -926,6 +926,7 @@ end
end
end
end
+
describe "#validity_out_between?" do
let(:empty_tm) {build(:time_table)}
it "should be false if empty calendar" do
@@ -1048,7 +1049,39 @@ end
# it { is_expected.to validate_uniqueness_of :objectid }
describe 'checksum' do
- it_behaves_like 'checksum support', :time_table
+ it_behaves_like 'checksum support'
+
+ it "handles newly built dates and periods" do
+ time_table = build(:time_table)
+ time_table.periods.build period_start: Time.now, period_end: 1.month.from_now
+ time_table.dates.build date: Time.now
+ time_table.save!
+ expect{time_table.update_checksum!}.to_not change{time_table.checksum}
+ expect(time_table.dates.count).to eq 1
+ expect(time_table.periods.count).to eq 1
+ end
+
+ it "changes when a date is updated" do
+ time_table = create(:time_table)
+ expect{time_table.dates.last.update_attribute(:date, Time.now)}.to change{time_table.reload.checksum}
+ end
+
+ it "changes when a date is added" do
+ time_table = create(:time_table)
+ expect(time_table).to receive(:update_checksum_without_callbacks!).at_least(:once).and_call_original
+ expect{create(:time_table_date, time_table: time_table, date: 1.year.ago)}.to change{time_table.checksum}
+ end
+
+ it "changes when a period is updated" do
+ time_table = create(:time_table)
+ expect{time_table.periods.last.update_attribute(:period_start, Time.now)}.to change{time_table.reload.checksum}
+ end
+
+ it "changes when a period is added" do
+ time_table = create(:time_table)
+ expect(time_table).to receive(:update_checksum_without_callbacks!).at_least(:once).and_call_original
+ expect{create(:time_table_period, time_table: time_table)}.to change{time_table.checksum}
+ end
end
describe "#excluded_days" do
@@ -1068,8 +1101,6 @@ end
end
end
-
-
describe "#effective_days" do
before do
subject.periods.clear
@@ -1094,8 +1125,6 @@ end
end
end
-
-
describe "#optimize_overlapping_periods" do
before do
subject.periods.clear
@@ -1187,4 +1216,99 @@ end
expect(subject.tag_list.size).to eq(2)
end
end
+
+ describe "#intersect_periods!" do
+ let(:time_table) { Chouette::TimeTable.new }
+ let(:periods) do
+ [
+ Date.new(2018, 1, 1)..Date.new(2018, 2, 1),
+ ]
+ end
+
+ it "remove a date not included in given periods" do
+ time_table.dates.build date: Date.new(2017,12,31)
+ time_table.intersect_periods! periods
+ expect(time_table.dates).to be_empty
+ end
+
+ it "keep a date included in given periods" do
+ time_table.dates.build date: Date.new(2018,1,15)
+ expect{time_table.intersect_periods! periods}.to_not change(time_table, :dates)
+ end
+
+ it "remove a period not included in given periods" do
+ time_table.periods.build period_start: Date.new(2017,12,1), period_end: Date.new(2017,12,31)
+ time_table.intersect_periods! periods
+ expect(time_table.periods).to be_empty
+ end
+
+ it "modify a start period if not included in given periods" do
+ period = time_table.periods.build period_start: Date.new(2017,12,1), period_end: Date.new(2018,1,15)
+ time_table.intersect_periods! periods
+ expect(period.period_start).to eq(Date.new(2018, 1, 1))
+ end
+
+ it "modify a end period if not included in given periods" do
+ period = time_table.periods.build period_start: Date.new(2018,1,15), period_end: Date.new(2018,3,1)
+ time_table.intersect_periods! periods
+ expect(period.period_end).to eq(Date.new(2018, 2, 1))
+ end
+
+ it "keep a period included in given periods" do
+ time_table.periods.build period_start: Date.new(2018,1,10), period_end: Date.new(2018,1,20)
+ expect{time_table.intersect_periods! periods}.to_not change(time_table, :periods)
+ end
+
+ end
+
+ describe "#remove_periods!" do
+ let(:time_table) { Chouette::TimeTable.new }
+ let(:periods) do
+ [
+ Date.new(2018, 1, 1)..Date.new(2018, 2, 1),
+ ]
+ end
+
+ it "remove a date included in given periods" do
+ time_table.dates.build date: Date.new(2018,1,15)
+ time_table.remove_periods! periods
+ expect(time_table.dates).to be_empty
+ end
+
+ it "keep a date not included in given periods" do
+ time_table.dates.build date: Date.new(2017,12,31)
+ expect{time_table.remove_periods! periods}.to_not change(time_table, :dates)
+ end
+
+ it "modify a end period if included in given periods" do
+ period = time_table.periods.build period_start: Date.new(2017,12,1), period_end: Date.new(2018,1,15)
+ time_table.remove_periods! periods
+ expect(period.period_end).to eq(Date.new(2017, 12, 31))
+ end
+
+ it "modify a start period if included in given periods" do
+ period = time_table.periods.build period_start: Date.new(2018,1,15), period_end: Date.new(2018,3,1)
+ time_table.remove_periods! periods
+ expect(period.period_start).to eq(Date.new(2018, 2, 2))
+ end
+
+ it "remove a period included in given periods" do
+ time_table.periods.build period_start: Date.new(2018,1,10), period_end: Date.new(2018,1,20)
+ time_table.remove_periods! periods
+ expect(time_table.periods).to be_empty
+ end
+
+ it "split a period including a given period" do
+ time_table.periods.build period_start: Date.new(2017,12,1), period_end: Date.new(2018,3,1)
+ time_table.remove_periods! periods
+
+ expected_ranges = [
+ Date.new(2017,12,1)..Date.new(2017,12,31),
+ Date.new(2018,2,2)..Date.new(2018,3,1)
+ ]
+ expect(time_table.periods.map(&:range)).to eq(expected_ranges)
+ end
+
+ end
+
end
diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb
index df8a630fe..a97559a0c 100644
--- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb
+++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb
@@ -1,10 +1,12 @@
require 'spec_helper'
RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do
+ subject { create(:vehicle_journey_at_stop) }
+
describe 'checksum' do
let(:at_stop) { build_stubbed(:vehicle_journey_at_stop) }
- it_behaves_like 'checksum support', :vehicle_journey_at_stop
+ it_behaves_like 'checksum support'
context '#checksum_attributes' do
it 'should return attributes' do
@@ -40,6 +42,41 @@ RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do
end
end
+ context "the different times" do
+ let (:at_stop) { build_stubbed(:vehicle_journey_at_stop) }
+
+ describe "without a TimeZone" do
+ it "should not offset times" do
+ expect(at_stop.departure).to eq at_stop.departure_local
+ expect(at_stop.arrival).to eq at_stop.arrival_local
+ end
+ end
+
+
+ describe "with a TimeZone" do
+ before(:each) do
+ stop = at_stop.stop_point.stop_area
+ stop.time_zone = "Mexico City"
+ end
+
+ it "should offset times" do
+ expect(at_stop.departure_local).to eq at_stop.send(:format_time, at_stop.departure_time - 6.hours)
+ expect(at_stop.arrival_local).to eq at_stop.send(:format_time, at_stop.arrival_time - 6.hours)
+ end
+
+ context "with a wrong Timezone" do
+ before do
+ at_stop.stop_point.stop_area.time_zone = "Gotham City"
+ end
+
+ it "should not offset times" do
+ expect(at_stop.departure_local).to eq at_stop.send(:format_time, at_stop.departure_time)
+ expect(at_stop.arrival_local).to eq at_stop.send(:format_time, at_stop.arrival_time)
+ end
+ end
+ end
+ end
+
describe "#validate" do
it "displays the proper error message when day offset exceeds the max" do
bad_offset = Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX + 1
diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb
index ac9b21ceb..76e73d9cf 100644
--- a/spec/models/chouette/vehicle_journey_spec.rb
+++ b/spec/models/chouette/vehicle_journey_spec.rb
@@ -1,7 +1,10 @@
require 'spec_helper'
describe Chouette::VehicleJourney, :type => :model do
+ subject { create(:vehicle_journey) }
+
it { is_expected.to be_versioned }
+ it { should have_and_belong_to_many(:purchase_windows) }
it "must be valid with an at-stop day offset of 1" do
vehicle_journey = create(
@@ -20,7 +23,183 @@ describe Chouette::VehicleJourney, :type => :model do
end
describe 'checksum' do
- it_behaves_like 'checksum support', :vehicle_journey
+ it_behaves_like 'checksum support'
+ it "changes when a vjas is updated" do
+ vehicle_journey = create(:vehicle_journey)
+ expect{vehicle_journey.vehicle_journey_at_stops.last.update_attribute(:departure_time, Time.now)}.to change{vehicle_journey.reload.checksum}
+ end
+
+ it "changes when a vjas is added" do
+ vehicle_journey = create(:vehicle_journey)
+ expect(vehicle_journey).to receive(:update_checksum_without_callbacks!).at_least(:once).and_call_original
+ expect{create(:vehicle_journey_at_stop, vehicle_journey: vehicle_journey)}.to change{vehicle_journey.checksum}
+ end
+ end
+
+ describe "#with_stop_area_ids" do
+ subject(:result){Chouette::VehicleJourney.with_stop_area_ids(ids)}
+ let(:ids){[]}
+ let(:common_stop_area){ create :stop_area}
+ let!(:journey_1){ create :vehicle_journey }
+ let!(:journey_2){ create :vehicle_journey }
+
+ before(:each) do
+ journey_1.journey_pattern.stop_points.last.update_attribute :stop_area_id, common_stop_area.id
+ journey_2.journey_pattern.stop_points.last.update_attribute :stop_area_id, common_stop_area.id
+ expect(journey_1.stop_areas).to include(common_stop_area)
+ expect(journey_2.stop_areas).to include(common_stop_area)
+ end
+ context "with no value" do
+ it "should return all journeys" do
+ expect(result).to eq Chouette::VehicleJourney.all
+ end
+ end
+
+ context "with a single value" do
+ let(:ids){[journey_1.stop_areas.first.id]}
+ it "should return all journeys" do
+ expect(result).to eq [journey_1]
+ end
+
+ context "with a common area" do
+ let(:ids){[common_stop_area.id]}
+ it "should return all journeys" do
+ expect(result.to_a.sort).to eq [journey_1, journey_2].sort
+ end
+ end
+ end
+
+ context "with a couple of values" do
+ let(:ids){[journey_1.stop_areas.first.id, common_stop_area.id]}
+ it "should return only the matching journeys" do
+ expect(result).to eq [journey_1]
+ end
+ end
+
+ end
+
+ describe '#in_purchase_window' do
+ let(:start_date){2.month.ago.to_date}
+ let(:end_date){1.month.ago.to_date}
+
+ subject{Chouette::VehicleJourney.in_purchase_window start_date..end_date}
+
+ let!(:without_purchase_window){ create :vehicle_journey }
+ let!(:without_matching_purchase_window){
+ pw = create :purchase_window, referential: Referential.first, date_ranges: [(end_date+1.day..end_date+2.days)]
+ pw2 = create :purchase_window, referential: Referential.first, date_ranges: [(end_date+10.day..end_date+20.days)]
+ create :vehicle_journey, purchase_windows: [pw, pw2]
+ }
+ let!(:included_purchase_window){
+ pw = create :purchase_window, referential: Referential.first, date_ranges: [(start_date..end_date)]
+ pw2 = create :purchase_window, referential: Referential.first
+ create :vehicle_journey, purchase_windows: [pw, pw2]
+ }
+ let!(:overlapping_purchase_window){
+ pw = create :purchase_window, referential: Referential.first, date_ranges: [(end_date..end_date+1.day)]
+ pw2 = create :purchase_window, referential: Referential.first
+ create :vehicle_journey, purchase_windows: [pw, pw2]
+ }
+
+
+ it "should not include VJ with no purchase window" do
+ expect(subject).to_not include without_purchase_window
+ end
+
+ it "should not include VJ with no matching purchase window" do
+ expect(subject).to_not include without_matching_purchase_window
+ end
+
+ it "should include VJ with included purchase window" do
+ expect(subject).to include included_purchase_window
+ end
+
+ it "should include VJ with overlapping purchase_window purchase window" do
+ expect(subject).to include overlapping_purchase_window
+ end
+ end
+
+ describe '#in_time_table' do
+ let(:start_date){2.month.ago.to_date}
+ let(:end_date){1.month.ago.to_date}
+
+ subject{Chouette::VehicleJourney.with_matching_timetable start_date..end_date}
+
+ context "without time table" do
+ let!(:vehicle_journey){ create :vehicle_journey }
+ it "should not include VJ " do
+ expect(subject).to_not include vehicle_journey
+ end
+ end
+
+ context "without a time table matching on a regular day" do
+ let(:timetable){
+ period = create :time_table_period, period_start: start_date-2.day, period_end: start_date
+ create :time_table, periods: [period], dates_count: 0
+ }
+ let!(:vehicle_journey){ create :vehicle_journey, time_tables: [timetable] }
+ it "should include VJ " do
+ expect(subject).to include vehicle_journey
+ end
+ end
+
+ context "without a time table matching on a regular day" do
+ let(:timetable){
+ period = create :time_table_period, period_start: end_date, period_end: end_date+1.day
+ create :time_table, periods: [period], dates_count: 0
+ }
+ let!(:vehicle_journey){ create :vehicle_journey, time_tables: [timetable] }
+ it "should include VJ " do
+ expect(subject).to include vehicle_journey
+ end
+ end
+
+ context "with a time table with a matching period but not the right day" do
+ let(:start_date){end_date - 1.day}
+ let(:end_date){Time.now.end_of_week.to_date}
+
+ let(:timetable){
+ period = create :time_table_period, period_start: start_date-1.month, period_end: end_date+1.month
+ create :time_table, periods: [period], int_day_types: 4 + 8, dates_count: 0
+ }
+ let!(:vehicle_journey){ create :vehicle_journey, time_tables: [timetable] }
+ it "should not include VJ " do
+ expect(subject).to_not include vehicle_journey
+ end
+ end
+
+ context "with a time table with a matching period but day opted-out" do
+ let(:start_date){end_date - 1.day}
+ let(:end_date){Time.now.end_of_week.to_date}
+
+ let(:timetable){
+ tt = create :time_table, dates_count: 0, periods_count: 0
+ create :time_table_period, period_start: start_date-1.month, period_end: start_date-1.day, time_table: tt
+ create(:time_table_date, :date => start_date, in_out: false, time_table: tt)
+ tt
+ }
+ let!(:vehicle_journey){ create :vehicle_journey, time_tables: [timetable] }
+ it "should not include VJ " do
+ expect(subject).to_not include vehicle_journey
+ end
+ end
+
+ context "with a time table with no matching period but not the right extra day" do
+ let(:start_date){end_date - 1.day}
+ let(:end_date){Time.now.end_of_week.to_date}
+
+ let(:timetable){
+ tt = create :time_table, dates_count: 0, periods_count: 0
+ create :time_table_period, period_start: start_date-1.month, period_end: start_date-1.day, time_table: tt
+ create(:time_table_date, :date => start_date, in_out: true, time_table: tt)
+ tt
+ }
+ let!(:vehicle_journey){ create :vehicle_journey, time_tables: [timetable] }
+ it "should include VJ " do
+ expect(subject).to include vehicle_journey
+ end
+ end
+
end
describe "vjas_departure_time_must_be_before_next_stop_arrival_time",
@@ -63,10 +242,12 @@ describe Chouette::VehicleJourney, :type => :model do
at_stop[att.to_s] = vjas.send(att) unless vjas.send(att).nil?
end
- [:arrival_time, :departure_time].map do |att|
- at_stop[att.to_s] = {
- 'hour' => vjas.send(att).strftime('%H'),
- 'minute' => vjas.send(att).strftime('%M'),
+ at_stop["stop_point_objectid"] = vjas&.stop_point&.objectid
+
+ [:arrival, :departure].map do |att|
+ at_stop["#{att}_time"] = {
+ 'hour' => vjas.send("#{att}_local_time").strftime('%H'),
+ 'minute' => vjas.send("#{att}_local_time").strftime('%M'),
}
end
at_stop
@@ -76,7 +257,10 @@ describe Chouette::VehicleJourney, :type => :model do
vj.slice('objectid', 'published_journey_name', 'journey_pattern_id', 'company_id').tap do |item|
item['vehicle_journey_at_stops'] = []
item['time_tables'] = []
+ item['purchase_windows'] = []
item['footnotes'] = []
+ item['purchase_windows'] = []
+ item['custom_fields'] = vj.custom_fields
vj.vehicle_journey_at_stops.each do |vjas|
item['vehicle_journey_at_stops'] << vehicle_journey_at_stop_to_state(vjas)
@@ -91,18 +275,44 @@ describe Chouette::VehicleJourney, :type => :model do
let(:collection) { [state] }
it 'should create new vj from state' do
- new_vj = build(:vehicle_journey, objectid: nil, published_journey_name: 'dummy', route: route, journey_pattern: journey_pattern)
+ create(:custom_field, code: :energy)
+ new_vj = build(:vehicle_journey, objectid: nil, published_journey_name: 'dummy', route: route, journey_pattern: journey_pattern, custom_field_values: {energy: 99})
collection << vehicle_journey_to_state(new_vj)
expect {
Chouette::VehicleJourney.state_update(route, collection)
}.to change {Chouette::VehicleJourney.count}.by(1)
- expect(collection.last['objectid']).not_to be_nil
obj = Chouette::VehicleJourney.last
+ expect(obj).to receive(:after_commit_objectid).and_call_original
+
+ # For some reason we have to force it
+ obj.after_commit_objectid
obj.run_callbacks(:commit)
+ expect(collection.last['objectid']).to eq obj.objectid
expect(obj.published_journey_name).to eq 'dummy'
+ expect(obj.custom_fields["energy"]["value"]).to eq 99
+ end
+
+ it 'should expect local times' do
+ new_vj = build(:vehicle_journey, objectid: nil, published_journey_name: 'dummy', route: route, journey_pattern: journey_pattern)
+ stop_area = create(:stop_area, time_zone: "Mexico City")
+ stop_point = create(:stop_point, stop_area: stop_area)
+ new_vj.vehicle_journey_at_stops << build(:vehicle_journey_at_stop, vehicle_journey: vehicle_journey, stop_point: stop_point)
+ data = vehicle_journey_to_state(new_vj)
+ data['vehicle_journey_at_stops'][0]["departure_time"]["hour"] = "15"
+ data['vehicle_journey_at_stops'][0]["arrival_time"]["hour"] = "12"
+ collection << data
+ expect {
+ Chouette::VehicleJourney.state_update(route, collection)
+ }.to change {Chouette::VehicleJourney.count}.by(1)
+ created = Chouette::VehicleJourney.last.vehicle_journey_at_stops.last
+ expect(created.stop_point).to eq stop_point
+ expect(created.departure_local_time.hour).to_not eq created.departure_time.hour
+ expect(created.arrival_local_time.hour).to_not eq created.arrival_time.hour
+ expect(created.departure_local_time.hour).to eq 15
+ expect(created.arrival_local_time.hour).to eq 12
end
it 'should save vehicle_journey_at_stops of newly created vj' do
@@ -159,6 +369,23 @@ describe Chouette::VehicleJourney, :type => :model do
expect(vehicle_journey.reload.time_tables).to be_empty
end
+ it 'should update vj purchase_windows association from state' do
+ 2.times{state['purchase_windows'] << create(:purchase_window, referential: referential).slice('id', 'name', 'objectid', 'color')}
+ vehicle_journey.update_has_and_belongs_to_many_from_state(state)
+
+ expected = state['purchase_windows'].map{|tt| tt['id']}
+ actual = vehicle_journey.reload.purchase_windows.map(&:id)
+ expect(actual).to match_array(expected)
+ end
+
+ it 'should clear vj purchase_windows association when remove from state' do
+ vehicle_journey.purchase_windows << create(:purchase_window, referential: referential)
+ state['purchase_windows'] = []
+ vehicle_journey.update_has_and_belongs_to_many_from_state(state)
+
+ expect(vehicle_journey.reload.purchase_windows).to be_empty
+ end
+
it 'should update vj footnote association from state' do
2.times{state['footnotes'] << create(:footnote, line: route.line).slice('id', 'code', 'label', 'line_id')}
vehicle_journey.update_has_and_belongs_to_many_from_state(state)
@@ -182,14 +409,24 @@ describe Chouette::VehicleJourney, :type => :model do
expect(vehicle_journey.reload.company_id).to eq state['company']['id']
end
+ it "handles vehicle journey company deletion" do
+ vehicle_journey.update(company: create(:company))
+ state.delete('company')
+ Chouette::VehicleJourney.state_update(route, collection)
+
+ expect(vehicle_journey.reload.company_id).to be_nil
+ end
+
it 'should update vj attributes from state' do
state['published_journey_name'] = 'edited_name'
state['published_journey_identifier'] = 'edited_identifier'
+ state['custom_fields'] = {energy: {value: 99}}
Chouette::VehicleJourney.state_update(route, collection)
expect(state['errors']).to be_nil
expect(vehicle_journey.reload.published_journey_name).to eq state['published_journey_name']
expect(vehicle_journey.reload.published_journey_identifier).to eq state['published_journey_identifier']
+ expect(vehicle_journey.reload.custom_field_value("energy")).to eq 99
end
it 'should return errors when validation failed' do
@@ -379,8 +616,7 @@ describe Chouette::VehicleJourney, :type => :model do
end
describe ".where_departure_time_between" do
- it "selects vehicle journeys whose departure times are between the
- specified range" do
+ it "selects vehicle journeys whose departure times are between the specified range" do
journey_early = create(
:vehicle_journey,
stop_departure_time: '02:00:00'
@@ -395,7 +631,7 @@ describe Chouette::VehicleJourney, :type => :model do
journey_pattern: journey_pattern,
stop_departure_time: '03:00:00'
)
- journey_late = create(
+ create(
:vehicle_journey,
route: route,
journey_pattern: journey_pattern,
diff --git a/spec/models/compliance_check_spec.rb b/spec/models/compliance_check_spec.rb
index f83d78c29..ffa59245c 100644
--- a/spec/models/compliance_check_spec.rb
+++ b/spec/models/compliance_check_spec.rb
@@ -15,4 +15,36 @@ RSpec.describe ComplianceCheck, type: :model do
it { should validate_presence_of :name }
it { should validate_presence_of :code }
it { should validate_presence_of :origin_code }
+
+ describe ".abort_old" do
+ it "changes check sets older than 4 hours to aborted" do
+ Timecop.freeze(Time.now) do
+ old_check_set = create(
+ :compliance_check_set,
+ status: 'pending',
+ created_at: 4.hours.ago - 1.minute
+ )
+ current_check_set = create(:compliance_check_set, status: 'pending')
+
+ ComplianceCheckSet.abort_old
+
+ expect(current_check_set.reload.status).to eq('pending')
+ expect(old_check_set.reload.status).to eq('aborted')
+ end
+ end
+
+ it "doesn't work on check sets with a `finished_status`" do
+ Timecop.freeze(Time.now) do
+ check_set = create(
+ :compliance_check_set,
+ status: 'successful',
+ created_at: 4.hours.ago - 1.minute
+ )
+
+ ComplianceCheckSet.abort_old
+
+ expect(check_set.reload.status).to eq('successful')
+ end
+ end
+ end
end
diff --git a/spec/models/compliance_control_class_level_defaults/generic_attribute_control/pattern_cccld_spec.rb b/spec/models/compliance_control_class_level_defaults/generic_attribute_control/pattern_cccld_spec.rb
index 9610cc796..90bb660ee 100644
--- a/spec/models/compliance_control_class_level_defaults/generic_attribute_control/pattern_cccld_spec.rb
+++ b/spec/models/compliance_control_class_level_defaults/generic_attribute_control/pattern_cccld_spec.rb
@@ -4,4 +4,7 @@ RSpec.describe GenericAttributeControl::Pattern, type: :model do
let( :factory ){ :generic_attribute_control_pattern }
it_behaves_like 'ComplianceControl Class Level Defaults'
+ it_behaves_like 'has target attribute'
+
+ it { should validate_presence_of(:pattern) }
end
diff --git a/spec/models/compliance_control_class_level_defaults/generic_attribute_control/uniqueness_cccld_spec.rb b/spec/models/compliance_control_class_level_defaults/generic_attribute_control/uniqueness_cccld_spec.rb
index e4ab8d2cd..c4874b5a2 100644
--- a/spec/models/compliance_control_class_level_defaults/generic_attribute_control/uniqueness_cccld_spec.rb
+++ b/spec/models/compliance_control_class_level_defaults/generic_attribute_control/uniqueness_cccld_spec.rb
@@ -4,4 +4,5 @@ RSpec.describe GenericAttributeControl::Uniqueness, type: :model do
let( :factory ){ :generic_attribute_control_uniqueness }
it_behaves_like 'ComplianceControl Class Level Defaults'
+ it_behaves_like 'has target attribute'
end
diff --git a/spec/models/compliance_control_spec.rb b/spec/models/compliance_control_spec.rb
index 4267459ea..5cffba58d 100644
--- a/spec/models/compliance_control_spec.rb
+++ b/spec/models/compliance_control_spec.rb
@@ -1,5 +1,15 @@
RSpec.describe ComplianceControl, type: :model do
+ context 'dynamic attributes' do
+ let(:compliance_control1) { build_stubbed :compliance_control }
+ let(:compliance_control2) { build_stubbed :compliance_control, type: 'VehicleJouneyControl::TimeTable' }
+
+ it 'should always return a array' do
+ expect(compliance_control1.class.dynamic_attributes).to be_kind_of Array
+ expect(compliance_control2.class.dynamic_attributes).to be_kind_of Array
+ end
+ end
+
context 'standard validation' do
let(:compliance_control) { build_stubbed :compliance_control }
diff --git a/spec/models/compliance_control_validations/genric_attribute_validation/min_max_validation_spec.rb b/spec/models/compliance_control_validations/genric_attribute_validation/min_max_validation_spec.rb
index 4d30d61e3..138f7aae1 100644
--- a/spec/models/compliance_control_validations/genric_attribute_validation/min_max_validation_spec.rb
+++ b/spec/models/compliance_control_validations/genric_attribute_validation/min_max_validation_spec.rb
@@ -4,5 +4,6 @@ RSpec.describe GenericAttributeControl::MinMax do
subject{ build factory }
it_behaves_like 'has min_max_values'
+ it_behaves_like 'has target attribute'
end
diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb
new file mode 100644
index 000000000..51128b0a2
--- /dev/null
+++ b/spec/models/custom_field_spec.rb
@@ -0,0 +1,35 @@
+require 'rails_helper'
+
+RSpec.describe CustomField, type: :model do
+ let( :vj ){ create :vehicle_journey, custom_field_values: {energy: 99} }
+
+ context "validates" do
+ it { should validate_uniqueness_of(:name).scoped_to(:resource_type, :workgroup_id) }
+ it { should validate_uniqueness_of(:code).scoped_to(:resource_type, :workgroup_id).case_insensitive }
+ end
+
+ context "field access" do
+ let( :custom_field ){ build_stubbed :custom_field }
+
+ it "option's values can be accessed by a key" do
+ expect( custom_field.options['capacity'] ).to eq("0")
+ end
+ end
+
+
+ context "custom fields for a resource" do
+ let!( :fields ){ [create(:custom_field), create(:custom_field, code: :energy)] }
+ let!( :instance_fields ){
+ {
+ fields[0].code => fields[0].slice(:code, :name, :field_type, :options).update(value: nil),
+ "energy" => fields[1].slice(:code, :name, :field_type, :options).update(value: 99)
+ }
+ }
+ it { expect(Chouette::VehicleJourney.custom_fields).to eq(fields) }
+ it { expect(vj.custom_fields).to eq(instance_fields) }
+ end
+
+ context "custom field_values for a resource" do
+ it { expect(vj.custom_field_value("energy")).to eq(99) }
+ end
+end
diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb
index 3e4128865..8b85f151b 100644
--- a/spec/models/import_spec.rb
+++ b/spec/models/import_spec.rb
@@ -10,8 +10,9 @@ RSpec.describe Import, type: :model do
it { should validate_presence_of(:workbench) }
it { should validate_presence_of(:creator) }
- it { should allow_value('file.zip').for(:file).with_message(I18n.t('activerecord.errors.models.import.attributes.file.wrong_file_extension')) }
- it { should_not allow_values('file.json', 'file.png', 'file.pdf').for(:file) }
+ include ActionDispatch::TestProcess
+ it { should allow_value(fixture_file_upload('OFFRE_TRANSDEV_2017030112251.zip')).for(:file) }
+ it { should_not allow_value(fixture_file_upload('users.json')).for(:file).with_message(I18n.t('errors.messages.extension_whitelist_error', extension: '"json"', allowed_types: "zip")) }
let(:workbench_import) {netex_import.parent}
let(:workbench_import_with_completed_steps) do
@@ -28,6 +29,58 @@ RSpec.describe Import, type: :model do
)
end
+ describe ".abort_old" do
+ it "changes imports older than 4 hours to aborted" do
+ Timecop.freeze(Time.now) do
+ old_import = create(
+ :workbench_import,
+ status: 'pending',
+ created_at: 4.hours.ago - 1.minute
+ )
+ current_import = create(:workbench_import, status: 'pending')
+
+ Import.abort_old
+
+ expect(current_import.reload.status).to eq('pending')
+ expect(old_import.reload.status).to eq('aborted')
+ end
+ end
+
+ it "doesn't work on imports with a `finished_status`" do
+ Timecop.freeze(Time.now) do
+ import = create(
+ :workbench_import,
+ status: 'successful',
+ created_at: 4.hours.ago - 1.minute
+ )
+
+ Import.abort_old
+
+ expect(import.reload.status).to eq('successful')
+ end
+ end
+
+ it "only works on the caller type" do
+ Timecop.freeze(Time.now) do
+ workbench_import = create(
+ :workbench_import,
+ status: 'pending',
+ created_at: 4.hours.ago - 1.minute
+ )
+ netex_import = create(
+ :netex_import,
+ status: 'pending',
+ created_at: 4.hours.ago - 1.minute
+ )
+
+ NetexImport.abort_old
+
+ expect(workbench_import.reload.status).to eq('pending')
+ expect(netex_import.reload.status).to eq('aborted')
+ end
+ end
+ end
+
describe "#destroy" do
it "must destroy all child imports" do
netex_import = create(:netex_import)
diff --git a/spec/models/merge_spec.rb b/spec/models/merge_spec.rb
new file mode 100644
index 000000000..92f8f74b1
--- /dev/null
+++ b/spec/models/merge_spec.rb
@@ -0,0 +1,58 @@
+require "rails_helper"
+
+RSpec.describe Merge do
+
+ it "should work" do
+ stop_area_referential = FactoryGirl.create :stop_area_referential
+ 10.times { FactoryGirl.create :stop_area, stop_area_referential: stop_area_referential }
+
+ line_referential = FactoryGirl.create :line_referential
+ company = FactoryGirl.create :company, line_referential: line_referential
+ 10.times { FactoryGirl.create :line, line_referential: line_referential, company: company, network: nil }
+
+ workbench = FactoryGirl.create :workbench, line_referential: line_referential, stop_area_referential: stop_area_referential
+
+ referential_metadata = FactoryGirl.create(:referential_metadata, lines: line_referential.lines.limit(3))
+
+ referential = FactoryGirl.create :referential,
+ workbench: workbench,
+ organisation: workbench.organisation,
+ metadatas: [referential_metadata]
+
+ factor = 1
+
+ referential.switch do
+ line_referential.lines.each do |line|
+ factor.times do
+ stop_areas = stop_area_referential.stop_areas.order("random()").limit(5)
+ FactoryGirl.create :route, line: line, stop_areas: stop_areas, stop_points_count: 0
+ end
+ end
+
+ referential.routes.each do |route|
+ factor.times do
+ FactoryGirl.create :journey_pattern, route: route, stop_points: route.stop_points.sample(3)
+ end
+ end
+
+ referential.journey_patterns.each do |journey_pattern|
+ factor.times do
+ FactoryGirl.create :vehicle_journey, journey_pattern: journey_pattern, company: company
+ end
+ end
+
+ shared_time_table = FactoryGirl.create :time_table
+
+ referential.vehicle_journeys.each do |vehicle_journey|
+ vehicle_journey.time_tables << shared_time_table
+
+ specific_time_table = FactoryGirl.create :time_table
+ vehicle_journey.time_tables << specific_time_table
+ end
+ end
+
+ merge = Merge.create!(workbench: referential.workbench, referentials: [referential, referential])
+ merge.merge!
+ end
+
+end
diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb
index 1217666f7..595b08058 100644
--- a/spec/models/organisation_spec.rb
+++ b/spec/models/organisation_spec.rb
@@ -2,8 +2,19 @@ describe Organisation, :type => :model do
it { should validate_presence_of(:name) }
it { should validate_uniqueness_of(:code) }
- it 'should have a valid factory' do
- expect(FactoryGirl.build(:organisation)).to be_valid
+ subject { build_stubbed :organisation }
+
+ it 'has a valid factory' do
+ expect_it.to be_valid
+ end
+
+ context 'lines_set' do
+ it 'has no lines' do
+ expect( subject.lines_set ).to eq(Set.new())
+ end
+ it 'has two lines' do
+ expect( build_stubbed(:org_with_lines).lines_set ).to eq(Set.new(%w{C00109 C00108}))
+ end
end
# it "create a rule_parameter_set" do
@@ -51,4 +62,26 @@ describe Organisation, :type => :model do
expect{Organisation.portail_sync}.to change{ Organisation.count }.by(4)
end
end
+
+ describe "#has_feature?" do
+
+ let(:organisation) { Organisation.new }
+
+ it 'return false if Organisation features is nil' do
+ organisation.features = nil
+ expect(organisation.has_feature?(:dummy)).to be_falsy
+ end
+
+ it 'return true if Organisation features contains given feature' do
+ organisation.features = %w{present}
+ expect(organisation.has_feature?(:present)).to be_truthy
+ end
+
+ it "return false if Organisation features doesn't contains given feature" do
+ organisation.features = %w{other}
+ expect(organisation.has_feature?(:absent)).to be_falsy
+ end
+
+ end
+
end
diff --git a/spec/models/referential/referential_lock_during_creation_spec.rb b/spec/models/referential/referential_lock_during_creation_spec.rb
new file mode 100644
index 000000000..717a96136
--- /dev/null
+++ b/spec/models/referential/referential_lock_during_creation_spec.rb
@@ -0,0 +1,198 @@
+RSpec.describe Referential, type: :model do
+ let (:workbench) { create(:workbench) }
+
+ def with_a_mutual_lock timeout: false
+ @with_a_mutual_lock = true
+ yield
+ thread_1 = Thread.new do
+ ActiveRecord::Base.transaction do
+ # seize LOCK
+ @locking_thread_content.try :call
+ sleep 10
+ # release LOCK
+ end
+ end
+
+ thread_2 = Thread.new do
+ sleep 5
+ ActiveRecord::Base.transaction do
+ if timeout
+ ActiveRecord::Base.connection.execute "SET lock_timeout = '1s'"
+ end
+ # waits for LOCK, (because of sleep 5)
+ @waiting_thread_content.try :call
+ # when lock was eventually obtained validation failed
+ end
+ end
+
+ thread_1.join
+ if timeout
+ expect do
+ thread_2.join
+ end.to raise_error(TableLockTimeoutError)
+ else
+ thread_2.join
+ end
+ @locking_thread_content = nil
+ @waiting_thread_content = nil
+ @with_a_mutual_lock = false
+ end
+
+ def locking_thread
+ raise "this method is intended to be called inside a `with_a_mutual_lock`" unless @with_a_mutual_lock
+ @locking_thread_content = yield
+ end
+
+ def waiting_thread
+ @waiting_thread_content = yield
+ end
+
+ context "when two identical Referentials are created, only one is saved" do
+ it "works synchronously" do
+ referential_1 = build(
+ :referential,
+ workbench: workbench,
+ organisation: workbench.organisation
+ )
+ referential_2 = referential_1.dup
+ referential_2.slug = "#{referential_1.slug}_different"
+
+ metadata_1 = build(:referential_metadata, referential: nil)
+ metadata_2 = metadata_1.dup
+
+ referential_1.metadatas << metadata_1
+ referential_2.metadatas << metadata_2
+
+ referential_1.save
+ referential_2.save
+
+ expect(referential_1).to be_persisted
+ expect(referential_2).not_to be_persisted
+ end
+
+ context truncation: true do
+ it "works asynchronously" do
+ skip('The truncation strategy breaks all subsequent tests (See #5295)')
+
+ begin
+ referential_1 = build(
+ :referential,
+ workbench: workbench,
+ organisation: workbench.organisation
+ )
+ referential_2 = referential_1.dup
+ referential_2.slug = "#{referential_1.slug}_different"
+ referential_3 = nil
+
+ metadata_1 = build(:referential_metadata, referential: nil)
+ metadata_2 = metadata_1.dup
+
+ referential_1.metadatas << metadata_1
+ referential_2.metadatas << metadata_2
+
+ with_a_mutual_lock do
+ locking_thread do
+ referential_1.save
+ end
+ waiting_thread do
+ referential_2.save
+ referential_3 = create(:referential)
+ end
+ end
+
+ expect(referential_1).to be_persisted
+ expect(referential_2).not_to be_persisted
+ expect(referential_3).to be_persisted
+ ensure
+ Apartment::Tenant.drop(referential_1.slug) if referential_1.persisted?
+ Apartment::Tenant.drop(referential_2.slug) if referential_2.persisted?
+
+ if referential_3.try(:persisted?)
+ Apartment::Tenant.drop(referential_3.slug)
+ end
+ end
+ end
+
+ it "works asynchronously when one is updated" do
+ skip('The truncation strategy breaks all subsequent tests (See #5295)')
+
+ begin
+ referential_1 = nil
+ referential_2 = nil
+
+ ActiveRecord::Base.transaction do
+ referential_1 = create(
+ :referential,
+ workbench: workbench,
+ organisation: workbench.organisation
+ )
+ referential_2 = referential_1.dup
+ referential_2.name = 'Another'
+ referential_2.slug = "#{referential_1.slug}_different"
+ referential_2.save!
+ end
+
+ metadata_2 = build(:referential_metadata, referential: nil)
+ metadata_1 = metadata_2.dup
+ with_a_mutual_lock do
+ locking_thread do
+ referential_1.metadatas_attributes = [metadata_1.attributes]
+ referential_1.save
+ end
+ waiting_thread do
+ referential_2.metadatas_attributes = [metadata_2.attributes]
+ referential_2.save
+ end
+ end
+
+ expect(referential_1).to be_valid
+ expect(referential_2).not_to be_valid
+ ensure
+ Apartment::Tenant.drop(referential_1.slug) if referential_1.persisted?
+ Apartment::Tenant.drop(referential_2.slug) if referential_2.persisted?
+ end
+ end
+ end
+ end
+
+ context "when two Referentials are created at the same time", truncation: true do
+ it "raises an error when the DB lock timeout is reached" do
+ skip('The truncation strategy breaks all subsequent tests (See #5295)')
+
+ begin
+ referential_1 = build(
+ :referential,
+ workbench: workbench,
+ organisation: workbench.organisation
+ )
+ referential_2 = referential_1.dup
+ referential_2.slug = "#{referential_1.slug}_different"
+ referential_3 = nil
+
+ metadata_1 = build(:referential_metadata, referential: nil)
+ metadata_2 = metadata_1.dup
+
+ referential_1.metadatas << metadata_1
+ referential_2.metadatas << metadata_2
+ with_a_mutual_lock timeout: true do
+ locking_thread do
+ referential_1.save
+ end
+ waiting_thread do
+ referential_2.save
+ referential_3 = create(:referential)
+ end
+ end
+
+ expect(referential_1).to be_persisted
+ ensure
+ Apartment::Tenant.drop(referential_1.slug) if referential_1.persisted?
+ Apartment::Tenant.drop(referential_2.slug) if referential_2.persisted?
+
+ if referential_3.try(:persisted?)
+ Apartment::Tenant.drop(referential_3.slug)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/referential_cloning_spec.rb b/spec/models/referential_cloning_spec.rb
index 5acd433ec..815e05a67 100644
--- a/spec/models/referential_cloning_spec.rb
+++ b/spec/models/referential_cloning_spec.rb
@@ -1,6 +1,7 @@
require 'spec_helper'
RSpec.describe ReferentialCloning, :type => :model do
+
it 'should have a valid factory' do
expect(FactoryGirl.build(:referential_cloning)).to be_valid
end
@@ -8,11 +9,79 @@ RSpec.describe ReferentialCloning, :type => :model do
it { should belong_to :source_referential }
it { should belong_to :target_referential }
- describe "ReferentialCloningWorker" do
+ describe 'after commit' do
+ let(:referential_cloning) { FactoryGirl.create(:referential_cloning) }
+
+ it 'invoke clone method' do
+ expect(referential_cloning).to receive(:clone)
+ referential_cloning.run_callbacks(:commit)
+ end
+ end
+
+ describe '#clone' do
let(:referential_cloning) { FactoryGirl.create(:referential_cloning) }
it "should schedule a job in worker" do
- expect{referential_cloning.run_callbacks(:commit)}.to change {ReferentialCloningWorker.jobs.count}.by(1)
+ expect{referential_cloning.clone}.to change {ReferentialCloningWorker.jobs.count}.by(1)
end
end
+
+ describe '#clone!' do
+ let(:source_referential) { Referential.new slug: "source"}
+ let(:target_referential) { Referential.new slug: "target"}
+ let(:referential_cloning) do
+ ReferentialCloning.new source_referential: source_referential,
+ target_referential: target_referential
+ end
+
+ let(:cloner) { double }
+
+ it 'creates a schema cloner with source and target schemas and clone schema' do
+ expect(AF83::SchemaCloner).to receive(:new).with(source_referential.slug, target_referential.slug).and_return(cloner)
+ expect(cloner).to receive(:clone_schema)
+
+ referential_cloning.clone!
+ end
+ end
+
+ describe '#clone_with_status!' do
+ let(:referential_cloning) do
+ ReferentialCloning.new(target_referential: Referential.new(slug: "target"))
+ end
+
+ before do
+ allow(referential_cloning).to receive(:clone!)
+ end
+
+ it 'invokes clone! method' do
+ expect(referential_cloning).to receive(:clone!)
+ referential_cloning.clone_with_status!
+ end
+
+ context 'when clone_schema is performed without error' do
+ it "should have successful status" do
+ referential_cloning.clone_with_status!
+ expect(referential_cloning.status).to eq("successful")
+ end
+ end
+
+ context 'when clone_schema raises an error' do
+ it "should have failed status" do
+ expect(referential_cloning).to receive(:clone!).and_raise("#fail")
+ referential_cloning.clone_with_status!
+ expect(referential_cloning.status).to eq("failed")
+ end
+ end
+
+ it "defines started_at" do
+ referential_cloning.clone_with_status!
+ expect(referential_cloning.started_at).not_to be_nil
+ end
+
+ it "defines ended_at" do
+ referential_cloning.clone_with_status!
+ expect(referential_cloning.ended_at).not_to be_nil
+ end
+
+ end
end
diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb
index d0b1d6447..6d699f759 100644
--- a/spec/models/referential_spec.rb
+++ b/spec/models/referential_spec.rb
@@ -1,13 +1,6 @@
-require 'spec_helper'
-
describe Referential, :type => :model do
let(:ref) { create :workbench_referential, metadatas: [create(:referential_metadata)] }
- # it "create a rule_parameter_set" do
- # referential = create(:referential)
- # expect(referential.rule_parameter_sets.size).to eq(1)
- # end
-
it { should have_many(:metadatas) }
it { should belong_to(:workbench) }
it { should belong_to(:referential_suite) }
@@ -131,4 +124,40 @@ describe Referential, :type => :model do
end
end
end
+
+ context "to be referential_read_only or not to be referential_read_only" do
+ let( :referential ){ build_stubbed( :referential ) }
+
+ context "in the beginning" do
+ it{ expect( referential ).not_to be_referential_read_only }
+ end
+
+ context "after archivation" do
+ before{ referential.archived_at = 1.day.ago }
+ it{ expect( referential ).to be_referential_read_only }
+ end
+
+ context "used in a ReferentialSuite" do
+ before { referential.referential_suite_id = 42 }
+
+ it{ expect( referential ).to be_referential_read_only }
+
+ it "return true to in_referential_suite?" do
+ expect(referential).to be_in_referential_suite
+ end
+
+ it "don't use detect_overlapped_referentials in validation" do
+ expect(referential).to_not receive(:detect_overlapped_referentials)
+ expect(referential).to be_valid
+ end
+ end
+
+ context "archived and finalised" do
+ before do
+ referential.archived_at = 1.month.ago
+ referential.referential_suite_id = 53
+ end
+ it{ expect( referential ).to be_referential_read_only }
+ end
+ end
end
diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb
index 3b9ed6b07..2f1fe39da 100644
--- a/spec/models/workbench_spec.rb
+++ b/spec/models/workbench_spec.rb
@@ -12,6 +12,7 @@ RSpec.describe Workbench, :type => :model do
it { should belong_to(:organisation) }
it { should belong_to(:line_referential) }
it { should belong_to(:stop_area_referential) }
+ it { should belong_to(:workgroup) }
it { should belong_to(:output).class_name('ReferentialSuite') }
it { should have_many(:lines).through(:line_referential) }
@@ -36,7 +37,7 @@ RSpec.describe Workbench, :type => :model do
let(:workbench) { create :workbench, organisation: organisation }
it 'should filter lines based on my organisation functional_scope' do
- ids.insert('STIF:CODIFLIGNE:Line:0000').each do |id|
+ (ids + ['STIF:CODIFLIGNE:Line:0000']).each do |id|
create :line, objectid: id, line_referential: workbench.line_referential
end
lines = workbench.lines
diff --git a/spec/models/workgroup_spec.rb b/spec/models/workgroup_spec.rb
new file mode 100644
index 000000000..ac8d3fc98
--- /dev/null
+++ b/spec/models/workgroup_spec.rb
@@ -0,0 +1,30 @@
+require 'rails_helper'
+
+RSpec.describe Workgroup, type: :model do
+ context "associations" do
+ let( :workgroup ){ build_stubbed :workgroup, line_referential_id: 53, stop_area_referential_id: 42 }
+
+ it{ should have_many(:workbenches) }
+ it{ should validate_uniqueness_of(:name) }
+
+ it 'is not valid without a stop_area_referential' do
+ workgroup.stop_area_referential_id = nil
+ expect( workgroup ).not_to be_valid
+ end
+ it 'is not valid without a line_referential' do
+ workgroup.line_referential_id = nil
+ expect( workgroup ).not_to be_valid
+ end
+ it 'is valid with both assoications' do
+ expect( workgroup ).to be_valid
+ end
+ end
+
+ context "find organisations" do
+ let( :workgroup ){ create :workgroup }
+ let!( :workbench1 ){ create :workbench, workgroup: workgroup }
+ let!( :workbench2 ){ create :workbench, workgroup: workgroup }
+
+ it{ expect( Set.new(workgroup.organisations) ).to eq(Set.new([ workbench1.organisation, workbench2.organisation ])) }
+ end
+end