diff options
Diffstat (limited to 'spec/models')
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 18 | ||||
| -rw-r--r-- | spec/models/clean_up_spec.rb | 64 | ||||
| -rw-r--r-- | spec/models/compliance_check_set_spec.rb | 9 | ||||
| -rw-r--r-- | spec/models/export/resource_spec.rb | 1 | ||||
| -rw-r--r-- | spec/models/import/import_resource_spec.rb | 1 | ||||
| -rw-r--r-- | spec/models/import/import_spec.rb | 35 | ||||
| -rw-r--r-- | spec/models/import/netex_import_spec.rb | 51 | ||||
| -rw-r--r-- | spec/models/merge_spec.rb | 24 | ||||
| -rw-r--r-- | spec/models/referential/referential_oid_format_from_wkbch_spec.rb | 3 | ||||
| -rw-r--r-- | spec/models/referential_spec.rb | 141 | ||||
| -rw-r--r-- | spec/models/route_spec.rb | 5 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 4 |
12 files changed, 285 insertions, 71 deletions
diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index e1cbb57cb..05560f9e3 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -36,6 +36,16 @@ describe Chouette::VehicleJourney, :type => :model do expect{create(:vehicle_journey_at_stop, vehicle_journey: vehicle_journey)}.to change{vehicle_journey.checksum} end + it "changes when a vjas is deleted" do + vehicle_journey = create(:vehicle_journey) + 3.times do + create(:vehicle_journey_at_stop, vehicle_journey: vehicle_journey).run_callbacks(:commit) + end + vjas = vehicle_journey.vehicle_journey_at_stops.last + expect(vehicle_journey).to receive(:update_checksum_without_callbacks!).at_least(:once).and_call_original + expect{vjas.destroy; vjas.run_callbacks(:commit)}.to change{vehicle_journey.reload.checksum} + end + context "when custom_field_values change" do let(:vehicle_journey){ create(:vehicle_journey, custom_field_values: {custom_field.code.to_s => former_value}) } let(:custom_field){ create :custom_field, field_type: :string, code: :energy, name: :energy, resource_type: "VehicleJourney" } @@ -923,7 +933,7 @@ describe Chouette::VehicleJourney, :type => :model do "2000-01-01 #{new_time.hour}:#{new_time.min}:#{new_time.sec} UTC".to_time end - describe "#fill_passing_time_at_borders" do + describe "#fill_passing_times!" do before do start = create :stop_area border = create :stop_area, kind: :non_commercial, area_type: :border @@ -952,7 +962,7 @@ describe Chouette::VehicleJourney, :type => :model do @journey = create :vehicle_journey, journey_pattern: journey_pattern @journey.vehicle_journey_at_stops.destroy_all departure_time = Time.now.noon - @start = create :vehicle_journey_at_stop, stop_point: start_point, vehicle_journey: @journey, departure_time: departure_time + @start = create :vehicle_journey_at_stop, stop_point: start_point, vehicle_journey: @journey, departure_time: departure_time, arrival_time: departure_time @target = create :vehicle_journey_at_stop, stop_point: border_point, vehicle_journey: @journey, arrival_time: nil, departure_time: nil @target_2 = create :vehicle_journey_at_stop, stop_point: border_point_2, vehicle_journey: @journey, arrival_time: nil, departure_time: nil @middle = create :vehicle_journey_at_stop, stop_point: middle_point, vehicle_journey: @journey, arrival_time: @start.arrival_time + 4.hours, departure_time: @start.departure_time + 4.hours @@ -962,7 +972,7 @@ describe Chouette::VehicleJourney, :type => :model do end it "should compute passing time" do - @journey.reload.fill_passing_time_at_borders + @journey.reload.fill_passing_times! expect(@target.reload.arrival_time.to_i).to eq offset_passing_time(@start.reload.departure_time, 1.0/3 * (@middle.reload.arrival_time - @start.departure_time)).to_i expect(@target_2.reload.arrival_time).to eq @target.arrival_time expect(@target.departure_time).to eq @target.arrival_time @@ -979,7 +989,7 @@ describe Chouette::VehicleJourney, :type => :model do end it "should compute passing time" do - @journey.reload.fill_passing_time_at_borders + @journey.reload.fill_passing_times! expect(@target.reload.arrival_time.to_i).to eq offset_passing_time(@start.reload.departure_time, 1.0/3 * (@middle.reload.arrival_time - @start.departure_time)).to_i expect(@target_2.reload.arrival_time).to eq @target.arrival_time expect(@target.departure_time).to eq @target.arrival_time diff --git a/spec/models/clean_up_spec.rb b/spec/models/clean_up_spec.rb index afd2d8721..f39ca2f2b 100644 --- a/spec/models/clean_up_spec.rb +++ b/spec/models/clean_up_spec.rb @@ -264,4 +264,68 @@ RSpec.describe CleanUp, :type => :model do } end end + + describe "#destroy_routes_outside_referential" do + let(:line_referential) { create(:line_referential) } + let(:line) { create(:line, line_referential: line_referential) } + let(:metadata) { create(:referential_metadata, lines: [line]) } + let(:referential) { create(:workbench_referential, metadatas: [metadata]) } + let(:cleaner) { create(:clean_up, referential: referential) } + + it "destroys routes not in the the referential" do + route = create(:route) + + cleaner.destroy_routes_outside_referential + + expect(Chouette::Route.exists?(route.id)).to be false + + line.routes.each do |route| + expect(route).not_to be_destroyed + end + end + + it "cascades destruction of vehicle journeys and journey patterns" do + vehicle_journey = create(:vehicle_journey) + + cleaner.destroy_routes_outside_referential + + expect(Chouette::Route.exists?(vehicle_journey.route.id)).to be false + expect( + Chouette::JourneyPattern.exists?(vehicle_journey.journey_pattern.id) + ).to be false + expect(Chouette::VehicleJourney.exists?(vehicle_journey.id)).to be false + end + end + + describe "#destroy_empty" do + it "calls the appropriate destroy methods" do + cleaner = create(:clean_up) + + expect(cleaner).to receive(:destroy_vehicle_journeys) + expect(cleaner).to receive(:destroy_journey_patterns) + expect(cleaner).to receive(:destroy_routes) + + cleaner.destroy_empty + end + end + + describe "#run_methods" do + let(:cleaner) { create(:clean_up) } + + it "calls methods in the :methods attribute" do + cleaner = create( + :clean_up, + methods: [:destroy_routes_outside_referential] + ) + + expect(cleaner).to receive(:destroy_routes_outside_referential) + cleaner.run_methods + end + + it "doesn't do anything if :methods is nil" do + cleaner = create(:clean_up) + + expect { cleaner.run_methods }.not_to raise_error + end + end end diff --git a/spec/models/compliance_check_set_spec.rb b/spec/models/compliance_check_set_spec.rb index b6f854829..fc32b940b 100644 --- a/spec/models/compliance_check_set_spec.rb +++ b/spec/models/compliance_check_set_spec.rb @@ -12,7 +12,6 @@ RSpec.describe ComplianceCheckSet, type: :model do it { should have_many :compliance_checks } it { should have_many :compliance_check_blocks } - describe "#update_status" do it "updates :status to successful when all resources are OK" do @@ -42,10 +41,8 @@ RSpec.describe ComplianceCheckSet, type: :model do status: 'OK' ) - updated = check_set.update_status - - expect(updated).to be true - expect(check_set.status).to eq('failed') + check_set.update_status + expect(check_set.reload.status).to eq('failed') end it "updates :status to warning when one resource is WARNING" do @@ -63,7 +60,7 @@ RSpec.describe ComplianceCheckSet, type: :model do check_set.update_status - expect(check_set.status).to eq('warning') + expect(check_set.reload.status).to eq('warning') end it "updates :status to successful when resources are IGNORED" do diff --git a/spec/models/export/resource_spec.rb b/spec/models/export/resource_spec.rb index 7537cd2a8..efab5d630 100644 --- a/spec/models/export/resource_spec.rb +++ b/spec/models/export/resource_spec.rb @@ -7,7 +7,6 @@ RSpec.describe Export::Resource, :type => :model do it { should validate_presence_of(:name) } it { should validate_presence_of(:resource_type) } - it { should validate_presence_of(:reference) } describe 'states' do let(:export_resource) { create(:export_resource) } diff --git a/spec/models/import/import_resource_spec.rb b/spec/models/import/import_resource_spec.rb index 7d2eab8f1..f2ba7c203 100644 --- a/spec/models/import/import_resource_spec.rb +++ b/spec/models/import/import_resource_spec.rb @@ -7,7 +7,6 @@ RSpec.describe Import::Resource, :type => :model do it { should validate_presence_of(:name) } it { should validate_presence_of(:resource_type) } - it { should validate_presence_of(:reference) } describe 'states' do let(:import_resource) { create(:import_resource) } diff --git a/spec/models/import/import_spec.rb b/spec/models/import/import_spec.rb index b11c4922c..4a2ae9b26 100644 --- a/spec/models/import/import_spec.rb +++ b/spec/models/import/import_spec.rb @@ -280,36 +280,15 @@ RSpec.describe Import::Base, type: :model do expect(netex_import.referential.ready).to be false end - shared_examples( - "makes child referentials `ready` when status is finished" - ) do |finished_status| - it "makes child referentials `ready` when status is finished" do - workbench_import = create(:workbench_import, status: finished_status) - netex_import = create(:netex_import, parent: workbench_import) - netex_import.referential.update(ready: false) + it "makes child referentials `ready` when status is successful" do + workbench_import = create(:workbench_import, status: 'successful') + netex_import = create(:netex_import, parent: workbench_import) + netex_import.referential.update(ready: false) - workbench_import.update_referentials - netex_import.referential.reload + workbench_import.update_referentials + netex_import.referential.reload - expect(netex_import.referential.ready).to be true - end + expect(netex_import.referential.ready).to be true end - - include_examples( - "makes child referentials `ready` when status is finished", - "successful" - ) - include_examples( - "makes child referentials `ready` when status is finished", - "failed" - ) - include_examples( - "makes child referentials `ready` when status is finished", - "aborted" - ) - include_examples( - "makes child referentials `ready` when status is finished", - "canceled" - ) end end diff --git a/spec/models/import/netex_import_spec.rb b/spec/models/import/netex_import_spec.rb index 6424fbfe1..727b2559d 100644 --- a/spec/models/import/netex_import_spec.rb +++ b/spec/models/import/netex_import_spec.rb @@ -1,38 +1,41 @@ RSpec.describe Import::Netex, type: [:model, :with_commit] do - let( :boiv_iev_uri ){ URI("#{Rails.configuration.iev_url}/boiv_iev/referentials/importer/new?id=#{subject.id}")} + # FIXME call_iev_callback is called from create_with_referential! + # The test process must be refactored - before do - allow(Thread).to receive(:new).and_yield - end + # let( :boiv_iev_uri ){ URI("#{Rails.configuration.iev_url}/boiv_iev/referentials/importer/new?id=#{subject.id}")} - context 'with referential' do - subject{ build( :netex_import, id: random_int ) } + # before do + # allow(Thread).to receive(:new).and_yield + # end - it 'will trigger the Java API' do - with_stubbed_request(:get, boiv_iev_uri) do |request| - with_commit{ subject.save! } - expect(request).to have_been_requested - end - end - end + # context 'with referential' do + # subject{ build( :netex_import, id: random_int ) } - context 'without referential' do - subject { build :netex_import, referential_id: nil } + # it 'will trigger the Java API' do + # with_stubbed_request(:get, boiv_iev_uri) do |request| + # with_commit{ subject.save! } + # expect(request).to have_been_requested + # end + # end + # end - it 'its status is forced to aborted and the Java API is not callled' do - with_stubbed_request(:get, boiv_iev_uri) do |request| - with_commit{ subject.save! } - expect(subject.reload.status).to eq('aborted') - expect(request).not_to have_been_requested - end - end - end + # context 'without referential' do + # subject { build :netex_import, referential_id: nil } + + # it 'its status is forced to aborted and the Java API is not callled' do + # with_stubbed_request(:get, boiv_iev_uri) do |request| + # with_commit{ subject.save! } + # expect(subject.reload.status).to eq('aborted') + # expect(request).not_to have_been_requested + # end + # end + # end describe "#destroy" do it "must destroy its associated Referential if ready: false" do workbench_import = create(:workbench_import) - referential_ready_false = create(:referential, ready: false) + referential_ready_false = create(:referential, status: :pending) referential_ready_true = create(:referential, ready: true) create( :netex_import, diff --git a/spec/models/merge_spec.rb b/spec/models/merge_spec.rb index 242fcddcd..8c3f48272 100644 --- a/spec/models/merge_spec.rb +++ b/spec/models/merge_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Merge do 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 } + 4.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 @@ -19,7 +19,7 @@ RSpec.describe Merge do organisation: workbench.organisation, metadatas: [referential_metadata] - factor = 1 + factor = 2 stop_points_positions = {} routing_constraint_zones = {} @@ -32,7 +32,7 @@ RSpec.describe Merge do end end - referential.routes.each do |route| + referential.routes.each_with_index do |route, index| route.stop_points.each do |sp| sp.set_list_position 0 end @@ -47,9 +47,18 @@ RSpec.describe Merge do routing_constraint_zones[route.id][constraint_zone.checksum] = constraint_zone end - route.reload.update_checksum! + if index.even? + route.wayback = :outbound + else + route.update_column :wayback, :inbound + route.opposite_route = route.opposite_route_candidates.sample + end + route.save! + + route.reload.update_checksum! expect(route.reload.checksum).to_not eq checksum + factor.times do FactoryGirl.create :journey_pattern, route: route, stop_points: route.stop_points.sample(3) end @@ -83,12 +92,14 @@ RSpec.describe Merge do output.routes.each do |route| stop_points = nil old_route = nil + old_opposite_route = nil referential.switch do old_route = Chouette::Route.find_by(checksum: route.checksum) stop_points = {} old_route.routing_constraint_zones.each do |constraint_zone| stop_points[constraint_zone.checksum] = constraint_zone.stop_points.map(&:registration_number) end + old_opposite_route = old_route.opposite_route end routing_constraint_zones[old_route.id].each do |checksum, constraint_zone| new_constraint_zone = route.routing_constraint_zones.where(checksum: checksum).last @@ -99,6 +110,8 @@ RSpec.describe Merge do route.vehicle_journeys.each do |vehicle_journey| expect(vehicle_journey.ignored_routing_contraint_zones.size).to eq vehicle_journey.ignored_routing_contraint_zone_ids.size end + + expect(route.opposite_route&.checksum).to eq(old_opposite_route&.checksum) end # Let's check stop_point positions are respected @@ -109,6 +122,9 @@ RSpec.describe Merge do end end + expect(output.state).to eq :active + expect(referential.reload.state).to eq :archived + end end diff --git a/spec/models/referential/referential_oid_format_from_wkbch_spec.rb b/spec/models/referential/referential_oid_format_from_wkbch_spec.rb index 6783ab55d..a693e80c9 100644 --- a/spec/models/referential/referential_oid_format_from_wkbch_spec.rb +++ b/spec/models/referential/referential_oid_format_from_wkbch_spec.rb @@ -58,7 +58,8 @@ RSpec.describe Referential do stop_area_referential: source.stop_area_referential, created_from: source, objectid_format: source.objectid_format, - metadatas: source.metadatas.map { |m| ReferentialMetadata.new_from(m, functional_scope) }) + metadatas: source.metadatas.map { |m| ReferentialMetadata.new_from(m, functional_scope) }, + ready: false) described_class.new_from( source, functional_scope ) end diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb index 5dfab5348..2ca0e737d 100644 --- a/spec/models/referential_spec.rb +++ b/spec/models/referential_spec.rb @@ -29,6 +29,146 @@ describe Referential, :type => :model do end end + context "creation" do + subject(:referential) { Referential.create name: "test", objectid_format: :netex, organisation: create(:organisation), line_referential: create(:line_referential), stop_area_referential: create(:stop_area_referential) } + it "should activate by default" do + expect(referential).to be_valid + expect(referential.state).to eq :active + end + end + + context ".last_operation" do + subject(:operation){ referential.last_operation } + it "should return nothing" do + expect(operation).to be_nil + end + + context "with a netex import" do + let!(:import) do + import = create :netex_import + import.referential = referential + import.save + import + end + + it "should return the import" do + expect(operation).to eq import + end + end + + context "with 2 netex imports" do + let!(:other_import) do + import = create :netex_import + import.referential = referential + import.save + import + end + let!(:import) do + import = create :netex_import + import.referential = referential + import.save + import + end + + it "should return the last import" do + expect(operation).to eq import + end + end + + context "with a gtfs import" do + let!(:import) do + import = create :gtfs_import + import.referential = referential + import.save + import + end + + it "should return the import" do + expect(operation).to eq import + end + end + + context "with a cleanup" do + let!(:cleanup) do + cleanup = create :clean_up + cleanup.referential = referential + cleanup.save + cleanup + end + + it "should return nothing" do + expect(operation).to be_nil + end + end + end + + context ".state" do + it "should return the expected values" do + referential = build :referential + referential.ready = false + expect(referential.state).to eq :pending + referential.failed_at = Time.now + expect(referential.state).to eq :failed + referential.ready = true + referential.failed_at = nil + expect(referential.state).to eq :active + referential.archived_at = Time.now + expect(referential.state).to eq :archived + end + + context "the scopes" do + it "should filter the referentials" do + referential = create :referential + referential.pending! + expect(Referential.pending).to include referential + expect(Referential.failed).to_not include referential + expect(Referential.active).to_not include referential + expect(Referential.archived).to_not include referential + + referential = create :referential + referential.failed! + expect(Referential.pending).to_not include referential + expect(Referential.failed).to include referential + expect(Referential.active).to_not include referential + expect(Referential.archived).to_not include referential + + referential = create :referential + referential.active! + expect(Referential.pending).to_not include referential + expect(Referential.failed).to_not include referential + expect(Referential.active).to include referential + expect(Referential.archived).to_not include referential + + referential = create :referential + referential.archived! + expect(Referential.pending).to_not include referential + expect(Referential.failed).to_not include referential + expect(Referential.active).to_not include referential + expect(Referential.archived).to include referential + end + end + + context 'pending_while' do + it "should preserve the state" do + referential = create :referential + referential.archived! + expect(referential.state).to eq :archived + referential.pending_while do + expect(referential.state).to eq :pending + end + expect(referential.state).to eq :archived + begin + referential.pending_while do + expect(referential.state).to eq :pending + raise + end + rescue + end + expect(referential.state).to eq :archived + end + end + end + context ".referential_ids_in_periode" do it 'should retrieve referential id in periode range' do range = ref.metadatas.first.periodes.sample @@ -72,6 +212,7 @@ describe Referential, :type => :model do it 'should create a Referential' do ref expect { saved_clone }.to change{Referential.count}.by(1) + expect(saved_clone.state).to eq :pending end xit 'should create a ReferentialCloning' do diff --git a/spec/models/route_spec.rb b/spec/models/route_spec.rb index 58524038b..dbf00ab0b 100644 --- a/spec/models/route_spec.rb +++ b/spec/models/route_spec.rb @@ -2,6 +2,11 @@ require 'spec_helper' RSpec.describe Chouette::Route, :type => :model do subject(:route){ create :route } + context "the checksum" do + it "should change when a stop is removed" do + expect{route.stop_points.last.destroy}.to change {route.reload.checksum} + end + end context "metadatas" do it "should be empty at first" do expect(Chouette::Route.has_metadata?).to be_truthy diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 51ccfccd3..17fa38d4a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -90,8 +90,8 @@ RSpec.describe User, :type => :model do expect(user.name).to eq('Alban Peignier') expect(user.email).to eq('alban.peignier@af83.com') - expect(user.updated_at.utc).to be_within(1.second).of Time.now - expect(user.synced_at.utc).to be_within(1.second).of Time.now + expect(user.updated_at.utc).to be_within(3.second).of Time.now + expect(user.synced_at.utc).to be_within(3.second).of Time.now end it 'should update organisation assignement' do |
