debian-mirror-gitlab/spec/lib/gitlab/database/each_database_spec.rb

101 lines
3.8 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::EachDatabase do
describe '.each_database_connection' do
2022-04-04 11:22:00 +05:30
before do
allow(Gitlab::Database).to receive(:database_base_models)
.and_return({ main: ActiveRecord::Base, ci: Ci::ApplicationRecord }.with_indifferent_access)
2021-12-11 22:18:48 +05:30
end
2022-04-04 11:22:00 +05:30
it 'yields each connection after connecting SharedModel', :add_ci_connection do
expect(Gitlab::Database::SharedModel).to receive(:using_connection)
.with(ActiveRecord::Base.connection).ordered.and_yield
2021-12-11 22:18:48 +05:30
2022-04-04 11:22:00 +05:30
expect(Gitlab::Database::SharedModel).to receive(:using_connection)
.with(Ci::ApplicationRecord.connection).ordered.and_yield
2021-12-11 22:18:48 +05:30
2022-04-04 11:22:00 +05:30
expect { |b| described_class.each_database_connection(&b) }
.to yield_successive_args(
[ActiveRecord::Base.connection, 'main'],
[Ci::ApplicationRecord.connection, 'ci']
)
2021-12-11 22:18:48 +05:30
end
end
describe '.each_model_connection' do
2022-04-04 11:22:00 +05:30
context 'when the model inherits from SharedModel', :add_ci_connection do
let(:model1) { Class.new(Gitlab::Database::SharedModel) }
let(:model2) { Class.new(Gitlab::Database::SharedModel) }
2021-12-11 22:18:48 +05:30
2022-04-04 11:22:00 +05:30
before do
allow(Gitlab::Database).to receive(:database_base_models)
.and_return({ main: ActiveRecord::Base, ci: Ci::ApplicationRecord }.with_indifferent_access)
end
it 'yields each model with SharedModel connected to each database connection' do
expect_yielded_models([model1, model2], [
{ model: model1, connection: ActiveRecord::Base.connection, name: 'main' },
{ model: model1, connection: Ci::ApplicationRecord.connection, name: 'ci' },
{ model: model2, connection: ActiveRecord::Base.connection, name: 'main' },
{ model: model2, connection: Ci::ApplicationRecord.connection, name: 'ci' }
])
end
context 'when the model limits connection names' do
before do
model1.limit_connection_names = %i[main]
model2.limit_connection_names = %i[ci]
end
it 'only yields the model with SharedModel connected to the limited connections' do
expect_yielded_models([model1, model2], [
{ model: model1, connection: ActiveRecord::Base.connection, name: 'main' },
{ model: model2, connection: Ci::ApplicationRecord.connection, name: 'ci' }
])
end
end
end
context 'when the model does not inherit from SharedModel' do
let(:main_model) { Class.new(ActiveRecord::Base) }
let(:ci_model) { Class.new(Ci::ApplicationRecord) }
let(:main_connection) { double(:connection) }
let(:ci_connection) { double(:connection) }
before do
allow(main_model).to receive(:connection).and_return(main_connection)
allow(ci_model).to receive(:connection).and_return(ci_connection)
allow(main_connection).to receive_message_chain('pool.db_config.name').and_return('main')
allow(ci_connection).to receive_message_chain('pool.db_config.name').and_return('ci')
end
it 'yields each model after connecting SharedModel' do
expect_yielded_models([main_model, ci_model], [
{ model: main_model, connection: main_connection, name: 'main' },
{ model: ci_model, connection: ci_connection, name: 'ci' }
])
end
2021-12-11 22:18:48 +05:30
end
2022-04-04 11:22:00 +05:30
def expect_yielded_models(models_to_iterate, expected_values)
times_yielded = 0
described_class.each_model_connection(models_to_iterate) do |model, name|
expected = expected_values[times_yielded]
2021-12-11 22:18:48 +05:30
2022-04-04 11:22:00 +05:30
expect(model).to be(expected[:model])
expect(model.connection).to be(expected[:connection])
expect(name).to eq(expected[:name])
2021-12-11 22:18:48 +05:30
2022-04-04 11:22:00 +05:30
times_yielded += 1
2021-12-11 22:18:48 +05:30
end
2022-04-04 11:22:00 +05:30
expect(times_yielded).to eq(expected_values.size)
2021-12-11 22:18:48 +05:30
end
end
end