diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2019-02-05 18:22:25 +0100 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2019-02-06 15:24:46 +0100 |
commit | c982edfa1969770696c2e85a8e32160eb0304cbc (patch) | |
tree | de446e480e2a89271b7871550d9904935c00e9ae /spec/models/application_record_spec.rb | |
parent | 02cc32c65579573e340603d09280e5b9e88f7a01 (diff) | |
download | gitlab-ce-c982edfa1969770696c2e85a8e32160eb0304cbc.tar.gz |
Avoid race conditions when creating GpgSignature
This avoids race conditions when creating GpgSignature.
Diffstat (limited to 'spec/models/application_record_spec.rb')
-rw-r--r-- | spec/models/application_record_spec.rb | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/spec/models/application_record_spec.rb b/spec/models/application_record_spec.rb index ca23f581fdc..fd25132ed3a 100644 --- a/spec/models/application_record_spec.rb +++ b/spec/models/application_record_spec.rb @@ -11,7 +11,7 @@ describe ApplicationRecord do end end - describe '#safe_find_or_create_by' do + 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) allow(Suggestion).to receive(:find_or_create_by).and_call_original @@ -20,4 +20,17 @@ describe ApplicationRecord do .to change { Suggestion.count }.by(1) end end + + describe '.safe_find_or_create_by!' do + it 'creates a record using safe_find_or_create_by' do + expect(Suggestion).to receive(:find_or_create_by).and_call_original + + expect(Suggestion.safe_find_or_create_by!(build(:suggestion).attributes)) + .to be_a(Suggestion) + end + + it 'raises a validation error if the record was not persisted' do + expect { Suggestion.find_or_create_by!(note: nil) }.to raise_error(ActiveRecord::RecordInvalid) + end + end end |