diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /spec/models/application_record_spec.rb | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'spec/models/application_record_spec.rb')
-rw-r--r-- | spec/models/application_record_spec.rb | 68 |
1 files changed, 35 insertions, 33 deletions
diff --git a/spec/models/application_record_spec.rb b/spec/models/application_record_spec.rb index d9ab326505b..5ea1907543a 100644 --- a/spec/models/application_record_spec.rb +++ b/spec/models/application_record_spec.rb @@ -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 |