2018-11-22 04:35:28 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ApplicationRecord do
|
|
|
|
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-02-05 12:22:25 -05:00
|
|
|
describe '.safe_find_or_create_by' do
|
2019-02-04 08:39:54 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
expect { Suggestion.safe_find_or_create_by(build(:suggestion).attributes) }
|
|
|
|
.to change { Suggestion.count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
2019-02-05 12:22:25 -05: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!(build(:suggestion).attributes))
|
|
|
|
.to be_a(Suggestion)
|
|
|
|
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
|
|
|
|
end
|
2018-11-22 04:35:28 -05:00
|
|
|
end
|