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,49 +30,51 @@ RSpec.describe ApplicationRecord do ...@@ -30,49 +30,51 @@ RSpec.describe ApplicationRecord do
end end
end end
describe '.safe_find_or_create_by' do context 'safe find or create methods' do
it 'creates the user avoiding race conditions' do let_it_be(:note) { create(:diff_note_on_merge_request) }
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) } let(:suggestion_attributes) { attributes_for(:suggestion).merge!(note_id: note.id) }
.to change { Suggestion.count }.by(1)
end
it 'passes a block to find_or_create_by' do describe '.safe_find_or_create_by' do
attributes = build(:suggestion).attributes it 'creates the suggestion 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 do |block| expect { Suggestion.safe_find_or_create_by(suggestion_attributes) }
Suggestion.safe_find_or_create_by(attributes, &block) .to change { Suggestion.count }.by(1)
end.to yield_with_args(an_object_having_attributes(attributes)) end
end
it 'does not create a record when is not valid' do it 'passes a block to find_or_create_by' do
raw_usage_data = RawUsageData.safe_find_or_create_by({ recorded_at: nil }) expect do |block|
Suggestion.safe_find_or_create_by(suggestion_attributes, &block)
end.to yield_with_args(an_object_having_attributes(suggestion_attributes))
end
expect(raw_usage_data.id).to be_nil it 'does not create a record when is not valid' do
expect(raw_usage_data).not_to be_valid raw_usage_data = RawUsageData.safe_find_or_create_by({ recorded_at: nil })
end
end
describe '.safe_find_or_create_by!' do expect(raw_usage_data.id).to be_nil
it 'creates a record using safe_find_or_create_by' do expect(raw_usage_data).not_to be_valid
expect(Suggestion).to receive(:find_or_create_by).and_call_original end
expect(Suggestion.safe_find_or_create_by!(build(:suggestion).attributes))
.to be_a(Suggestion)
end end
it 'raises a validation error if the record was not persisted' do describe '.safe_find_or_create_by!' do
expect { Suggestion.find_or_create_by!(note: nil) }.to raise_error(ActiveRecord::RecordInvalid) it 'creates a record using safe_find_or_create_by' do
end expect(Suggestion).to receive(:find_or_create_by).and_call_original
expect(Suggestion.safe_find_or_create_by!(suggestion_attributes))
.to be_a(Suggestion)
end
it 'passes a block to find_or_create_by' do it 'raises a validation error if the record was not persisted' do
attributes = build(:suggestion).attributes expect { Suggestion.find_or_create_by!(note: nil) }.to raise_error(ActiveRecord::RecordInvalid)
end
expect do |block| it 'passes a block to find_or_create_by' do
Suggestion.safe_find_or_create_by!(attributes, &block) expect do |block|
end.to yield_with_args(an_object_having_attributes(attributes)) Suggestion.safe_find_or_create_by!(suggestion_attributes, &block)
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