diff options
| author | Luc Donnet | 2018-03-12 15:08:32 +0100 |
|---|---|---|
| committer | GitHub | 2018-03-12 15:08:32 +0100 |
| commit | 43a5e136c1b35483675918120405cd4f5bea3397 (patch) | |
| tree | 5faa64a1bfac1bda0152941cece46fc28279e06e /spec | |
| parent | ba30cc2c8772dbd1934d032e2a3a6a66795df4e2 (diff) | |
| parent | c5d5eef9f107008a29536321fa0ff56209620632 (diff) | |
| download | chouette-core-43a5e136c1b35483675918120405cd4f5bea3397.tar.bz2 | |
Merge pull request #362 from af83/6133-new-exports
6133 New exports
Diffstat (limited to 'spec')
37 files changed, 569 insertions, 453 deletions
diff --git a/spec/controllers/api/v1/imports_controller_spec.rb b/spec/controllers/api/v1/imports_controller_spec.rb index 8077dd052..f7022115a 100644 --- a/spec/controllers/api/v1/imports_controller_spec.rb +++ b/spec/controllers/api/v1/imports_controller_spec.rb @@ -30,7 +30,7 @@ RSpec.describe Api::V1::ImportsController, type: :controller do it 'should be successful' do expect { post :create, workbench_id: workbench.id, workbench_import: {name: "test", file: file, creator: 'test'}, format: :json - }.to change{WorkbenchImport.count}.by(1) + }.to change{Import::Workbench.count}.by(1) expect(response).to be_success end end diff --git a/spec/controllers/exports_controller_spec.rb b/spec/controllers/exports_controller_spec.rb index 6cd6e4c54..3a67497ec 100644 --- a/spec/controllers/exports_controller_spec.rb +++ b/spec/controllers/exports_controller_spec.rb @@ -1,22 +1,97 @@ -require 'spec_helper' - -describe ExportsController, :type => :controller do +RSpec.describe ExportsController, :type => :controller do login_user - describe "GET 'new'" do - it "returns http success" do - pending - get 'new' + let(:workbench) { create :workbench } + let(:export) { create(:netex_export, workbench: workbench) } + + describe 'GET #new' do + it 'should be successful if authorized' do + get :new, workbench_id: workbench.id expect(response).to be_success end + + it 'should be unsuccessful unless authorized' do + remove_permissions('exports.create', from_user: @user, save: true) + get :new, workbench_id: workbench.id + expect(response).not_to be_success + end end - describe "GET 'index'" do - it "returns http success" do - pending - get 'index' - expect(response).to be_success + describe "POST #create" do + let(:params){ {name: "foo"} } + let(:request){ post :create, workbench_id: workbench.id, export: params } + it 'should create no objects' do + expect{request}.to_not change{Export::Base.count} + end + + context "with full params" do + let(:params){{ + name: "foo", + type: "Export::Netex", + duration: 12, + export_type: :line + }} + + it 'should be successful' do + expect{request}.to change{Export::Base.count}.by(1) + end + + it "displays a flash message" do + request + expect(controller).to set_flash[:notice].to( + I18n.t('flash.exports.create.notice') + ) + end + end + + context "with missing options" do + let(:params){{ + name: "foo", + type: "Export::Workgroup" + }} + + it 'should be unsuccessful' do + expect{request}.to change{Export::Base.count}.by(0) + end + end + + context "with all options" do + let(:params){{ + name: "foo", + type: "Export::Workgroup", + duration: 90 + }} + + it 'should be successful' do + expect{request}.to change{Export::Base.count}.by(1) + end + end + + context "with wrong type" do + let(:params){{ + name: "foo", + type: "Export::Foo" + }} + + it 'should be unsuccessful' do + expect{request}.to raise_error ActiveRecord::SubclassNotFound + end end end + describe 'POST #upload' do + context "with the token" do + it 'should be successful' do + post :upload, workbench_id: workbench.id, id: export.id, token: export.token_upload + expect(response).to be_redirect + end + end + + context "without the token" do + it 'should be unsuccessful' do + post :upload, workbench_id: workbench.id, id: export.id, token: "foo" + expect(response).to_not be_success + end + end + end end diff --git a/spec/controllers/vehicle_journey_imports_controller_spec.rb b/spec/controllers/vehicle_journey_imports_controller_spec.rb deleted file mode 100644 index 633f90b70..000000000 --- a/spec/controllers/vehicle_journey_imports_controller_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe ImportTasksController, :type => :controller do - login_user -end diff --git a/spec/factories/exports.rb b/spec/factories/exports.rb deleted file mode 100644 index 34427edb8..000000000 --- a/spec/factories/exports.rb +++ /dev/null @@ -1,5 +0,0 @@ -FactoryGirl.define do - factory :export do - referential { Referential.find_by_slug("first") } - end -end diff --git a/spec/factories/exports/export_messages.rb b/spec/factories/exports/export_messages.rb new file mode 100644 index 000000000..55394ec45 --- /dev/null +++ b/spec/factories/exports/export_messages.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :export_message, class: Export::Message do + association :export + association :resource, factory: :export_resource + criticity :info + end +end diff --git a/spec/factories/exports/export_resources.rb b/spec/factories/exports/export_resources.rb new file mode 100644 index 000000000..8e38235cd --- /dev/null +++ b/spec/factories/exports/export_resources.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :export_resource, class: Export::Resource do + sequence(:name) { |n| "Export resource #{n}" } + association :export, factory: :netex_export + status :WARNING + resource_type 'type' + reference 'reference' + end +end diff --git a/spec/factories/exports/exports.rb b/spec/factories/exports/exports.rb new file mode 100644 index 000000000..c8aaf30a9 --- /dev/null +++ b/spec/factories/exports/exports.rb @@ -0,0 +1,34 @@ +FactoryGirl.define do + factory :export, class: Export::Base do + sequence(:name) { |n| "Export #{n}" } + current_step_id "MyString" + current_step_progress 1.5 + association :workbench + association :referential + status :new + started_at nil + ended_at nil + creator 'rspec' + + after(:build) do |export| + export.class.skip_callback(:create, :before, :initialize_fields) + end + end + + factory :bad_export, class: Export::Base do + sequence(:name) { |n| "Export #{n}" } + current_step_id "MyString" + current_step_progress 1.5 + association :workbench + association :referential + file {File.open(File.join(Rails.root, 'spec', 'fixtures', 'terminated_job.json'))} + status :new + started_at nil + ended_at nil + creator 'rspec' + + after(:build) do |export| + export.class.skip_callback(:create, :before, :initialize_fields) + end + end +end diff --git a/spec/factories/exports/netex_exports.rb b/spec/factories/exports/netex_exports.rb new file mode 100644 index 000000000..0648bbc56 --- /dev/null +++ b/spec/factories/exports/netex_exports.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :netex_export, class: Export::Netex, parent: :export do + association :parent, factory: :workgroup_export + export_type :line + duration 90 + end +end diff --git a/spec/factories/exports/workgroup_exports.rb b/spec/factories/exports/workgroup_exports.rb new file mode 100644 index 000000000..f5dfb6b94 --- /dev/null +++ b/spec/factories/exports/workgroup_exports.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :workgroup_export, class: Export::Workgroup, parent: :export do + duration 90 + end +end diff --git a/spec/factories/import_messages.rb b/spec/factories/imports/import_messages.rb index 5d936679a..f5edf1685 100644 --- a/spec/factories/import_messages.rb +++ b/spec/factories/imports/import_messages.rb @@ -1,5 +1,5 @@ FactoryGirl.define do - factory :import_message do + factory :import_message, class: Import::Message do association :import association :resource, factory: :import_resource criticity :info diff --git a/spec/factories/import_resources.rb b/spec/factories/imports/import_resources.rb index 76afcc486..aaf7e3111 100644 --- a/spec/factories/import_resources.rb +++ b/spec/factories/imports/import_resources.rb @@ -1,5 +1,5 @@ FactoryGirl.define do - factory :import_resource do + factory :import_resource, class: Import::Resource do association :import status :WARNING sequence(:name) { |n| "Import resource #{n}" } diff --git a/spec/factories/imports.rb b/spec/factories/imports/imports.rb index e07447b60..cb7764cc6 100644 --- a/spec/factories/imports.rb +++ b/spec/factories/imports/imports.rb @@ -1,5 +1,5 @@ FactoryGirl.define do - factory :import do + factory :import, class: Import::Base do sequence(:name) { |n| "Import #{n}" } current_step_id "MyString" current_step_progress 1.5 @@ -16,7 +16,7 @@ FactoryGirl.define do end end - factory :bad_import do + factory :bad_import, class: Import::Base do sequence(:name) { |n| "Import #{n}" } current_step_id "MyString" current_step_progress 1.5 diff --git a/spec/factories/netex_imports.rb b/spec/factories/imports/netex_imports.rb index b59267a0a..7ee6839e8 100644 --- a/spec/factories/netex_imports.rb +++ b/spec/factories/imports/netex_imports.rb @@ -1,7 +1,7 @@ FactoryGirl.define do - factory :netex_import, class: NetexImport, parent: :import do + factory :netex_import, class: Import::Netex, parent: :import do file { File.open(Rails.root.join('spec', 'fixtures', 'OFFRE_TRANSDEV_2017030112251.zip')) } association :parent, factory: :workbench_import - + end end diff --git a/spec/factories/workbench_imports.rb b/spec/factories/imports/workbench_imports.rb index 466bfe688..5ed1ee4e5 100644 --- a/spec/factories/workbench_imports.rb +++ b/spec/factories/imports/workbench_imports.rb @@ -1,5 +1,5 @@ FactoryGirl.define do - factory :workbench_import, class: WorkbenchImport, parent: :import do + factory :workbench_import, class: Import::Workbench, parent: :import do file { File.open(Rails.root.join('spec', 'fixtures', 'OFFRE_TRANSDEV_2017030112251.zip')) } end end diff --git a/spec/models/export/export_message_spec.rb b/spec/models/export/export_message_spec.rb new file mode 100644 index 000000000..61a3b6319 --- /dev/null +++ b/spec/models/export/export_message_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe Export::Message, :type => :model do + it { should validate_presence_of(:criticity) } + it { should belong_to(:export) } + it { should belong_to(:resource) } +end diff --git a/spec/models/export/export_resource_spec.rb b/spec/models/export/export_resource_spec.rb new file mode 100644 index 000000000..7537cd2a8 --- /dev/null +++ b/spec/models/export/export_resource_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe Export::Resource, :type => :model do + it { should belong_to(:export) } + + it { should enumerize(:status).in("OK", "ERROR", "WARNING", "IGNORED") } + + 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) } + + it 'should initialize with new state' do + expect(export_resource.status).to eq("WARNING") + end + end +end diff --git a/spec/models/export/export_spec.rb b/spec/models/export/export_spec.rb new file mode 100644 index 000000000..ca94c1ff1 --- /dev/null +++ b/spec/models/export/export_spec.rb @@ -0,0 +1,239 @@ +RSpec.describe Export::Base, type: :model do + + it { should belong_to(:referential) } + it { should belong_to(:workbench) } + it { should belong_to(:parent) } + + it { should enumerize(:status).in("aborted", "canceled", "failed", "new", "pending", "running", "successful", "warning") } + + it { should validate_presence_of(:workbench) } + it { should validate_presence_of(:creator) } + + include ActionDispatch::TestProcess + it { should allow_value(fixture_file_upload('OFFRE_TRANSDEV_2017030112251.zip')).for(:file) } + it { should_not allow_value(fixture_file_upload('reflex_updated.xml')).for(:file).with_message(I18n.t('errors.messages.extension_whitelist_error', extension: '"xml"', allowed_types: "zip, csv, json")) } + + let(:workgroup_export) {netex_export.parent} + let(:workgroup_export_with_completed_steps) do + build_stubbed( + :workgroup_export, + total_steps: 2, + current_step: 2 + ) + end + + let(:netex_export) do + create( + :netex_export + ) + end + + describe ".abort_old" do + it "changes exports older than 4 hours to aborted" do + Timecop.freeze(Time.now) do + old_export = create( + :workgroup_export, + status: 'pending', + created_at: 4.hours.ago - 1.minute + ) + current_export = create(:workgroup_export, status: 'pending') + + Export::Base.abort_old + + expect(current_export.reload.status).to eq('pending') + expect(old_export.reload.status).to eq('aborted') + end + end + + it "doesn't work on exports with a `finished_status`" do + Timecop.freeze(Time.now) do + export = create( + :workgroup_export, + status: 'successful', + created_at: 4.hours.ago - 1.minute + ) + + Export::Base.abort_old + + expect(export.reload.status).to eq('successful') + end + end + + it "only works on the caller type" do + Timecop.freeze(Time.now) do + workgroup_export = create( + :workgroup_export, + status: 'pending', + created_at: 4.hours.ago - 1.minute + ) + netex_export = create( + :netex_export, + status: 'pending', + created_at: 4.hours.ago - 1.minute + ) + + Export::Netex.abort_old + + expect(workgroup_export.reload.status).to eq('pending') + expect(netex_export.reload.status).to eq('aborted') + end + end + end + + describe "#destroy" do + it "must destroy all child exports" do + netex_export = create(:netex_export) + + netex_export.parent.destroy + + expect(netex_export.parent).to be_destroyed + expect(Export::Netex.count).to eq(0) + end + + it "must destroy all associated Export::Messages" do + export = create(:netex_export) + create(:export_resource, export: export) + + export.destroy + + expect(Export::Resource.count).to eq(0) + end + + it "must destroy all associated Export::Resources" do + export = create(:netex_export) + create(:export_message, export: export) + + export.destroy + + expect(Export::Message.count).to eq(0) + end + end + + describe "#notify_parent" do + it "must call #child_change on its parent" do + allow(netex_export).to receive(:update) + + expect(workgroup_export).to receive(:child_change) + netex_export.status = :foo + netex_export.notify_parent + end + + it "must update the :notified_parent_at field of the child export" do + allow(workgroup_export).to receive(:child_change) + + Timecop.freeze(Time.now) do + netex_export.status = :bar + + netex_export.notify_parent + expect(netex_export.notified_parent_at).to eq Time.now + expect(netex_export.reload.notified_parent_at).to eq Time.now + end + end + end + + describe "#child_change" do + it "calls #update_status" do + allow(workgroup_export).to receive(:update) + + expect(workgroup_export).to receive(:update_status) + workgroup_export.child_change + end + end + + describe "#update_status" do + shared_examples( + "updates :status to failed when >=1 child has failing status" + ) do |failure_status| + it "updates :status to failed when >=1 child has failing status" do + workgroup_export = create(:workgroup_export) + create( + :netex_export, + parent: workgroup_export, + status: failure_status + ) + + workgroup_export.update_status + + expect(workgroup_export.status).to eq('failed') + end + end + + include_examples( + "updates :status to failed when >=1 child has failing status", + "failed" + ) + include_examples( + "updates :status to failed when >=1 child has failing status", + "aborted" + ) + include_examples( + "updates :status to failed when >=1 child has failing status", + "canceled" + ) + + it "updates :status to successful when all children are successful" do + workgroup_export = create(:workgroup_export) + exports = create_list( + :netex_export, + 2, + parent: workgroup_export, + status: 'successful' + ) + + workgroup_export.update_status + + expect(workgroup_export.status).to eq('successful') + end + + it "updates :status to failed when any child has failed" do + workgroup_export = create(:workgroup_export) + [ + 'failed', + 'successful' + ].each do |status| + create( + :netex_export, + parent: workgroup_export, + status: status + ) + end + + workgroup_export.update_status + + expect(workgroup_export.status).to eq('failed') + end + + it "updates :status to warning when any child has warning or successful" do + workgroup_export = create(:workgroup_export) + [ + 'warning', + 'successful' + ].each do |status| + create( + :netex_export, + parent: workgroup_export, + status: status + ) + end + + workgroup_export.update_status + + expect(workgroup_export.status).to eq('warning') + end + + it "updates :ended_at to now when status is finished" do + workgroup_export = create(:workgroup_export) + create( + :netex_export, + parent: workgroup_export, + status: 'failed' + ) + + Timecop.freeze(Time.now) do + workgroup_export.update_status + + expect(workgroup_export.ended_at).to eq(Time.now) + end + end + end +end diff --git a/spec/models/export/netex_export_spec.rb b/spec/models/export/netex_export_spec.rb new file mode 100644 index 000000000..d9cccd6ad --- /dev/null +++ b/spec/models/export/netex_export_spec.rb @@ -0,0 +1,19 @@ +RSpec.describe Export::Netex, type: [:model, :with_commit] do + + let( :boiv_iev_uri ){ URI("#{Rails.configuration.iev_url}/boiv_iev/referentials/exporter/new?id=#{subject.id}")} + + before do + allow(Thread).to receive(:new).and_yield + end + + context 'with referential' do + subject{ build( :netex_export, id: random_int ) } + + 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 +end diff --git a/spec/models/export/workgroup_export_spec.rb b/spec/models/export/workgroup_export_spec.rb new file mode 100644 index 000000000..c812b2b21 --- /dev/null +++ b/spec/models/export/workgroup_export_spec.rb @@ -0,0 +1,10 @@ +RSpec.describe Export::Workgroup, type: [:model, :with_commit] do + it { should validate_presence_of(:duration) } + + it "should set options" do + expect(Export::Workgroup.options).to have_key :duration + expect(Export::Workgroup.options[:duration][:required]).to be_truthy + expect(Export::Workgroup.options[:duration][:default_value]).to eq 90 + expect(Export::Workgroup.options[:duration][:type]).to eq :integer + end +end diff --git a/spec/models/export_log_message_spec.rb b/spec/models/export_log_message_spec.rb deleted file mode 100644 index 5ab32dec0..000000000 --- a/spec/models/export_log_message_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'spec_helper' - -describe ExportLogMessage, :type => :model do - - # describe "#attributes" do - - # subject { create :export_log_message } - - # it "should read json stored in database" do - # subject.update_attribute :arguments, { "key" => "value"} - # expect(subject.raw_attributes).to eq({ "key" => "value"}.to_json) - # end - - # end - -end diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb deleted file mode 100644 index 13953078a..000000000 --- a/spec/models/export_spec.rb +++ /dev/null @@ -1,66 +0,0 @@ -# require 'spec_helper' - -# describe Export, :type => :model do - -# subject { create :export } - -# RSpec::Matchers.define :be_log_message do |expected| -# match do |actual| -# actual and expected.all? { |k,v| actual[k.to_s] == v } -# end -# end - -# describe "#export" do - -# before(:each) do -# allow(subject).to receive_messages :exporter => double(:export => true) -# end - -# it "should create a ExportLogmessage :started when started" do -# subject.export -# expect(subject.log_messages.first).to be_log_message(:key => "started") -# end - -# it "should create a ExportLogmessage :completed when completed" do -# subject.export -# expect(subject.log_messages.last).to be_log_message(:key => "completed") -# end - -# it "should create a ExportLogmessage :failed when failed" do -# pending -# # subject.loader.stub(:export).and_raise("export failed") -# subject.export -# expect(subject.log_messages.last).to be_log_message(:key => "failed") -# end - -# end - -# describe "#options" do - -# it "should be empty by default" do -# expect(subject.options).to be_empty -# end - -# end - -# describe ".types" do - -# it "should return available Export implementations" do -# expect(Export.types).to match_array(%w{NeptuneExport CsvExport GtfsExport NetexExport KmlExport HubExport}) -# end - -# end - -# describe ".new" do - -# it "should use type attribute to create a subclass" do -# expect(Export.new(:type => "NeptuneExport")).to be_an_instance_of(NeptuneExport) -# end - -# end - -# it_behaves_like TypeIdsModelable do -# let(:type_ids_model) { subject} -# end - -# end diff --git a/spec/models/export_task_spec.rb b/spec/models/export_task_spec.rb deleted file mode 100644 index 1a52a6175..000000000 --- a/spec/models/export_task_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'spec_helper' - -describe ExportTask, :type => :model do - - it { should_not validate_presence_of(:start_date) } - it { should_not validate_presence_of(:end_date) } - -end diff --git a/spec/models/gtfs_export_spec.rb b/spec/models/gtfs_export_spec.rb index ccc98e872..0ef3660f5 100644 --- a/spec/models/gtfs_export_spec.rb +++ b/spec/models/gtfs_export_spec.rb @@ -1,33 +1,33 @@ require 'spec_helper' -describe GtfsExport, :type => :model do - - describe "#time_zone" do - - context "when exported data are not StopAreas" do - - before do - subject.references_type = "network" - end - - it "should be mandatory" do - should validate_presence_of(:time_zone) - end - - end - - context "when export data are StopArea" do - - before do - subject.references_type = "stop_area" - end - - it "should be mandatory" do - should_not validate_presence_of(:time_zone) - end - - end - - end - -end +# describe GtfsExport, :type => :model do +# +# describe "#time_zone" do +# +# context "when exported data are not StopAreas" do +# +# before do +# subject.references_type = "network" +# end +# +# it "should be mandatory" do +# should validate_presence_of(:time_zone) +# end +# +# end +# +# context "when export data are StopArea" do +# +# before do +# subject.references_type = "stop_area" +# end +# +# it "should be mandatory" do +# should_not validate_presence_of(:time_zone) +# end +# +# end +# +# end +# +# end diff --git a/spec/models/gtfs_import_spec.rb b/spec/models/gtfs_import_spec.rb index 07cc1905d..5cb69332c 100644 --- a/spec/models/gtfs_import_spec.rb +++ b/spec/models/gtfs_import_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe GtfsImport, :type => :model do +describe Import::Gtfs, :type => :model do # describe "#object_id_prefix" do diff --git a/spec/models/import_message_spec.rb b/spec/models/import/import_message_spec.rb index 2d8aac2b7..48e03a2cc 100644 --- a/spec/models/import_message_spec.rb +++ b/spec/models/import/import_message_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe ImportMessage, :type => :model do +RSpec.describe Import::Message, :type => :model do it { should validate_presence_of(:criticity) } it { should belong_to(:import) } it { should belong_to(:resource) } diff --git a/spec/models/import_resource_spec.rb b/spec/models/import/import_resource_spec.rb index c88bb5dd2..7d2eab8f1 100644 --- a/spec/models/import_resource_spec.rb +++ b/spec/models/import/import_resource_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe ImportResource, :type => :model do +RSpec.describe Import::Resource, :type => :model do it { should belong_to(:import) } it { should enumerize(:status).in("OK", "ERROR", "WARNING", "IGNORED") } diff --git a/spec/models/import_spec.rb b/spec/models/import/import_spec.rb index 8b85f151b..c41d5ba53 100644 --- a/spec/models/import_spec.rb +++ b/spec/models/import/import_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe Import, type: :model do +RSpec.describe Import::Base, type: :model do it { should belong_to(:referential) } it { should belong_to(:workbench) } @@ -24,7 +24,7 @@ RSpec.describe Import, type: :model do end let(:netex_import) do - build_stubbed( + create( :netex_import ) end @@ -39,7 +39,7 @@ RSpec.describe Import, type: :model do ) current_import = create(:workbench_import, status: 'pending') - Import.abort_old + Import::Base.abort_old expect(current_import.reload.status).to eq('pending') expect(old_import.reload.status).to eq('aborted') @@ -54,7 +54,7 @@ RSpec.describe Import, type: :model do created_at: 4.hours.ago - 1.minute ) - Import.abort_old + Import::Base.abort_old expect(import.reload.status).to eq('successful') end @@ -73,7 +73,7 @@ RSpec.describe Import, type: :model do created_at: 4.hours.ago - 1.minute ) - NetexImport.abort_old + Import::Netex.abort_old expect(workbench_import.reload.status).to eq('pending') expect(netex_import.reload.status).to eq('aborted') @@ -88,25 +88,25 @@ RSpec.describe Import, type: :model do netex_import.parent.destroy expect(netex_import.parent).to be_destroyed - expect(NetexImport.count).to eq(0) + expect(Import::Netex.count).to eq(0) end - it "must destroy all associated ImportMessages" do + it "must destroy all associated Import::Messages" do import = create(:import) create(:import_resource, import: import) import.destroy - expect(ImportResource.count).to eq(0) + expect(Import::Resource.count).to eq(0) end - it "must destroy all associated ImportResources" do + it "must destroy all associated Import::Resources" do import = create(:import) create(:import_message, import: import) import.destroy - expect(ImportMessage.count).to eq(0) + expect(Import::Message.count).to eq(0) end end @@ -115,19 +115,18 @@ RSpec.describe Import, type: :model do allow(netex_import).to receive(:update) expect(workbench_import).to receive(:child_change) - + netex_import.status = :foo netex_import.notify_parent end it "must update the :notified_parent_at field of the child import" do allow(workbench_import).to receive(:child_change) - - Timecop.freeze(DateTime.now) do - expect(netex_import).to receive(:update).with( - notified_parent_at: DateTime.now - ) + Timecop.freeze(Time.now) do + netex_import.status = :bar netex_import.notify_parent + expect(netex_import.notified_parent_at).to eq Time.now + expect(netex_import.reload.notified_parent_at).to eq Time.now end end end diff --git a/spec/models/import/netex_import_spec.rb b/spec/models/import/netex_import_spec.rb index 8ffeed1f4..6424fbfe1 100644 --- a/spec/models/import/netex_import_spec.rb +++ b/spec/models/import/netex_import_spec.rb @@ -1,8 +1,7 @@ -RSpec.describe NetexImport, type: [:model, :with_commit] do +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}")} - before do allow(Thread).to receive(:new).and_yield end @@ -30,4 +29,42 @@ RSpec.describe NetexImport, type: [:model, :with_commit] do 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_true = create(:referential, ready: true) + create( + :netex_import, + parent: workbench_import, + referential: referential_ready_false + ) + create( + :netex_import, + parent: workbench_import, + referential: referential_ready_true + ) + + workbench_import.destroy + + expect( + Referential.where(id: referential_ready_false.id).exists? + ).to be false + expect( + Referential.where(id: referential_ready_true.id).exists? + ).to be true + end + + it "doesn't try to destroy nil referentials" do + workbench_import = create(:workbench_import) + create( + :netex_import, + parent: workbench_import, + referential: nil + ) + + expect { workbench_import.destroy }.not_to raise_error + end + end + end diff --git a/spec/models/import_service_spec.rb b/spec/models/import_service_spec.rb deleted file mode 100644 index e7ee062d6..000000000 --- a/spec/models/import_service_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'spec_helper' - -describe ImportService, :type => :model do - - let(:referential) { create(:referential, :slug => "test") } - - subject { ImportService.new(referential) } - - describe '.find' do - - it "should build an import with a scheduled job" do - end - - it "should build an import with a terminated job" do - end - - end - -end diff --git a/spec/models/import_task_spec.rb b/spec/models/import_task_spec.rb deleted file mode 100644 index 3aa006a69..000000000 --- a/spec/models/import_task_spec.rb +++ /dev/null @@ -1,196 +0,0 @@ -# require 'spec_helper' - -# describe ImportTask, :type => :model do - -# subject { build :import_task } - -# describe ".new" do - -# it "should use type attribute to create a subclass" do -# expect(ImportTask.new(:format => "Neptune")).to be_an_instance_of(NeptuneImport) -# expect(ImportTask.new(:format => "Gtfs")).to be_an_instance_of(GtfsImport) -# expect(ImportTask.new(:format => "Netex")).to be_an_instance_of(NetexImport) -# expect(ImportTask.new(:format => "Csv")).to be_an_instance_of(CsvImport) - -# expect(NeptuneImport.new).to be_an_instance_of(NeptuneImport) -# expect(GtfsImport.new).to be_an_instance_of(GtfsImport) -# expect(NetexImport.new).to be_an_instance_of(NetexImport) -# expect(CsvImport.new).to be_an_instance_of(CsvImport) -# end - -# end - -# describe "#delayed_import" do -# before(:each) do -# allow(subject).to receive_messages( :delay => double( :import => true)) -# end -# it "should call delay#import" do -# expect(subject.delay).to receive( :import) -# subject.send :delayed_import -# end -# end - -# describe ".create" do -# before(:each) do -# allow(subject).to receive_messages( :save_resources => true ) -# end -# it "should call save_resource" do -# expect(subject).to receive( :save_resources) -# subject.send :save -# end -# it "should update file_path with #saved_resources" do -# subject.send :save -# expect(ImportTask.find( subject.id).file_path).to eq(subject.send( :saved_resources)) -# end -# it "should have a compliance_check_task" do -# subject.send :save -# expect(ImportTask.find( subject.id).compliance_check_task).not_to be_nil -# end -# end - -# describe "#compliance_check_task" do -# let(:rule_parameter_set){ Factory( :rule_parameter_set) } -# let(:import_task){ Factory(:import_task, :rule_parameter_set_id => rule_parameter_set.id) } -# let(:compliance_check_task){ import_task.compliance_check_task } - -# it "should have same #referential as import_task" do -# expect(compliance_check_task.referential).to eq(import_task.referential) -# end - -# it "should have same #rule_parameter_set_id as import_task" do -# expect(compliance_check_task.rule_parameter_set_id).to eq(import_task.rule_parameter_set_id) -# end - -# it "should have same #user_id as import_task" do -# expect(compliance_check_task.user_id).to eq(import_task.user_id) -# end - -# it "should have same #user_name as import_task" do -# expect(compliance_check_task.user_name).to eq(import_task.user_name) -# end -# end - -# describe "#file_path_extension" do -# let(:import_task){ Factory(:import_task) } -# context "zip file to import" do -# before(:each) do -# import_task.file_path = "aaa/bbb.zip" -# end -# it "should return zip" do -# expect(import_task.file_path_extension).to eq("zip") -# end -# end -# context "xml file to import" do -# before(:each) do -# import_task.file_path = "aaa/bbb.xml" -# end -# it "should return xml" do -# expect(import_task.file_path_extension).to eq("xml") -# end -# end -# context "csv file to import" do -# before(:each) do -# import_task.file_path = "aaa/bbb.csv" -# end -# it "should return csv" do -# expect(import_task.file_path_extension).to eq("basic") -# end -# end - -# end - -# context "options attributes" do -# let(:import_task){ Factory(:import_task) } -# describe "#no_save" do -# it "should read parameter_set['no_save']" do -# import_task.parameter_set[ "no_save"] = "dummy" -# expect(import_task.no_save).to eq("dummy") -# end -# end -# describe "#format" do -# it "should read parameter_set['format']" do -# import_task.parameter_set[ "format"] = "dummy" -# expect(import_task.format).to eq("dummy") -# end -# end -# describe "#file_path" do -# it "should read parameter_set['file_path']" do -# import_task.parameter_set[ "file_path"] = "dummy" -# expect(import_task.file_path).to eq("dummy") -# end -# end -# describe "#no_save=" do -# it "should read parameter_set['no_save']" do -# import_task.no_save = "dummy" -# expect(import_task.parameter_set[ "no_save"]).to eq(false) -# end -# end -# describe "#format=" do -# it "should read parameter_set['format']" do -# import_task.format = "dummy" -# expect(import_task.parameter_set[ "format"]).to eq("dummy") -# end -# end -# describe "#file_path=" do -# it "should read parameter_set['file_path']" do -# import_task.file_path = "dummy" -# expect(import_task.parameter_set[ "file_path"]).to eq("dummy") -# end -# end -# end - -# describe "#chouette_command" do -# it "should be a Chouette::Command instance" do -# expect(subject.send( :chouette_command).class).to eq(Chouette::Command) -# end -# it "should have schema same as referential.slug" do -# expect(subject.send( :chouette_command).schema).to eq(subject.referential.slug) -# end -# end - -# describe "#import" do -# let(:import_task){ Factory(:import_task) } -# let(:chouette_command) { "dummy" } -# context "for failing import" do -# before(:each) do -# allow(chouette_command).to receive( :run!).and_raise( "dummy") -# allow(import_task).to receive_messages( :chouette_command => chouette_command) -# end -# it "should have status 'failed'" do -# import_task.import -# expect(import_task.status).to eq("failed") -# end -# it "should have status 'failed' for compliance_check_task" do -# import_task.import -# expect(import_task.compliance_check_task.status).to eq("failed") -# end -# end -# context "for successful import" do -# before(:each) do -# allow(import_task).to receive_messages( :chouette_command => double( :run! => true )) -# end -# it "should have status 'completed'" do -# import_task.import -# expect(import_task.status).to eq("completed") -# end -# it "should have status 'completed' for compliance_check_task" do -# import_task.import -# expect(import_task.status).to eq("completed") -# end -# end -# end - -# describe "#import" do -# let(:import_task){ Factory(:import_task) } -# let(:command_args){ "dummy" } -# before(:each) do -# allow(import_task).to receive_messages( :chouette_command => double( :run! => true )) -# allow(import_task).to receive_messages( :chouette_command_args => command_args) -# end -# it "should call chouette_command.run! with :c => 'import', :id => id" do -# expect(import_task.send( :chouette_command)).to receive( :run! ).with( command_args) -# import_task.import -# end -# end - -# end diff --git a/spec/models/netex_export_spec.rb b/spec/models/netex_export_spec.rb index 1d09fa07f..345bf4d5a 100644 --- a/spec/models/netex_export_spec.rb +++ b/spec/models/netex_export_spec.rb @@ -1,10 +1,10 @@ require 'spec_helper' -describe NetexExport, :type => :model do - - # describe '#export_options' do - # subject { super().export_options } - # it { is_expected.to include(:format => :netex) } - # end - -end +# describe NetexExport, :type => :model do +# +# # describe '#export_options' do +# # subject { super().export_options } +# # it { is_expected.to include(:format => :netex) } +# # end +# +# end diff --git a/spec/models/netex_import_spec.rb b/spec/models/netex_import_spec.rb deleted file mode 100644 index c6051a869..000000000 --- a/spec/models/netex_import_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -RSpec.describe NetexImport, type: :model do - 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_true = create(:referential, ready: true) - create( - :netex_import, - parent: workbench_import, - referential: referential_ready_false - ) - create( - :netex_import, - parent: workbench_import, - referential: referential_ready_true - ) - - workbench_import.destroy - - expect( - Referential.where(id: referential_ready_false.id).exists? - ).to be false - expect( - Referential.where(id: referential_ready_true.id).exists? - ).to be true - end - - it "doesn't try to destroy nil referentials" do - workbench_import = create(:workbench_import) - create( - :netex_import, - parent: workbench_import, - referential: nil - ) - - expect { workbench_import.destroy }.not_to raise_error - end - end -end diff --git a/spec/models/simple_exporter_spec.rb b/spec/models/simple_exporter_spec.rb index 75051aeb9..a42daafe1 100644 --- a/spec/models/simple_exporter_spec.rb +++ b/spec/models/simple_exporter_spec.rb @@ -5,7 +5,7 @@ RSpec.describe SimpleExporter do SimpleExporter.define :foo expect do SimpleExporter.new(configuration_name: :test).export - end.to raise_error + end.to raise_error(RuntimeError) end end context "with a complete configuration" do @@ -18,9 +18,9 @@ RSpec.describe SimpleExporter do it "should define an exporter" do expect{SimpleExporter.find_configuration(:foo)}.to_not raise_error expect{SimpleExporter.new(configuration_name: :foo, filepath: "").export}.to_not raise_error - expect{SimpleExporter.find_configuration(:bar)}.to raise_error - expect{SimpleExporter.new(configuration_name: :bar, filepath: "")}.to raise_error - expect{SimpleExporter.new(configuration_name: :bar, filepath: "").export}.to raise_error + expect{SimpleExporter.find_configuration(:bar)}.to raise_error(RuntimeError) + expect{SimpleExporter.new(configuration_name: :bar, filepath: "")}.to raise_error(RuntimeError) + expect{SimpleExporter.new(configuration_name: :bar, filepath: "").export}.to raise_error(RuntimeError) expect{SimpleExporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleExporter.count}.by 1 end end @@ -33,7 +33,7 @@ RSpec.describe SimpleExporter do config.add_column :name config.add_column :name end - end.to raise_error + end.to raise_error(RuntimeError) end end end diff --git a/spec/models/simple_importer_spec.rb b/spec/models/simple_importer_spec.rb index 5f9eb0651..8f4d7cfdd 100644 --- a/spec/models/simple_importer_spec.rb +++ b/spec/models/simple_importer_spec.rb @@ -6,7 +6,7 @@ RSpec.describe SimpleImporter do SimpleImporter.define :foo expect do SimpleImporter.new(configuration_name: :foo, filepath: "").import - end.to raise_error + end.to raise_error(RuntimeError) end end context "with a complete configuration" do @@ -20,8 +20,8 @@ RSpec.describe SimpleImporter do expect{SimpleImporter.find_configuration(:foo)}.to_not raise_error expect{SimpleImporter.new(configuration_name: :foo, filepath: "")}.to_not raise_error expect{SimpleImporter.new(configuration_name: :foo, filepath: "").import}.to_not raise_error - expect{SimpleImporter.find_configuration(:bar)}.to raise_error - expect{SimpleImporter.new(configuration_name: :bar, filepath: "")}.to raise_error + expect{SimpleImporter.find_configuration(:bar)}.to raise_error(RuntimeError) + expect{SimpleImporter.new(configuration_name: :bar, filepath: "")}.to raise_error(RuntimeError) expect{SimpleImporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleImporter.count}.by 1 end end @@ -49,7 +49,7 @@ RSpec.describe SimpleImporter do end it "should import the given file" do - expect{importer.import verbose: true}.to change{Chouette::StopArea.count}.by 1 + expect{importer.import verbose: false}.to change{Chouette::StopArea.count}.by 1 expect(importer.status).to eq "success" stop = Chouette::StopArea.last expect(stop.name).to eq "Nom du Stop" diff --git a/spec/requests/api/v1/netex_import_spec.rb b/spec/requests/api/v1/netex_import_spec.rb index 8597c1d32..14dac9a25 100644 --- a/spec/requests/api/v1/netex_import_spec.rb +++ b/spec/requests/api/v1/netex_import_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe "NetexImport", type: :request do +RSpec.describe "Import::Netex", type: :request do describe 'POST netex_imports' do @@ -39,7 +39,7 @@ RSpec.describe "NetexImport", type: :request do post_request.(netex_import: legal_attributes) expect( response ).to be_success expect( json_response_body ).to eq( - 'id' => NetexImport.last.id, + 'id' => Import::Netex.last.id, 'referential_id' => Referential.last.id, 'workbench_id' => workbench.id ) @@ -51,7 +51,7 @@ RSpec.describe "NetexImport", type: :request do create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00108', line_referential: workbench.line_referential) create(:line, objectid: 'STIF:CODIFLIGNE:Line:C00109', line_referential: workbench.line_referential) - expect{ post_request.(netex_import: legal_attributes) }.to change{NetexImport.count}.by(1) + expect{ post_request.(netex_import: legal_attributes) }.to change{Import::Netex.count}.by(1) end it 'creates a correct Referential', pending: 'see #5073' do @@ -96,7 +96,7 @@ RSpec.describe "NetexImport", type: :request do end it 'does not create an Import object' do - expect{ post_request.(netex_import: illegal_attributes) }.not_to change{Import.count} + expect{ post_request.(netex_import: illegal_attributes) }.not_to change{Import::Base.count} end it 'might not create a referential' do diff --git a/spec/services/parent_notifier_spec.rb b/spec/services/parent_notifier_spec.rb index ecf508fcd..d2dc6b184 100644 --- a/spec/services/parent_notifier_spec.rb +++ b/spec/services/parent_notifier_spec.rb @@ -20,7 +20,7 @@ RSpec.describe ParentNotifier do expect(netex_import).to receive(:notify_parent) end - ParentNotifier.new(Import).notify_when_finished(netex_imports) + ParentNotifier.new(Import::Base).notify_when_finished(netex_imports) end it "doesn't call #notify_parent if its `notified_parent_at` is set" do @@ -33,7 +33,7 @@ RSpec.describe ParentNotifier do expect(netex_import).not_to receive(:notify_parent) - ParentNotifier.new(Import).notify_when_finished + ParentNotifier.new(Import::Base).notify_when_finished end end @@ -46,8 +46,10 @@ RSpec.describe ParentNotifier do notified_parent_at: nil ) + Import::Base.where(id: netex_import).update_all notified_parent_at: nil + expect( - ParentNotifier.new(Import).objects_pending_notification + ParentNotifier.new(Import::Base).objects_pending_notification ).to eq([netex_import]) end @@ -55,7 +57,7 @@ RSpec.describe ParentNotifier do create(:import, parent: nil) expect( - ParentNotifier.new(Import).objects_pending_notification + ParentNotifier.new(Import::Base).objects_pending_notification ).to be_empty end @@ -70,7 +72,7 @@ RSpec.describe ParentNotifier do end expect( - ParentNotifier.new(Import).objects_pending_notification + ParentNotifier.new(Import::Base).objects_pending_notification ).to be_empty end @@ -83,7 +85,7 @@ RSpec.describe ParentNotifier do ) expect( - ParentNotifier.new(Import).objects_pending_notification + ParentNotifier.new(Import::Base).objects_pending_notification ).to be_empty end end diff --git a/spec/support/permissions.rb b/spec/support/permissions.rb index 95afd6c1c..825e44725 100644 --- a/spec/support/permissions.rb +++ b/spec/support/permissions.rb @@ -17,6 +17,7 @@ module Support connection_links calendars footnotes + exports imports merges journey_patterns |
