2018-11-22 04:35:28 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe ApplicationRecord do
|
2018-11-22 04:35:28 -05:00
|
|
|
describe '#id_in' do
|
|
|
|
let(:records) { create_list(:user, 3) }
|
|
|
|
|
|
|
|
it 'returns records of the ids' do
|
|
|
|
expect(User.id_in(records.last(2).map(&:id))).to eq(records.last(2))
|
|
|
|
end
|
|
|
|
end
|
2019-02-04 08:39:54 -05:00
|
|
|
|
2019-04-12 00:19:45 -04:00
|
|
|
describe '.safe_ensure_unique' do
|
|
|
|
let(:model) { build(:suggestion) }
|
|
|
|
let(:klass) { model.class }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(model).to receive(:save).and_raise(ActiveRecord::RecordNotUnique)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when ActiveRecord::RecordNotUnique is raised' do
|
|
|
|
expect(model).to receive(:save).once
|
|
|
|
expect(klass.safe_ensure_unique { model.save }).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries based on retry count specified' do
|
|
|
|
expect(model).to receive(:save).exactly(3).times
|
|
|
|
expect(klass.safe_ensure_unique(retries: 2) { model.save }).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
context 'safe find or create methods' do
|
|
|
|
let_it_be(:note) { create(:diff_note_on_merge_request) }
|
2019-02-04 08:39:54 -05:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
let(:suggestion_attributes) { attributes_for(:suggestion).merge!(note_id: note.id) }
|
2020-08-05 11:09:59 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
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
|
2020-08-05 11:09:59 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
expect { Suggestion.safe_find_or_create_by(suggestion_attributes) }
|
|
|
|
.to change { Suggestion.count }.by(1)
|
|
|
|
end
|
2020-08-17 17:09:56 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
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
|
2020-08-17 17:09:56 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
it 'does not create a record when is not valid' do
|
|
|
|
raw_usage_data = RawUsageData.safe_find_or_create_by({ recorded_at: nil })
|
2019-02-05 12:22:25 -05:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
expect(raw_usage_data.id).to be_nil
|
|
|
|
expect(raw_usage_data).not_to be_valid
|
|
|
|
end
|
2019-02-05 12:22:25 -05:00
|
|
|
end
|
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
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
|
2020-08-05 11:09:59 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
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
|
2020-08-05 11:09:59 -04:00
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
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
|
2020-08-05 11:09:59 -04:00
|
|
|
end
|
2019-02-05 12:22:25 -05:00
|
|
|
end
|
2019-05-11 08:06:44 -04:00
|
|
|
|
|
|
|
describe '.underscore' do
|
|
|
|
it 'returns the underscored value of the class as a string' do
|
|
|
|
expect(MergeRequest.underscore).to eq('merge_request')
|
|
|
|
end
|
|
|
|
end
|
2020-07-16 20:09:37 -04:00
|
|
|
|
|
|
|
describe '.at_most' do
|
|
|
|
it 'limits the number of records returned' do
|
|
|
|
create_list(:user, 3)
|
|
|
|
expect(User.at_most(2).count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
2020-10-14 08:08:58 -04:00
|
|
|
|
|
|
|
describe '.where_exists' do
|
|
|
|
it 'produces a WHERE EXISTS query' do
|
|
|
|
user = create(:user)
|
|
|
|
|
|
|
|
expect(User.where_exists(User.limit(1))).to eq([user])
|
|
|
|
end
|
|
|
|
end
|
2018-11-22 04:35:28 -05:00
|
|
|
end
|