summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2019-02-04 14:39:54 +0100
committerBob Van Landuyt <bob@vanlanduyt.co>2019-02-05 14:44:41 +0100
commitccd8a9b2821c964b85533c253430041712ef195e (patch)
treefb39012eb785dcfaa6b46a062e32b3fe4a042d53 /spec
parentd6b39ea7fb31e243c59b0ca66b0fd4de3296f004 (diff)
downloadgitlab-ce-ccd8a9b2821c964b85533c253430041712ef195e.tar.gz
Adds helper for `find_or_create_by` in transaction
This allows us to call `find_or_create_by` on all models and scopes.
Diffstat (limited to 'spec')
-rw-r--r--spec/models/application_record_spec.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/spec/models/application_record_spec.rb b/spec/models/application_record_spec.rb
index 68aed387bfc..ca23f581fdc 100644
--- a/spec/models/application_record_spec.rb
+++ b/spec/models/application_record_spec.rb
@@ -10,4 +10,14 @@ describe ApplicationRecord do
expect(User.id_in(records.last(2).map(&:id))).to eq(records.last(2))
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)
+ allow(Suggestion).to receive(:find_or_create_by).and_call_original
+
+ expect { Suggestion.safe_find_or_create_by(build(:suggestion).attributes) }
+ .to change { Suggestion.count }.by(1)
+ end
+ end
end