debian-mirror-gitlab/spec/lib/gitlab/database/schema_validation/database_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
3.2 KiB
Ruby
Raw Normal View History

2023-04-23 21:23:45 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-06-20 00:43:36 +05:30
RSpec.shared_examples 'database schema assertions for' do |fetch_by_name_method, exists_method, all_objects_method|
2023-05-27 22:25:52 +05:30
subject(:database) { described_class.new(connection) }
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
let(:database_model) { Gitlab::Database.database_base_models['main'] }
2023-04-23 21:23:45 +05:30
let(:connection) { database_model.connection }
2023-06-20 00:43:36 +05:30
before do
allow(connection).to receive(:select_rows).and_return(results)
allow(connection).to receive(:exec_query).and_return(results)
end
2023-04-23 21:23:45 +05:30
2023-06-20 00:43:36 +05:30
describe "##{fetch_by_name_method}" do
it 'returns nil when schema object does not exists' do
expect(database.public_send(fetch_by_name_method, 'invalid-object-name')).to be_nil
2023-05-27 22:25:52 +05:30
end
2023-06-20 00:43:36 +05:30
it 'returns the schema object by name' do
expect(database.public_send(fetch_by_name_method, valid_schema_object_name).name).to eq(valid_schema_object_name)
2023-05-27 22:25:52 +05:30
end
2023-06-20 00:43:36 +05:30
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
describe "##{exists_method}" do
it 'returns true when schema object exists' do
expect(database.public_send(exists_method, valid_schema_object_name)).to be_truthy
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
it 'returns false when schema object does not exists' do
expect(database.public_send(exists_method, 'invalid-object')).to be_falsey
end
end
2023-04-23 21:23:45 +05:30
2023-06-20 00:43:36 +05:30
describe "##{all_objects_method}" do
it 'returns all the schema objects' do
schema_objects = database.public_send(all_objects_method)
2023-04-23 21:23:45 +05:30
2023-06-20 00:43:36 +05:30
expect(schema_objects).to all(be_a(schema_object))
expect(schema_objects.map(&:name)).to eq([valid_schema_object_name])
2023-04-23 21:23:45 +05:30
end
2023-06-20 00:43:36 +05:30
end
end
2023-04-23 21:23:45 +05:30
2023-06-20 00:43:36 +05:30
RSpec.describe Gitlab::Database::SchemaValidation::Database, feature_category: :database do
context 'when having indexes' do
let(:schema_object) { Gitlab::Database::SchemaValidation::SchemaObjects::Index }
let(:valid_schema_object_name) { 'index' }
let(:results) do
[['index', 'CREATE UNIQUE INDEX "index" ON public.achievements USING btree (namespace_id, lower(name))']]
2023-04-23 21:23:45 +05:30
end
2023-06-20 00:43:36 +05:30
include_examples 'database schema assertions for', 'fetch_index_by_name', 'index_exists?', 'indexes'
2023-04-23 21:23:45 +05:30
end
2023-05-27 22:25:52 +05:30
context 'when having triggers' do
let(:schema_object) { Gitlab::Database::SchemaValidation::SchemaObjects::Trigger }
2023-06-20 00:43:36 +05:30
let(:valid_schema_object_name) { 'my_trigger' }
2023-05-27 22:25:52 +05:30
let(:results) do
2023-06-20 00:43:36 +05:30
[['my_trigger', 'CREATE TRIGGER my_trigger BEFORE INSERT ON todos FOR EACH ROW EXECUTE FUNCTION trigger()']]
2023-05-27 22:25:52 +05:30
end
2023-06-20 00:43:36 +05:30
include_examples 'database schema assertions for', 'fetch_trigger_by_name', 'trigger_exists?', 'triggers'
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
context 'when having tables' do
let(:schema_object) { Gitlab::Database::SchemaValidation::SchemaObjects::Table }
let(:valid_schema_object_name) { 'my_table' }
let(:results) do
[
{
'table_name' => 'my_table',
'column_name' => 'id',
'not_null' => true,
'data_type' => 'bigint',
2023-07-09 08:55:56 +05:30
'partition_key' => false,
2023-06-20 00:43:36 +05:30
'column_default' => "nextval('audit_events_id_seq'::regclass)"
},
{
'table_name' => 'my_table',
'column_name' => 'details',
'not_null' => false,
'data_type' => 'text',
2023-07-09 08:55:56 +05:30
'partition_key' => false,
2023-06-20 00:43:36 +05:30
'column_default' => nil
}
]
2023-05-27 22:25:52 +05:30
end
2023-04-23 21:23:45 +05:30
2023-06-20 00:43:36 +05:30
include_examples 'database schema assertions for', 'fetch_table_by_name', 'table_exists?', 'tables'
2023-04-23 21:23:45 +05:30
end
end