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) }
|
2021-05-18 08:10:26 -04:00
|
|
|
let_it_be(:note) { create(:diff_note_on_merge_request) }
|
|
|
|
|
2019-04-12 00:19:45 -04:00
|
|
|
let(:klass) { model.class }
|
|
|
|
|
|
|
|
before do
|
2021-05-18 08:10:26 -04:00
|
|
|
allow(model).to receive(:save!).and_raise(ActiveRecord::RecordNotUnique)
|
2019-04-12 00:19:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when ActiveRecord::RecordNotUnique is raised' do
|
2021-05-18 08:10:26 -04:00
|
|
|
expect(model).to receive(:save!).once
|
|
|
|
model.note_id = note.id
|
|
|
|
expect(klass.safe_ensure_unique { model.save! }).to be_falsey
|
2019-04-12 00:19:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries based on retry count specified' do
|
2021-05-18 08:10:26 -04:00
|
|
|
expect(model).to receive(:save!).exactly(3).times
|
|
|
|
model.note_id = note.id
|
|
|
|
expect(klass.safe_ensure_unique(retries: 2) { model.save! }).to be_falsey
|
2019-04-12 00:19:45 -04:00
|
|
|
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
|
|
|
|
2021-08-25 02:11:32 -04:00
|
|
|
describe '.safe_find_or_create_by' do
|
2020-09-10 08:08:54 -04:00
|
|
|
it 'creates the suggestion avoiding race conditions' do
|
2021-08-18 17:08:44 -04:00
|
|
|
existing_suggestion = double(:Suggestion)
|
2020-08-05 11:09:59 -04:00
|
|
|
|
2021-08-18 17:08:44 -04:00
|
|
|
expect(Suggestion).to receive(:find_by).and_return(nil, existing_suggestion)
|
|
|
|
expect(Suggestion).to receive(:create).and_raise(ActiveRecord::RecordNotUnique)
|
|
|
|
|
|
|
|
expect(Suggestion.safe_find_or_create_by(suggestion_attributes)).to eq(existing_suggestion)
|
2020-09-10 08:08:54 -04:00
|
|
|
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
|
|
|
|
|
2021-08-25 02:11:32 -04:00
|
|
|
describe '.safe_find_or_create_by!' do
|
2020-09-10 08:08:54 -04:00
|
|
|
it 'creates a record using safe_find_or_create_by' do
|
|
|
|
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
|
2020-11-04 10:08:41 -05:00
|
|
|
expect { Suggestion.safe_find_or_create_by!(note: nil) }
|
|
|
|
.to raise_error(ActiveRecord::RecordInvalid)
|
2020-09-10 08:08:54 -04:00
|
|
|
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-11-04 10:08:41 -05:00
|
|
|
|
|
|
|
it 'raises a record not found error in case of attributes mismatch' do
|
|
|
|
suggestion = Suggestion.safe_find_or_create_by!(suggestion_attributes)
|
|
|
|
attributes = suggestion_attributes.merge(outdated: !suggestion.outdated)
|
|
|
|
|
|
|
|
expect { Suggestion.safe_find_or_create_by!(attributes) }
|
|
|
|
.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
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
|
|
|
|
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
|
2021-03-23 05:09:17 -04:00
|
|
|
|
2022-03-13 23:07:26 -04:00
|
|
|
describe '.where_not_exists' do
|
|
|
|
it 'produces a WHERE NOT EXISTS query' do
|
|
|
|
create(:user, :two_factor_via_u2f)
|
|
|
|
user_2 = create(:user)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
User.where_not_exists(
|
|
|
|
U2fRegistration.where(U2fRegistration.arel_table[:user_id].eq(User.arel_table[:id])))
|
|
|
|
).to match_array([user_2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-21 11:08:52 -04:00
|
|
|
describe '.transaction', :delete do
|
|
|
|
it 'opens a new transaction' do
|
|
|
|
expect(described_class.connection.transaction_open?).to be false
|
|
|
|
|
|
|
|
Project.transaction do
|
|
|
|
expect(Project.connection.transaction_open?).to be true
|
|
|
|
|
|
|
|
Project.transaction(requires_new: true) do
|
|
|
|
expect(Project.connection.transaction_open?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not increment a counter when a transaction is not nested' do
|
|
|
|
expect(described_class.connection.transaction_open?).to be false
|
|
|
|
|
|
|
|
expect(::Gitlab::Database::Metrics)
|
|
|
|
.not_to receive(:subtransactions_increment)
|
|
|
|
|
|
|
|
Project.transaction do
|
|
|
|
expect(Project.connection.transaction_open?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
Project.transaction(requires_new: true) do
|
|
|
|
expect(Project.connection.transaction_open?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments a counter when a nested transaction is created' do
|
|
|
|
expect(described_class.connection.transaction_open?).to be false
|
|
|
|
|
|
|
|
expect(::Gitlab::Database::Metrics)
|
|
|
|
.to receive(:subtransactions_increment)
|
|
|
|
.with('Project')
|
|
|
|
.once
|
|
|
|
|
|
|
|
Project.transaction do
|
|
|
|
Project.transaction(requires_new: true) do
|
|
|
|
expect(Project.connection.transaction_open?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-24 11:10:36 -04:00
|
|
|
|
|
|
|
it 'increments a counter when a transaction is created in ActiveRecord' do
|
|
|
|
expect(described_class.connection.transaction_open?).to be false
|
|
|
|
|
|
|
|
expect(::Gitlab::Database::Metrics)
|
|
|
|
.to receive(:subtransactions_increment)
|
2021-12-20 07:10:29 -05:00
|
|
|
.with('ApplicationRecord')
|
2021-08-24 11:10:36 -04:00
|
|
|
.once
|
|
|
|
|
2021-12-20 07:10:29 -05:00
|
|
|
ApplicationRecord.transaction do
|
|
|
|
ApplicationRecord.transaction(requires_new: true) do
|
|
|
|
expect(ApplicationRecord.connection.transaction_open?).to be true
|
2021-08-24 11:10:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-07-21 11:08:52 -04:00
|
|
|
end
|
|
|
|
|
2021-03-23 05:09:17 -04:00
|
|
|
describe '.with_fast_read_statement_timeout' do
|
|
|
|
context 'when the query runs faster than configured timeout' do
|
|
|
|
it 'executes the query without error' do
|
|
|
|
result = nil
|
|
|
|
|
|
|
|
expect do
|
|
|
|
described_class.with_fast_read_statement_timeout(100) do
|
|
|
|
result = described_class.connection.exec_query('SELECT 1')
|
|
|
|
end
|
|
|
|
end.not_to raise_error
|
|
|
|
|
|
|
|
expect(result).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This query hangs for 10ms and then gets cancelled. As there is no
|
|
|
|
# other way to test the timeout for sure, 10ms of waiting seems to be
|
|
|
|
# reasonable!
|
|
|
|
context 'when the query runs longer than configured timeout' do
|
|
|
|
it 'cancels the query and raises an exception' do
|
|
|
|
expect do
|
|
|
|
described_class.with_fast_read_statement_timeout(10) do
|
|
|
|
described_class.connection.exec_query('SELECT pg_sleep(0.1)')
|
|
|
|
end
|
|
|
|
end.to raise_error(ActiveRecord::QueryCanceled)
|
|
|
|
end
|
|
|
|
end
|
2021-05-27 02:10:47 -04:00
|
|
|
|
|
|
|
context 'with database load balancing' do
|
2021-09-21 14:11:18 -04:00
|
|
|
let(:session) { Gitlab::Database::LoadBalancing::Session.new }
|
2021-05-27 02:10:47 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(::Gitlab::Database::LoadBalancing::Session).to receive(:current).and_return(session)
|
|
|
|
allow(session).to receive(:fallback_to_replicas_for_ambiguous_queries).and_yield
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'yields control' do
|
|
|
|
expect do |blk|
|
|
|
|
described_class.with_fast_read_statement_timeout(&blk)
|
|
|
|
end.to yield_control.once
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the query runs faster than configured timeout' do
|
|
|
|
it 'executes the query without error' do
|
|
|
|
result = nil
|
|
|
|
|
|
|
|
expect do
|
|
|
|
described_class.with_fast_read_statement_timeout(100) do
|
|
|
|
result = described_class.connection.exec_query('SELECT 1')
|
|
|
|
end
|
|
|
|
end.not_to raise_error
|
|
|
|
|
|
|
|
expect(result).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This query hangs for 10ms and then gets cancelled. As there is no
|
|
|
|
# other way to test the timeout for sure, 10ms of waiting seems to be
|
|
|
|
# reasonable!
|
|
|
|
context 'when the query runs longer than configured timeout' do
|
|
|
|
it 'cancels the query and raiss an exception' do
|
|
|
|
expect do
|
|
|
|
described_class.with_fast_read_statement_timeout(10) do
|
|
|
|
described_class.connection.exec_query('SELECT pg_sleep(0.1)')
|
|
|
|
end
|
|
|
|
end.to raise_error(ActiveRecord::QueryCanceled)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-23 05:09:17 -04:00
|
|
|
end
|
2021-09-01 05:10:58 -04:00
|
|
|
|
|
|
|
describe '.default_select_columns' do
|
|
|
|
shared_examples_for 'selects identically to the default' do
|
|
|
|
it 'generates the same sql as the default' do
|
|
|
|
expected_sql = test_model.all.to_sql
|
|
|
|
generated_sql = test_model.all.select(test_model.default_select_columns).to_sql
|
|
|
|
|
|
|
|
expect(expected_sql).to eq(generated_sql)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
ApplicationRecord.connection.execute(<<~SQL)
|
|
|
|
create table tests (
|
|
|
|
id bigserial primary key not null,
|
|
|
|
ignore_me text
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
context 'without an ignored column' do
|
|
|
|
let(:test_model) do
|
|
|
|
Class.new(ApplicationRecord) do
|
|
|
|
self.table_name = 'tests'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'selects identically to the default'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an ignored column' do
|
|
|
|
let(:test_model) do
|
|
|
|
Class.new(ApplicationRecord) do
|
|
|
|
include IgnorableColumns
|
|
|
|
self.table_name = 'tests'
|
|
|
|
|
|
|
|
ignore_columns :ignore_me, remove_after: '2100-01-01', remove_with: '99.12'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'selects identically to the default'
|
|
|
|
end
|
|
|
|
end
|
2018-11-22 04:35:28 -05:00
|
|
|
end
|