diff options
author | Patrick Bajao <ebajao@gitlab.com> | 2019-04-12 07:19:45 +0300 |
---|---|---|
committer | Patrick Bajao <ebajao@gitlab.com> | 2019-04-12 07:19:47 +0300 |
commit | 208338fde6750ad327fb8ab57877869435520436 (patch) | |
tree | b1386bbf36eb6b248f756b648db2c3238e213ee6 /spec/models/application_record_spec.rb | |
parent | dc8848794bfd2f06345d4dbba8a918aa09ee07a8 (diff) |
Add ApplicationRecord#safe_ensure_unique method
Port of https://dev.gitlab.org/gitlab/gitlab-ee/merge_requests/866
to CE excluding the migration and service changes as they don't
apply to CE.
Diffstat (limited to 'spec/models/application_record_spec.rb')
-rw-r--r-- | spec/models/application_record_spec.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/spec/models/application_record_spec.rb b/spec/models/application_record_spec.rb index fd25132ed3a..cc90a998d3f 100644 --- a/spec/models/application_record_spec.rb +++ b/spec/models/application_record_spec.rb @@ -11,6 +11,25 @@ describe ApplicationRecord do end end + describe '.safe_ensure_unique' do + let(:model) { build(:suggestion) } + let(:klass) { model.class } + + before do + allow(model).to receive(:save).and_raise(ActiveRecord::RecordNotUnique) + end + + it 'returns false when ActiveRecord::RecordNotUnique is raised' do + expect(model).to receive(:save).once + expect(klass.safe_ensure_unique { model.save }).to be_falsey + end + + it 'retries based on retry count specified' do + expect(model).to receive(:save).exactly(3).times + expect(klass.safe_ensure_unique(retries: 2) { model.save }).to be_falsey + end + end + describe '.safe_find_or_create_by' do it 'creates the user avoiding race conditions' do expect(Suggestion).to receive(:find_or_create_by).and_raise(ActiveRecord::RecordNotUnique) |