diff options
Diffstat (limited to 'spec/models/bulk_imports')
-rw-r--r-- | spec/models/bulk_imports/batch_tracker_spec.rb | 15 | ||||
-rw-r--r-- | spec/models/bulk_imports/entity_spec.rb | 25 | ||||
-rw-r--r-- | spec/models/bulk_imports/export_status_spec.rb | 117 | ||||
-rw-r--r-- | spec/models/bulk_imports/export_upload_spec.rb | 16 | ||||
-rw-r--r-- | spec/models/bulk_imports/tracker_spec.rb | 26 |
5 files changed, 193 insertions, 6 deletions
diff --git a/spec/models/bulk_imports/batch_tracker_spec.rb b/spec/models/bulk_imports/batch_tracker_spec.rb index 336943228c7..1c7cbc0cb8c 100644 --- a/spec/models/bulk_imports/batch_tracker_spec.rb +++ b/spec/models/bulk_imports/batch_tracker_spec.rb @@ -13,4 +13,19 @@ RSpec.describe BulkImports::BatchTracker, type: :model, feature_category: :impor it { is_expected.to validate_presence_of(:batch_number) } it { is_expected.to validate_uniqueness_of(:batch_number).scoped_to(:tracker_id) } end + + describe 'scopes' do + describe '.in_progress' do + it 'returns only batches that are in progress' do + created = create(:bulk_import_batch_tracker, :created) + started = create(:bulk_import_batch_tracker, :started) + create(:bulk_import_batch_tracker, :finished) + create(:bulk_import_batch_tracker, :timeout) + create(:bulk_import_batch_tracker, :failed) + create(:bulk_import_batch_tracker, :skipped) + + expect(described_class.in_progress).to contain_exactly(created, started) + end + end + end end diff --git a/spec/models/bulk_imports/entity_spec.rb b/spec/models/bulk_imports/entity_spec.rb index b822786579b..ce143a1aa33 100644 --- a/spec/models/bulk_imports/entity_spec.rb +++ b/spec/models/bulk_imports/entity_spec.rb @@ -191,6 +191,24 @@ RSpec.describe BulkImports::Entity, type: :model, feature_category: :importers d expect(described_class.by_user_id(user.id)).to contain_exactly(entity_1, entity_2) end end + + describe '.stale' do + it 'returns entities that are stale' do + entity_1 = create(:bulk_import_entity, updated_at: 3.days.ago) + create(:bulk_import_entity) + + expect(described_class.stale).to contain_exactly(entity_1) + end + end + + describe '.order_by_updated_at_and_id' do + it 'returns entities ordered by updated_at and id' do + entity_1 = create(:bulk_import_entity, updated_at: 3.days.ago) + entity_2 = create(:bulk_import_entity, updated_at: 2.days.ago) + + expect(described_class.order_by_updated_at_and_id(:desc)).to eq([entity_2, entity_1]) + end + end end describe '.all_human_statuses' do @@ -444,6 +462,13 @@ RSpec.describe BulkImports::Entity, type: :model, feature_category: :importers d expect(entity.has_failures).to eq(true) end + + it 'sets the has_failures flag on the parent import' do + create(:bulk_import_failure, entity: entity) + + expect { entity.update_has_failures } + .to change { entity.bulk_import.has_failures? }.from(false).to(true) + end end context 'when entity does not have failures' do diff --git a/spec/models/bulk_imports/export_status_spec.rb b/spec/models/bulk_imports/export_status_spec.rb index c3faa2db19c..aa3bce78534 100644 --- a/spec/models/bulk_imports/export_status_spec.rb +++ b/spec/models/bulk_imports/export_status_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe BulkImports::ExportStatus, feature_category: :importers do +RSpec.describe BulkImports::ExportStatus, :clean_gitlab_redis_cache, feature_category: :importers do let_it_be(:relation) { 'labels' } let_it_be(:import) { create(:bulk_import) } let_it_be(:config) { create(:bulk_import_configuration, bulk_import: import) } @@ -100,7 +100,7 @@ RSpec.describe BulkImports::ExportStatus, feature_category: :importers do end it 'returns false' do - expect(subject.started?).to eq(false) + expect(subject.failed?).to eq(false) end end @@ -113,8 +113,8 @@ RSpec.describe BulkImports::ExportStatus, feature_category: :importers do end end - it 'returns false' do - expect(subject.started?).to eq(false) + it 'returns true' do + expect(subject.failed?).to eq(true) end end end @@ -156,7 +156,7 @@ RSpec.describe BulkImports::ExportStatus, feature_category: :importers do end it 'returns false' do - expect(subject.started?).to eq(false) + expect(subject.empty?).to eq(false) end end end @@ -282,4 +282,111 @@ RSpec.describe BulkImports::ExportStatus, feature_category: :importers do end end end + + describe 'caching' do + let(:cached_status) do + subject.send(:status) + subject.send(:status_from_cache) + end + + shared_examples 'does not result in a cached status' do + specify do + expect(cached_status).to be_nil + end + end + + shared_examples 'results in a cached status' do + specify do + expect(cached_status).to include('status' => status) + end + + context 'when something goes wrong during export status fetch' do + before do + allow_next_instance_of(BulkImports::Clients::HTTP) do |client| + allow(client).to receive(:get).and_raise( + BulkImports::NetworkError.new("Unsuccessful response", response: nil) + ) + end + end + + include_examples 'does not result in a cached status' + end + end + + context 'when export status is started' do + let(:status) { BulkImports::Export::STARTED } + + it_behaves_like 'does not result in a cached status' + end + + context 'when export status is failed' do + let(:status) { BulkImports::Export::FAILED } + + it_behaves_like 'results in a cached status' + end + + context 'when export status is finished' do + let(:status) { BulkImports::Export::FINISHED } + + it_behaves_like 'results in a cached status' + end + + context 'when export status is not present' do + let(:status) { nil } + + it_behaves_like 'does not result in a cached status' + end + + context 'when the cache is empty' do + let(:status) { BulkImports::Export::FAILED } + + it 'fetches the status from the remote' do + expect(subject).to receive(:status_from_remote).and_call_original + expect(subject.send(:status)).to include('status' => status) + end + end + + context 'when the cache is not empty' do + let(:status) { BulkImports::Export::FAILED } + + before do + Gitlab::Cache::Import::Caching.write( + described_class.new(tracker, 'labels').send(:cache_key), + { 'status' => 'mock status' }.to_json + ) + end + + it 'does not fetch the status from the remote' do + expect(subject).not_to receive(:status_from_remote) + expect(subject.send(:status)).to eq({ 'status' => 'mock status' }) + end + + context 'with a different entity' do + before do + tracker.entity = create(:bulk_import_entity, bulk_import: import, source_full_path: 'foo') + end + + it 'fetches the status from the remote' do + expect(subject).to receive(:status_from_remote).and_call_original + expect(subject.send(:status)).to include('status' => status) + end + end + + context 'with a different relation' do + let_it_be(:relation) { 'merge_requests' } + + let(:response_double) do + instance_double(HTTParty::Response, parsed_response: [ + { 'relation' => 'labels', 'status' => status }, + { 'relation' => 'merge_requests', 'status' => status } + ]) + end + + it 'fetches the status from the remote' do + expect(subject).to receive(:status_from_remote).and_call_original + expect(subject.send(:status)).to include('status' => status) + end + end + end + end end diff --git a/spec/models/bulk_imports/export_upload_spec.rb b/spec/models/bulk_imports/export_upload_spec.rb index d9ae41af0db..ca45fe73b0e 100644 --- a/spec/models/bulk_imports/export_upload_spec.rb +++ b/spec/models/bulk_imports/export_upload_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe BulkImports::ExportUpload do +RSpec.describe BulkImports::ExportUpload, type: :model, feature_category: :importers do subject { described_class.new(export: create(:bulk_import_export)) } describe 'associations' do @@ -20,4 +20,18 @@ RSpec.describe BulkImports::ExportUpload do expect(subject.public_send(method).url).to eq(url) end + + describe 'ActiveRecord callbacks' do + let(:after_save_callbacks) { described_class._save_callbacks.select { |cb| cb.kind == :after } } + let(:after_commit_callbacks) { described_class._commit_callbacks.select { |cb| cb.kind == :after } } + + def find_callback(callbacks, key) + callbacks.find { |cb| cb.filter == key } + end + + it 'export file is stored in after_commit callback' do + expect(find_callback(after_commit_callbacks, :store_export_file!)).to be_present + expect(find_callback(after_save_callbacks, :store_export_file!)).to be_nil + end + end end diff --git a/spec/models/bulk_imports/tracker_spec.rb b/spec/models/bulk_imports/tracker_spec.rb index edd9adfa5f6..25cd5489a9f 100644 --- a/spec/models/bulk_imports/tracker_spec.rb +++ b/spec/models/bulk_imports/tracker_spec.rb @@ -83,5 +83,31 @@ RSpec.describe BulkImports::Tracker, type: :model, feature_category: :importers "'InexistingPipeline' is not a valid BulkImport Pipeline" ) end + + context 'when using delegation methods' do + context 'with group pipelines' do + let(:entity) { create(:bulk_import_entity) } + + it 'does not raise' do + entity.pipelines.each do |pipeline| + tracker = create(:bulk_import_tracker, entity: entity, pipeline_name: pipeline[:pipeline]) + expect { tracker.abort_on_failure? }.not_to raise_error + expect { tracker.file_extraction_pipeline? }.not_to raise_error + end + end + end + + context 'with project pipelines' do + let(:entity) { create(:bulk_import_entity, :project_entity) } + + it 'does not raise' do + entity.pipelines.each do |pipeline| + tracker = create(:bulk_import_tracker, entity: entity, pipeline_name: pipeline[:pipeline]) + expect { tracker.abort_on_failure? }.not_to raise_error + expect { tracker.file_extraction_pipeline? }.not_to raise_error + end + end + end + end end end |