:tanuki:
') end expect do filter(':tanuki:
:party-parrot:
') - end.not_to exceed_all_query_limit(control_count.count) + end.not_to exceed_all_query_limit(control) end it 'uses custom emoji from ancestor group' do diff --git a/spec/lib/banzai/filter/issuable_reference_expansion_filter_spec.rb b/spec/lib/banzai/filter/issuable_reference_expansion_filter_spec.rb index 06bb0edc92c..d14f218763f 100644 --- a/spec/lib/banzai/filter/issuable_reference_expansion_filter_spec.rb +++ b/spec/lib/banzai/filter/issuable_reference_expansion_filter_spec.rb @@ -259,15 +259,15 @@ RSpec.describe Banzai::Filter::IssuableReferenceExpansionFilter, feature_categor # warm up filter(link, context) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do filter(link, context) - end.count + end - expect(control_count).to eq 12 + expect(control.count).to eq 12 expect do filter("#{link} #{link2}", context) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end end @@ -419,15 +419,15 @@ RSpec.describe Banzai::Filter::IssuableReferenceExpansionFilter, feature_categor # warm up filter(link, context) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do filter(link, context) - end.count + end - expect(control_count).to eq 10 + expect(control.count).to eq 10 expect do filter("#{link} #{link2}", context) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end end diff --git a/spec/lib/banzai/filter/references/alert_reference_filter_spec.rb b/spec/lib/banzai/filter/references/alert_reference_filter_spec.rb index 9a2e68aaae0..0bdd64c360d 100644 --- a/spec/lib/banzai/filter/references/alert_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/alert_reference_filter_spec.rb @@ -230,11 +230,11 @@ RSpec.describe Banzai::Filter::References::AlertReferenceFilter, feature_categor it 'does not have N+1 per multiple references per project', :use_sql_query_cache do markdown = alert_reference.to_s - max_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do reference_filter(markdown) - end.count + end - expect(max_count).to eq 1 + expect(control.count).to eq 1 markdown = "#{alert_reference} ^alert#2 ^alert#3 ^alert#4 #{alert2_reference}" @@ -248,11 +248,9 @@ RSpec.describe Banzai::Filter::References::AlertReferenceFilter, feature_categor # 1x2 for alerts in each project # Total == 7 # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359 - max_count += 6 - expect do reference_filter(markdown) - end.not_to exceed_all_query_limit(max_count) + end.not_to exceed_all_query_limit(control).with_threshold(6) end end end diff --git a/spec/lib/banzai/filter/references/commit_reference_filter_spec.rb b/spec/lib/banzai/filter/references/commit_reference_filter_spec.rb index 35a3f20f7b7..730554857df 100644 --- a/spec/lib/banzai/filter/references/commit_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/commit_reference_filter_spec.rb @@ -283,11 +283,11 @@ RSpec.describe Banzai::Filter::References::CommitReferenceFilter, feature_catego it 'does not have N+1 per multiple references per project', :use_sql_query_cache do markdown = commit_reference.to_s - max_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do reference_filter(markdown) - end.count + end - expect(max_count).to eq 0 + expect(control.count).to eq 0 markdown = "#{commit_reference} 8b95f2f1 8b95f2f2 8b95f2f3 #{commit2_reference} #{commit3_reference}" @@ -298,11 +298,9 @@ RSpec.describe Banzai::Filter::References::CommitReferenceFilter, feature_catego # 1 for loading the namespaces associated to the project # 1 for loading the routes associated with the namespace # Total = 5 - max_count += 5 - expect do reference_filter(markdown) - end.not_to exceed_all_query_limit(max_count) + end.not_to exceed_all_query_limit(control).with_threshold(5) end end end diff --git a/spec/lib/banzai/filter/references/design_reference_filter_spec.rb b/spec/lib/banzai/filter/references/design_reference_filter_spec.rb index fd03d7c0d27..678d6619101 100644 --- a/spec/lib/banzai/filter/references/design_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/design_reference_filter_spec.rb @@ -240,7 +240,7 @@ RSpec.describe Banzai::Filter::References::DesignReferenceFilter, feature_catego * #1[not a valid reference.gif] MD - baseline = ActiveRecord::QueryRecorder.new { process(one_ref_per_project) } + control = ActiveRecord::QueryRecorder.new { process(one_ref_per_project) } # each project mentioned requires 2 queries: # @@ -253,7 +253,7 @@ RSpec.describe Banzai::Filter::References::DesignReferenceFilter, feature_catego # In addition there is a 1 query overhead for all the projects at the # start. Currently, the baseline for 2 projects is `2 * 2 + 1 = 5` queries # - expect { process(multiple_references) }.not_to exceed_query_limit(baseline.count) + expect { process(multiple_references) }.not_to exceed_query_limit(control) end end diff --git a/spec/lib/banzai/filter/references/external_issue_reference_filter_spec.rb b/spec/lib/banzai/filter/references/external_issue_reference_filter_spec.rb index 823f006c98a..acc59c85cbf 100644 --- a/spec/lib/banzai/filter/references/external_issue_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/external_issue_reference_filter_spec.rb @@ -338,9 +338,9 @@ RSpec.describe Banzai::Filter::References::ExternalIssueReferenceFilter, feature single_reference = "External Issue #{issue1.to_reference}" multiple_references = "External Issues #{issue1.to_reference} and #{issue2.to_reference}" - control_count = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html }.count + control = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html } - expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control_count) + expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control) end end end diff --git a/spec/lib/banzai/filter/references/issue_reference_filter_spec.rb b/spec/lib/banzai/filter/references/issue_reference_filter_spec.rb index d16188e99a3..fd947e3e9cb 100644 --- a/spec/lib/banzai/filter/references/issue_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/issue_reference_filter_spec.rb @@ -41,9 +41,9 @@ RSpec.describe Banzai::Filter::References::IssueReferenceFilter, feature_categor single_reference = "Issue #{issue.to_reference}" multiple_references = "Issues #{issue.to_reference} and #{another_issue.to_reference}" - control_count = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html }.count + control = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html } - expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control_count) + expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control) end end diff --git a/spec/lib/banzai/filter/references/label_reference_filter_spec.rb b/spec/lib/banzai/filter/references/label_reference_filter_spec.rb index 81b08a4c516..bcc256813c9 100644 --- a/spec/lib/banzai/filter/references/label_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/label_reference_filter_spec.rb @@ -35,13 +35,13 @@ RSpec.describe Banzai::Filter::References::LabelReferenceFilter, feature_categor # Run this once to establish a baseline reference_filter("Label #{reference}") - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do reference_filter("Label #{reference}") end labels_markdown = Array.new(10, "Label #{reference}").join('\n') - expect { reference_filter(labels_markdown) }.not_to exceed_all_query_limit(control_count.count) + expect { reference_filter(labels_markdown) }.not_to exceed_all_query_limit(control) end it 'includes a data-project attribute' do diff --git a/spec/lib/banzai/filter/references/merge_request_reference_filter_spec.rb b/spec/lib/banzai/filter/references/merge_request_reference_filter_spec.rb index ccc8478c7d8..e3036993f7b 100644 --- a/spec/lib/banzai/filter/references/merge_request_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/merge_request_reference_filter_spec.rb @@ -26,9 +26,9 @@ RSpec.describe Banzai::Filter::References::MergeRequestReferenceFilter, feature_ single_reference = "Merge request #{merge.to_reference}" multiple_references = "Merge requests #{merge.to_reference} and #{another_merge.to_reference}" - control_count = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html }.count + control = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html } - expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control_count) + expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control) end end diff --git a/spec/lib/banzai/filter/references/project_reference_filter_spec.rb b/spec/lib/banzai/filter/references/project_reference_filter_spec.rb index c55fff78756..12af94507b6 100644 --- a/spec/lib/banzai/filter/references/project_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/project_reference_filter_spec.rb @@ -115,17 +115,17 @@ RSpec.describe Banzai::Filter::References::ProjectReferenceFilter, feature_categ # warm up first reference_filter(markdown) - max_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do reference_filter(markdown) - end.count + end - expect(max_count).to eq 2 + expect(control.count).to eq 2 markdown = "#{normal_project_reference} #{invalidate_reference(normal_project_reference)} #{group_project_reference} #{nested_project_reference}" expect do reference_filter(markdown) - end.not_to exceed_all_query_limit(max_count) + end.not_to exceed_all_query_limit(control) end end end diff --git a/spec/lib/banzai/filter/references/reference_cache_spec.rb b/spec/lib/banzai/filter/references/reference_cache_spec.rb index 04877931610..b4d9a08e4c6 100644 --- a/spec/lib/banzai/filter/references/reference_cache_spec.rb +++ b/spec/lib/banzai/filter/references/reference_cache_spec.rb @@ -70,13 +70,13 @@ RSpec.describe Banzai::Filter::References::ReferenceCache, feature_category: :te filter_single = filter_class.new(doc_single, project: project) cache_single = described_class.new(filter_single, { project: project }, {}) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do cache_single.load_references_per_parent(filter_single.nodes) cache_single.load_parent_per_reference cache_single.load_records_per_parent - end.count + end - expect(control_count).to eq 3 + expect(control.count).to eq 3 # Since this is an issue filter that is not batching issue queries # across projects, we have to account for that. # 1 for for routes to find routes.source_id of projects matching paths @@ -88,13 +88,11 @@ RSpec.describe Banzai::Filter::References::ReferenceCache, feature_category: :te # 1x2 for groups # 1x2 for work_item_types # Total = 11 - max_count = control_count + 8 - expect do cache.load_references_per_parent(filter.nodes) cache.load_parent_per_reference cache.load_records_per_parent - end.not_to exceed_query_limit(max_count) + end.not_to exceed_query_limit(control).with_threshold(8) end end diff --git a/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb b/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb index 00eac7262f4..51c5551dda8 100644 --- a/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/snippet_reference_filter_spec.rb @@ -229,11 +229,11 @@ RSpec.describe Banzai::Filter::References::SnippetReferenceFilter, feature_categ it 'does not have N+1 per multiple references per project', :use_sql_query_cache do markdown = "#{reference} $9999990" - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do reference_filter(markdown) - end.count + end - expect(control_count).to eq 1 + expect(control.count).to eq 1 markdown = "#{reference} $9999990 $9999991 $9999992 $9999993 #{reference2} something/cool$12" @@ -247,11 +247,9 @@ RSpec.describe Banzai::Filter::References::SnippetReferenceFilter, feature_categ # 1x2 for snippets in each project == 2 # Total = 7 # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359 - max_count = control_count + 6 - expect do reference_filter(markdown) - end.not_to exceed_all_query_limit(max_count) + end.not_to exceed_all_query_limit(control).with_threshold(6) end end end diff --git a/spec/lib/banzai/filter/references/work_item_reference_filter_spec.rb b/spec/lib/banzai/filter/references/work_item_reference_filter_spec.rb index e59e53891bf..cf245ccc72a 100644 --- a/spec/lib/banzai/filter/references/work_item_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/work_item_reference_filter_spec.rb @@ -306,9 +306,9 @@ RSpec.describe Banzai::Filter::References::WorkItemReferenceFilter, feature_cate single_reference = "Work item #{work_item.to_reference}" multiple_references = "Work items #{work_item.to_reference} and #{another_work_item.to_reference}" - control_count = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html }.count + control = ActiveRecord::QueryRecorder.new { reference_filter(single_reference).to_html } - expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control_count) + expect { reference_filter(multiple_references).to_html }.not_to exceed_query_limit(control) end end end diff --git a/spec/lib/banzai/issuable_extractor_spec.rb b/spec/lib/banzai/issuable_extractor_spec.rb index 5bbd98592e7..fe1a2bd9a2e 100644 --- a/spec/lib/banzai/issuable_extractor_spec.rb +++ b/spec/lib/banzai/issuable_extractor_spec.rb @@ -45,9 +45,9 @@ RSpec.describe Banzai::IssuableExtractor, feature_category: :team_planning do second_call_queries = ActiveRecord::QueryRecorder.new do extractor.extract([issue_link, work_item_link, merge_request_link]) - end.count + end - expect(second_call_queries).to eq 0 + expect(second_call_queries.count).to eq 0 end end end diff --git a/spec/lib/banzai/reference_parser/snippet_parser_spec.rb b/spec/lib/banzai/reference_parser/snippet_parser_spec.rb index 8f4148be2dc..0f3834c2dc8 100644 --- a/spec/lib/banzai/reference_parser/snippet_parser_spec.rb +++ b/spec/lib/banzai/reference_parser/snippet_parser_spec.rb @@ -37,11 +37,11 @@ RSpec.describe Banzai::ReferenceParser::SnippetParser, feature_category: :team_p # Run this once to establish a baseline visible_references(:public) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do subject.nodes_visible_to_user(user, [link]) end - expect { subject.nodes_visible_to_user(user, Array.new(10, link)) }.not_to exceed_all_query_limit(control_count.count) + expect { subject.nodes_visible_to_user(user, Array.new(10, link)) }.not_to exceed_all_query_limit(control) end it 'creates a reference for guest for a public snippet' do diff --git a/spec/lib/gitlab/data_builder/pipeline_spec.rb b/spec/lib/gitlab/data_builder/pipeline_spec.rb index ad7cd2dc736..5fa61b1680d 100644 --- a/spec/lib/gitlab/data_builder/pipeline_spec.rb +++ b/spec/lib/gitlab/data_builder/pipeline_spec.rb @@ -184,14 +184,14 @@ RSpec.describe Gitlab::DataBuilder::Pipeline, feature_category: :continuous_inte create(:ci_build, :deploy_to_production, :with_deployment, user: user, project: project, pipeline: pipeline) # We need `.to_json` as the build hook data is wrapped within `Gitlab::Lazy` - control_count = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).to_json }.count + control = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).to_json } # Adding more builds to the pipeline and serializing the data again create_list(:ci_build, 3, user: user, project: project, pipeline: pipeline) create(:ci_build, :start_review_app, :with_deployment, user: user, project: project, pipeline: pipeline) create(:ci_build, :stop_review_app, :with_deployment, user: user, project: project, pipeline: pipeline) - expect { described_class.build(pipeline.reload).to_json }.not_to exceed_query_limit(control_count) + expect { described_class.build(pipeline.reload).to_json }.not_to exceed_query_limit(control) end it "with multiple retried builds" do @@ -201,14 +201,14 @@ RSpec.describe Gitlab::DataBuilder::Pipeline, feature_category: :continuous_inte create(:ci_build, :deploy_to_production, :retried, :with_deployment, user: user, project: project, pipeline: pipeline) # We need `.to_json` as the build hook data is wrapped within `Gitlab::Lazy` - control_count = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).with_retried_builds.to_json }.count + control = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).with_retried_builds.to_json } # Adding more builds to the pipeline and serializing the data again create_list(:ci_build, 3, :retried, user: user, project: project, pipeline: pipeline) create(:ci_build, :start_review_app, :retried, :with_deployment, user: user, project: project, pipeline: pipeline) create(:ci_build, :stop_review_app, :retried, :with_deployment, user: user, project: project, pipeline: pipeline) - expect { described_class.build(pipeline.reload).with_retried_builds.to_json }.not_to exceed_query_limit(control_count) + expect { described_class.build(pipeline.reload).with_retried_builds.to_json }.not_to exceed_query_limit(control) end end end diff --git a/spec/lib/gitlab/git_access_spec.rb b/spec/lib/gitlab/git_access_spec.rb index 71f9a7d0f0e..82daaba6448 100644 --- a/spec/lib/gitlab/git_access_spec.rb +++ b/spec/lib/gitlab/git_access_spec.rb @@ -1062,14 +1062,14 @@ RSpec.describe Gitlab::GitAccess, :aggregate_failures, feature_category: :system # additional queries. access.check('git-receive-pack', changes) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do access.check('git-receive-pack', changes) end changes = ['6f6d7e7ed 570e7b2ab refs/heads/master', '6f6d7e7ed 570e7b2ab refs/heads/feature'] # There is still an N+1 query with protected branches - expect { access.check('git-receive-pack', changes) }.not_to exceed_query_limit(control_count).with_threshold(2) + expect { access.check('git-receive-pack', changes) }.not_to exceed_query_limit(control).with_threshold(2) end it 'raises TimeoutError when #check_access! raises a timeout error' do diff --git a/spec/lib/gitlab/tracking/destinations/database_events_snowplow_spec.rb b/spec/lib/gitlab/tracking/destinations/database_events_snowplow_spec.rb deleted file mode 100644 index 5a5c7123971..00000000000 --- a/spec/lib/gitlab/tracking/destinations/database_events_snowplow_spec.rb +++ /dev/null @@ -1,140 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Tracking::Destinations::DatabaseEventsSnowplow, :do_not_stub_snowplow_by_default, feature_category: :application_instrumentation do - let(:emitter) { SnowplowTracker::Emitter.new(endpoint: 'localhost', options: { buffer_size: 1 }) } - - let(:tracker) do - SnowplowTracker::Tracker - .new( - emitters: [emitter], - subject: SnowplowTracker::Subject.new, - namespace: 'namespace', - app_id: 'app_id' - ) - end - - before do - stub_application_setting(snowplow_app_id: '_abc123_') - end - - around do |example| - freeze_time { example.run } - end - - context 'when snowplow is enabled' do - before do - allow(SnowplowTracker::AsyncEmitter) - .to receive(:new) - .with(endpoint: endpoint, - options: - { - protocol: 'https', - on_success: subject.method(:increment_successful_events_emissions), - on_failure: subject.method(:failure_callback) - } - ).and_return(emitter) - - allow(SnowplowTracker::Tracker) - .to receive(:new) - .with( - emitters: [emitter], - subject: an_instance_of(SnowplowTracker::Subject), - namespace: described_class::SNOWPLOW_NAMESPACE, - app_id: '_abc123_' - ).and_return(tracker) - end - - describe '#event' do - let(:endpoint) { 'localhost:9091' } - let(:event_params) do - { - category: 'category', - action: 'action', - label: 'label', - property: 'property', - value: 1.5, - context: nil, - tstamp: (Time.now.to_f * 1000).to_i - } - end - - context 'when on gitlab.com environment' do - let(:endpoint) { 'db-snowplow.trx.gitlab.net' } - - before do - stub_application_setting(snowplow_database_collector_hostname: endpoint) - end - - it 'sends event to tracker' do - allow(Gitlab).to receive(:com?).and_return(true) - allow(tracker).to receive(:track_struct_event).and_call_original - - subject.event('category', 'action', label: 'label', property: 'property', value: 1.5) - - expect(tracker).to have_received(:track_struct_event).with(event_params) - end - end - - it 'sends event to tracker' do - allow(tracker).to receive(:track_struct_event).and_call_original - - subject.event('category', 'action', label: 'label', property: 'property', value: 1.5) - - expect(tracker).to have_received(:track_struct_event).with(event_params) - end - - it 'increase total snowplow events counter' do - counter = double - - expect(counter).to receive(:increment) - expect(Gitlab::Metrics).to receive(:counter) - .with(:gitlab_db_events_snowplow_events_total, 'Number of Snowplow events') - .and_return(counter) - - subject.event('category', 'action', label: 'label', property: 'property', value: 1.5) - end - end - end - - context 'for callbacks' do - describe 'on success' do - it 'increase gitlab_successful_snowplow_events_total counter' do - counter = double - - expect(counter).to receive(:increment).with({}, 2) - expect(Gitlab::Metrics).to receive(:counter) - .with( - :gitlab_db_events_snowplow_successful_events_total, - 'Number of successful Snowplow events emissions').and_return(counter) - - subject.method(:increment_successful_events_emissions).call(2) - end - end - - describe 'on failure' do - it 'increase gitlab_failed_snowplow_events_total counter and logs failures', :aggregate_failures do - counter = double - error_message = "Issue database_event_update failed to be reported to collector at localhost:9091" - failures = [{ "e" => "se", - "se_ca" => "Issue", - "se_la" => "issues", - "se_ac" => "database_event_update" }] - allow(Gitlab::Metrics).to receive(:counter) - .with( - :gitlab_db_events_snowplow_successful_events_total, - 'Number of successful Snowplow events emissions').and_call_original - - expect(Gitlab::AppLogger).to receive(:error).with(error_message) - expect(counter).to receive(:increment).with({}, 1) - expect(Gitlab::Metrics).to receive(:counter) - .with( - :gitlab_db_events_snowplow_failed_events_total, - 'Number of failed Snowplow events emissions').and_return(counter) - - subject.method(:failure_callback).call(2, failures) - end - end - end -end diff --git a/spec/lib/gitlab/tracking_spec.rb b/spec/lib/gitlab/tracking_spec.rb index 46213532071..6e4ab00d04f 100644 --- a/spec/lib/gitlab/tracking_spec.rb +++ b/spec/lib/gitlab/tracking_spec.rb @@ -165,38 +165,6 @@ RSpec.describe Gitlab::Tracking, feature_category: :application_instrumentation end end - describe '.database_event' do - context 'when the action is not passed in as a string' do - it 'allows symbols' do - expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception) - - described_class.database_event('category', :some_action) - end - - it 'allows nil' do - expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception) - - described_class.database_event('category', nil) - end - - it 'allows integers' do - expect(Gitlab::ErrorTracking).not_to receive(:track_and_raise_for_dev_exception) - - described_class.database_event('category', 1) - end - end - - it_behaves_like 'rescued error raised by destination class' do - let(:category) { 'Issue' } - let(:action) { 'created' } - let(:destination_class) { Gitlab::Tracking::Destinations::DatabaseEventsSnowplow } - - subject(:tracking_method) { described_class.database_event(category, action) } - end - - it_behaves_like 'delegates to destination', Gitlab::Tracking::Destinations::DatabaseEventsSnowplow, :database_event - end - describe '.event' do context 'when the action is not passed in as a string' do it 'allows symbols' do diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb index e835681a233..34311a8ae22 100644 --- a/spec/mailers/notify_spec.rb +++ b/spec/mailers/notify_spec.rb @@ -2461,23 +2461,27 @@ RSpec.describe Notify, feature_category: :code_review_workflow do end it 'avoids N+1 cached queries when rendering html', :use_sql_query_cache, :request_store do - control_count = ActiveRecord::QueryRecorder.new(query_recorder_debug: true, skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(query_recorder_debug: true, skip_cached: false) do subject.html_part end create_list(:diff_note_on_merge_request, 3, review: review, project: project, author: review.author, noteable: merge_request) - expect { described_class.new_review_email(recipient.id, review.id).html_part }.not_to exceed_all_query_limit(control_count) + expect do + described_class.new_review_email(recipient.id, review.id).html_part + end.not_to exceed_all_query_limit(control) end it 'avoids N+1 cached queries when rendering text', :use_sql_query_cache, :request_store do - control_count = ActiveRecord::QueryRecorder.new(query_recorder_debug: true, skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(query_recorder_debug: true, skip_cached: false) do subject.text_part end create_list(:diff_note_on_merge_request, 3, review: review, project: project, author: review.author, noteable: merge_request) - expect { described_class.new_review_email(recipient.id, review.id).text_part }.not_to exceed_all_query_limit(control_count) + expect do + described_class.new_review_email(recipient.id, review.id).text_part + end.not_to exceed_all_query_limit(control) end end diff --git a/spec/models/analytics/cycle_analytics/stage_spec.rb b/spec/models/analytics/cycle_analytics/stage_spec.rb index 54ae0feca2c..abe041ae5d6 100644 --- a/spec/models/analytics/cycle_analytics/stage_spec.rb +++ b/spec/models/analytics/cycle_analytics/stage_spec.rb @@ -81,20 +81,4 @@ RSpec.describe Analytics::CycleAnalytics::Stage, feature_category: :value_stream expect(current_event_pairs).to eq(expected_event_pairs) end end - - it_behaves_like 'database events tracking' do - let(:namespace) { create(:group) } - let(:value_stream) { create(:cycle_analytics_value_stream) } - let(:record) { described_class.create!(stage_params) } - let(:update_params) { { name: 'st 2' } } - let(:stage_params) do - { - namespace: namespace, - name: 'st1', - start_event_identifier: :merge_request_created, - end_event_identifier: :merge_request_merged, - group_value_stream_id: value_stream.id - } - end - end end diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 5fc5bbd41ff..a95f56ea714 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -576,9 +576,9 @@ RSpec.describe Clusters::Cluster, :use_clean_rails_memory_store_caching, it 'avoids N+1 queries' do another_project = create(:project) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do described_class.ancestor_clusters_for_clusterable(another_project, hierarchy_order: hierarchy_order) - end.count + end cluster2 = create(:cluster, :provided_by_gcp, :group) child2 = cluster2.group @@ -587,7 +587,7 @@ RSpec.describe Clusters::Cluster, :use_clean_rails_memory_store_caching, expect do described_class.ancestor_clusters_for_clusterable(project, hierarchy_order: hierarchy_order) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end context 'for a group' do diff --git a/spec/models/concerns/database_event_tracking_spec.rb b/spec/models/concerns/database_event_tracking_spec.rb deleted file mode 100644 index a99b4737537..00000000000 --- a/spec/models/concerns/database_event_tracking_spec.rb +++ /dev/null @@ -1,85 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe DatabaseEventTracking, :snowplow, feature_category: :service_ping do - before do - allow(Gitlab::Tracking).to receive(:database_event).and_call_original - end - - let(:test_class) do - Class.new(ActiveRecord::Base) do - include DatabaseEventTracking - - self.table_name = 'application_setting_terms' - - self::SNOWPLOW_ATTRIBUTES = %w[id].freeze # rubocop:disable RSpec/LeakyConstantDeclaration - end - end - - subject(:create_test_class_record) { test_class.create!(id: 1, terms: "") } - - context 'if event emmiter failed' do - before do - allow(Gitlab::Tracking).to receive(:database_event).and_raise(StandardError) # rubocop:disable RSpec/ExpectGitlabTracking - end - - it 'tracks the exception' do - expect(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) - - create_test_class_record - end - end - - describe 'event tracking' do - let(:category) { test_class.to_s } - let(:event) { 'database_event' } - - it 'when created' do - create_test_class_record - - expect_snowplow_event( - tracking_method: :database_event, - category: category, - action: "#{event}_create", - label: 'application_setting_terms', - property: 'create', - namespace: nil, - project: nil, - "id" => 1 - ) - end - - it 'when updated' do - create_test_class_record - test_class.first.update!(id: 3) - - expect_snowplow_event( - tracking_method: :database_event, - category: category, - action: "#{event}_update", - label: 'application_setting_terms', - property: 'update', - namespace: nil, - project: nil, - "id" => 3 - ) - end - - it 'when destroyed' do - create_test_class_record - test_class.first.destroy! - - expect_snowplow_event( - tracking_method: :database_event, - category: category, - action: "#{event}_destroy", - label: 'application_setting_terms', - property: 'destroy', - namespace: nil, - project: nil, - "id" => 1 - ) - end - end -end diff --git a/spec/models/concerns/routable_spec.rb b/spec/models/concerns/routable_spec.rb index e71392f7bbc..c6135e769cc 100644 --- a/spec/models/concerns/routable_spec.rb +++ b/spec/models/concerns/routable_spec.rb @@ -89,7 +89,7 @@ RSpec.shared_examples 'routable resource' do context 'when use_includes: true' do it 'includes route information when loading records' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do described_class.where_full_path_in([record.full_path, record_2.full_path], use_includes: true) .map(&:route) end @@ -103,7 +103,7 @@ RSpec.shared_examples 'routable resource' do record_4.full_path ], use_includes: true) .map(&:route) - end.to issue_same_number_of_queries_as(control_count) + end.to issue_same_number_of_queries_as(control) end end diff --git a/spec/models/merge_request/metrics_spec.rb b/spec/models/merge_request/metrics_spec.rb index e9e4956dc41..8d1d503b323 100644 --- a/spec/models/merge_request/metrics_spec.rb +++ b/spec/models/merge_request/metrics_spec.rb @@ -93,12 +93,4 @@ RSpec.describe MergeRequest::Metrics do end end end - - it_behaves_like 'database events tracking', feature_category: :service_ping do - let(:merge_request) { create(:merge_request) } - - let(:record) { merge_request.metrics } - let(:namespace) { nil } - let(:update_params) { { pipeline_id: 1, updated_at: Date.tomorrow } } - end end diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index 5665bce8f08..67b8931f0c5 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -897,12 +897,14 @@ RSpec.describe Namespace, feature_category: :groups_and_projects do it 'does not cause N+1 query in fetching registries' do stub_container_registry_tags(repository: :any, tags: []) - control_count = ActiveRecord::QueryRecorder.new { namespace.any_project_has_container_registry_tags? }.count + control = ActiveRecord::QueryRecorder.new { namespace.any_project_has_container_registry_tags? } other_repositories = create_list(:container_repository, 2) create(:project, namespace: namespace, container_repositories: other_repositories) - expect { namespace.first_project_with_container_registry_tags }.not_to exceed_query_limit(control_count + 1) + expect do + namespace.first_project_with_container_registry_tags + end.not_to exceed_query_limit(control).with_threshold(1) end end diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index 5aa3ac3a2ea..59795059642 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -448,13 +448,13 @@ RSpec.describe Note, feature_category: :team_planning do # Project authorization checks are cached, establish a baseline retrieve_participants - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do retrieve_participants end create(:note_on_commit, project: note.project, note: 'another note', noteable_id: commit.id) - expect { retrieve_participants }.not_to exceed_query_limit(control_count) + expect { retrieve_participants }.not_to exceed_query_limit(control) end end diff --git a/spec/models/preloaders/commit_status_preloader_spec.rb b/spec/models/preloaders/commit_status_preloader_spec.rb index 85ea784335c..0453b6267ed 100644 --- a/spec/models/preloaders/commit_status_preloader_spec.rb +++ b/spec/models/preloaders/commit_status_preloader_spec.rb @@ -21,13 +21,13 @@ RSpec.describe Preloaders::CommitStatusPreloader do it 'prevents N+1 for specified relations', :use_sql_query_cache do execute - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do call_each_relation(statuses.sample(3)) end expect do call_each_relation(statuses) - end.to issue_same_number_of_queries_as(control_count) + end.to issue_same_number_of_queries_as(control) end private diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index a3db29e7ec4..7b8b6376aea 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2342,11 +2342,11 @@ RSpec.describe Project, factory_default: :keep, feature_category: :groups_and_pr it 'avoids n + 1', :aggregate_failures do create(:prometheus_integration) run_test = -> { described_class.include_integration(:prometheus_integration).map(&:prometheus_integration) } - control_count = ActiveRecord::QueryRecorder.new { run_test.call } + control = ActiveRecord::QueryRecorder.new { run_test.call } create(:prometheus_integration) expect(run_test.call.count).to eq(2) - expect { run_test.call }.not_to exceed_query_limit(control_count) + expect { run_test.call }.not_to exceed_query_limit(control) end end @@ -6593,17 +6593,17 @@ RSpec.describe Project, factory_default: :keep, feature_category: :groups_and_pr let_it_be(:subject) { create(:project) } it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_integrations }.count + control = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_integrations } - expect(control_count).to be <= 4 + expect(control.count).to be <= 4 end it 'avoids N+1 database queries with more available integrations' do allow(Integration).to receive(:available_integration_names).and_return(%w[pushover]) - control_count = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_integrations } + control = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_integrations } allow(Integration).to receive(:available_integration_names).and_call_original - expect { subject.find_or_initialize_integrations }.not_to exceed_query_limit(control_count) + expect { subject.find_or_initialize_integrations }.not_to exceed_query_limit(control) end context 'with disabled integrations' do @@ -6650,11 +6650,11 @@ RSpec.describe Project, factory_default: :keep, feature_category: :groups_and_pr it 'avoids N+1 database queries' do allow(Integration).to receive(:available_integration_names).and_return(%w[prometheus pushover]) - control_count = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_integration('prometheus') }.count + control = ActiveRecord::QueryRecorder.new { subject.find_or_initialize_integration('prometheus') } allow(Integration).to receive(:available_integration_names).and_call_original - expect { subject.find_or_initialize_integration('prometheus') }.not_to exceed_query_limit(control_count) + expect { subject.find_or_initialize_integration('prometheus') }.not_to exceed_query_limit(control) end it 'returns nil if integration is disabled' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6d3e9058f91..58d64742c92 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -4573,13 +4573,13 @@ RSpec.describe User, feature_category: :user_profile do it 'avoids N+1 queries' do fresh_user = described_class.find(user.id) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do fresh_user.solo_owned_groups - end.count + end create(:group).add_owner(user) - expect { solo_owned_groups }.not_to exceed_query_limit(control_count) + expect { solo_owned_groups }.not_to exceed_query_limit(control) end end end diff --git a/spec/requests/api/ci/jobs_spec.rb b/spec/requests/api/ci/jobs_spec.rb index 941aa3734a3..9e1203bc720 100644 --- a/spec/requests/api/ci/jobs_spec.rb +++ b/spec/requests/api/ci/jobs_spec.rb @@ -133,12 +133,12 @@ RSpec.describe API::Ci::Jobs, feature_category: :continuous_integration do end it 'avoids N+1 queries', :skip_before_request do - control_count = ActiveRecord::QueryRecorder.new { perform_request }.count + control = ActiveRecord::QueryRecorder.new { perform_request } running_job = create(:ci_build, :running, project: project, user: user, pipeline: pipeline, artifacts_expire_at: 1.day.since) running_job.save! - expect { perform_request }.not_to exceed_query_limit(control_count) + expect { perform_request }.not_to exceed_query_limit(control) end it_behaves_like 'returns common pipeline data' do @@ -432,7 +432,7 @@ RSpec.describe API::Ci::Jobs, feature_category: :continuous_integration do first_build.user = create(:user) first_build.save! - control_count = ActiveRecord::QueryRecorder.new { go }.count + control = ActiveRecord::QueryRecorder.new { go } second_pipeline = create(:ci_empty_pipeline, project: project, sha: project.commit.id, ref: project.default_branch) second_build = create(:ci_build, :trace_artifact, :artifacts, :test_reports, pipeline: second_pipeline) @@ -440,7 +440,7 @@ RSpec.describe API::Ci::Jobs, feature_category: :continuous_integration do second_build.user = create(:user) second_build.save! - expect { go }.not_to exceed_query_limit(control_count) + expect { go }.not_to exceed_query_limit(control) end context 'filter project with one scope element' do diff --git a/spec/requests/api/ci/pipeline_schedules_spec.rb b/spec/requests/api/ci/pipeline_schedules_spec.rb index f534b093b7c..588991096b5 100644 --- a/spec/requests/api/ci/pipeline_schedules_spec.rb +++ b/spec/requests/api/ci/pipeline_schedules_spec.rb @@ -42,15 +42,15 @@ RSpec.describe API::Ci::PipelineSchedules, feature_category: :continuous_integra # We need at least two users to trigger a preload for that relation. create_pipeline_schedules(1) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do get api("/projects/#{project.id}/pipeline_schedules", developer) - end.count + end create_pipeline_schedules(5) expect do get api("/projects/#{project.id}/pipeline_schedules", developer) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end %w[active inactive].each do |target| diff --git a/spec/requests/api/ci/pipelines_spec.rb b/spec/requests/api/ci/pipelines_spec.rb index eef125e1bc3..ef169dbe872 100644 --- a/spec/requests/api/ci/pipelines_spec.rb +++ b/spec/requests/api/ci/pipelines_spec.rb @@ -471,15 +471,15 @@ RSpec.describe API::Ci::Pipelines, feature_category: :continuous_integration do end it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api("/projects/#{project.id}/pipelines/#{pipeline.id}/jobs", api_user), params: query - end.count + end create_list(:ci_build, 3, :trace_artifact, :artifacts, :test_reports, pipeline: pipeline) expect do get api("/projects/#{project.id}/pipelines/#{pipeline.id}/jobs", api_user), params: query - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end context 'pipeline has retried jobs' do @@ -671,15 +671,15 @@ RSpec.describe API::Ci::Pipelines, feature_category: :continuous_integration do end it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api("/projects/#{project.id}/pipelines/#{pipeline.id}/bridges", api_user), params: query - end.count + end 3.times { create_bridge(pipeline) } expect do get api("/projects/#{project.id}/pipelines/#{pipeline.id}/bridges", api_user), params: query - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end diff --git a/spec/requests/api/ci/runners_spec.rb b/spec/requests/api/ci/runners_spec.rb index 187880e16a4..b4394f47105 100644 --- a/spec/requests/api/ci/runners_spec.rb +++ b/spec/requests/api/ci/runners_spec.rb @@ -965,7 +965,7 @@ RSpec.describe API::Ci::Runners, :aggregate_failures, feature_category: :fleet_v expect do get api("/runners/#{shared_runner.id}/jobs", admin, admin_mode: true) - end.not_to exceed_query_limit(control.count) + end.not_to exceed_query_limit(control) end it 'batches loading of commits' do diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb index 4ec5d195ff8..67388f657f4 100644 --- a/spec/requests/api/commits_spec.rb +++ b/spec/requests/api/commits_spec.rb @@ -1842,11 +1842,11 @@ RSpec.describe API::Commits, feature_category: :source_code_management do it 'are returned without N + 1' do get api(route, current_user) # warm up the cache - control_count = ActiveRecord::QueryRecorder.new { get api(route, current_user) }.count + control = ActiveRecord::QueryRecorder.new { get api(route, current_user) } create(:diff_note_on_commit, project: project, author: create(:user)) - expect { get api(route, current_user) }.not_to exceed_query_limit(control_count) + expect { get api(route, current_user) }.not_to exceed_query_limit(control) end end end @@ -2386,11 +2386,11 @@ RSpec.describe API::Commits, feature_category: :source_code_management do it 'returns multiple merge requests without N + 1' do perform_request(user) - control_count = ActiveRecord::QueryRecorder.new { perform_request(user) }.count + control = ActiveRecord::QueryRecorder.new { perform_request(user) } create(:merge_request, :closed, source_project: project, source_branch: 'master', target_branch: 'feature') - expect { perform_request(user) }.not_to exceed_query_limit(control_count) + expect { perform_request(user) }.not_to exceed_query_limit(control) end end diff --git a/spec/requests/api/deploy_keys_spec.rb b/spec/requests/api/deploy_keys_spec.rb index 30c345ef458..ca19a97ae49 100644 --- a/spec/requests/api/deploy_keys_spec.rb +++ b/spec/requests/api/deploy_keys_spec.rb @@ -135,11 +135,11 @@ RSpec.describe API::DeployKeys, :aggregate_failures, feature_category: :continuo it 'returns multiple deploy keys without N + 1' do perform_request - control_count = ActiveRecord::QueryRecorder.new { perform_request }.count + control = ActiveRecord::QueryRecorder.new { perform_request } create(:deploy_key, public: true, projects: [project], user: maintainer) - expect { perform_request }.not_to exceed_query_limit(control_count) + expect { perform_request }.not_to exceed_query_limit(control) end end diff --git a/spec/requests/api/deployments_spec.rb b/spec/requests/api/deployments_spec.rb index 5a8e1649e75..f68307df779 100644 --- a/spec/requests/api/deployments_spec.rb +++ b/spec/requests/api/deployments_spec.rb @@ -143,11 +143,11 @@ RSpec.describe API::Deployments, feature_category: :continuous_delivery do it 'returns multiple deployments without N + 1' do perform_request # warm up the cache - control_count = ActiveRecord::QueryRecorder.new { perform_request }.count + control = ActiveRecord::QueryRecorder.new { perform_request } create(:deployment, :success, project: project, deployable: build, iid: 21, ref: 'master') - expect { perform_request }.not_to exceed_query_limit(control_count) + expect { perform_request }.not_to exceed_query_limit(control) end end diff --git a/spec/requests/api/feature_flags_spec.rb b/spec/requests/api/feature_flags_spec.rb index 4fb0dfbb070..2e513194627 100644 --- a/spec/requests/api/feature_flags_spec.rb +++ b/spec/requests/api/feature_flags_spec.rb @@ -67,12 +67,12 @@ RSpec.describe API::FeatureFlags, feature_category: :feature_flags do end it 'does not have N+1 problem' do - control_count = ActiveRecord::QueryRecorder.new { subject } + control = ActiveRecord::QueryRecorder.new { subject } create_list(:operations_feature_flag, 3, project: project) expect { get api("/projects/#{project.id}/feature_flags", user) } - .not_to exceed_query_limit(control_count) + .not_to exceed_query_limit(control) end it_behaves_like 'check user permission' diff --git a/spec/requests/api/graphql/achievements/user_achievements_query_spec.rb b/spec/requests/api/graphql/achievements/user_achievements_query_spec.rb index 32048ea1432..94678bd18da 100644 --- a/spec/requests/api/graphql/achievements/user_achievements_query_spec.rb +++ b/spec/requests/api/graphql/achievements/user_achievements_query_spec.rb @@ -89,14 +89,14 @@ RSpec.describe 'UserAchievements', feature_category: :user_profile do end it 'can lookahead to eliminate N+1 queries', :use_clean_rails_memory_store_caching do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do post_graphql(query, current_user: user) - end.count + end user2 = create(:user) create(:user_achievement, achievement: achievement, user: user2) - expect { post_graphql(query, current_user: user) }.not_to exceed_all_query_limit(control_count) + expect { post_graphql(query, current_user: user) }.not_to exceed_all_query_limit(control) end context 'when the achievements feature flag is disabled' do diff --git a/spec/requests/api/graphql/projects/projects_spec.rb b/spec/requests/api/graphql/projects/projects_spec.rb index 84b8c2285f0..dfebcb7c42c 100644 --- a/spec/requests/api/graphql/projects/projects_spec.rb +++ b/spec/requests/api/graphql/projects/projects_spec.rb @@ -45,14 +45,14 @@ RSpec.describe 'getting a collection of projects', feature_category: :source_cod it 'avoids N+1 queries', :use_sql_query_cache, :clean_gitlab_redis_cache do post_graphql(single_project_query, current_user: current_user) - query_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do post_graphql(single_project_query, current_user: current_user) - end.count + end # There is an N+1 query for max_member_access_for_user_ids expect do post_graphql(query, current_user: current_user) - end.not_to exceed_all_query_limit(query_count + 5) + end.not_to exceed_all_query_limit(control).with_threshold(5) end it 'returns the expected projects' do diff --git a/spec/requests/api/graphql/user/user_achievements_query_spec.rb b/spec/requests/api/graphql/user/user_achievements_query_spec.rb index 2e6c3dcba61..ccff5bdf919 100644 --- a/spec/requests/api/graphql/user/user_achievements_query_spec.rb +++ b/spec/requests/api/graphql/user/user_achievements_query_spec.rb @@ -60,14 +60,14 @@ RSpec.describe 'UserAchievements', feature_category: :user_profile do end it 'can lookahead to eliminate N+1 queries', :use_clean_rails_memory_store_caching do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do post_graphql(query, current_user: user) - end.count + end achievement2 = create(:achievement, namespace: group) create_list(:user_achievement, 2, achievement: achievement2, user: user) - expect { post_graphql(query, current_user: user) }.not_to exceed_all_query_limit(control_count) + expect { post_graphql(query, current_user: user) }.not_to exceed_all_query_limit(control) end context 'when the achievements feature flag is disabled for a namespace' do diff --git a/spec/requests/api/graphql/work_item_spec.rb b/spec/requests/api/graphql/work_item_spec.rb index fe77b7ae736..c6d44b057a7 100644 --- a/spec/requests/api/graphql/work_item_spec.rb +++ b/spec/requests/api/graphql/work_item_spec.rb @@ -199,7 +199,7 @@ RSpec.describe 'Query.work_item(id)', feature_category: :team_planning do it 'avoids N+1 queries' do post_graphql(query, current_user: current_user) # warm up - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do post_graphql(query, current_user: current_user) end @@ -207,7 +207,7 @@ RSpec.describe 'Query.work_item(id)', feature_category: :team_planning do expect do post_graphql(query, current_user: current_user) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end context 'when user is guest' do diff --git a/spec/requests/api/group_milestones_spec.rb b/spec/requests/api/group_milestones_spec.rb index 82a4311f7d0..7b4075b3aeb 100644 --- a/spec/requests/api/group_milestones_spec.rb +++ b/spec/requests/api/group_milestones_spec.rb @@ -141,11 +141,11 @@ RSpec.describe API::GroupMilestones, feature_category: :team_planning do it 'returns multiple issues without performing N + 1' do perform_request - control_count = ActiveRecord::QueryRecorder.new { perform_request }.count + control = ActiveRecord::QueryRecorder.new { perform_request } create(:issue, project: project, milestone: milestone) - expect { perform_request }.not_to exceed_query_limit(control_count) + expect { perform_request }.not_to exceed_query_limit(control) end end diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb index d1158cba16e..6b949962e53 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -660,24 +660,24 @@ RSpec.describe API::Groups, feature_category: :groups_and_projects do get api("/groups/#{group1.id}", user1) expect(response).to have_gitlab_http_status(:ok) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do get api("/groups/#{group1.id}", user1) - end.count + end create(:project, namespace: group1) expect do get api("/groups/#{group1.id}", user1) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end it 'avoids N+1 queries with shared group links' do # setup at least 1 shared group, so that we record the queries that preload the nested associations too. create(:group_group_link, shared_group: group1, shared_with_group: create(:group)) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do get api("/groups/#{group1.id}", user1) - end.count + end # setup "n" more shared groups create(:group_group_link, shared_group: group1, shared_with_group: create(:group)) @@ -686,7 +686,7 @@ RSpec.describe API::Groups, feature_category: :groups_and_projects do # test that no of queries for 1 shared group is same as for n shared groups expect do get api("/groups/#{group1.id}", user1) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end @@ -1364,15 +1364,15 @@ RSpec.describe API::Groups, feature_category: :groups_and_projects do get api("/groups/#{group1.id}/projects", user1) expect(response).to have_gitlab_http_status(:ok) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do get api("/groups/#{group1.id}/projects", user1) - end.count + end create(:project, namespace: group1) expect do get api("/groups/#{group1.id}/projects", user1) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end @@ -1563,15 +1563,15 @@ RSpec.describe API::Groups, feature_category: :groups_and_projects do subject expect(response).to have_gitlab_http_status(:ok) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do subject - end.count + end create(:project_group_link, project: create(:project), group: group1) expect do subject - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end diff --git a/spec/requests/api/invitations_spec.rb b/spec/requests/api/invitations_spec.rb index dc02e830027..60f3c4780eb 100644 --- a/spec/requests/api/invitations_spec.rb +++ b/spec/requests/api/invitations_spec.rb @@ -412,7 +412,7 @@ RSpec.describe API::Invitations, feature_category: :user_profile do expect do post invitations_url(project, maintainer), params: { email: emails, access_level: Member::DEVELOPER } - end.not_to exceed_all_query_limit(control.count).with_threshold(unresolved_n_plus_ones) + end.not_to exceed_all_query_limit(control).with_threshold(unresolved_n_plus_ones) end it 'does not exceed expected queries count for user_ids', :request_store, :use_sql_query_cache do @@ -430,7 +430,7 @@ RSpec.describe API::Invitations, feature_category: :user_profile do expect do post invitations_url(project, maintainer), params: { user_id: users.map(&:id).join(','), access_level: Member::DEVELOPER } - end.not_to exceed_all_query_limit(control.count).with_threshold(unresolved_n_plus_ones) + end.not_to exceed_all_query_limit(control).with_threshold(unresolved_n_plus_ones) end it 'does not exceed expected queries count with secondary emails', :request_store, :use_sql_query_cache do @@ -453,7 +453,7 @@ RSpec.describe API::Invitations, feature_category: :user_profile do expect do post invitations_url(project, maintainer), params: { email: emails, access_level: Member::DEVELOPER } - end.not_to exceed_all_query_limit(control.count).with_threshold(unresolved_n_plus_ones) + end.not_to exceed_all_query_limit(control).with_threshold(unresolved_n_plus_ones) end end @@ -491,7 +491,7 @@ RSpec.describe API::Invitations, feature_category: :user_profile do expect do post invitations_url(group, maintainer), params: { email: emails, access_level: Member::DEVELOPER } - end.not_to exceed_all_query_limit(control.count).with_threshold(unresolved_n_plus_ones) + end.not_to exceed_all_query_limit(control).with_threshold(unresolved_n_plus_ones) end it 'does not exceed expected queries count for secondary emails', :request_store, :use_sql_query_cache do @@ -514,7 +514,7 @@ RSpec.describe API::Invitations, feature_category: :user_profile do expect do post invitations_url(group, maintainer), params: { email: emails, access_level: Member::DEVELOPER } - end.not_to exceed_all_query_limit(control.count).with_threshold(unresolved_n_plus_ones) + end.not_to exceed_all_query_limit(control).with_threshold(unresolved_n_plus_ones) end end diff --git a/spec/requests/api/issue_links_spec.rb b/spec/requests/api/issue_links_spec.rb index fcb199a91a4..a4a9eca92b9 100644 --- a/spec/requests/api/issue_links_spec.rb +++ b/spec/requests/api/issue_links_spec.rb @@ -40,11 +40,11 @@ RSpec.describe API::IssueLinks, feature_category: :team_planning do it 'returns multiple links without N + 1' do perform_request(user) - control_count = ActiveRecord::QueryRecorder.new { perform_request(user) }.count + control = ActiveRecord::QueryRecorder.new { perform_request(user) } create(:issue_link, source: issue, target: create(:issue, project: project)) - expect { perform_request(user) }.not_to exceed_query_limit(control_count) + expect { perform_request(user) }.not_to exceed_query_limit(control) end end end diff --git a/spec/requests/api/issues/get_project_issues_spec.rb b/spec/requests/api/issues/get_project_issues_spec.rb index 9e54ec08486..6719297f54f 100644 --- a/spec/requests/api/issues/get_project_issues_spec.rb +++ b/spec/requests/api/issues/get_project_issues_spec.rb @@ -233,9 +233,9 @@ RSpec.describe API::Issues, feature_category: :team_planning do issues = create_list(:issue, 3, project: project, closed_by: user) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api("/projects/#{project.id}/issues", user) - end.count + end milestone = create(:milestone, project: project) create(:issue, project: project, milestone: milestone, closed_by: create(:user)) @@ -245,7 +245,7 @@ RSpec.describe API::Issues, feature_category: :team_planning do expect do get api("/projects/#{project.id}/issues", user) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end it 'returns 404 when project does not exist' do @@ -361,9 +361,9 @@ RSpec.describe API::Issues, feature_category: :team_planning do let(:label_c) { create(:label, title: 'bar', project: project) } it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api("/projects/#{project.id}/issues?with_labels_details=true", user) - end.count + end new_issue = create(:issue, project: project) create(:label_link, label: label, target: new_issue) @@ -372,7 +372,7 @@ RSpec.describe API::Issues, feature_category: :team_planning do expect do get api("/projects/#{project.id}/issues?with_labels_details=true", user) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb index 6000fa29dc4..6ba51080bf0 100644 --- a/spec/requests/api/merge_requests_spec.rb +++ b/spec/requests/api/merge_requests_spec.rb @@ -193,7 +193,7 @@ RSpec.describe API::MergeRequests, :aggregate_failures, feature_category: :sourc control = ActiveRecord::QueryRecorder.new do get api(path, user) - end.count + end mr = create(:merge_request) create(:label_link, label: label, target: mr) @@ -1232,7 +1232,7 @@ RSpec.describe API::MergeRequests, :aggregate_failures, feature_category: :sourc it 'avoids N+1 queries', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/330335' do control = ActiveRecord::QueryRecorder.new do get api("/projects/#{project.id}/merge_requests", user) - end.count + end create(:merge_request, author: user, assignees: [user], source_project: project, target_project: project, created_at: base_time) diff --git a/spec/requests/api/project_import_spec.rb b/spec/requests/api/project_import_spec.rb index 49471b98eba..a73f3366dcb 100644 --- a/spec/requests/api/project_import_spec.rb +++ b/spec/requests/api/project_import_spec.rb @@ -62,9 +62,9 @@ RSpec.describe API::ProjectImport, :aggregate_failures, feature_category: :impor it_behaves_like 'requires import source to be enabled' it 'executes a limited number of queries', :use_clean_rails_redis_caching do - control_count = ActiveRecord::QueryRecorder.new { subject }.count + control = ActiveRecord::QueryRecorder.new { subject } - expect(control_count).to be <= 111 + expect(control.count).to be <= 111 end it 'schedules an import using a namespace' do diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index b8e029385e3..cf6152a9b67 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -1152,7 +1152,7 @@ RSpec.describe API::Projects, :aggregate_failures, feature_category: :groups_and expect do request - end.not_to exceed_all_query_limit(control.count) + end.not_to exceed_all_query_limit(control) end end @@ -3799,7 +3799,7 @@ RSpec.describe API::Projects, :aggregate_failures, feature_category: :groups_and expect do post api("/projects/#{project.id}/import_project_members/#{measure_project.id}", user) - end.not_to exceed_all_query_limit(control.count).with_threshold(unresolved_n_plus_ones) + end.not_to exceed_all_query_limit(control).with_threshold(unresolved_n_plus_ones) end it 'returns 200 when it successfully imports members from another project' do diff --git a/spec/requests/api/releases_spec.rb b/spec/requests/api/releases_spec.rb index 493dc4e72c6..0c811a21fb0 100644 --- a/spec/requests/api/releases_spec.rb +++ b/spec/requests/api/releases_spec.rb @@ -156,9 +156,9 @@ RSpec.describe API::Releases, :aggregate_failures, feature_category: :release_or create(:release, :with_evidence, project: project, tag: 'v0.1', author: maintainer) create(:release_link, release: project.releases.first) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api("/projects/#{project.id}/releases", maintainer) - end.count + end create_list(:release, 2, :with_evidence, project: project, author: maintainer) create_list(:release, 2, project: project) @@ -167,7 +167,7 @@ RSpec.describe API::Releases, :aggregate_failures, feature_category: :release_or expect do get api("/projects/#{project.id}/releases", maintainer) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end it 'serializes releases for the first time and read cached data from the second time' do @@ -1715,9 +1715,9 @@ RSpec.describe API::Releases, :aggregate_failures, feature_category: :release_or subject expect(response).to have_gitlab_http_status(:ok) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do subject - end.count + end subgroups = create_list(:group, 10, parent: group1) projects = create_list(:project, 10, namespace: subgroups[0]) @@ -1725,7 +1725,7 @@ RSpec.describe API::Releases, :aggregate_failures, feature_category: :release_or expect do subject - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end end diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 86c4e04ef71..de3460208b7 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -265,9 +265,9 @@ RSpec.describe API::Users, :aggregate_failures, feature_category: :user_profile end it 'avoids N+1 queries when requested by admin' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api(path, admin) - end.count + end create_list(:user, 3) @@ -277,19 +277,19 @@ RSpec.describe API::Users, :aggregate_failures, feature_category: :user_profile expect do get api(path, admin) - end.not_to exceed_all_query_limit(control_count + 3) + end.not_to exceed_all_query_limit(control).with_threshold(3) end it 'avoids N+1 queries when requested by a regular user' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api(path, user) - end.count + end create_list(:user, 3) expect do get api(path, user) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end @@ -2272,16 +2272,16 @@ RSpec.describe API::Users, :aggregate_failures, feature_category: :user_profile it 'avoids N+1 queries' do second_project.add_maintainer(user) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do get api(path, user) - end.count + end deploy_key = create(:deploy_key, user: second_user) create(:deploy_keys_project, project: second_project, deploy_key_id: deploy_key.id) expect do get api(path, user) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end end @@ -2328,15 +2328,15 @@ RSpec.describe API::Users, :aggregate_failures, feature_category: :user_profile end it 'avoids N+1 queries', :request_store do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do request - end.count + end create_list(:key, 2, user: user) expect do request - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end end @@ -3044,15 +3044,15 @@ RSpec.describe API::Users, :aggregate_failures, feature_category: :user_profile end it 'avoids N+1 queries', :request_store do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do request - end.count + end create_list(:key, 2, user: user) expect do request - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end diff --git a/spec/requests/groups/milestones_controller_spec.rb b/spec/requests/groups/milestones_controller_spec.rb index 54a25333c02..ed24ad6489f 100644 --- a/spec/requests/groups/milestones_controller_spec.rb +++ b/spec/requests/groups/milestones_controller_spec.rb @@ -18,14 +18,16 @@ RSpec.describe Groups::MilestonesController, feature_category: :team_planning do public_project = create(:project, :public, :merge_requests_enabled, :issues_enabled, group: public_group) create(:milestone, project: public_project) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { get group_milestones_path(public_group, format: :json) }.count + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do + get group_milestones_path(public_group, format: :json) + end projects = create_list(:project, 2, :public, :merge_requests_enabled, :issues_enabled, group: public_group) projects.each do |project| create(:milestone, project: project) end - expect { get group_milestones_path(public_group, format: :json) }.not_to exceed_all_query_limit(control_count) + expect { get group_milestones_path(public_group, format: :json) }.not_to exceed_all_query_limit(control) expect(response).to have_gitlab_http_status(:ok) milestones = json_response @@ -66,11 +68,11 @@ RSpec.describe Groups::MilestonesController, feature_category: :team_planning do it 'avoids N+1 database queries' do perform_request # warm up the cache - control_count = ActiveRecord::QueryRecorder.new { perform_request }.count + control = ActiveRecord::QueryRecorder.new { perform_request } create(:merge_request, milestone: milestone, source_project: project, source_branch: 'fix') - expect { perform_request }.not_to exceed_query_limit(control_count) + expect { perform_request }.not_to exceed_query_limit(control) end end end diff --git a/spec/requests/groups/registry/repositories_controller_spec.rb b/spec/requests/groups/registry/repositories_controller_spec.rb index f54acf118bb..e4818676f81 100644 --- a/spec/requests/groups/registry/repositories_controller_spec.rb +++ b/spec/requests/groups/registry/repositories_controller_spec.rb @@ -20,13 +20,13 @@ RSpec.describe Groups::Registry::RepositoriesController, feature_category: :cont create(:container_repository, project: project) endpoint = group_container_registries_path(group, format: :json) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { get(endpoint) }.count + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { get(endpoint) } create_list(:project, 2, group: group).each do |project| create_list(:container_repository, 2, project: project) end - expect { get(endpoint) }.not_to exceed_all_query_limit(control_count) + expect { get(endpoint) }.not_to exceed_all_query_limit(control) # sanity check that response is 200 expect(response).to have_gitlab_http_status(:ok) diff --git a/spec/requests/projects/pipelines_controller_spec.rb b/spec/requests/projects/pipelines_controller_spec.rb index aa3fefdef14..8be4fecea04 100644 --- a/spec/requests/projects/pipelines_controller_spec.rb +++ b/spec/requests/projects/pipelines_controller_spec.rb @@ -25,14 +25,14 @@ RSpec.describe Projects::PipelinesController, feature_category: :continuous_inte create_pipelines - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get_pipelines_index - end.count + end create_pipelines # There appears to be one extra query for Pipelines#has_warnings? for some reason - expect { get_pipelines_index }.not_to exceed_all_query_limit(control_count + 1) + expect { get_pipelines_index }.not_to exceed_all_query_limit(control).with_threshold(1) expect(response).to have_gitlab_http_status(:ok) expect(json_response['pipelines'].count).to eq(11) end @@ -56,9 +56,9 @@ RSpec.describe Projects::PipelinesController, feature_category: :continuous_inte it 'does not execute N+1 queries' do request_build_stage - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do request_build_stage - end.count + end create(:ci_build, pipeline: pipeline, stage: 'build') @@ -70,7 +70,7 @@ RSpec.describe Projects::PipelinesController, feature_category: :continuous_inte status: :failed) end - expect { request_build_stage }.not_to exceed_all_query_limit(control_count) + expect { request_build_stage }.not_to exceed_all_query_limit(control) expect(response).to have_gitlab_http_status(:ok) end @@ -134,14 +134,14 @@ RSpec.describe Projects::PipelinesController, feature_category: :continuous_inte request_build_stage(retried: true) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do request_build_stage(retried: true) - end.count + end create(:ci_build, :retried, :failed, pipeline: pipeline, stage: 'build') create(:ci_build, :failed, pipeline: pipeline, stage: 'build') - expect { request_build_stage(retried: true) }.not_to exceed_all_query_limit(control_count) + expect { request_build_stage(retried: true) }.not_to exceed_all_query_limit(control) expect(response).to have_gitlab_http_status(:ok) end diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rb index 365b20ad4aa..eaf11653256 100644 --- a/spec/requests/search_controller_spec.rb +++ b/spec/requests/search_controller_spec.rb @@ -112,7 +112,7 @@ RSpec.describe SearchController, type: :request, feature_category: :global_searc control = ActiveRecord::QueryRecorder.new { send_search_request(params_for_one) } expect(response.body).to include('search-results') # Confirm search results to prevent false positives - expect { send_search_request(params_for_many) }.not_to exceed_query_limit(control.count) + expect { send_search_request(params_for_many) }.not_to exceed_query_limit(control) expect(response.body).to include('search-results') # Confirm search results to prevent false positives end end @@ -125,7 +125,7 @@ RSpec.describe SearchController, type: :request, feature_category: :global_searc control = ActiveRecord::QueryRecorder.new { send_search_request(params_for_one) } expect(response.body).to include('search-results') # Confirm search results to prevent false positives - expect { send_search_request(params_for_many) }.not_to exceed_query_limit(control.count) + expect { send_search_request(params_for_many) }.not_to exceed_query_limit(control) expect(response.body).to include('search-results') # Confirm search results to prevent false positives end end diff --git a/spec/services/ci/abort_pipelines_service_spec.rb b/spec/services/ci/abort_pipelines_service_spec.rb index 60f3ee11442..af6a70989c9 100644 --- a/spec/services/ci/abort_pipelines_service_spec.rb +++ b/spec/services/ci/abort_pipelines_service_spec.rb @@ -70,12 +70,12 @@ RSpec.describe Ci::AbortPipelinesService, feature_category: :continuous_integrat end it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new { abort_project_pipelines }.count + control = ActiveRecord::QueryRecorder.new { abort_project_pipelines } pipelines = create_list(:ci_pipeline, 5, :running, project: project) create_list(:ci_build, 5, :running, pipeline: pipelines.first) - expect { abort_project_pipelines }.not_to exceed_query_limit(control_count) + expect { abort_project_pipelines }.not_to exceed_query_limit(control) end context 'with live build logs' do diff --git a/spec/services/ci/expire_pipeline_cache_service_spec.rb b/spec/services/ci/expire_pipeline_cache_service_spec.rb index 3d0ce456aa5..a74b820de09 100644 --- a/spec/services/ci/expire_pipeline_cache_service_spec.rb +++ b/spec/services/ci/expire_pipeline_cache_service_spec.rb @@ -106,7 +106,7 @@ RSpec.describe Ci::ExpirePipelineCacheService, feature_category: :continuous_int create(:ci_sources_pipeline, pipeline: pipeline) create(:ci_sources_pipeline, source_job: create(:ci_build, pipeline: pipeline)) - expect { subject.execute(pipeline) }.not_to exceed_query_limit(control.count) + expect { subject.execute(pipeline) }.not_to exceed_query_limit(control) end end diff --git a/spec/services/ci/job_artifacts/destroy_all_expired_service_spec.rb b/spec/services/ci/job_artifacts/destroy_all_expired_service_spec.rb index c060c72ffb2..bdb4ed182dc 100644 --- a/spec/services/ci/job_artifacts/destroy_all_expired_service_spec.rb +++ b/spec/services/ci/job_artifacts/destroy_all_expired_service_spec.rb @@ -44,7 +44,7 @@ RSpec.describe Ci::JobArtifacts::DestroyAllExpiredService, :clean_gitlab_redis_s more_artifacts - expect { subject }.not_to exceed_query_limit(control.count) + expect { subject }.not_to exceed_query_limit(control) end end diff --git a/spec/services/ci/retry_job_service_spec.rb b/spec/services/ci/retry_job_service_spec.rb index 1646afde21d..1708f475e6b 100644 --- a/spec/services/ci/retry_job_service_spec.rb +++ b/spec/services/ci/retry_job_service_spec.rb @@ -403,11 +403,11 @@ RSpec.describe Ci::RetryJobService, feature_category: :continuous_integration do end it 'does not cause an N+1 when updating the job ownership' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { service.execute(job) }.count + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { service.execute(job) } create_list(:ci_build, 2, :skipped, pipeline: pipeline, ci_stage: deploy_stage) - expect { service.execute(job) }.not_to exceed_all_query_limit(control_count) + expect { service.execute(job) }.not_to exceed_all_query_limit(control) end end diff --git a/spec/services/ci/update_build_queue_service_spec.rb b/spec/services/ci/update_build_queue_service_spec.rb index 4fd4492278d..c5959127f34 100644 --- a/spec/services/ci/update_build_queue_service_spec.rb +++ b/spec/services/ci/update_build_queue_service_spec.rb @@ -331,11 +331,11 @@ RSpec.describe Ci::UpdateBuildQueueService, feature_category: :continuous_integr let!(:project_runner) { create(:ci_runner, :project, :online, projects: [project], tag_list: %w[a b c]) } it 'does execute the same amount of queries regardless of number of runners' do - control_count = ActiveRecord::QueryRecorder.new { subject.tick(build) }.count + control = ActiveRecord::QueryRecorder.new { subject.tick(build) } create_list(:ci_runner, 10, :project, :online, projects: [project], tag_list: %w[b c d]) - expect { subject.tick(build) }.not_to exceed_all_query_limit(control_count) + expect { subject.tick(build) }.not_to exceed_all_query_limit(control) end end end diff --git a/spec/services/issue_links/list_service_spec.rb b/spec/services/issue_links/list_service_spec.rb index b5cc8c4dcdc..f9e5e88aff0 100644 --- a/spec/services/issue_links/list_service_spec.rb +++ b/spec/services/issue_links/list_service_spec.rb @@ -33,7 +33,7 @@ RSpec.describe IssueLinks::ListService, feature_category: :team_planning do end it 'ensures no N+1 queries are made' do - control_count = ActiveRecord::QueryRecorder.new { subject }.count + control = ActiveRecord::QueryRecorder.new { subject } project = create :project, :public milestone = create :milestone, project: project @@ -44,7 +44,7 @@ RSpec.describe IssueLinks::ListService, feature_category: :team_planning do create :issue_link, source: issue_x, target: issue_z create :issue_link, source: issue_y, target: issue_z - expect { subject }.not_to exceed_query_limit(control_count) + expect { subject }.not_to exceed_query_limit(control) end it 'returns related issues JSON' do diff --git a/spec/services/issues/export_csv_service_spec.rb b/spec/services/issues/export_csv_service_spec.rb index 83dfca923fb..016174f9888 100644 --- a/spec/services/issues/export_csv_service_spec.rb +++ b/spec/services/issues/export_csv_service_spec.rb @@ -175,11 +175,11 @@ RSpec.describe Issues::ExportCsvService, :with_license, feature_category: :team_ let(:labeled_issues) { create_list(:labeled_issue, 2, project: project, author: user, labels: [feature_label, idea_label]) } it 'does not run a query for each label link' do - control_count = ActiveRecord::QueryRecorder.new { csv }.count + control = ActiveRecord::QueryRecorder.new { csv } labeled_issues - expect { csv }.not_to exceed_query_limit(control_count) + expect { csv }.not_to exceed_query_limit(control) expect(csv.count).to eq(4) end diff --git a/spec/services/issues/referenced_merge_requests_service_spec.rb b/spec/services/issues/referenced_merge_requests_service_spec.rb index 4781daf7688..6748292d389 100644 --- a/spec/services/issues/referenced_merge_requests_service_spec.rb +++ b/spec/services/issues/referenced_merge_requests_service_spec.rb @@ -39,13 +39,13 @@ RSpec.describe Issues::ReferencedMergeRequestsService, feature_category: :team_p context 'performance' do it 'does not run extra queries when extra namespaces are included', :use_clean_rails_memory_store_caching do service.execute(issue) # warm cache - control_count = ActiveRecord::QueryRecorder.new { service.execute(issue) }.count + control = ActiveRecord::QueryRecorder.new { service.execute(issue) } third_project = create(:project, :public) create_closing_mr(source_project: third_project) service.execute(issue) # warm cache - expect { service.execute(issue) }.not_to exceed_query_limit(control_count) + expect { service.execute(issue) }.not_to exceed_query_limit(control) end it 'preloads the head pipeline for each merge request, and its routes' do @@ -58,12 +58,12 @@ RSpec.describe Issues::ReferencedMergeRequestsService, feature_category: :team_p end closing_mr_other_project.update!(head_pipeline: create(:ci_pipeline)) - control_count = ActiveRecord::QueryRecorder.new { service.execute(reloaded_issue).each(&pipeline_routes) } + control = ActiveRecord::QueryRecorder.new { service.execute(reloaded_issue).each(&pipeline_routes) } closing_mr.update!(head_pipeline: create(:ci_pipeline)) expect { service.execute(issue).each(&pipeline_routes) } - .not_to exceed_query_limit(control_count) + .not_to exceed_query_limit(control) end it 'only loads issue notes once' do @@ -95,12 +95,12 @@ RSpec.describe Issues::ReferencedMergeRequestsService, feature_category: :team_p context 'performance' do it 'does not run a query for each note author', :use_clean_rails_memory_store_caching do service.referenced_merge_requests(issue) # warm cache - control_count = ActiveRecord::QueryRecorder.new { service.referenced_merge_requests(issue) }.count + control = ActiveRecord::QueryRecorder.new { service.referenced_merge_requests(issue) } create(:note, project: project, noteable: issue, author: create(:user)) service.referenced_merge_requests(issue) # warm cache - expect { service.referenced_merge_requests(issue) }.not_to exceed_query_limit(control_count) + expect { service.referenced_merge_requests(issue) }.not_to exceed_query_limit(control) end end end @@ -121,12 +121,12 @@ RSpec.describe Issues::ReferencedMergeRequestsService, feature_category: :team_p context 'performance' do it 'does not run a query for each note author', :use_clean_rails_memory_store_caching do service.closed_by_merge_requests(issue) # warm cache - control_count = ActiveRecord::QueryRecorder.new { service.closed_by_merge_requests(issue) }.count + control = ActiveRecord::QueryRecorder.new { service.closed_by_merge_requests(issue) } create(:note, :system, project: project, noteable: issue, author: create(:user)) service.closed_by_merge_requests(issue) # warm cache - expect { service.closed_by_merge_requests(issue) }.not_to exceed_query_limit(control_count) + expect { service.closed_by_merge_requests(issue) }.not_to exceed_query_limit(control) end end end diff --git a/spec/services/labels/available_labels_service_spec.rb b/spec/services/labels/available_labels_service_spec.rb index 2b398210034..3a1474e4fef 100644 --- a/spec/services/labels/available_labels_service_spec.rb +++ b/spec/services/labels/available_labels_service_spec.rb @@ -42,11 +42,15 @@ RSpec.describe Labels::AvailableLabelsService, feature_category: :team_planning it 'do not cause additional query for finding labels' do label_titles = [project_label.title] - control_count = ActiveRecord::QueryRecorder.new { described_class.new(user, project, labels: label_titles).find_or_create_by_titles } + control = ActiveRecord::QueryRecorder.new do + described_class.new(user, project, labels: label_titles).find_or_create_by_titles + end new_label = create(:label, project: project) label_titles = [project_label.title, new_label.title] - expect { described_class.new(user, project, labels: label_titles).find_or_create_by_titles }.not_to exceed_query_limit(control_count) + expect do + described_class.new(user, project, labels: label_titles).find_or_create_by_titles + end.not_to exceed_query_limit(control) end end end diff --git a/spec/services/merge_requests/pushed_branches_service_spec.rb b/spec/services/merge_requests/pushed_branches_service_spec.rb index de99fb244d3..bcde2fd5165 100644 --- a/spec/services/merge_requests/pushed_branches_service_spec.rb +++ b/spec/services/merge_requests/pushed_branches_service_spec.rb @@ -37,11 +37,11 @@ RSpec.describe MergeRequests::PushedBranchesService, feature_category: :source_c end it 'returns empty result without any SQL query performed' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do expect(service.execute).to be_empty - end.count + end - expect(control_count).to be_zero + expect(control.count).to be_zero end end end diff --git a/spec/services/merge_requests/reload_diffs_service_spec.rb b/spec/services/merge_requests/reload_diffs_service_spec.rb index 77056cbe541..a6654989374 100644 --- a/spec/services/merge_requests/reload_diffs_service_spec.rb +++ b/spec/services/merge_requests/reload_diffs_service_spec.rb @@ -45,11 +45,11 @@ RSpec.describe MergeRequests::ReloadDiffsService, :use_clean_rails_memory_store_ current_user merge_request - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do subject.execute - end.count + end - expect { subject.execute }.not_to exceed_query_limit(control_count) + expect { subject.execute }.not_to exceed_query_limit(control) end end end diff --git a/spec/services/notification_recipients/build_service_spec.rb b/spec/services/notification_recipients/build_service_spec.rb index bfd1dcd7d80..b4788428f14 100644 --- a/spec/services/notification_recipients/build_service_spec.rb +++ b/spec/services/notification_recipients/build_service_spec.rb @@ -21,13 +21,13 @@ RSpec.describe NotificationRecipients::BuildService, feature_category: :team_pla service.build_new_note_recipients(note) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do service.build_new_note_recipients(note) end create_user - expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count).with_threshold(threshold) + expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control).with_threshold(threshold) end end @@ -76,13 +76,15 @@ RSpec.describe NotificationRecipients::BuildService, feature_category: :team_pla service.build_new_review_recipients(review) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do service.build_new_review_recipients(review) end create_user - expect { service.build_new_review_recipients(review) }.not_to exceed_query_limit(control_count).with_threshold(threshold) + expect do + service.build_new_review_recipients(review) + end.not_to exceed_query_limit(control).with_threshold(threshold) end end @@ -130,13 +132,13 @@ RSpec.describe NotificationRecipients::BuildService, feature_category: :team_pla service.build_requested_review_recipients(note) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do service.build_requested_review_recipients(note) end create_user - expect { service.build_requested_review_recipients(note) }.not_to exceed_query_limit(control_count) + expect { service.build_requested_review_recipients(note) }.not_to exceed_query_limit(control) end end end diff --git a/spec/services/projects/fork_service_spec.rb b/spec/services/projects/fork_service_spec.rb index 30fcdc27a78..39d7e6a84d4 100644 --- a/spec/services/projects/fork_service_spec.rb +++ b/spec/services/projects/fork_service_spec.rb @@ -224,6 +224,7 @@ RSpec.describe Projects::ForkService, feature_category: :source_code_management it "creates fork with lowest level" do forked_project = fork_project(@from_project, @to_user, using_service: true) + expect(forked_project).to be_persisted expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) end end @@ -233,10 +234,11 @@ RSpec.describe Projects::ForkService, feature_category: :source_code_management stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PRIVATE]) end - it "creates fork with private visibility levels" do + it "doesn't create a fork" do forked_project = fork_project(@from_project, @to_user, using_service: true) - expect(forked_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) + expect(forked_project).not_to be_persisted + expect(forked_project.errors[:visibility_level]).to eq ['private has been restricted by your GitLab administrator'] end end end diff --git a/spec/services/projects/participants_service_spec.rb b/spec/services/projects/participants_service_spec.rb index 2d048d5258d..167df7996ca 100644 --- a/spec/services/projects/participants_service_spec.rb +++ b/spec/services/projects/participants_service_spec.rb @@ -43,27 +43,27 @@ RSpec.describe Projects::ParticipantsService, feature_category: :groups_and_proj it 'avoids N+1 UserDetail queries' do project.add_developer(create(:user)) - control_count = ActiveRecord::QueryRecorder.new { run_service.to_a }.count + control = ActiveRecord::QueryRecorder.new { run_service.to_a } BatchLoader::Executor.clear_current project.add_developer(create(:user, status: build(:user_status, availability: :busy))) - expect { run_service.to_a }.not_to exceed_query_limit(control_count) + expect { run_service.to_a }.not_to exceed_query_limit(control) end it 'avoids N+1 groups queries' do group_1 = create(:group) group_1.add_owner(user) - control_count = ActiveRecord::QueryRecorder.new { run_service }.count + control = ActiveRecord::QueryRecorder.new { run_service } BatchLoader::Executor.clear_current group_2 = create(:group) group_2.add_owner(user) - expect { run_service }.not_to exceed_query_limit(control_count) + expect { run_service }.not_to exceed_query_limit(control) end end diff --git a/spec/services/repositories/changelog_service_spec.rb b/spec/services/repositories/changelog_service_spec.rb index 1b5300672e3..d77a68288a5 100644 --- a/spec/services/repositories/changelog_service_spec.rb +++ b/spec/services/repositories/changelog_service_spec.rb @@ -164,7 +164,7 @@ RSpec.describe Repositories::ChangelogService, feature_category: :source_code_ma RequestStore.clear! - expect { request.call(sha3) }.not_to exceed_query_limit(control.count) + expect { request.call(sha3) }.not_to exceed_query_limit(control) end context 'when one of commits does not exist' do diff --git a/spec/services/todo_service_spec.rb b/spec/services/todo_service_spec.rb index 6e246f21e84..df00859fd52 100644 --- a/spec/services/todo_service_spec.rb +++ b/spec/services/todo_service_spec.rb @@ -1276,9 +1276,9 @@ RSpec.describe TodoService, feature_category: :team_planning do # Excluding queries for user permissions because those do execute N+1 queries allow_any_instance_of(User).to receive(:can?).and_return(true) - control_count = ActiveRecord::QueryRecorder.new { service.update_note(note_mentioning_1_user, author, skip_users) }.count + control = ActiveRecord::QueryRecorder.new { service.update_note(note_mentioning_1_user, author, skip_users) } - expect { service.update_note(note_mentioning_3_users, author, skip_users) }.not_to exceed_query_limit(control_count) + expect { service.update_note(note_mentioning_3_users, author, skip_users) }.not_to exceed_query_limit(control) end end diff --git a/spec/services/todos/destroy/destroyed_issuable_service_spec.rb b/spec/services/todos/destroy/destroyed_issuable_service_spec.rb index 63ff189ede5..cccf1a2cfa8 100644 --- a/spec/services/todos/destroy/destroyed_issuable_service_spec.rb +++ b/spec/services/todos/destroy/destroyed_issuable_service_spec.rb @@ -14,7 +14,7 @@ RSpec.describe Todos::Destroy::DestroyedIssuableService, feature_category: :team let_it_be(:done_todo) { create(:todo, :done, project: target.project, target: target, user: user) } it 'deletes todos for specified target ID and type' do - control_count = ActiveRecord::QueryRecorder.new { subject }.count + control = ActiveRecord::QueryRecorder.new { subject } # Create more todos for the target create(:todo, :pending, project: target.project, target: target, user: user) @@ -22,7 +22,7 @@ RSpec.describe Todos::Destroy::DestroyedIssuableService, feature_category: :team create(:todo, :done, project: target.project, target: target, user: user) create(:todo, :done, project: target.project, target: target, user: user) - expect { subject }.not_to exceed_query_limit(control_count) + expect { subject }.not_to exceed_query_limit(control) end it 'invalidates todos cache counts of todo users', :use_clean_rails_redis_caching do diff --git a/spec/services/user_project_access_changed_service_spec.rb b/spec/services/user_project_access_changed_service_spec.rb index a50bd3ee2f1..8236d892072 100644 --- a/spec/services/user_project_access_changed_service_spec.rb +++ b/spec/services/user_project_access_changed_service_spec.rb @@ -77,7 +77,7 @@ RSpec.describe UserProjectAccessChangedService, feature_category: :system_access it 'avoids N+1 cached queries', :use_sql_query_cache, :request_store do # Run this once to establish a baseline - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do service.execute end @@ -87,7 +87,7 @@ RSpec.describe UserProjectAccessChangedService, feature_category: :system_access .with([[1], [2], [3], [4], [5]]) .and_return(10) - expect { service.execute }.not_to exceed_all_query_limit(control_count.count) + expect { service.execute }.not_to exceed_all_query_limit(control) end end end diff --git a/spec/services/users/update_todo_count_cache_service_spec.rb b/spec/services/users/update_todo_count_cache_service_spec.rb index eec637cf5b4..d69a4ba99b7 100644 --- a/spec/services/users/update_todo_count_cache_service_spec.rb +++ b/spec/services/users/update_todo_count_cache_service_spec.rb @@ -44,9 +44,9 @@ RSpec.describe Users::UpdateTodoCountCacheService, feature_category: :team_plann end it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new { execute_single }.count + control = ActiveRecord::QueryRecorder.new { execute_single } - expect { execute_all }.not_to exceed_query_limit(control_count) + expect { execute_all }.not_to exceed_query_limit(control) end it 'executes one query per batch of users' do diff --git a/spec/support/rspec_order_todo.yml b/spec/support/rspec_order_todo.yml index 0715e56c130..cb6cffd064f 100644 --- a/spec/support/rspec_order_todo.yml +++ b/spec/support/rspec_order_todo.yml @@ -7116,7 +7116,6 @@ - './spec/models/concerns/counter_attribute_spec.rb' - './spec/models/concerns/cron_schedulable_spec.rb' - './spec/models/concerns/cross_database_modification_spec.rb' -- './spec/models/concerns/database_event_tracking_spec.rb' - './spec/models/concerns/database_reflection_spec.rb' - './spec/models/concerns/delete_with_limit_spec.rb' - './spec/models/concerns/deployment_platform_spec.rb' diff --git a/spec/support/shared_examples/controllers/githubish_import_controller_shared_examples.rb b/spec/support/shared_examples/controllers/githubish_import_controller_shared_examples.rb index c921da10347..94208e29d77 100644 --- a/spec/support/shared_examples/controllers/githubish_import_controller_shared_examples.rb +++ b/spec/support/shared_examples/controllers/githubish_import_controller_shared_examples.rb @@ -125,9 +125,9 @@ RSpec.shared_examples 'a GitHub-ish import controller: GET status' do group_a.add_owner(user) create(:project, :import_started, import_type: provider, namespace: user.namespace) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get :status, format: :json - end.count + end stub_client(repos: [repo, org_repo], orgs: []) group_b = create(:group) @@ -135,7 +135,7 @@ RSpec.shared_examples 'a GitHub-ish import controller: GET status' do create(:project, :import_started, import_type: provider, namespace: user.namespace) expect { get :status, format: :json } - .not_to exceed_all_query_limit(control_count) + .not_to exceed_all_query_limit(control) end context 'when user is not allowed to import projects' do diff --git a/spec/support/shared_examples/controllers/issuables_list_metadata_shared_examples.rb b/spec/support/shared_examples/controllers/issuables_list_metadata_shared_examples.rb index 446bc4cd92f..461dcf2fcb6 100644 --- a/spec/support/shared_examples/controllers/issuables_list_metadata_shared_examples.rb +++ b/spec/support/shared_examples/controllers/issuables_list_metadata_shared_examples.rb @@ -65,7 +65,7 @@ RSpec.shared_examples 'issuables list meta-data' do |issuable_type, action = nil issuable.update!(source_project: fork_project(project)) end - expect { get_action(action, project) }.not_to exceed_query_limit(control.count) + expect { get_action(action, project) }.not_to exceed_query_limit(control) end describe "when given empty collection" do diff --git a/spec/support/shared_examples/controllers/snippet_shared_examples.rb b/spec/support/shared_examples/controllers/snippet_shared_examples.rb index f49cc979368..bf8183525a9 100644 --- a/spec/support/shared_examples/controllers/snippet_shared_examples.rb +++ b/spec/support/shared_examples/controllers/snippet_shared_examples.rb @@ -17,12 +17,12 @@ RSpec.shared_examples 'snippets views' do project = create(:project, namespace: user.namespace) create(:project_snippet, project: project, author: user) - control_count = ActiveRecord::QueryRecorder.new { get(:index, params: params) }.count + control = ActiveRecord::QueryRecorder.new { get(:index, params: params) } project = create(:project, namespace: user.namespace) create(:project_snippet, project: project, author: user) - expect { get(:index, params: params) }.not_to exceed_query_limit(control_count) + expect { get(:index, params: params) }.not_to exceed_query_limit(control) end end end diff --git a/spec/support/shared_examples/models/database_event_tracking_shared_examples.rb b/spec/support/shared_examples/models/database_event_tracking_shared_examples.rb deleted file mode 100644 index 56b36b3ea07..00000000000 --- a/spec/support/shared_examples/models/database_event_tracking_shared_examples.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -RSpec.shared_examples 'database events tracking' do - describe 'events tracking' do - # required definitions: - # :record, :update_params - # - # other available attributes: - # :project, :namespace - - let(:user) { nil } - let(:category) { described_class.to_s } - let(:label) { described_class.table_name } - let(:action) { "database_event_#{property}" } - let(:record_tracked_attributes) { record.attributes.slice(*described_class::SNOWPLOW_ATTRIBUTES.map(&:to_s)) } - let(:base_extra) { record_tracked_attributes.merge(project: try(:project), namespace: try(:namespace)) } - - before do - allow(Gitlab::Tracking).to receive(:database_event).and_call_original - end - - describe '#create' do - it_behaves_like 'Snowplow event tracking', overrides: { tracking_method: :database_event } do - subject(:create_record) { record } - - let(:extra) { base_extra } - let(:property) { 'create' } - end - end - - describe '#update', :freeze_time do - it_behaves_like 'Snowplow event tracking', overrides: { tracking_method: :database_event } do - subject(:update_record) { record.update!(update_params) } - - let(:extra) { base_extra.merge(update_params.stringify_keys) } - let(:property) { 'update' } - end - end - - describe '#destroy' do - it_behaves_like 'Snowplow event tracking', overrides: { tracking_method: :database_event } do - subject(:delete_record) { record.destroy! } - - let(:extra) { base_extra } - let(:property) { 'destroy' } - end - end - end -end diff --git a/spec/support/shared_examples/models/relative_positioning_shared_examples.rb b/spec/support/shared_examples/models/relative_positioning_shared_examples.rb index 2b46c8c8fb9..692320d45d5 100644 --- a/spec/support/shared_examples/models/relative_positioning_shared_examples.rb +++ b/spec/support/shared_examples/models/relative_positioning_shared_examples.rb @@ -175,15 +175,15 @@ RSpec.shared_examples 'a class that supports relative positioning' do create_items_with_positions(10..12) a, b, c, d, e, f, *xs = create_items_with_positions([nil] * 10) - baseline = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do described_class.move_nulls_to_end([a, b]) end expect { described_class.move_nulls_to_end([c, d, e, f]) } - .not_to exceed_query_limit(baseline) + .not_to exceed_query_limit(control) expect { described_class.move_nulls_to_end(xs) } - .not_to exceed_query_limit(baseline.count) + .not_to exceed_query_limit(control) end end diff --git a/spec/support/shared_examples/serializers/environment_serializer_shared_examples.rb b/spec/support/shared_examples/serializers/environment_serializer_shared_examples.rb index b7247f1f243..2976018b60f 100644 --- a/spec/support/shared_examples/serializers/environment_serializer_shared_examples.rb +++ b/spec/support/shared_examples/serializers/environment_serializer_shared_examples.rb @@ -12,7 +12,7 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do # See also: https://gitlab.com/gitlab-org/gitlab/-/issues/373151 relax_count = 4 - expect { serialize(grouping: true) }.not_to exceed_query_limit(control.count + relax_count) + expect { serialize(grouping: true) }.not_to exceed_query_limit(control).with_threshold(relax_count) end it 'avoids N+1 database queries without grouping', :request_store do @@ -27,7 +27,7 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do # See also: https://gitlab.com/gitlab-org/gitlab/-/issues/373151 relax_count = 5 - expect { serialize(grouping: false) }.not_to exceed_query_limit(control.count + relax_count) + expect { serialize(grouping: false) }.not_to exceed_query_limit(control).with_threshold(relax_count) end it 'does not preload for environments that does not exist in the page', :request_store do diff --git a/spec/support/shared_examples/services/count_service_shared_examples.rb b/spec/support/shared_examples/services/count_service_shared_examples.rb index 54c6ff79976..42fe170d2c4 100644 --- a/spec/support/shared_examples/services/count_service_shared_examples.rb +++ b/spec/support/shared_examples/services/count_service_shared_examples.rb @@ -10,10 +10,10 @@ RSpec.shared_examples 'a counter caching service' do describe '#count' do it 'caches the count', :request_store do subject.delete_cache - control_count = ActiveRecord::QueryRecorder.new { subject.count }.count + control = ActiveRecord::QueryRecorder.new { subject.count } subject.delete_cache - expect { 2.times { subject.count } }.not_to exceed_query_limit(control_count) + expect { 2.times { subject.count } }.not_to exceed_query_limit(control) end end diff --git a/spec/support/shared_examples/services/destroy_label_links_shared_examples.rb b/spec/support/shared_examples/services/destroy_label_links_shared_examples.rb index d2b52468c25..459c957091c 100644 --- a/spec/support/shared_examples/services/destroy_label_links_shared_examples.rb +++ b/spec/support/shared_examples/services/destroy_label_links_shared_examples.rb @@ -8,13 +8,13 @@ RSpec.shared_examples_for 'service deleting label links of an issuable' do end it 'deletes label links for specified target ID and type' do - control_count = ActiveRecord::QueryRecorder.new { execute }.count + control = ActiveRecord::QueryRecorder.new { execute } # Create more label links for the target create(:label_link, target: target) create(:label_link, target: target) - expect { execute }.not_to exceed_query_limit(control_count) + expect { execute }.not_to exceed_query_limit(control) expect(target.reload.label_links.count).to eq(0) end end diff --git a/spec/support/shared_examples/services/packages/debian/generate_distribution_shared_examples.rb b/spec/support/shared_examples/services/packages/debian/generate_distribution_shared_examples.rb index cb544f42765..97dd2aa96d4 100644 --- a/spec/support/shared_examples/services/packages/debian/generate_distribution_shared_examples.rb +++ b/spec/support/shared_examples/services/packages/debian/generate_distribution_shared_examples.rb @@ -244,10 +244,10 @@ RSpec.shared_examples 'Generate Debian Distribution and component files' do end create_list(:debian_package, 10, project: project, published_in: project_distribution) - control_count = ActiveRecord::QueryRecorder.new { subject2 }.count + control = ActiveRecord::QueryRecorder.new { subject2 } create_list(:debian_package, 10, project: project, published_in: project_distribution) - expect { subject3 }.not_to exceed_query_limit(control_count) + expect { subject3 }.not_to exceed_query_limit(control) end end diff --git a/spec/workers/jira_connect/sync_project_worker_spec.rb b/spec/workers/jira_connect/sync_project_worker_spec.rb index b617508bb3a..83bce97cd51 100644 --- a/spec/workers/jira_connect/sync_project_worker_spec.rb +++ b/spec/workers/jira_connect/sync_project_worker_spec.rb @@ -51,11 +51,11 @@ RSpec.describe JiraConnect::SyncProjectWorker, factory_default: :keep, feature_c end it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new { perform(project.id, update_sequence_id) }.count + control = ActiveRecord::QueryRecorder.new { perform(project.id, update_sequence_id) } create(:merge_request, :unique_branches, title: 'TEST-123') - expect { perform(project.id, update_sequence_id) }.not_to exceed_query_limit(control_count) + expect { perform(project.id, update_sequence_id) }.not_to exceed_query_limit(control) end context 'with branches to sync' do