diff options
| author | Xinhui | 2017-08-08 12:04:50 +0200 |
|---|---|---|
| committer | Xinhui | 2017-08-08 12:44:39 +0200 |
| commit | 117954f2666744b5301a7ffefb0b7d494fd49514 (patch) | |
| tree | 8bd03f0c5b33d3842ae07aa252e194e7d33044da /spec | |
| parent | 2cf8146cdb4a28fe247108f98c5bc7eb05940e05 (diff) | |
| parent | d524e5474a913df296d4f4586eba4b879bad8b6f (diff) | |
| download | chouette-core-117954f2666744b5301a7ffefb0b7d494fd49514.tar.bz2 | |
Merge branch 'checksum'
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/factories/chouette_journey_pattern.rb | 10 | ||||
| -rw-r--r-- | spec/factories/chouette_routes.rb | 1 | ||||
| -rw-r--r-- | spec/factories/chouette_time_table.rb | 8 | ||||
| -rw-r--r-- | spec/factories/chouette_time_table_dates.rb | 5 | ||||
| -rw-r--r-- | spec/factories/chouette_time_table_periods.rb | 7 | ||||
| -rw-r--r-- | spec/factories/chouette_vehicle_journey.rb | 1 | ||||
| -rw-r--r-- | spec/factories/chouette_vehicle_journey_at_stop.rb | 5 | ||||
| -rw-r--r-- | spec/models/chouette/footnote_spec.rb | 19 | ||||
| -rw-r--r-- | spec/models/chouette/journey_pattern_spec.rb | 3 | ||||
| -rw-r--r-- | spec/models/chouette/route/route_base_spec.rb | 7 | ||||
| -rw-r--r-- | spec/models/chouette/routing_constraint_zone_spec.rb | 4 | ||||
| -rw-r--r-- | spec/models/chouette/time_table_period_spec.rb | 8 | ||||
| -rw-r--r-- | spec/models/chouette/time_table_spec.rb | 246 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_at_stop_spec.rb | 15 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 5 | ||||
| -rw-r--r-- | spec/support/checksum_support.rb | 53 |
16 files changed, 131 insertions, 266 deletions
diff --git a/spec/factories/chouette_journey_pattern.rb b/spec/factories/chouette_journey_pattern.rb index bf55b286f..62241f313 100644 --- a/spec/factories/chouette_journey_pattern.rb +++ b/spec/factories/chouette_journey_pattern.rb @@ -1,14 +1,14 @@ FactoryGirl.define do - + factory :journey_pattern_common, :class => Chouette::JourneyPattern do sequence(:name) { |n| "jp name #{n}" } sequence(:published_name) { |n| "jp publishedname #{n}" } sequence(:comment) { |n| "jp comment #{n}" } sequence(:registration_number) { |n| "jp registration_number #{n}" } sequence(:objectid) { |n| "test:JourneyPattern:#{n}" } - + association :route, :factory => :route - + factory :journey_pattern do after(:create) do |j| j.stop_point_ids = j.route.stop_points.map(&:id) @@ -16,7 +16,7 @@ FactoryGirl.define do j.arrival_stop_point_id = j.route.stop_points.last.id end end - + factory :journey_pattern_odd do after(:create) do |j| j.stop_point_ids = j.route.stop_points.select { |sp| sp.position%2==0}.map(&:id) @@ -24,7 +24,7 @@ FactoryGirl.define do j.arrival_stop_point_id = j.stop_point_ids.last end end - + factory :journey_pattern_even do after(:create) do |j| j.stop_point_ids = j.route.stop_points.select { |sp| sp.position%2==1}.map(&:id) diff --git a/spec/factories/chouette_routes.rb b/spec/factories/chouette_routes.rb index c1a9423c5..a707bcbf6 100644 --- a/spec/factories/chouette_routes.rb +++ b/spec/factories/chouette_routes.rb @@ -18,6 +18,7 @@ FactoryGirl.define do after(:create) do |route, evaluator| create_list(:stop_point, evaluator.stop_points_count, route: route) + route.reload end factory :route_with_journey_patterns do diff --git a/spec/factories/chouette_time_table.rb b/spec/factories/chouette_time_table.rb index 6480df79d..b410d4ab8 100644 --- a/spec/factories/chouette_time_table.rb +++ b/spec/factories/chouette_time_table.rb @@ -1,12 +1,4 @@ FactoryGirl.define do - - factory :time_table_date, :class => Chouette::TimeTableDate do - association :time_table, :factory => :time_table - end - - factory :time_table_period, :class => Chouette::TimeTablePeriod do - end - factory :time_table, :class => Chouette::TimeTable do sequence(:comment) { |n| "Timetable #{n}" } sequence(:objectid) { |n| "test:Timetable:#{n}" } diff --git a/spec/factories/chouette_time_table_dates.rb b/spec/factories/chouette_time_table_dates.rb new file mode 100644 index 000000000..62fdb3917 --- /dev/null +++ b/spec/factories/chouette_time_table_dates.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :time_table_date, class: Chouette::TimeTableDate do + association :time_table + end +end diff --git a/spec/factories/chouette_time_table_periods.rb b/spec/factories/chouette_time_table_periods.rb new file mode 100644 index 000000000..66640bbcc --- /dev/null +++ b/spec/factories/chouette_time_table_periods.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :time_table_period, class: Chouette::TimeTablePeriod do + association :time_table + period_start { Date.today } + period_end { Date.today + 1.month } + end +end diff --git a/spec/factories/chouette_vehicle_journey.rb b/spec/factories/chouette_vehicle_journey.rb index e7ecb79ac..d1e00cd1d 100644 --- a/spec/factories/chouette_vehicle_journey.rb +++ b/spec/factories/chouette_vehicle_journey.rb @@ -11,6 +11,7 @@ FactoryGirl.define do end factory :vehicle_journey do + association :company, factory: :company transient do stop_arrival_time '01:00:00' stop_departure_time '03:00:00' diff --git a/spec/factories/chouette_vehicle_journey_at_stop.rb b/spec/factories/chouette_vehicle_journey_at_stop.rb index c452a1317..831e347d4 100644 --- a/spec/factories/chouette_vehicle_journey_at_stop.rb +++ b/spec/factories/chouette_vehicle_journey_at_stop.rb @@ -1,8 +1,9 @@ FactoryGirl.define do - factory :vehicle_journey_at_stop, :class => Chouette::VehicleJourneyAtStop do association :vehicle_journey, :factory => :vehicle_journey + departure_day_offset { 0 } + departure_time { Time.now } + arrival_time { Time.now - 1.hour } end - end diff --git a/spec/models/chouette/footnote_spec.rb b/spec/models/chouette/footnote_spec.rb index 5c09e3931..98d751499 100644 --- a/spec/models/chouette/footnote_spec.rb +++ b/spec/models/chouette/footnote_spec.rb @@ -1,9 +1,22 @@ require 'spec_helper' -describe Chouette::Footnote do - - subject { build(:footnote) } +describe Chouette::Footnote, type: :model do + let(:footnote) { create(:footnote) } it { should validate_presence_of :line } + describe 'checksum' do + it_behaves_like 'checksum support', :footnote + + context '#checksum_attributes' do + it 'should return code and label' do + expected = [footnote.code, footnote.label] + expect(footnote.checksum_attributes).to include(*expected) + end + + it 'should not return other atrributes' do + expect(footnote.checksum_attributes).to_not include(footnote.updated_at) + end + end + end end diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb index 26d220056..047022ade 100644 --- a/spec/models/chouette/journey_pattern_spec.rb +++ b/spec/models/chouette/journey_pattern_spec.rb @@ -1,6 +1,9 @@ require 'spec_helper' describe Chouette::JourneyPattern, :type => :model do + describe 'checksum' do + it_behaves_like 'checksum support', :journey_pattern + end # context 'validate minimum stop_points size' do # let(:journey_pattern) { create :journey_pattern } diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb index 08f201022..c93b311ff 100644 --- a/spec/models/chouette/route/route_base_spec.rb +++ b/spec/models/chouette/route/route_base_spec.rb @@ -1,6 +1,9 @@ RSpec.describe Chouette::Route, :type => :model do subject { create(:route) } + describe 'checksum' do + it_behaves_like 'checksum support', :route + end describe '#objectid' do subject { super().objectid } @@ -62,8 +65,4 @@ RSpec.describe Chouette::Route, :type => :model do end end end - end - - - diff --git a/spec/models/chouette/routing_constraint_zone_spec.rb b/spec/models/chouette/routing_constraint_zone_spec.rb index 0165c369d..054cfb9e6 100644 --- a/spec/models/chouette/routing_constraint_zone_spec.rb +++ b/spec/models/chouette/routing_constraint_zone_spec.rb @@ -9,6 +9,10 @@ describe Chouette::RoutingConstraintZone, type: :model do # shoulda matcher to validate length of array ? xit { is_expected.to validate_length_of(:stop_point_ids).is_at_least(2) } + describe 'checksum' do + it_behaves_like 'checksum support', :routing_constraint_zone + end + describe 'validations' do it 'validates the presence of route_id' do expect { diff --git a/spec/models/chouette/time_table_period_spec.rb b/spec/models/chouette/time_table_period_spec.rb index 07dc602cb..cc1a3ae09 100644 --- a/spec/models/chouette/time_table_period_spec.rb +++ b/spec/models/chouette/time_table_period_spec.rb @@ -4,11 +4,15 @@ describe Chouette::TimeTablePeriod, :type => :model do let!(:time_table) { create(:time_table)} subject { create(:time_table_period ,:time_table => time_table, :period_start => Date.new(2014,6,30), :period_end => Date.new(2014,7,6) ) } - let!(:p2) {create(:time_table_period ,:time_table => time_table, :period_start => Date.new(2014,7,6), :period_end => Date.new(2014,7,14) ) } + let!(:p2) {create(:time_table_period ,:time_table => time_table, :period_start => Date.new(2014,7,6), :period_end => Date.new(2014,7,14) ) } it { is_expected.to validate_presence_of :period_start } it { is_expected.to validate_presence_of :period_end } - + + describe 'checksum' do + it_behaves_like 'checksum support', :time_table_period + end + describe "#overlap" do context "when periods intersect, " do it "should detect period overlap" do diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb index f13e13d52..c4eaeaaf0 100644 --- a/spec/models/chouette/time_table_spec.rb +++ b/spec/models/chouette/time_table_spec.rb @@ -840,252 +840,15 @@ end :period_end => Date.new(2013, 05, 30)) expect(time_table.intersects([ Date.new(2013, 05, 27), Date.new(2013, 05, 28)])).to eq([ Date.new(2013, 05, 27), Date.new(2013, 05, 28)]) end - - - end - - describe "#include_day?" do - it "should return true if a date equal day" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1") - time_table.dates << Chouette::TimeTableDate.new( :date => Date.today, :in_out => true) - expect(time_table.include_day?(Date.today)).to eq(true) - end - - it "should return true if a period include day" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12) # Day type monday and tuesday - time_table.periods << Chouette::TimeTablePeriod.new( - :period_start => Date.new(2013, 05, 27), - :period_end => Date.new(2013, 05, 29)) - expect(time_table.include_day?( Date.new(2013, 05, 27))).to eq(true) - end - end - - describe "#include_in_dates?" do - it "should return true if a date equal day" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1") - time_table.dates << Chouette::TimeTableDate.new( :date => Date.today, :in_out => true) - expect(time_table.include_in_dates?(Date.today)).to eq(true) - end - - it "should return false if a period include day but that is exclued" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12) # Day type monday and tuesday - excluded_date = Date.new(2013, 05, 27) - time_table.dates << Chouette::TimeTableDate.new( :date => excluded_date, :in_out => false) - expect(time_table.include_in_dates?( excluded_date)).to be_falsey - end - end - - describe "#include_in_periods?" do - it "should return true if a period include day" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 4) - time_table.periods << Chouette::TimeTablePeriod.new( - :period_start => Date.new(2012, 1, 1), - :period_end => Date.new(2012, 01, 30)) - expect(time_table.include_in_periods?(Date.new(2012, 1, 2))).to eq(true) - end - - it "should return false if a period include day but that is exclued" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 12) # Day type monday and tuesday - excluded_date = Date.new(2013, 05, 27) - time_table.dates << Chouette::TimeTableDate.new( :date => excluded_date, :in_out => false) - time_table.periods << Chouette::TimeTablePeriod.new( - :period_start => Date.new(2013, 05, 27), - :period_end => Date.new(2013, 05, 29)) - expect(time_table.include_in_periods?( excluded_date)).to be_falsey - end - end - - describe "#include_in_overlap_dates?" do - it "should return true if a day is included in overlap dates" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 4) - time_table.periods << Chouette::TimeTablePeriod.new( - :period_start => Date.new(2012, 1, 1), - :period_end => Date.new(2012, 01, 30)) - time_table.dates << Chouette::TimeTableDate.new( :date => Date.new(2012, 1, 2), :in_out => true) - expect(time_table.include_in_overlap_dates?(Date.new(2012, 1, 2))).to eq(true) - end - it "should return false if the day is excluded" do - time_table = Chouette::TimeTable.create!(:comment => "Test", :objectid => "test:Timetable:1", :int_day_types => 4) - time_table.periods << Chouette::TimeTablePeriod.new( - :period_start => Date.new(2012, 1, 1), - :period_end => Date.new(2012, 01, 30)) - time_table.dates << Chouette::TimeTableDate.new( :date => Date.new(2012, 1, 2), :in_out => false) - expect(time_table.include_in_overlap_dates?(Date.new(2012, 1, 2))).to be_falsey - end - end - - describe "#dates" do - it "should have with position 0" do - expect(subject.dates.first.position).to eq(0) - end - context "when first date has been removed" do - before do - subject.dates.first.destroy - end - it "should begin with position 0" do - expect(subject.dates.first.position).to eq(0) - end - end - end - describe "#validity_out_between?" do - let(:empty_tm) {build(:time_table)} - it "should be false if empty calendar" do - expect(empty_tm.validity_out_between?( Date.today, Date.today + 7.day)).to be_falsey - end - it "should be true if caldendar is out during start_date and end_date period" do - start_date = subject.bounding_dates.max - 2.day - end_date = subject.bounding_dates.max + 2.day - expect(subject.validity_out_between?( start_date, end_date)).to be_truthy - end - it "should be false if calendar is out on start date" do - start_date = subject.bounding_dates.max - end_date = subject.bounding_dates.max + 2.day - expect(subject.validity_out_between?( start_date, end_date)).to be_falsey - end - it "should be false if calendar is out on end date" do - start_date = subject.bounding_dates.max - 2.day - end_date = subject.bounding_dates.max - expect(subject.validity_out_between?( start_date, end_date)).to be_truthy - end - it "should be false if calendar is out after start_date" do - start_date = subject.bounding_dates.max + 2.day - end_date = subject.bounding_dates.max + 4.day - expect(subject.validity_out_between?( start_date, end_date)).to be_falsey - end - end - describe "#validity_out_from_on?" do - let(:empty_tm) {build(:time_table)} - it "should be false if empty calendar" do - expect(empty_tm.validity_out_from_on?( Date.today)).to be_falsey - end - it "should be true if caldendar ends on expected date" do - expected_date = subject.bounding_dates.max - expect(subject.validity_out_from_on?( expected_date)).to be_truthy - end - it "should be true if calendar ends before expected date" do - expected_date = subject.bounding_dates.max + 30.day - expect(subject.validity_out_from_on?( expected_date)).to be_truthy - end - it "should be false if calendars ends after expected date" do - expected_date = subject.bounding_dates.max - 30.day - expect(subject.validity_out_from_on?( expected_date)).to be_falsey - end - end - describe "#bounding_dates" do - context "when timetable contains only periods" do - before do - subject.dates = [] - subject.save - end - it "should retreive periods.period_start.min and periods.period_end.max" do - expect(subject.bounding_dates.min).to eq(subject.periods.map(&:period_start).min) - expect(subject.bounding_dates.max).to eq(subject.periods.map(&:period_end).max) - end - end - context "when timetable contains only dates" do - before do - subject.periods = [] - subject.save - end - it "should retreive dates.min and dates.max" do - expect(subject.bounding_dates.min).to eq(subject.dates.map(&:date).min) - expect(subject.bounding_dates.max).to eq(subject.dates.map(&:date).max) - end - end - it "should contains min date" do - min_date = subject.bounding_dates.min - subject.dates.each do |tm_date| - expect(min_date <= tm_date.date).to be_truthy - end - subject.periods.each do |tm_period| - expect(min_date <= tm_period.period_start).to be_truthy - end - - end - it "should contains max date" do - max_date = subject.bounding_dates.max - subject.dates.each do |tm_date| - expect(tm_date.date <= max_date).to be_truthy - end - subject.periods.each do |tm_period| - expect(tm_period.period_end <= max_date).to be_truthy - end - - end - end - describe "#periods" do - it "should begin with position 0" do - expect(subject.periods.first.position).to eq(0) - end - context "when first period has been removed" do - before do - subject.periods.first.destroy - end - it "should begin with position 0" do - expect(subject.periods.first.position).to eq(0) - end - end - it "should have period_start before period_end" do - period = Chouette::TimeTablePeriod.new - period.period_start = Date.today - period.period_end = Date.today + 10 - expect(period.valid?).to be_truthy - end - it "should not have period_start after period_end" do - period = Chouette::TimeTablePeriod.new - period.period_start = Date.today - period.period_end = Date.today - 10 - expect(period.valid?).to be_falsey - end - it "should not have period_start equal to period_end" do - period = Chouette::TimeTablePeriod.new - period.period_start = Date.today - period.period_end = Date.today - expect(period.valid?).to be_falsey - end end - describe "#effective_days_of_periods" do - before do - subject.periods.clear - subject.periods << Chouette::TimeTablePeriod.new( - :period_start => Date.new(2014, 6, 30), - :period_end => Date.new(2014, 7, 6)) - subject.int_day_types = 4|8|16 - end - it "should return monday to wednesday" do - expect(subject.effective_days_of_periods.size).to eq(3) - expect(subject.effective_days_of_periods[0]).to eq(Date.new(2014, 6, 30)) - expect(subject.effective_days_of_periods[1]).to eq(Date.new(2014, 7, 1)) - expect(subject.effective_days_of_periods[2]).to eq(Date.new(2014, 7, 2)) - end - it "should return thursday" do - expect(subject.effective_days_of_periods(Chouette::TimeTable.valid_days(32)).size).to eq(1) - expect(subject.effective_days_of_periods(Chouette::TimeTable.valid_days(32))[0]).to eq(Date.new(2014, 7, 3)) - end - - end + # it { is_expected.to validate_presence_of :comment } + # it { is_expected.to validate_uniqueness_of :objectid } - describe "#included_days" do - before do - subject.dates.clear - subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,16), :in_out => true) - subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,17), :in_out => false) - subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,18), :in_out => true) - subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,19), :in_out => false) - subject.dates << Chouette::TimeTableDate.new( :date => Date.new(2014,7,20), :in_out => true) - end - it "should return 3 dates" do - days = subject.included_days - expect(days.size).to eq(3) - expect(days[0]).to eq(Date.new(2014, 7, 16)) - expect(days[1]).to eq(Date.new(2014,7, 18)) - expect(days[2]).to eq(Date.new(2014, 7,20)) - end + describe 'checksum' do + it_behaves_like 'checksum support', :time_table end - - describe "#excluded_days" do before do subject.dates.clear @@ -1223,5 +986,4 @@ end expect(subject.tag_list.size).to eq(2) 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 d999ed1a8..4f9d12730 100644 --- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb +++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb @@ -1,6 +1,21 @@ require 'spec_helper' RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do + describe 'checksum' do + let(:at_stop) { build_stubbed(:vehicle_journey_at_stop) } + + it_behaves_like 'checksum support', :vehicle_journey_at_stop + + context '#checksum_attributes' do + it 'should return attributes' do + expected = [at_stop.departure_time.to_s(:time), at_stop.arrival_time.to_s(:time)] + expected << at_stop.departure_day_offset.to_s + expected << at_stop.arrival_day_offset.to_s + expect(at_stop.checksum_attributes).to include(*expected) + end + end + end + describe "#day_offset_outside_range?" do let (:at_stop) { build_stubbed(:vehicle_journey_at_stop) } diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 3c04a77cc..52f2ab42d 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -17,8 +17,13 @@ describe Chouette::VehicleJourney, :type => :model do expect(vehicle_journey).to be_valid end + describe 'checksum' do + it_behaves_like 'checksum support', :vehicle_journey + end + describe "vjas_departure_time_must_be_before_next_stop_arrival_time", skip: "Validation currently commented out because it interferes with day offsets" do + let(:vehicle_journey) { create :vehicle_journey } let(:vjas) { vehicle_journey.vehicle_journey_at_stops } diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb new file mode 100644 index 000000000..14ea3c55e --- /dev/null +++ b/spec/support/checksum_support.rb @@ -0,0 +1,53 @@ +shared_examples 'checksum support' do |factory_name| + let(:instance) { create(factory_name) } + + describe '#current_checksum_source' do + let(:attributes) { ['code_value', 'label_value'] } + let(:seperator) { ChecksumSupport::SEPARATOR } + let(:nil_value) { ChecksumSupport::VALUE_FOR_NIL_ATTRIBUTE } + + before do + allow_any_instance_of(instance.class).to receive(:checksum_attributes).and_return(attributes) + end + + it 'should separate attribute by seperator' do + expect(instance.current_checksum_source).to eq("code_value#{seperator}label_value") + end + + context 'nil value' do + let(:attributes) { ['code_value', nil] } + + it 'should replace nil attributes by default value' do + source = "code_value#{seperator}#{nil_value}" + expect(instance.current_checksum_source).to eq(source) + end + end + + context 'empty array' do + let(:attributes) { ['code_value', []] } + + it 'should convert to nil' do + source = "code_value#{seperator}#{nil_value}" + expect(instance.current_checksum_source).to eq(source) + end + end + end + + it 'should save checksum on create' do + expect(instance.checksum).to_not be_nil + end + + it 'should save checksum_source' do + expect(instance.checksum_source).to_not be_nil + end + + it 'should trigger set_current_checksum_source on save' do + expect(instance).to receive(:set_current_checksum_source) + instance.save + end + + it 'should trigger update_checksum on save' do + expect(instance).to receive(:update_checksum) + instance.save + end +end |
