diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-20 18:19:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-20 18:19:03 +0300 |
commit | 14bd84b61276ef29b97d23642d698de769bacfd2 (patch) | |
tree | f9eba90140c1bd874211dea17750a0d422c04080 /spec/lib/gitlab/database/schema_validation/database_spec.rb | |
parent | 891c388697b2db0d8ee0c8358a9bdbf6dc56d581 (diff) |
Add latest changes from gitlab-org/gitlab@15-10-stable-eev15.10.0-rc42
Diffstat (limited to 'spec/lib/gitlab/database/schema_validation/database_spec.rb')
-rw-r--r-- | spec/lib/gitlab/database/schema_validation/database_spec.rb | 111 |
1 files changed, 88 insertions, 23 deletions
diff --git a/spec/lib/gitlab/database/schema_validation/database_spec.rb b/spec/lib/gitlab/database/schema_validation/database_spec.rb index c0026f91b46..eadaf683a29 100644 --- a/spec/lib/gitlab/database/schema_validation/database_spec.rb +++ b/spec/lib/gitlab/database/schema_validation/database_spec.rb @@ -3,43 +3,108 @@ require 'spec_helper' RSpec.describe Gitlab::Database::SchemaValidation::Database, feature_category: :database do - let(:database_name) { 'main' } - let(:database_indexes) do - [['index', 'CREATE UNIQUE INDEX "index" ON public.achievements USING btree (namespace_id, lower(name))']] - end + subject(:database) { described_class.new(connection) } - let(:query_result) { instance_double('ActiveRecord::Result', rows: database_indexes) } - let(:database_model) { Gitlab::Database.database_base_models[database_name] } + let(:database_model) { Gitlab::Database.database_base_models['main'] } let(:connection) { database_model.connection } - subject(:database) { described_class.new(connection) } + context 'when having indexes' do + let(:schema_object) { Gitlab::Database::SchemaValidation::SchemaObjects::Index } + let(:results) do + [['index', 'CREATE UNIQUE INDEX "index" ON public.achievements USING btree (namespace_id, lower(name))']] + end - before do - allow(connection).to receive(:exec_query).and_return(query_result) - end + before do + allow(connection).to receive(:select_rows).and_return(results) + end + + describe '#fetch_index_by_name' do + context 'when index does not exist' do + it 'returns nil' do + index = database.fetch_index_by_name('non_existing_index') + + expect(index).to be_nil + end + end + + it 'returns index by name' do + index = database.fetch_index_by_name('index') + + expect(index.name).to eq('index') + end + end + + describe '#index_exists?' do + context 'when index exists' do + it 'returns true' do + index_exists = database.index_exists?('index') + + expect(index_exists).to be_truthy + end + end - describe '#fetch_index_by_name' do - context 'when index does not exist' do - it 'returns nil' do - index = database.fetch_index_by_name('non_existing_index') + context 'when index does not exist' do + it 'returns false' do + index_exists = database.index_exists?('non_existing_index') - expect(index).to be_nil + expect(index_exists).to be_falsey + end end end - it 'returns index by name' do - index = database.fetch_index_by_name('index') + describe '#indexes' do + it 'returns indexes' do + indexes = database.indexes - expect(index.name).to eq('index') + expect(indexes).to all(be_a(schema_object)) + expect(indexes.map(&:name)).to eq(['index']) + end end end - describe '#indexes' do - it 'returns indexes' do - indexes = database.indexes + context 'when having triggers' do + let(:schema_object) { Gitlab::Database::SchemaValidation::SchemaObjects::Trigger } + let(:results) do + { 'my_trigger' => 'CREATE TRIGGER my_trigger BEFORE INSERT ON todos FOR EACH ROW EXECUTE FUNCTION trigger()' } + end + + before do + allow(database).to receive(:fetch_triggers).and_return(results) + end + + describe '#fetch_trigger_by_name' do + context 'when trigger does not exist' do + it 'returns nil' do + expect(database.fetch_trigger_by_name('non_existing_trigger')).to be_nil + end + end + + it 'returns trigger by name' do + expect(database.fetch_trigger_by_name('my_trigger').name).to eq('my_trigger') + end + end + + describe '#trigger_exists?' do + context 'when trigger exists' do + it 'returns true' do + expect(database.trigger_exists?('my_trigger')).to be_truthy + end + end + + context 'when trigger does not exist' do + it 'returns false' do + expect(database.trigger_exists?('non_existing_trigger')).to be_falsey + end + end + end - expect(indexes).to all(be_a(Gitlab::Database::SchemaValidation::Index)) - expect(indexes.map(&:name)).to eq(['index']) + describe '#triggers' do + it 'returns triggers' do + triggers = database.triggers + + expect(triggers).to all(be_a(schema_object)) + expect(triggers.map(&:name)).to eq(['my_trigger']) + end end end end |