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/support/shared_examples/models | |
parent | 87ef501eacd66d7166183d20d84e33de022f7002 (diff) | |
download | gitlab-ce-f1e2fca19a90a6992c2020cf8c2159cfb0b61bca.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support/shared_examples/models')
-rw-r--r-- | spec/support/shared_examples/models/concerns/bulk_insert_safe_shared_examples.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/support/shared_examples/models/concerns/bulk_insert_safe_shared_examples.rb b/spec/support/shared_examples/models/concerns/bulk_insert_safe_shared_examples.rb index 78d0945ea63..c6180a5a196 100644 --- a/spec/support/shared_examples/models/concerns/bulk_insert_safe_shared_examples.rb +++ b/spec/support/shared_examples/models/concerns/bulk_insert_safe_shared_examples.rb @@ -35,4 +35,44 @@ RSpec.shared_examples 'a BulkInsertSafe model' do |klass| expect { target_class.belongs_to(:other_record) }.not_to raise_error end end + + describe '.bulk_insert!' do + context 'when all items are valid' do + it 'inserts them all' do + items = valid_items_for_bulk_insertion + + expect(items).not_to be_empty + expect { target_class.bulk_insert!(items) }.to change { target_class.count }.by(items.size) + end + + it 'returns true' do + items = valid_items_for_bulk_insertion + + expect(items).not_to be_empty + expect(target_class.bulk_insert!(items)).to be true + end + end + + context 'when some items are invalid' do + it 'does not insert any of them and raises an error' do + items = invalid_items_for_bulk_insertion + + # it is not always possible to create invalid items + if items.any? + expect { target_class.bulk_insert!(items) }.to raise_error(ActiveRecord::RecordInvalid) + expect(target_class.count).to eq(0) + end + end + + it 'inserts them anyway when bypassing validations' do + items = invalid_items_for_bulk_insertion + + # it is not always possible to create invalid items + if items.any? + expect(target_class.bulk_insert!(items, validate: false)).to be(true) + expect(target_class.count).to eq(items.size) + end + end + end + end end |