Commit 83dca9d2 authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'fix-usage-of-suggestion-factory' into 'master'

Refactor usage of suggestion factory to support parent strategy

See merge request gitlab-org/gitlab!41768
parents 2109632c c2ca1678
...@@ -30,21 +30,24 @@ RSpec.describe ApplicationRecord do ...@@ -30,21 +30,24 @@ RSpec.describe ApplicationRecord do
end end
end end
context 'safe find or create methods' do
let_it_be(:note) { create(:diff_note_on_merge_request) }
let(:suggestion_attributes) { attributes_for(:suggestion).merge!(note_id: note.id) }
describe '.safe_find_or_create_by' do describe '.safe_find_or_create_by' do
it 'creates the user avoiding race conditions' do it 'creates the suggestion avoiding race conditions' do
expect(Suggestion).to receive(:find_or_create_by).and_raise(ActiveRecord::RecordNotUnique) expect(Suggestion).to receive(:find_or_create_by).and_raise(ActiveRecord::RecordNotUnique)
allow(Suggestion).to receive(:find_or_create_by).and_call_original allow(Suggestion).to receive(:find_or_create_by).and_call_original
expect { Suggestion.safe_find_or_create_by(build(:suggestion).attributes) } expect { Suggestion.safe_find_or_create_by(suggestion_attributes) }
.to change { Suggestion.count }.by(1) .to change { Suggestion.count }.by(1)
end end
it 'passes a block to find_or_create_by' do it 'passes a block to find_or_create_by' do
attributes = build(:suggestion).attributes
expect do |block| expect do |block|
Suggestion.safe_find_or_create_by(attributes, &block) Suggestion.safe_find_or_create_by(suggestion_attributes, &block)
end.to yield_with_args(an_object_having_attributes(attributes)) end.to yield_with_args(an_object_having_attributes(suggestion_attributes))
end end
it 'does not create a record when is not valid' do it 'does not create a record when is not valid' do
...@@ -59,7 +62,7 @@ RSpec.describe ApplicationRecord do ...@@ -59,7 +62,7 @@ RSpec.describe ApplicationRecord do
it 'creates a record using 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).to receive(:find_or_create_by).and_call_original
expect(Suggestion.safe_find_or_create_by!(build(:suggestion).attributes)) expect(Suggestion.safe_find_or_create_by!(suggestion_attributes))
.to be_a(Suggestion) .to be_a(Suggestion)
end end
...@@ -68,11 +71,10 @@ RSpec.describe ApplicationRecord do ...@@ -68,11 +71,10 @@ RSpec.describe ApplicationRecord do
end end
it 'passes a block to find_or_create_by' do it 'passes a block to find_or_create_by' do
attributes = build(:suggestion).attributes
expect do |block| expect do |block|
Suggestion.safe_find_or_create_by!(attributes, &block) Suggestion.safe_find_or_create_by!(suggestion_attributes, &block)
end.to yield_with_args(an_object_having_attributes(attributes)) end.to yield_with_args(an_object_having_attributes(suggestion_attributes))
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment