diff options
| -rw-r--r-- | app/models/chouette/route.rb | 2 | ||||
| -rw-r--r-- | app/models/chouette/routing_constraint_zone.rb | 4 | ||||
| -rw-r--r-- | app/models/concerns/checksum_support.rb | 25 | ||||
| -rw-r--r-- | spec/factories/chouette_routes.rb | 1 | ||||
| -rw-r--r-- | spec/models/chouette/footnote_spec.rb | 12 | ||||
| -rw-r--r-- | spec/models/chouette/journey_pattern_spec.rb | 3 | ||||
| -rw-r--r-- | spec/models/chouette/route/route_base_spec.rb | 4 | ||||
| -rw-r--r-- | spec/models/chouette/routing_constraint_zone_spec.rb | 2 | ||||
| -rw-r--r-- | spec/models/chouette/time_table_period_spec.rb | 2 | ||||
| -rw-r--r-- | spec/models/chouette/time_table_spec.rb | 2 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_at_stop_spec.rb | 4 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 4 | ||||
| -rw-r--r-- | spec/support/checksum_support.rb | 67 |
13 files changed, 96 insertions, 36 deletions
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index 47c18af09..5cc5d8b0d 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -133,7 +133,7 @@ module Chouette def checksum_attributes values = self.slice(*['name', 'published_name', 'wayback']).values values.tap do |attrs| - attrs << self.stop_points.sort_by(&:position).map{|sp| "#{sp.stop_area.user_objectid}#{sp.for_boarding}#{sp.for_alighting}" }.join + attrs << self.stop_points.sort_by(&:position).map{|sp| [sp.stop_area.user_objectid, sp.for_boarding, sp.for_alighting]} attrs << self.routing_constraint_zones.map(&:checksum) end end diff --git a/app/models/chouette/routing_constraint_zone.rb b/app/models/chouette/routing_constraint_zone.rb index 903922241..58703598e 100644 --- a/app/models/chouette/routing_constraint_zone.rb +++ b/app/models/chouette/routing_constraint_zone.rb @@ -25,7 +25,9 @@ module Chouette end def checksum_attributes - self.stop_points.map(&:stop_area).map(&:user_objectid) + [ + self.stop_points.map(&:stop_area).map(&:user_objectid) + ] end def stop_points_belong_to_route diff --git a/app/models/concerns/checksum_support.rb b/app/models/concerns/checksum_support.rb index a76995b0f..92103798e 100644 --- a/app/models/concerns/checksum_support.rb +++ b/app/models/concerns/checksum_support.rb @@ -24,10 +24,29 @@ module ChecksumSupport self.attributes.values end + def checksum_replace_nil_or_empty_values values + # Replace empty array by nil & nil by VALUE_FOR_NIL_ATTRIBUTE + values + .map { |x| x.present? && x || VALUE_FOR_NIL_ATTRIBUTE } + .map do |item| + item = + if item.kind_of?(Array) + checksum_replace_nil_or_empty_values(item) + else + item + end + end + end + def current_checksum_source - source = self.checksum_attributes.map{ |x| x unless x.try(:empty?) } - source = source.map{ |x| x || VALUE_FOR_NIL_ATTRIBUTE } - source.map(&:to_s).join(SEPARATOR) + source = checksum_replace_nil_or_empty_values(self.checksum_attributes) + source.map{ |item| + if item.kind_of?(Array) + item.map{ |x| x.kind_of?(Array) ? "(#{x.join(',')})" : x }.join(',') + else + item + end + }.join(SEPARATOR) end def set_current_checksum_source diff --git a/spec/factories/chouette_routes.rb b/spec/factories/chouette_routes.rb index 7443d08bc..92a50b924 100644 --- a/spec/factories/chouette_routes.rb +++ b/spec/factories/chouette_routes.rb @@ -19,6 +19,7 @@ FactoryGirl.define do after(:create) do |route, evaluator| create_list(:stop_point, evaluator.stop_points_count, route: route) route.reload + route.update_checksum! end factory :route_with_journey_patterns do 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 b5eb9004c..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 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/routing_constraint_zone_spec.rb b/spec/models/chouette/routing_constraint_zone_spec.rb index 0282cb8b1..bda6bb04a 100644 --- a/spec/models/chouette/routing_constraint_zone_spec.rb +++ b/spec/models/chouette/routing_constraint_zone_spec.rb @@ -11,7 +11,7 @@ describe Chouette::RoutingConstraintZone, type: :model do 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 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 a501f234a..bb88877b9 100644 --- a/spec/models/chouette/time_table_spec.rb +++ b/spec/models/chouette/time_table_spec.rb @@ -1049,7 +1049,7 @@ 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) diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb index 02306883c..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 diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 7279980a3..76e73d9cf 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -1,6 +1,8 @@ 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) } @@ -21,7 +23,7 @@ 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} diff --git a/spec/support/checksum_support.rb b/spec/support/checksum_support.rb index e02d9f9f3..f8dffb1b7 100644 --- a/spec/support/checksum_support.rb +++ b/spec/support/checksum_support.rb @@ -1,25 +1,23 @@ -shared_examples 'checksum support' do |factory_name| - let(:instance) { create(factory_name) } - +shared_examples 'checksum support' do describe '#current_checksum_source' do let(:attributes) { ['code_value', 'label_value'] } - let(:seperator) { ChecksumSupport::SEPARATOR } + let(:separator) { 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) + allow_any_instance_of(subject.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") + it 'should separate attribute by separator' do + expect(subject.current_checksum_source).to eq("code_value#{separator}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) + source = "code_value#{separator}#{nil_value}" + expect(subject.current_checksum_source).to eq(source) end end @@ -27,27 +25,62 @@ shared_examples 'checksum support' do |factory_name| let(:attributes) { ['code_value', []] } it 'should convert to nil' do - source = "code_value#{seperator}#{nil_value}" - expect(instance.current_checksum_source).to eq(source) + source = "code_value#{separator}#{nil_value}" + expect(subject.current_checksum_source).to eq(source) + end + end + + context 'array value' do + let(:attributes) { [['v1', 'v2', 'v3'], 'code_value'] } + + it 'should convert to list' do + source = "v1,v2,v3#{separator}code_value" + expect(subject.current_checksum_source).to eq(source) + end + end + + context 'array of array value' do + let(:attributes) { [[['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']], 'code_value'] } + + it 'should convert to list' do + source = "(a1,a2,a3),(b1,b2,b3)#{separator}code_value" + expect(subject.current_checksum_source).to eq(source) + end + end + + context 'array of array value, with empty array' do + let(:attributes) { [[['a1', 'a2', 'a3'], []], 'code_value'] } + + it 'should convert to list' do + source = "(a1,a2,a3),-#{separator}code_value" + expect(subject.current_checksum_source).to eq(source) end end end it 'should save checksum on create' do - expect(instance.checksum).to_not be_nil + expect(subject.checksum).to_not be_nil end it 'should save checksum_source' do - expect(instance.checksum_source).to_not be_nil + expect(subject.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).at_least(:once) - instance.save + expect(subject).to receive(:set_current_checksum_source).at_least(:once) + subject.save end it 'should trigger update_checksum on save' do - expect(instance).to receive(:update_checksum).at_least(:once) - instance.save + expect(subject).to receive(:update_checksum).at_least(:once) + subject.save + end + + it "doesn't change the checksum on save if the source hasn't been changed" do + checksum = subject.checksum + + subject.save + + expect(subject.checksum).to eq(checksum) end end |
