diff options
Diffstat (limited to 'spec/models/concerns')
-rw-r--r-- | spec/models/concerns/bulk_insert_safe_spec.rb | 42 | ||||
-rw-r--r-- | spec/models/concerns/checksummable_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/concerns/ci/has_status_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/concerns/vulnerability_finding_helpers_spec.rb | 27 |
4 files changed, 74 insertions, 19 deletions
diff --git a/spec/models/concerns/bulk_insert_safe_spec.rb b/spec/models/concerns/bulk_insert_safe_spec.rb index 209ee1264d5..172986c142c 100644 --- a/spec/models/concerns/bulk_insert_safe_spec.rb +++ b/spec/models/concerns/bulk_insert_safe_spec.rb @@ -17,6 +17,7 @@ RSpec.describe BulkInsertSafe do t.binary :sha_value, null: false, limit: 20 t.jsonb :jsonb_value, null: false t.belongs_to :bulk_insert_parent_item, foreign_key: true, null: true + t.timestamps null: true t.index :name, unique: true end @@ -179,29 +180,26 @@ RSpec.describe BulkInsertSafe do end context 'with returns option set' do + let(:items) { bulk_insert_item_class.valid_list(1) } + + subject(:bulk_insert) { bulk_insert_item_class.bulk_insert!(items, returns: returns) } + context 'when is set to :ids' do - it 'return an array with the primary key values for all inserted records' do - items = bulk_insert_item_class.valid_list(1) + let(:returns) { :ids } - expect(bulk_insert_item_class.bulk_insert!(items, returns: :ids)).to contain_exactly(a_kind_of(Integer)) - end + it { is_expected.to contain_exactly(a_kind_of(Integer)) } end context 'when is set to nil' do - it 'returns an empty array' do - items = bulk_insert_item_class.valid_list(1) + let(:returns) { nil } - expect(bulk_insert_item_class.bulk_insert!(items, returns: nil)).to eq([]) - end + it { is_expected.to eq([]) } end - context 'when is set to anything else' do - it 'raises an error' do - items = bulk_insert_item_class.valid_list(1) + context 'when is set to a list of attributes' do + let(:returns) { [:id, :sha_value] } - expect { bulk_insert_item_class.bulk_insert!([items], returns: [:id, :name]) } - .to raise_error(ArgumentError, "returns needs to be :ids or nil") - end + it { is_expected.to contain_exactly([a_kind_of(Integer), '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12']) } end end end @@ -228,10 +226,20 @@ RSpec.describe BulkInsertSafe do end describe '.bulk_upsert!' do + subject(:bulk_upsert) { bulk_insert_item_class.bulk_upsert!([new_object], unique_by: %w[name]) } + it 'updates existing object' do - bulk_insert_item_class.bulk_upsert!([new_object], unique_by: %w[name]) + expect { bulk_upsert }.to change { existing_object.reload.secret_value }.to('new value') + end - expect(existing_object.reload.secret_value).to eq('new value') + context 'when the `created_at` attribute is provided' do + before do + new_object.created_at = 10.days.from_now + end + + it 'does not change the existing `created_at` value' do + expect { bulk_upsert }.not_to change { existing_object.reload.created_at } + end end end end @@ -250,7 +258,7 @@ RSpec.describe BulkInsertSafe do it 'successfully inserts an item' do expect(ActiveRecord::InsertAll).to receive(:new) .with( - bulk_insert_items_with_composite_pk_class, [new_object.as_json], on_duplicate: :raise, returning: false, unique_by: %w[id name] + bulk_insert_items_with_composite_pk_class.insert_all_proxy_class, [new_object.as_json], on_duplicate: :raise, returning: false, unique_by: %w[id name] ).and_call_original expect { bulk_insert_items_with_composite_pk_class.bulk_insert!([new_object]) }.to( diff --git a/spec/models/concerns/checksummable_spec.rb b/spec/models/concerns/checksummable_spec.rb index 3a0387333e8..93a65605b50 100644 --- a/spec/models/concerns/checksummable_spec.rb +++ b/spec/models/concerns/checksummable_spec.rb @@ -13,11 +13,19 @@ RSpec.describe Checksummable do end end - describe ".hexdigest" do + describe ".sha256_hexdigest" do it 'returns the SHA256 sum of the file' do expected = Digest::SHA256.file(__FILE__).hexdigest - expect(subject.hexdigest(__FILE__)).to eq(expected) + expect(subject.sha256_hexdigest(__FILE__)).to eq(expected) + end + end + + describe ".md5_hexdigest" do + it 'returns the MD5 sum of the file' do + expected = Digest::MD5.file(__FILE__).hexdigest + + expect(subject.md5_hexdigest(__FILE__)).to eq(expected) end end end diff --git a/spec/models/concerns/ci/has_status_spec.rb b/spec/models/concerns/ci/has_status_spec.rb index 0709a050056..9dfc7d84f89 100644 --- a/spec/models/concerns/ci/has_status_spec.rb +++ b/spec/models/concerns/ci/has_status_spec.rb @@ -363,6 +363,18 @@ RSpec.describe Ci::HasStatus do it_behaves_like 'not containing the job', status end end + + describe '.waiting_for_resource_or_upcoming' do + subject { CommitStatus.waiting_for_resource_or_upcoming } + + %i[created scheduled waiting_for_resource].each do |status| + it_behaves_like 'containing the job', status + end + + %i[running failed success canceled].each do |status| + it_behaves_like 'not containing the job', status + end + end end describe '::DEFAULT_STATUS' do diff --git a/spec/models/concerns/vulnerability_finding_helpers_spec.rb b/spec/models/concerns/vulnerability_finding_helpers_spec.rb new file mode 100644 index 00000000000..023ecccb520 --- /dev/null +++ b/spec/models/concerns/vulnerability_finding_helpers_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe VulnerabilityFindingHelpers do + let(:cls) do + Class.new do + include VulnerabilityFindingHelpers + + attr_accessor :report_type + + def initialize(report_type) + @report_type = report_type + end + end + end + + describe '#requires_manual_resolution?' do + it 'returns false if the finding does not require manual resolution' do + expect(cls.new('sast').requires_manual_resolution?).to eq(false) + end + + it 'returns true when the finding requires manual resolution' do + expect(cls.new('secret_detection').requires_manual_resolution?).to eq(true) + end + end +end |