diff options
Diffstat (limited to 'spec/models')
15 files changed, 290 insertions, 14 deletions
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..82c3f96bc --- /dev/null +++ b/spec/models/chouette/area_type_spec.rb @@ -0,0 +1,37 @@ +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) ) + end + end + + describe ".find" do + 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/journey_frequency_spec.rb b/spec/models/chouette/journey_frequency_spec.rb index 2e2088a0c..549439d90 100644 --- a/spec/models/chouette/journey_frequency_spec.rb +++ b/spec/models/chouette/journey_frequency_spec.rb @@ -5,7 +5,7 @@ describe Chouette::JourneyFrequency, type: :model do describe '#create' do context 'when valid' do - it 'should be created' do + xit 'should be created', '#5209 probably to be removed with Dead Code Elimination' do journey_frequency = build(:journey_frequency) journey_frequency.vehicle_journey_id = vehicle_journey.id expect(journey_frequency.save!).to be 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_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/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb index c6aeafaf8..9db0f11a5 100644 --- a/spec/models/chouette/stop_area_spec.rb +++ b/spec/models/chouette/stop_area_spec.rb @@ -426,5 +426,66 @@ 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 + + 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/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index ac9b21ceb..eb2a31794 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe Chouette::VehicleJourney, :type => :model do 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( @@ -76,7 +77,9 @@ 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'] = [] vj.vehicle_journey_at_stops.each do |vjas| item['vehicle_journey_at_stops'] << vehicle_journey_at_stop_to_state(vjas) @@ -159,6 +162,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) 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/import_spec.rb b/spec/models/import_spec.rb index 4e8aac3f4..ffb2360c2 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 @@ -128,7 +129,7 @@ RSpec.describe Import, type: :model do it "updates :status to successful when all children are successful" do workbench_import = create(:workbench_import) - create_list( + imports = create_list( :netex_import, 2, parent: workbench_import, @@ -140,7 +141,7 @@ RSpec.describe Import, type: :model do expect(workbench_import.status).to eq('successful') end - it "Updates :status to failed when any child has failed" do + it "updates :status to failed when any child has failed" do workbench_import = create(:workbench_import) [ 'failed', @@ -158,6 +159,24 @@ RSpec.describe Import, type: :model do expect(workbench_import.status).to eq('failed') end + it "updates :status to warning when any child has warning or successful" do + workbench_import = create(:workbench_import) + [ + 'warning', + 'successful' + ].each do |status| + create( + :netex_import, + parent: workbench_import, + status: status + ) + end + + workbench_import.update_status + + expect(workbench_import.status).to eq('warning') + end + it "updates :ended_at to now when status is finished" do workbench_import = create(:workbench_import) create( 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_cloning_spec.rb b/spec/models/referential_cloning_spec.rb index 5acd433ec..4327c98aa 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,69 @@ 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 } + + before do + allow(AF83::SchemaCloner).to receive(:new).and_return cloner + allow(cloner).to receive(:clone_schema) end + + 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 + + context 'when clone_schema is performed without error' do + it "should have successful status" do + referential_cloning.clone! + expect(referential_cloning.status).to eq("successful") + end + end + + context 'when clone_schema raises an error' do + it "should have failed status" do + expect(cloner).to receive(:clone_schema).and_raise("#fail") + referential_cloning.clone! + expect(referential_cloning.status).to eq("failed") + end + end + + it "defines started_at" do + referential_cloning.clone! + expect(referential_cloning.started_at).not_to be_nil + end + + it "defines ended_at" do + referential_cloning.clone! + expect(referential_cloning.ended_at).not_to be_nil + end + end end diff --git a/spec/models/workbench_spec.rb b/spec/models/workbench_spec.rb index 3b9ed6b07..caff00ae4 100644 --- a/spec/models/workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -36,7 +36,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 |
