diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-13 00:08:48 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-13 00:08:48 +0300 |
commit | 006e89697dd5165f355afc20fc6bb0cdfa7b381a (patch) | |
tree | 9095aeb37b2c80f3b0cc5a8dfd27baf93f05b61b /spec/support | |
parent | 43e3dc2f95a25c600e08f65d4f1c406a1a63ed3d (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/shared_examples/models/concerns/bulk_insert_safe_shared_examples.rb | 33 |
1 files changed, 33 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 new file mode 100644 index 00000000000..0a2b6616bb4 --- /dev/null +++ b/spec/support/shared_examples/models/concerns/bulk_insert_safe_shared_examples.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'a BulkInsertSafe model' do |target_class| + # We consider all callbacks unsafe for bulk insertions unless we have explicitly + # whitelisted them (esp. anything related to :save, :create, :commit etc.) + let(:callback_method_blacklist) do + ActiveRecord::Callbacks::CALLBACKS.reject do |callback| + cb_name = callback.to_s.gsub(/(before_|after_|around_)/, '').to_sym + BulkInsertSafe::CALLBACK_NAME_WHITELIST.include?(cb_name) + end.to_set + end + + context 'when calling class methods directly' do + it 'raises an error when method is not bulk-insert safe' do + callback_method_blacklist.each do |m| + expect { target_class.send(m, nil) }.to( + raise_error(BulkInsertSafe::MethodNotAllowedError), + "Expected call to #{m} to raise an error, but it didn't" + ) + end + end + + it 'does not raise an error when method is bulk-insert safe' do + BulkInsertSafe::CALLBACK_NAME_WHITELIST.each do |name| + expect { target_class.set_callback(name) {} }.not_to raise_error + end + end + + it 'does not raise an error when the call is triggered by belongs_to' do + expect { target_class.belongs_to(:other_record) }.not_to raise_error + end + end +end |