diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-27 00:09:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-27 00:09:20 +0300 |
commit | f825fd1d881ce077ad868a70fd8d7db6a49e4700 (patch) | |
tree | daeae56475be62f02a6a00d7af2f2161a8dc6c50 /spec/support | |
parent | 51ba1dfa3bed95ec8c3fdee9b682a7941c4c34cc (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/helpers/database/multiple_databases_helpers.rb (renamed from spec/support/database/multiple_databases.rb) | 75 | ||||
-rw-r--r-- | spec/support/migration.rb | 36 | ||||
-rw-r--r-- | spec/support/multiple_databases.rb | 25 | ||||
-rw-r--r-- | spec/support/shared_examples/models/wiki_shared_examples.rb | 49 | ||||
-rw-r--r-- | spec/support/shared_examples/services/work_items/widgets/milestone_service_shared_examples.rb | 42 |
5 files changed, 154 insertions, 73 deletions
diff --git a/spec/support/database/multiple_databases.rb b/spec/support/helpers/database/multiple_databases_helpers.rb index b6341c2caec..16f5168ca29 100644 --- a/spec/support/database/multiple_databases.rb +++ b/spec/support/helpers/database/multiple_databases_helpers.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Database - module MultipleDatabases + module MultipleDatabasesHelpers def skip_if_multiple_databases_not_setup skip 'Skipping because multiple databases not set up' unless Gitlab::Database.has_config?(:ci) end @@ -52,17 +52,17 @@ module Database # # rubocop:disable Database/MultipleDatabases def with_reestablished_active_record_base(reconnect: true) - connection_classes = ActiveRecord::Base.connection_handler.connection_pool_names.map(&:constantize).to_h do |klass| - [klass, klass.connection_db_config] - end + connection_classes = ActiveRecord::Base + .connection_handler + .connection_pool_names + .map(&:constantize) + .index_with(&:connection_db_config) original_handler = ActiveRecord::Base.connection_handler new_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new ActiveRecord::Base.connection_handler = new_handler - if reconnect - connection_classes.each { |klass, db_config| klass.establish_connection(db_config) } - end + connection_classes.each { |klass, db_config| klass.establish_connection(db_config) } if reconnect yield ensure @@ -95,9 +95,12 @@ module Database module ActiveRecordBaseEstablishConnection def establish_connection(*args) # rubocop:disable Database/MultipleDatabases - if connected? && connection&.transaction_open? && ActiveRecord::Base.connection_handler == ActiveRecord::Base.default_connection_handler - raise "Cannot re-establish '#{self}.establish_connection' within an open transaction (#{connection&.open_transactions.to_i}). " \ - "Use `with_reestablished_active_record_base` instead or add `:reestablished_active_record_base` to rspec context." + if connected? && + connection&.transaction_open? && + ActiveRecord::Base.connection_handler == ActiveRecord::Base.default_connection_handler + raise "Cannot re-establish '#{self}.establish_connection' within an open transaction " \ + "(#{connection&.open_transactions.to_i}). Use `with_reestablished_active_record_base` " \ + "instead or add `:reestablished_active_record_base` to rspec context." end # rubocop:enable Database/MultipleDatabases @@ -106,56 +109,4 @@ module Database end end -RSpec.configure do |config| - # Ensure database versions are memoized to prevent query counts from - # being affected by version checks. Note that - # Gitlab::Database.check_postgres_version_and_print_warning is called - # at startup, but that generates its own - # `Gitlab::Database::Reflection` so the result is not memoized by - # callers of `ApplicationRecord.database.version`, such as - # `Gitlab::Database::AsWithMaterialized.materialized_supported?`. - # TODO This can be removed once https://gitlab.com/gitlab-org/gitlab/-/issues/325639 is completed. - [ApplicationRecord, ::Ci::ApplicationRecord].each { |record| record.database.version } - - config.around(:each, :reestablished_active_record_base) do |example| - with_reestablished_active_record_base(reconnect: example.metadata.fetch(:reconnect, true)) do - example.run - end - end - - config.around(:each, :add_ci_connection) do |example| - with_added_ci_connection do - example.run - end - end - - config.append_after(:context, :migration) do - recreate_databases_and_seed_if_needed || ensure_schema_and_empty_tables - end - - config.around(:each, :migration) do |example| - self.class.use_transactional_tests = false - - migration_schema = example.metadata[:migration] - migration_schema = :gitlab_main if migration_schema == true - base_model = Gitlab::Database.schemas_to_base_models.fetch(migration_schema).first - - # Migration require an `ActiveRecord::Base` to point to desired database - if base_model != ActiveRecord::Base - with_reestablished_active_record_base do - reconfigure_db_connection( - model: ActiveRecord::Base, - config_model: base_model - ) - - example.run - end - else - example.run - end - - self.class.use_transactional_tests = true - end -end - ActiveRecord::Base.singleton_class.prepend(::Database::ActiveRecordBaseEstablishConnection) # rubocop:disable Database/MultipleDatabases diff --git a/spec/support/migration.rb b/spec/support/migration.rb index 490aa836d74..2a69630a29a 100644 --- a/spec/support/migration.rb +++ b/spec/support/migration.rb @@ -16,14 +16,42 @@ RSpec.configure do |config| schema_migrate_down! end + config.after(:context, :migration) do + Gitlab::CurrentSettings.clear_in_memory_application_settings! + end + + config.append_after(:context, :migration) do + recreate_databases_and_seed_if_needed || ensure_schema_and_empty_tables + end + + config.around(:each, :migration) do |example| + self.class.use_transactional_tests = false + + migration_schema = example.metadata[:migration] + migration_schema = :gitlab_main if migration_schema == true + base_model = Gitlab::Database.schemas_to_base_models.fetch(migration_schema).first + + # Migration require an `ActiveRecord::Base` to point to desired database + if base_model != ActiveRecord::Base + with_reestablished_active_record_base do + reconfigure_db_connection( + model: ActiveRecord::Base, + config_model: base_model + ) + + example.run + end + else + example.run + end + + self.class.use_transactional_tests = true + end + # Each example may call `migrate!`, so we must ensure we are migrated down every time config.before(:each, :migration) do use_fake_application_settings schema_migrate_down! end - - config.after(:context, :migration) do - Gitlab::CurrentSettings.clear_in_memory_application_settings! - end end diff --git a/spec/support/multiple_databases.rb b/spec/support/multiple_databases.rb new file mode 100644 index 00000000000..616cf00269c --- /dev/null +++ b/spec/support/multiple_databases.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + # Ensure database versions are memoized to prevent query counts from + # being affected by version checks. Note that + # Gitlab::Database.check_postgres_version_and_print_warning is called + # at startup, but that generates its own + # `Gitlab::Database::Reflection` so the result is not memoized by + # callers of `ApplicationRecord.database.version`, such as + # `Gitlab::Database::AsWithMaterialized.materialized_supported?`. + # TODO This can be removed once https://gitlab.com/gitlab-org/gitlab/-/issues/325639 is completed. + [ApplicationRecord, ::Ci::ApplicationRecord].each { |record| record.database.version } + + config.around(:each, :reestablished_active_record_base) do |example| + with_reestablished_active_record_base(reconnect: example.metadata.fetch(:reconnect, true)) do + example.run + end + end + + config.around(:each, :add_ci_connection) do |example| + with_added_ci_connection do + example.run + end + end +end diff --git a/spec/support/shared_examples/models/wiki_shared_examples.rb b/spec/support/shared_examples/models/wiki_shared_examples.rb index 1fa8ccf4b55..be7e19492d7 100644 --- a/spec/support/shared_examples/models/wiki_shared_examples.rb +++ b/spec/support/shared_examples/models/wiki_shared_examples.rb @@ -161,9 +161,10 @@ RSpec.shared_examples 'wiki model' do let(:wiki_pages) { subject.list_pages } before do - subject.create_page('index', 'This is an index') + # The order is intentional subject.create_page('index2', 'This is an index2') - subject.create_page('an index3', 'This is an index3') + subject.create_page('index', 'This is an index') + subject.create_page('index3', 'This is an index3') end it 'returns an array of WikiPage instances' do @@ -183,13 +184,47 @@ RSpec.shared_examples 'wiki model' do context 'with limit option' do it 'returns limited set of pages' do - expect(subject.list_pages(limit: 1).count).to eq(1) + expect( + subject.list_pages(limit: 1).map(&:title) + ).to eql(%w[index]) + end + + it 'returns all set of pages if limit is more than the total pages' do + expect(subject.list_pages(limit: 4).count).to eq(3) + end + + it 'returns all set of pages if limit is 0' do + expect(subject.list_pages(limit: 0).count).to eq(3) + end + end + + context 'with offset option' do + it 'returns offset-ed set of pages' do + expect( + subject.list_pages(offset: 1).map(&:title) + ).to eq(%w[index2 index3]) + + expect( + subject.list_pages(offset: 2).map(&:title) + ).to eq(["index3"]) + expect(subject.list_pages(offset: 3).count).to eq(0) + expect(subject.list_pages(offset: 4).count).to eq(0) + end + + it 'returns all set of pages if offset is 0' do + expect(subject.list_pages(offset: 0).count).to eq(3) + end + + it 'can combines with limit' do + expect( + subject.list_pages(offset: 1, limit: 1).map(&:title) + ).to eq(["index2"]) end end context 'with sorting options' do it 'returns pages sorted by title by default' do - pages = ['an index3', 'index', 'index2'] + pages = %w[index index2 index3] expect(subject.list_pages.map(&:title)).to eq(pages) expect(subject.list_pages(direction: 'desc').map(&:title)).to eq(pages.reverse) @@ -200,9 +235,9 @@ RSpec.shared_examples 'wiki model' do let(:pages) { subject.list_pages(load_content: true) } it 'loads WikiPage content' do - expect(pages.first.content).to eq('This is an index3') - expect(pages.second.content).to eq('This is an index') - expect(pages.third.content).to eq('This is an index2') + expect(pages.first.content).to eq('This is an index') + expect(pages.second.content).to eq('This is an index2') + expect(pages.third.content).to eq('This is an index3') end end end diff --git a/spec/support/shared_examples/services/work_items/widgets/milestone_service_shared_examples.rb b/spec/support/shared_examples/services/work_items/widgets/milestone_service_shared_examples.rb new file mode 100644 index 00000000000..ac17915c15a --- /dev/null +++ b/spec/support/shared_examples/services/work_items/widgets/milestone_service_shared_examples.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +RSpec.shared_examples "setting work item's milestone" do + context "when 'milestone' param does not exist" do + let(:params) { {} } + + it "does not set the work item's milestone" do + expect { execute_callback }.to not_change(work_item, :milestone) + end + end + + context "when 'milestone' is not in the work item's project's hierarchy" do + let(:another_group_milestone) { create(:milestone, group: create(:group)) } + let(:params) { { milestone_id: another_group_milestone.id } } + + it "does not set the work item's milestone" do + expect { execute_callback }.to not_change(work_item, :milestone) + end + end + + context 'when assigning a group milestone' do + let(:params) { { milestone_id: group_milestone.id } } + + it "sets the work item's milestone" do + expect { execute_callback } + .to change(work_item, :milestone) + .from(nil) + .to(group_milestone) + end + end + + context 'when assigning a project milestone' do + let(:params) { { milestone_id: project_milestone.id } } + + it "sets the work item's milestone" do + expect { execute_callback } + .to change(work_item, :milestone) + .from(nil) + .to(project_milestone) + end + end +end |