2022-07-16 23:28:13 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
RSpec.describe MigrationsHelpers, feature_category: :database do
|
2022-07-16 23:28:13 +05:30
|
|
|
let(:helper_class) do
|
|
|
|
Class.new.tap do |klass|
|
|
|
|
klass.include described_class
|
|
|
|
allow(klass).to receive(:metadata).and_return(metadata)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:metadata) { {} }
|
|
|
|
let(:helper) { helper_class.new }
|
|
|
|
|
|
|
|
describe '#active_record_base' do
|
|
|
|
it 'returns the main base model' do
|
|
|
|
expect(helper.active_record_base).to eq(ActiveRecord::Base)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ci database configured' do
|
|
|
|
before do
|
2023-04-23 21:23:45 +05:30
|
|
|
skip_if_multiple_databases_not_setup(:ci)
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the CI base model' do
|
|
|
|
expect(helper.active_record_base(database: :ci)).to eq(Ci::ApplicationRecord)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ci database not configured' do
|
|
|
|
before do
|
2023-04-23 21:23:45 +05:30
|
|
|
skip_if_multiple_databases_are_setup(:ci)
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the main base model' do
|
|
|
|
expect(helper.active_record_base(database: :ci)).to eq(ActiveRecord::Base)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises ArgumentError for bad database argument' do
|
|
|
|
expect { helper.active_record_base(database: :non_existent) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#table' do
|
|
|
|
it 'creates a class based on main base model' do
|
|
|
|
klass = helper.table(:projects)
|
|
|
|
expect(klass.connection_specification_name).to eq('ActiveRecord::Base')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ci database configured' do
|
|
|
|
before do
|
2023-04-23 21:23:45 +05:30
|
|
|
skip_if_multiple_databases_not_setup(:ci)
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'create a class based on the CI base model' do
|
|
|
|
klass = helper.table(:ci_builds, database: :ci)
|
|
|
|
expect(klass.connection_specification_name).to eq('Ci::ApplicationRecord')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ci database not configured' do
|
|
|
|
before do
|
2023-04-23 21:23:45 +05:30
|
|
|
skip_if_multiple_databases_are_setup(:ci)
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a class based on main base model' do
|
|
|
|
klass = helper.table(:ci_builds, database: :ci)
|
|
|
|
expect(klass.connection_specification_name).to eq('ActiveRecord::Base')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
|
|
|
|
describe '#reset_column_information' do
|
|
|
|
context 'with a regular ActiveRecord model class' do
|
|
|
|
let(:klass) { Project }
|
|
|
|
|
|
|
|
it 'calls reset_column_information' do
|
|
|
|
expect(klass).to receive(:reset_column_information)
|
|
|
|
|
|
|
|
helper.reset_column_information(klass)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an anonymous class with table name defined' do
|
|
|
|
let(:klass) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = :projects
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls reset_column_information' do
|
|
|
|
expect(klass).to receive(:reset_column_information)
|
|
|
|
|
|
|
|
helper.reset_column_information(klass)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an anonymous class with no table name defined' do
|
|
|
|
let(:klass) { Class.new(ActiveRecord::Base) }
|
|
|
|
|
|
|
|
it 'does not call reset_column_information' do
|
|
|
|
expect(klass).not_to receive(:reset_column_information)
|
|
|
|
|
|
|
|
helper.reset_column_information(klass)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|