diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-03 12:08:08 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-03 12:08:08 +0000 |
commit | f1e2fca19a90a6992c2020cf8c2159cfb0b61bca (patch) | |
tree | c084a29873f0fe6ff42555c590da6a9d8527df91 /spec/models | |
parent | 87ef501eacd66d7166183d20d84e33de022f7002 (diff) | |
download | gitlab-ce-f1e2fca19a90a6992c2020cf8c2159cfb0b61bca.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/concerns/bulk_insert_safe_spec.rb | 69 | ||||
-rw-r--r-- | spec/models/label_link_spec.rb | 5 | ||||
-rw-r--r-- | spec/models/merge_request_diff_commit_spec.rb | 5 | ||||
-rw-r--r-- | spec/models/merge_request_diff_file_spec.rb | 5 |
4 files changed, 80 insertions, 4 deletions
diff --git a/spec/models/concerns/bulk_insert_safe_spec.rb b/spec/models/concerns/bulk_insert_safe_spec.rb index 91884680738..9ebaedcf252 100644 --- a/spec/models/concerns/bulk_insert_safe_spec.rb +++ b/spec/models/concerns/bulk_insert_safe_spec.rb @@ -5,6 +5,8 @@ require 'spec_helper' describe BulkInsertSafe do class BulkInsertItem < ApplicationRecord include BulkInsertSafe + + validates :name, presence: true end module InheritedUnsafeMethods @@ -23,7 +25,36 @@ describe BulkInsertSafe do end end - it_behaves_like 'a BulkInsertSafe model', BulkInsertItem + before(:all) do + ActiveRecord::Schema.define do + create_table :bulk_insert_items, force: true do |t| + t.string :name, null: true + end + end + end + + after(:all) do + ActiveRecord::Schema.define do + drop_table :bulk_insert_items, force: true + end + end + + def build_valid_items_for_bulk_insertion + Array.new(10) do |n| + BulkInsertItem.new(name: "item-#{n}") + end + end + + def build_invalid_items_for_bulk_insertion + Array.new(10) do + BulkInsertItem.new # requires `name` to be set + end + end + + it_behaves_like 'a BulkInsertSafe model', BulkInsertItem do + let(:valid_items_for_bulk_insertion) { build_valid_items_for_bulk_insertion } + let(:invalid_items_for_bulk_insertion) { build_invalid_items_for_bulk_insertion } + end context 'when inheriting class methods' do it 'raises an error when method is not bulk-insert safe' do @@ -35,4 +66,40 @@ describe BulkInsertSafe do expect { BulkInsertItem.include(InheritedSafeMethods) }.not_to raise_error end end + + context 'primary keys' do + it 'raises error if primary keys are set prior to insertion' do + items = build_valid_items_for_bulk_insertion + items.each_with_index do |item, n| + item.id = n + end + + expect { BulkInsertItem.bulk_insert!(items) }.to raise_error(subject::PrimaryKeySetError) + end + end + + describe '.bulk_insert!' do + it 'inserts items in the given number of batches' do + items = build_valid_items_for_bulk_insertion + expect(items.size).to eq(10) + expect(BulkInsertItem).to receive(:insert_all!).twice + + BulkInsertItem.bulk_insert!(items, batch_size: 5) + end + + it 'rolls back the transaction when any item is invalid' do + # second batch is bad + all_items = build_valid_items_for_bulk_insertion + build_invalid_items_for_bulk_insertion + batch_size = all_items.size / 2 + + expect do + BulkInsertItem.bulk_insert!(all_items, batch_size: batch_size) rescue nil + end.not_to change { BulkInsertItem.count } + end + + it 'does nothing and returns true when items are empty' do + expect(BulkInsertItem.bulk_insert!([])).to be(true) + expect(BulkInsertItem.count).to eq(0) + end + end end diff --git a/spec/models/label_link_spec.rb b/spec/models/label_link_spec.rb index 0a5cb5374b0..7a179dcb419 100644 --- a/spec/models/label_link_spec.rb +++ b/spec/models/label_link_spec.rb @@ -8,5 +8,8 @@ describe LabelLink do it { is_expected.to belong_to(:label) } it { is_expected.to belong_to(:target) } - it_behaves_like 'a BulkInsertSafe model', LabelLink + it_behaves_like 'a BulkInsertSafe model', LabelLink do + let(:valid_items_for_bulk_insertion) { build_list(:label_link, 10) } + let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined + end end diff --git a/spec/models/merge_request_diff_commit_spec.rb b/spec/models/merge_request_diff_commit_spec.rb index a296122ae09..8b51c6fae08 100644 --- a/spec/models/merge_request_diff_commit_spec.rb +++ b/spec/models/merge_request_diff_commit_spec.rb @@ -6,7 +6,10 @@ describe MergeRequestDiffCommit do let(:merge_request) { create(:merge_request) } let(:project) { merge_request.project } - it_behaves_like 'a BulkInsertSafe model', MergeRequestDiffCommit + it_behaves_like 'a BulkInsertSafe model', MergeRequestDiffCommit do + let(:valid_items_for_bulk_insertion) { build_list(:merge_request_diff_commit, 10) } + let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined + end describe '#to_hash' do subject { merge_request.commits.first } diff --git a/spec/models/merge_request_diff_file_spec.rb b/spec/models/merge_request_diff_file_spec.rb index 6ecbc5bf832..40f7be5dc8f 100644 --- a/spec/models/merge_request_diff_file_spec.rb +++ b/spec/models/merge_request_diff_file_spec.rb @@ -3,7 +3,10 @@ require 'spec_helper' describe MergeRequestDiffFile do - it_behaves_like 'a BulkInsertSafe model', MergeRequestDiffFile + it_behaves_like 'a BulkInsertSafe model', MergeRequestDiffFile do + let(:valid_items_for_bulk_insertion) { build_list(:merge_request_diff_file, 10) } + let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined + end describe '#diff' do context 'when diff is not stored' do |