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
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
context 'safe find or create methods' do
let_it_be(:note) { create(:diff_note_on_merge_request) }
expect { Suggestion.safe_find_or_create_by(build(:suggestion).attributes) }
.to change { Suggestion.count }.by(1)
end
let(:suggestion_attributes) { attributes_for(:suggestion).merge!(note_id: note.id) }
it 'passes a block to find_or_create_by' do
attributes = build(:suggestion).attributes
describe '.safe_find_or_create_by' do
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|
Suggestion.safe_find_or_create_by(attributes, &block)
end.to yield_with_args(an_object_having_attributes(attributes))
end
expect { Suggestion.safe_find_or_create_by(suggestion_attributes) }
.to change { Suggestion.count }.by(1)
end
it 'does not create a record when is not valid' do
raw_usage_data = RawUsageData.safe_find_or_create_by({ recorded_at: nil })
it 'passes a block to find_or_create_by' do
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
expect(raw_usage_data).not_to be_valid
end
end
it 'does not create a record when is not valid' do
raw_usage_data = RawUsageData.safe_find_or_create_by({ recorded_at: nil })
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)
expect(raw_usage_data.id).to be_nil
expect(raw_usage_data).not_to be_valid
end
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
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!(suggestion_attributes))
.to be_a(Suggestion)
end
it 'passes a block to find_or_create_by' do
attributes = build(:suggestion).attributes
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
expect do |block|
Suggestion.safe_find_or_create_by!(attributes, &block)
end.to yield_with_args(an_object_having_attributes(attributes))
it 'passes a block to find_or_create_by' do
expect do |block|
Suggestion.safe_find_or_create_by!(suggestion_attributes, &block)
end.to yield_with_args(an_object_having_attributes(suggestion_attributes))
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