diff --git a/ee/spec/controllers/groups/group_members_controller_spec.rb b/ee/spec/controllers/groups/group_members_controller_spec.rb index 9820b245b5a934f10da40113445513d29301e910..236aef1d779eae895a350d4aa49400d2f7f34291 100644 --- a/ee/spec/controllers/groups/group_members_controller_spec.rb +++ b/ee/spec/controllers/groups/group_members_controller_spec.rb @@ -79,7 +79,7 @@ expect do get :index, params: { group_id: group.reload } - end.not_to exceed_all_query_limit(control.count).with_threshold(multiple_members_threshold + unresolved_n_plus_ones) + end.not_to exceed_all_query_limit(control).with_threshold(multiple_members_threshold + unresolved_n_plus_ones) end it 'avoids extra group_link database queries utilizing pre-loading' do diff --git a/ee/spec/controllers/operations_controller_spec.rb b/ee/spec/controllers/operations_controller_spec.rb index 2de4782764d446a04c0debebe5b085480a3b28c7..7e87f2f15707831a779ec4fe3609d73819e55408 100644 --- a/ee/spec/controllers/operations_controller_spec.rb +++ b/ee/spec/controllers/operations_controller_spec.rb @@ -480,17 +480,15 @@ def get_environments(format, params = nil) end context 'N+1 queries' do - subject { get_environments(:json) } - it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new { subject }.count + control = ActiveRecord::QueryRecorder.new { get_environments(:json) } projects = create_list(:project, 8) do |project| project.add_developer(user) end user.update!(ops_dashboard_projects: projects) - expect { subject }.not_to exceed_query_limit(control_count) + expect { get_environments(:json) }.not_to exceed_query_limit(control) end end end diff --git a/ee/spec/controllers/projects/feature_flag_issues_controller_spec.rb b/ee/spec/controllers/projects/feature_flag_issues_controller_spec.rb index 9e3b5f8c4a99e7a345bb9ea5ae674468981b348a..1afe2aa98a61d58a1c7723a216c724b10733e10a 100644 --- a/ee/spec/controllers/projects/feature_flag_issues_controller_spec.rb +++ b/ee/spec/controllers/projects/feature_flag_issues_controller_spec.rb @@ -137,14 +137,14 @@ def get_request(project, feature_flag) feature_flag, _, _ = setup sign_in(developer) - control_count = ActiveRecord::QueryRecorder.new { get_request(project, feature_flag) }.count + control = ActiveRecord::QueryRecorder.new { get_request(project, feature_flag) } issue_b = create(:issue, project: project) issue_c = create(:issue, project: project) create(:feature_flag_issue, feature_flag: feature_flag, issue: issue_b) create(:feature_flag_issue, feature_flag: feature_flag, issue: issue_c) - expect { get_request(project, feature_flag) }.not_to exceed_query_limit(control_count).with_threshold(4) + expect { get_request(project, feature_flag) }.not_to exceed_query_limit(control).with_threshold(4) end it 'returns only issues readable by the user' do diff --git a/ee/spec/controllers/projects/settings/repository_controller_spec.rb b/ee/spec/controllers/projects/settings/repository_controller_spec.rb index c2c7831677140e21d2339b36c1673b8b455fa523..9c51f9ea7e7e4ab8d35da8484fcff9c9cf364df0 100644 --- a/ee/spec/controllers/projects/settings/repository_controller_spec.rb +++ b/ee/spec/controllers/projects/settings/repository_controller_spec.rb @@ -144,12 +144,12 @@ context 'when the feature group protected branches disabled' do it 'does not perform N+1 sql queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { subject } + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { subject } create_list(:protected_branch, 2, project: project) create_list(:protected_branch, 2, project: nil, group: group) - expect { subject }.not_to exceed_all_query_limit(control_count) + expect { subject }.not_to exceed_all_query_limit(control) end end @@ -161,12 +161,12 @@ end it 'does not perform N+1 sql queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { subject } + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { subject } create_list(:protected_branch, 2, project: project) create_list(:protected_branch, 2, project: nil, group: group) - expect { subject }.not_to exceed_all_query_limit(control_count) + expect { subject }.not_to exceed_all_query_limit(control) end end end diff --git a/ee/spec/elastic/migrate/20230724070100_backfill_epics_spec.rb b/ee/spec/elastic/migrate/20230724070100_backfill_epics_spec.rb index b4441db6af172f322b22e1e66f7732295063edb3..d4e9127d51bc58ea69db94346b29acea73339d8f 100644 --- a/ee/spec/elastic/migrate/20230724070100_backfill_epics_spec.rb +++ b/ee/spec/elastic/migrate/20230724070100_backfill_epics_spec.rb @@ -48,12 +48,12 @@ end it 'does not have N+1' do - control_count = ActiveRecord::QueryRecorder.new { subject.migrate } + control = ActiveRecord::QueryRecorder.new { subject.migrate } create(:epic, group: create(:group, parent: create(:group))) ensure_elasticsearch_index! - expect { subject.migrate }.not_to exceed_query_limit(control_count) + expect { subject.migrate }.not_to exceed_query_limit(control) end context 'with more than one iterations in a batch' do diff --git a/ee/spec/finders/approval_rules/group_finder_spec.rb b/ee/spec/finders/approval_rules/group_finder_spec.rb index 6014e6fa0059d1a21f23e23b7c4474558c2edacf..c9bb1059db617e05bed75bd691b910563b0991b1 100644 --- a/ee/spec/finders/approval_rules/group_finder_spec.rb +++ b/ee/spec/finders/approval_rules/group_finder_spec.rb @@ -62,7 +62,7 @@ it 'avoids N+1 database queries' do rule.reload - count = ActiveRecord::QueryRecorder.new { subject.visible_groups }.count + control = ActiveRecord::QueryRecorder.new { subject.visible_groups } # Clear cached association and request cache rule.reload @@ -70,7 +70,7 @@ rule.groups << create(:group, :private, parent: private_accessible_group, name: 'private_accessible_subgroup2') - expect { described_class.new(rule, user).visible_groups }.not_to exceed_query_limit(count) + expect { described_class.new(rule, user).visible_groups }.not_to exceed_query_limit(control) end end end diff --git a/ee/spec/graphql/resolvers/vulnerabilities_resolver_spec.rb b/ee/spec/graphql/resolvers/vulnerabilities_resolver_spec.rb index 0cf7fa0aa8896e5376ce1b3314b31562d8e9f7f1..fc25c67edffd53e65f5fa69d4be19659680ecba1 100644 --- a/ee/spec/graphql/resolvers/vulnerabilities_resolver_spec.rb +++ b/ee/spec/graphql/resolvers/vulnerabilities_resolver_spec.rb @@ -187,13 +187,13 @@ let(:params) { { project_id: [project.id, project2.id] } } it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do resolve(described_class, obj: vulnerable, args: { project_id: [project2.id] }, ctx: { current_user: current_user }) - end.count + end expect do subject - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end end diff --git a/ee/spec/graphql/types/issue_type_spec.rb b/ee/spec/graphql/types/issue_type_spec.rb index 75f1a2fa588e8c8ab8230c3d76c9131d768749ac..b8921a54b3916e5cd060cb4d6828f258ebb2ad6c 100644 --- a/ee/spec/graphql/types/issue_type_spec.rb +++ b/ee/spec/graphql/types/issue_type_spec.rb @@ -35,7 +35,7 @@ # Warm up table schema and other data (e.g. SAML providers, license) GitlabSchema.execute(query, context: { current_user: user }) - control_count = ActiveRecord::QueryRecorder.new { GitlabSchema.execute(query, context: { current_user: user }) }.count + control = ActiveRecord::QueryRecorder.new { GitlabSchema.execute(query, context: { current_user: user }) } blocked_issue2 = create(:issue, project: project) blocking_issue2 = create(:issue, project: project) @@ -44,7 +44,7 @@ project2 = create(:project, :public, group: group) create(:issue, project: project2) - expect { GitlabSchema.execute(query, context: { current_user: user }) }.not_to exceed_query_limit(control_count) + expect { GitlabSchema.execute(query, context: { current_user: user }) }.not_to exceed_query_limit(control) end end diff --git a/ee/spec/graphql/types/pipeline_security_report_finding_type_spec.rb b/ee/spec/graphql/types/pipeline_security_report_finding_type_spec.rb index 1d25f7735968f2da14336dd9bc11252b066ba344..5efc070b651b588eaa34b41732f841a49379ff5f 100644 --- a/ee/spec/graphql/types/pipeline_security_report_finding_type_spec.rb +++ b/ee/spec/graphql/types/pipeline_security_report_finding_type_spec.rb @@ -172,8 +172,7 @@ # Warm up table schema and other data (e.g. SAML providers, license) GitlabSchema.execute(sast_query, context: { current_user: user }) - control_count = - ActiveRecord::QueryRecorder.new { run_with_clean_state(sast_query, context: { current_user: user }) }.count + control = ActiveRecord::QueryRecorder.new { run_with_clean_state(sast_query, context: { current_user: user }) } vulnerabilities = get_findings_from_response(subject).pluck('vulnerability').compact issues = get_issues_from_vulnerabilities(vulnerabilities) @@ -189,7 +188,7 @@ ) expect { run_with_clean_state(sast_query, context: { current_user: user }) } - .not_to exceed_query_limit(control_count) + .not_to exceed_query_limit(control) response = GitlabSchema.execute(sast_query, context: { current_user: user }) vulnerabilities = get_findings_from_response(response).pluck('vulnerability').compact @@ -257,8 +256,7 @@ # Warm up table schema and other data (e.g. SAML providers, license) GitlabSchema.execute(sast_query, context: { current_user: user }) - control_count = - ActiveRecord::QueryRecorder.new { run_with_clean_state(sast_query, context: { current_user: user }) }.count + control = ActiveRecord::QueryRecorder.new { run_with_clean_state(sast_query, context: { current_user: user }) } findings = get_findings_from_response(subject) issues = get_issues_from_findings(findings) @@ -273,7 +271,7 @@ ) expect { run_with_clean_state(sast_query, context: { current_user: user }) } - .not_to exceed_query_limit(control_count) + .not_to exceed_query_limit(control) response = GitlabSchema.execute(sast_query, context: { current_user: user }) findings = get_findings_from_response(response) diff --git a/ee/spec/helpers/projects/project_members_helper_spec.rb b/ee/spec/helpers/projects/project_members_helper_spec.rb index 76baa490210fa09c228eb35e366ec6960748b067..359ad1eac65438977417b9c10665b3fa105b9f32 100644 --- a/ee/spec/helpers/projects/project_members_helper_spec.rb +++ b/ee/spec/helpers/projects/project_members_helper_spec.rb @@ -22,9 +22,9 @@ end it 'does not execute N+1' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do call_project_members_app_data_json - end.count + end expect(project.members.count).to eq(2) @@ -34,7 +34,7 @@ expect(project.members.count).to eq(3) - expect { call_project_members_app_data_json }.not_to exceed_query_limit(control_count).with_threshold(11) # existing n+1 + expect { call_project_members_app_data_json }.not_to exceed_query_limit(control).with_threshold(11) # existing n+1 end def call_project_members_app_data_json diff --git a/ee/spec/lib/banzai/filter/references/epic_reference_filter_spec.rb b/ee/spec/lib/banzai/filter/references/epic_reference_filter_spec.rb index c5f1a39d08c4e16500d07d3f6a7a1633bfc3bfae..d9f093031b07cf8bb22766444ba994050d4d0b7a 100644 --- a/ee/spec/lib/banzai/filter/references/epic_reference_filter_spec.rb +++ b/ee/spec/lib/banzai/filter/references/epic_reference_filter_spec.rb @@ -305,11 +305,11 @@ def doc(reference = nil) # warm up reference_filter(markdown, context) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do reference_filter(markdown, context) - end.count + end - expect(control_count).to eq 1 + expect(control.count).to eq 1 markdown = "#{epic.to_reference} #{epic.group.full_path}&9999991 #{epic.group.full_path}&9999992 &9999993 #{epic2.to_reference(group)} #{epic2.group.full_path}&9999991 something/cool&12" @@ -321,11 +321,9 @@ def doc(reference = nil) # - 1x2 for epics in each group # Total = 5 # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359 - max_count = control_count + 4 - expect do reference_filter(markdown, context) - end.not_to exceed_all_query_limit(max_count) + end.not_to exceed_all_query_limit(control).with_threshold(4) end end diff --git a/ee/spec/lib/ee/backup/repositories_spec.rb b/ee/spec/lib/ee/backup/repositories_spec.rb index 2621e4573fc5ff7b1469149be35e871995f23d09..4750e3dde8c83b6ed6562a42ac8c91fa31af65ab 100644 --- a/ee/spec/lib/ee/backup/repositories_spec.rb +++ b/ee/spec/lib/ee/backup/repositories_spec.rb @@ -42,15 +42,15 @@ end it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do subject.dump(destination, backup_id) - end.count + end create_list(:group, 2, :wiki_repo) expect do subject.dump(destination, backup_id) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end context 'storages' do diff --git a/ee/spec/lib/ee/gitlab/ci/matching/runner_matcher_spec.rb b/ee/spec/lib/ee/gitlab/ci/matching/runner_matcher_spec.rb index beabc47c6db0b72a8676a383f718b2e11bd00402..452740cdbd87dfe6fa4517d9c39f581bdae8365c 100644 --- a/ee/spec/lib/ee/gitlab/ci/matching/runner_matcher_spec.rb +++ b/ee/spec/lib/ee/gitlab/ci/matching/runner_matcher_spec.rb @@ -93,7 +93,7 @@ it 'does not generate N+1 queries when loading the quota for project' do project.reload - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do runner_matcher.matches_quota?(build.build_matcher) end @@ -110,7 +110,7 @@ runner_matcher.matches_quota?(matcher) end end - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end end diff --git a/ee/spec/lib/elastic/latest/epic_instance_proxy_spec.rb b/ee/spec/lib/elastic/latest/epic_instance_proxy_spec.rb index 59de05a1dae85a70be5e0412573c8e693fd2efd0..381c66edb434d3a10a478a77db2b233f77276d90 100644 --- a/ee/spec/lib/elastic/latest/epic_instance_proxy_spec.rb +++ b/ee/spec/lib/elastic/latest/epic_instance_proxy_spec.rb @@ -48,7 +48,7 @@ expect do epic.__elasticsearch__.as_indexed_json - end.not_to exceed_query_limit(control.count) + end.not_to exceed_query_limit(control) end context 'with start date inherited date from child epic and due date inherited from milestone' do diff --git a/ee/spec/lib/gitlab/ci/parsers/security/dependency_list_spec.rb b/ee/spec/lib/gitlab/ci/parsers/security/dependency_list_spec.rb index 4d9e06f7a76690bab27f7c5e4efa2c7c5965f34e..b9ee527cf62570c392b15d14216b12ed159338e6 100644 --- a/ee/spec/lib/gitlab/ci/parsers/security/dependency_list_spec.rb +++ b/ee/spec/lib/gitlab/ci/parsers/security/dependency_list_spec.rb @@ -49,7 +49,7 @@ let_it_be(:finding_pipeline) { create(:vulnerabilities_finding_pipeline, finding: finding, pipeline: pipeline) } it 'does not causes N+1 query' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do artifact.each_blob do |blob| parser.parse!(blob, report) end @@ -65,7 +65,7 @@ parser.parse!(blob, report) end end - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end it 'merges vulnerability data' do diff --git a/ee/spec/lib/gitlab/elastic/document_reference_spec.rb b/ee/spec/lib/gitlab/elastic/document_reference_spec.rb index b5a17ff68c36ab3b981c9c4603d299a2914b86c6..da8fabc945e064baa8107079872c2305021cff79 100644 --- a/ee/spec/lib/gitlab/elastic/document_reference_spec.rb +++ b/ee/spec/lib/gitlab/elastic/document_reference_spec.rb @@ -261,7 +261,7 @@ # is run for each batch expect do collection.preload_database_records.map(&:database_record) - end.not_to exceed_query_limit(control.count * 2) + end.not_to exceed_query_limit(control).with_threshold(control.count) end end end diff --git a/ee/spec/lib/gitlab/geo/event_gap_tracking_spec.rb b/ee/spec/lib/gitlab/geo/event_gap_tracking_spec.rb index 7d5f5f072a96f781e118130e3c1ee9046178bbf8..ca17fce9bf28e76500e9202a9e71023c995d7b75 100644 --- a/ee/spec/lib/gitlab/geo/event_gap_tracking_spec.rb +++ b/ee/spec/lib/gitlab/geo/event_gap_tracking_spec.rb @@ -119,9 +119,9 @@ end create(:geo_event_log, :updated_event, id: gap_id) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do expect { gap_tracking.fill_gaps(&blk) }.to change { yielded.count }.by(1) - end.count + end travel_to(12.minutes.ago) do gap_tracking.check!(event_id_with_gap + 3) @@ -131,7 +131,7 @@ expect do expect { gap_tracking.fill_gaps(&blk) }.to change { yielded.count }.by(2) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end diff --git a/ee/spec/models/approval_wrapped_rule_spec.rb b/ee/spec/models/approval_wrapped_rule_spec.rb index e93a4d39a816cc10735d9d21d9699622f163e03d..46dfabe0943bc526645b8536ca5b82bb16a1842c 100644 --- a/ee/spec/models/approval_wrapped_rule_spec.rb +++ b/ee/spec/models/approval_wrapped_rule_spec.rb @@ -312,11 +312,11 @@ rule.users = [approver1] approved_approvers_for_rule_id(rule.id) # warm up the cache - control_count = ActiveRecord::QueryRecorder.new { approved_approvers_for_rule_id(rule.id) }.count + control = ActiveRecord::QueryRecorder.new { approved_approvers_for_rule_id(rule.id) } rule.users += [approver2, approver3] - expect { approved_approvers_for_rule_id(rule.id) }.not_to exceed_query_limit(control_count) + expect { approved_approvers_for_rule_id(rule.id) }.not_to exceed_query_limit(control) end def approved_approvers_for_rule_id(rule_id) diff --git a/ee/spec/models/burndown_spec.rb b/ee/spec/models/burndown_spec.rb index e11287b022c7dae6ce2420af03f7ba6dc056e5ec..25a22d595af6c7b56ca0e551398f51960f3eb401 100644 --- a/ee/spec/models/burndown_spec.rb +++ b/ee/spec/models/burndown_spec.rb @@ -179,15 +179,11 @@ travel_to(milestone.due_date) do create(:issue, milestone: milestone, weight: 2, project: project, author: user) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do - subject - end.count + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { subject } create_list(:issue, 3, milestone: milestone, weight: 2, project: project, author: user) - expect do - subject - end.not_to exceed_all_query_limit(control_count) + expect { subject }.not_to exceed_all_query_limit(control) end end end diff --git a/ee/spec/models/ci/pipeline_spec.rb b/ee/spec/models/ci/pipeline_spec.rb index 4a0bb5cd5188a951ac27a337d560b7f63571ee99..f8c38986823bfde133e9f7bfd6cd690478dfffd6 100644 --- a/ee/spec/models/ci/pipeline_spec.rb +++ b/ee/spec/models/ci/pipeline_spec.rb @@ -454,12 +454,12 @@ context 'with failed builds' do it 'does not runs queries on failed builds' do - control_count = ActiveRecord::QueryRecorder.new { subject }.count + control = ActiveRecord::QueryRecorder.new { subject } create(:ee_ci_build, :failed, :dependency_scanning, pipeline: pipeline, project: project) create(:ee_ci_build, :failed, :license_scanning, pipeline: pipeline, project: project) - expect { subject }.not_to exceed_query_limit(control_count) + expect { subject }.not_to exceed_query_limit(control) end end end diff --git a/ee/spec/models/ee/group_spec.rb b/ee/spec/models/ee/group_spec.rb index cf86a63fec43b8bf0be0d2fbda1edb4bdb8ee2fa..5a0018ca35daaac74525f78b80d36c5afdf26bc2 100644 --- a/ee/spec/models/ee/group_spec.rb +++ b/ee/spec/models/ee/group_spec.rb @@ -593,13 +593,13 @@ shared_examples 'group root ancestor' do it 'does not exceed SQL queries count' do groups = described_class.where(id: private_subgroup_1) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do described_class.groups_user_can(groups, user, :read_epic, **params) - end.count + end groups = described_class.where(id: [private_subgroup_1, private_subgroup_2]) expect { described_class.groups_user_can(groups, user, :read_epic, **params) } - .not_to exceed_query_limit(control_count + extra_query_count) + .not_to exceed_query_limit(control).with_threshold(extra_query_count) end end diff --git a/ee/spec/models/ee/project_team_spec.rb b/ee/spec/models/ee/project_team_spec.rb index 1208951bc43bd099a76978f683059bb9ae06cd15..4b44c430555e05d4475b1aa870d0a8aa68770f9d 100644 --- a/ee/spec/models/ee/project_team_spec.rb +++ b/ee/spec/models/ee/project_team_spec.rb @@ -12,11 +12,11 @@ subject(:import) { target_project.team.import(source_project, current_user) } it 'does not cause N+1 queries when checking user types' do - control_count = ActiveRecord::QueryRecorder.new { target_project.team.import(source_project, current_user) } + control = ActiveRecord::QueryRecorder.new { target_project.team.import(source_project, current_user) } create(:user, :security_policy_bot) { |user| source_project.add_guest(user) } - expect { import }.not_to exceed_query_limit(control_count) + expect { import }.not_to exceed_query_limit(control) end context 'when a source project member is a security policy bot' do diff --git a/ee/spec/models/vulnerabilities/projects_grade_spec.rb b/ee/spec/models/vulnerabilities/projects_grade_spec.rb index 5779969d62c7fadf6fc11032e026a6de5d7bef95..e1621ae4bf03000cf14a3baa8b53fcf173484216 100644 --- a/ee/spec/models/vulnerabilities/projects_grade_spec.rb +++ b/ee/spec/models/vulnerabilities/projects_grade_spec.rb @@ -162,9 +162,13 @@ it { is_expected.to match_array(expected_projects) } it 'preloads vulnerability statistic once for whole collection' do - control_count = ActiveRecord::QueryRecorder.new { described_class.new(group, 1, [project_3.id]).projects.map(&:vulnerability_statistic) }.count + control = ActiveRecord::QueryRecorder.new do + described_class.new(group, 1, [project_3.id]).projects.map(&:vulnerability_statistic) + end - expect { described_class.new(group, 1, [project_3.id, project_4.id]).projects.map(&:vulnerability_statistic) }.not_to exceed_query_limit(control_count) + expect do + described_class.new(group, 1, [project_3.id, project_4.id]).projects.map(&:vulnerability_statistic) + end.not_to exceed_query_limit(control) end end @@ -175,9 +179,13 @@ it { is_expected.to match_array(expected_projects) } it 'preloads vulnerability statistic once for whole collection' do - control_count = ActiveRecord::QueryRecorder.new { described_class.new(group, 1, [project_3.id]).projects.map(&:vulnerability_statistic) }.count + control = ActiveRecord::QueryRecorder.new do + described_class.new(group, 1, [project_3.id]).projects.map(&:vulnerability_statistic) + end - expect { described_class.new(group, 1, [project_3.id, project_4.id]).projects.map(&:vulnerability_statistic) }.not_to exceed_query_limit(control_count) + expect do + described_class.new(group, 1, [project_3.id, project_4.id]).projects.map(&:vulnerability_statistic) + end.not_to exceed_query_limit(control) end end end diff --git a/ee/spec/presenters/epic_presenter_spec.rb b/ee/spec/presenters/epic_presenter_spec.rb index 29f7376a918fdacd42ed3e213bd4e064cb37b402..013d94b632304a6e3602d322862025b768650384 100644 --- a/ee/spec/presenters/epic_presenter_spec.rb +++ b/ee/spec/presenters/epic_presenter_spec.rb @@ -55,9 +55,9 @@ epic2 = create(:epic, group: group1, parent: epic1) create(:epic, group: group2, parent: epic2) - control_count = ActiveRecord::QueryRecorder.new { presenter.show_data } + control = ActiveRecord::QueryRecorder.new { presenter.show_data } - expect { presenter.show_data }.not_to exceed_query_limit(control_count) + expect { presenter.show_data }.not_to exceed_query_limit(control) end it 'does not include subscribed in initial data' do diff --git a/ee/spec/requests/admin/credentials_controller_spec.rb b/ee/spec/requests/admin/credentials_controller_spec.rb index 1d76a655d386595dd7fb3e48da345652c178565e..b466892f46d0b0f611468e252a2ab4779eb7cf20 100644 --- a/ee/spec/requests/admin/credentials_controller_spec.rb +++ b/ee/spec/requests/admin/credentials_controller_spec.rb @@ -121,13 +121,13 @@ control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get admin_credentials_path(filter: 'gpg_keys') - end.count + end create(:gpg_key, user: new_user, key: GpgHelpers::User2.public_key) expect do get admin_credentials_path(filter: 'gpg_keys') - end.not_to exceed_query_limit(control + 1) + end.not_to exceed_query_limit(control).with_threshold(1) end end end diff --git a/ee/spec/requests/api/epic_boards_spec.rb b/ee/spec/requests/api/epic_boards_spec.rb index 75b34e0845e9092e7175fba3d708505a35fd51f8..b9ea6a2f8eec4585c3da556bd2e8324a3d226fb3 100644 --- a/ee/spec/requests/api/epic_boards_spec.rb +++ b/ee/spec/requests/api/epic_boards_spec.rb @@ -145,7 +145,7 @@ control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api(url, personal_access_token: pat), params: params - end.count + end [testing, feature, development].each do |label| board = create(:epic_board, group: group, labels: [testing]) @@ -232,7 +232,7 @@ control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api(url, personal_access_token: pat), params: params - end.count + end create_list(:group_label, 3, group: group) do |label| create(:epic_list, epic_board: board1, label: label) diff --git a/ee/spec/requests/api/epic_issues_spec.rb b/ee/spec/requests/api/epic_issues_spec.rb index 6b7bf1292f1c360ed5f4a4d933b1eafa457be6f9..67303a8c8110975da525e4c641dfb70b4884880b 100644 --- a/ee/spec/requests/api/epic_issues_spec.rb +++ b/ee/spec/requests/api/epic_issues_spec.rb @@ -101,13 +101,13 @@ def perform_request(params = {}) 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 } issue = create(:issue, project: project) create(:epic_issue, epic: epic, issue: issue) # Existing N + 1 for calculating subscribed? field: https://gitlab.com/gitlab-org/gitlab/-/issues/325898 - expect { perform_request }.not_to exceed_query_limit(control_count + 2) + expect { perform_request }.not_to exceed_query_limit(control).with_threshold(2) end end end diff --git a/ee/spec/requests/api/epics_spec.rb b/ee/spec/requests/api/epics_spec.rb index 7e46e23415f0313414b49825512605a2633dee31..4b708a107555677e5737316aa73a855b41d74888 100644 --- a/ee/spec/requests/api/epics_spec.rb +++ b/ee/spec/requests/api/epics_spec.rb @@ -93,7 +93,7 @@ control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api(url, personal_access_token: pat), params: params - end.count + end label_2 = create(:label) create_list(:labeled_epic, 2, group: group, labels: [label_2]) @@ -123,7 +123,7 @@ control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api(url, personal_access_token: pat), params: params - end.count + end label_2 = create(:label) create_list(:labeled_epic, 4, group: group, labels: [label_2]) diff --git a/ee/spec/requests/api/graphql/analytics/contribution_analytics/contributions_spec.rb b/ee/spec/requests/api/graphql/analytics/contribution_analytics/contributions_spec.rb index e7af55b52a936fbf28add6349900eee2c743a382..ce346f206a6f72a67164d334570d041fc2fa8931 100644 --- a/ee/spec/requests/api/graphql/analytics/contribution_analytics/contributions_spec.rb +++ b/ee/spec/requests/api/graphql/analytics/contribution_analytics/contributions_spec.rb @@ -67,10 +67,10 @@ def run_query # warm the query to avoid flakiness run_query - control_count = ActiveRecord::QueryRecorder.new { run_query } + control = ActiveRecord::QueryRecorder.new { run_query } create(:event, :pushed, project: project, author: create(:user), created_at: Date.parse('2022-01-05')) - expect { run_query }.not_to exceed_all_query_limit(control_count) + expect { run_query }.not_to exceed_all_query_limit(control) end end end diff --git a/ee/spec/requests/api/graphql/epics/epics_spec.rb b/ee/spec/requests/api/graphql/epics/epics_spec.rb index 42211a00efbb0697be6a4f5e2abac515e62fe813..528ff053ef3ea095fc6f90da21a8d59dc053470c 100644 --- a/ee/spec/requests/api/graphql/epics/epics_spec.rb +++ b/ee/spec/requests/api/graphql/epics/epics_spec.rb @@ -149,16 +149,16 @@ def query_epics_which_start_with_iid(iid) it 'can lookahead to prevent N+1 queries' do create_list(:event, 10, :created, target: epic, group: group) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do query_epics_with_events(1) - end.count + end events = graphql_dig_at(graphql_data, :group, :epics, :nodes, :events, :nodes) expect(events.count).to eq(1) expect do query_epics_with_events(10) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) data = graphql_data(fresh_response_data) events = graphql_dig_at(data, :group, :epics, :nodes, :events, :nodes) diff --git a/ee/spec/requests/api/graphql/group/epic/epic_aggregate_query_spec.rb b/ee/spec/requests/api/graphql/group/epic/epic_aggregate_query_spec.rb index 6ac476305169a18cc5de0bbbbe2e4366cd85bb00..e682105b3e10749b30fd1ea7a35373e099081f70 100644 --- a/ee/spec/requests/api/graphql/group/epic/epic_aggregate_query_spec.rb +++ b/ee/spec/requests/api/graphql/group/epic/epic_aggregate_query_spec.rb @@ -119,7 +119,7 @@ shared_examples 'efficient query' do it 'does not result in N+1' do - control_count = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) }.count + control = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) } query_with_multiple_epics = graphql_query_for('group', { fullPath: epic_with_issues.group.full_path }, query_graphql_field('epics', { iids: [epic_with_issues.iid, epic_without_issues.iid, parent_epic.iid] }, epic_aggregates_query)) @@ -128,7 +128,9 @@ # -> ee/app/policies/epic_policy.rb:4:in `block in <class:EpicPolicy>' # So I'll add n to this number to take this into account. # See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/27551#note_307716428 - expect { post_graphql(query_with_multiple_epics, current_user: current_user) }.not_to exceed_query_limit(control_count + 2) + expect do + post_graphql(query_with_multiple_epics, current_user: current_user) + end.not_to exceed_query_limit(control).with_threshold(2) end end diff --git a/ee/spec/requests/api/graphql/group/epics_spec.rb b/ee/spec/requests/api/graphql/group/epics_spec.rb index ed824ac7e58b03f9c973702b9b63321c3a4aae58..c178a5ef1f7bd4f16dcee69ae002e8a33eb828c4 100644 --- a/ee/spec/requests/api/graphql/group/epics_spec.rb +++ b/ee/spec/requests/api/graphql/group/epics_spec.rb @@ -218,9 +218,9 @@ def global_ids(*epics) # warm up post_graphql(query({ iids: [child_epic.iid] }), current_user: user) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do post_graphql(query({ iids: [child_epic.iid] }), current_user: user) - end.count + end epics_with_parent = create_list(:epic, 3, group: group) do |epic| epic.update!(parent: create(:epic, group: group)) @@ -228,7 +228,7 @@ def global_ids(*epics) expect do post_graphql(query({ iids: epics_with_parent.pluck(:iid) }), current_user: user) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end diff --git a/ee/spec/requests/api/graphql/group_query_spec.rb b/ee/spec/requests/api/graphql/group_query_spec.rb index 31ddc293e7b404481ede7a87bd52c41c1ac1afa1..9500efd3a79c610e5fc4b7e7466a508c7678122f 100644 --- a/ee/spec/requests/api/graphql/group_query_spec.rb +++ b/ee/spec/requests/api/graphql/group_query_spec.rb @@ -209,11 +209,11 @@ def group_query(group) create_list(:epic, 10, group: group) group.reload - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do query_epics(1) - end.count + end - expect { query_epics(10) }.not_to exceed_all_query_limit(control_count) + expect { query_epics(10) }.not_to exceed_all_query_limit(control) end end diff --git a/ee/spec/requests/api/graphql/mutations/merge_requests/set_assignees_spec.rb b/ee/spec/requests/api/graphql/mutations/merge_requests/set_assignees_spec.rb index 43d811e9df89510a7760d39e4bc47a8a104b44cf..8639bab7dbfb390b0eaadade3bd39c974010bcd9 100644 --- a/ee/spec/requests/api/graphql/mutations/merge_requests/set_assignees_spec.rb +++ b/ee/spec/requests/api/graphql/mutations/merge_requests/set_assignees_spec.rb @@ -99,7 +99,7 @@ def mutation_assignee_nodes add_one_assignee = mutation(input.merge(assignee_usernames: usernames.take(1)), mr_a) add_two_assignees = mutation(input.merge(assignee_usernames: usernames.last(2)), mr_b) - baseline = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do post_graphql_mutation(add_one_assignee, current_user: current_user) end @@ -112,7 +112,7 @@ def mutation_assignee_nodes # On top of which, we have to do an extra authorization fetch expect do post_graphql_mutation(add_two_assignees, current_user: current_user) - end.not_to exceed_query_limit(baseline.count + 3) + end.not_to exceed_query_limit(control).with_threshold(3) end end end diff --git a/ee/spec/requests/api/graphql/namespace/compliance_frameworks_spec.rb b/ee/spec/requests/api/graphql/namespace/compliance_frameworks_spec.rb index 0b5325396ee88d3b2f6b770db684331289e13625..227870c1d24da51e79e3128c24426a8ac409a6dd 100644 --- a/ee/spec/requests/api/graphql/namespace/compliance_frameworks_spec.rb +++ b/ee/spec/requests/api/graphql/namespace/compliance_frameworks_spec.rb @@ -202,9 +202,11 @@ post_graphql(query, current_user: current_user) post_graphql(multiple_namespace_query, current_user: current_user) - query_count = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) }.count + control = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) } - expect { post_graphql(multiple_namespace_query, current_user: current_user) }.not_to exceed_query_limit(query_count + 2) + expect do + post_graphql(multiple_namespace_query, current_user: current_user) + end.not_to exceed_query_limit(control).with_threshold(2) end it 'responds with the expected list of compliance frameworks' do diff --git a/ee/spec/requests/api/graphql/project/branch_rules/approval_project_rules_spec.rb b/ee/spec/requests/api/graphql/project/branch_rules/approval_project_rules_spec.rb index e102eb4a601e0cd74d91a5e7b186372921ced7bf..2a853008a4cd0033b18c66674a55a87335ecd0af 100644 --- a/ee/spec/requests/api/graphql/project/branch_rules/approval_project_rules_spec.rb +++ b/ee/spec/requests/api/graphql/project/branch_rules/approval_project_rules_spec.rb @@ -84,7 +84,7 @@ it 'avoids N+1 queries' do control = ActiveRecord::QueryRecorder.new do post_graphql(query, current_user: current_user, variables: variables) - end.count + end number_of_rules = 3 diff --git a/ee/spec/requests/api/graphql/projects/compliance_frameworks_spec.rb b/ee/spec/requests/api/graphql/projects/compliance_frameworks_spec.rb index 52bdbd093cdebdb05b2b270d7c1d639348706686..bce98190a3658eb9ddb30be274979c542a0c6c00 100644 --- a/ee/spec/requests/api/graphql/projects/compliance_frameworks_spec.rb +++ b/ee/spec/requests/api/graphql/projects/compliance_frameworks_spec.rb @@ -29,9 +29,9 @@ end it 'avoids N+1 queries', :use_sql_query_cache do - query_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { post_graphql(query, current_user: current_user) }.count + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { post_graphql(query, current_user: current_user) } - expect { post_graphql(single_project_query, current_user: current_user) }.not_to exceed_all_query_limit(query_count) + expect { post_graphql(single_project_query, current_user: current_user) }.not_to exceed_all_query_limit(control) end it 'contains the expected compliance framework' do diff --git a/ee/spec/requests/api/graphql/work_item_spec.rb b/ee/spec/requests/api/graphql/work_item_spec.rb index cef4f0d26063380dc0043d05794560f76e06f5de..af826dac736281f975bff94a1531a98cf1923f75 100644 --- a/ee/spec/requests/api/graphql/work_item_spec.rb +++ b/ee/spec/requests/api/graphql/work_item_spec.rb @@ -695,7 +695,7 @@ def find_note(work_item, starting_with) it 'avoids N+1 queries', :use_sql_query_cache do post_graphql(query, current_user: current_user) # warmup - 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 @@ -703,7 +703,7 @@ def find_note(work_item, starting_with) create(:work_item_link, source: item, target: work_item, link_type: 'blocks') end - expect { post_graphql(query, current_user: current_user) }.to issue_same_number_of_queries_as(control_count) + expect { post_graphql(query, current_user: current_user) }.to issue_same_number_of_queries_as(control) expect_graphql_errors_to_be_empty end end diff --git a/ee/spec/requests/api/group_protected_branches_spec.rb b/ee/spec/requests/api/group_protected_branches_spec.rb index cb3c88e88dc97990b1afdb555fa44b5cd1bb2e13..8a7cd162170a022c01591c645c436e9f101b08ac 100644 --- a/ee/spec/requests/api/group_protected_branches_spec.rb +++ b/ee/spec/requests/api/group_protected_branches_spec.rb @@ -64,7 +64,7 @@ describe 'avoid N+1 sql queries' do it 'does not perform N+1 sql queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do get api(route, owner), params: params.merge(per_page: 100) end @@ -72,7 +72,7 @@ expect do get api(route, owner), params: params.merge(per_page: 100) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end end diff --git a/ee/spec/requests/api/issues_spec.rb b/ee/spec/requests/api/issues_spec.rb index 09e9ccd3bf4a9cab38d2e1828e5c4d5548d70bf5..fc0bb820cb5783f034db5f31c7fd74054b172613 100644 --- a/ee/spec/requests/api/issues_spec.rb +++ b/ee/spec/requests/api/issues_spec.rb @@ -364,14 +364,14 @@ get api("/groups/#{group.id}/issues", user) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { get api("/groups/#{group.id}/issues", user) } + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { get api("/groups/#{group.id}/issues", user) } subgroup_2 = create(:group, parent: group) subgroup_2_project = create(:project, group: subgroup_2) create(:issue, project: subgroup_2_project, epic: create(:epic, group: subgroup_2)) - expect { get api("/groups/#{group.id}/issues", user) }.not_to exceed_query_limit(control_count) + expect { get api("/groups/#{group.id}/issues", user) }.not_to exceed_query_limit(control) end it 'avoids N+1 queries with iterations' do @@ -386,14 +386,14 @@ get api("/groups/#{group.id}/issues", user) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { get api("/groups/#{group.id}/issues", user) } + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { get api("/groups/#{group.id}/issues", user) } subgroup_2 = create(:group, parent: group) subgroup_2_project = create(:project, group: subgroup_2) create(:issue, project: subgroup_2_project, iteration: create(:iteration, iterations_cadence: create(:iterations_cadence, group: subgroup_2))) - expect { get api("/groups/#{group.id}/issues", user) }.not_to exceed_query_limit(control_count) + expect { get api("/groups/#{group.id}/issues", user) }.not_to exceed_query_limit(control) end end diff --git a/ee/spec/requests/api/merge_trains_spec.rb b/ee/spec/requests/api/merge_trains_spec.rb index 0617d3e0547c7b56ffda0ae5578d9c4bbcaecea9..72a1c3bb25b665bfb1d2031f6c00a7b5ff803da8 100644 --- a/ee/spec/requests/api/merge_trains_spec.rb +++ b/ee/spec/requests/api/merge_trains_spec.rb @@ -44,12 +44,11 @@ end it 'does not have N+1 problem' do - control_count = ActiveRecord::QueryRecorder.new { subject } + control = ActiveRecord::QueryRecorder.new { subject } create_list(:merge_train_car, 3, target_project: project) - expect { get api("/projects/#{project.id}/merge_trains", user) } - .not_to exceed_query_limit(control_count) + expect { get api("/projects/#{project.id}/merge_trains", user) }.not_to exceed_query_limit(control) end context 'when sort is specified' do diff --git a/ee/spec/requests/api/projects_spec.rb b/ee/spec/requests/api/projects_spec.rb index d7ce1dab2ae5d76dcfc273e7ad83c20bae1d10fe..2f7f7aa5b26dd3e8b755a6e4a54290910474d6d0 100644 --- a/ee/spec/requests/api/projects_spec.rb +++ b/ee/spec/requests/api/projects_spec.rb @@ -77,7 +77,7 @@ expect do get api('/projects', admin) - end.not_to exceed_all_query_limit(control.count) + end.not_to exceed_all_query_limit(control) end end diff --git a/ee/spec/requests/api/related_epic_links_spec.rb b/ee/spec/requests/api/related_epic_links_spec.rb index 7db5c89e5891f984fb4ada633d2b13a64c38ab35..78458b73bebec5d557769077dca519f9ec191297 100644 --- a/ee/spec/requests/api/related_epic_links_spec.rb +++ b/ee/spec/requests/api/related_epic_links_spec.rb @@ -213,11 +213,11 @@ def perform_request(user = nil, params = {}) 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(:related_epic_link, source: epic, target: create(:epic, group: group)) - expect { perform_request(user) }.not_to exceed_query_limit(control_count) + expect { perform_request(user) }.not_to exceed_query_limit(control) expect(response).to have_gitlab_http_status(:ok) end end @@ -287,11 +287,11 @@ def perform_request(user = nil, params = {}) 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(:related_epic_link, source: epic, target: create(:epic, group: group)) - expect { perform_request(user) }.not_to exceed_query_limit(control_count) + expect { perform_request(user) }.not_to exceed_query_limit(control) expect(response).to have_gitlab_http_status(:ok) end end diff --git a/ee/spec/requests/groups/clusters_controller_spec.rb b/ee/spec/requests/groups/clusters_controller_spec.rb index 9eec7e55add9890bbac2e347e780e8fc7fd5910f..a5eab96c5250be2f8f3bf725cae8a69570816ebb 100644 --- a/ee/spec/requests/groups/clusters_controller_spec.rb +++ b/ee/spec/requests/groups/clusters_controller_spec.rb @@ -25,7 +25,7 @@ def go end it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { go }.count + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { go } deployment_count = 2 create_list(:deployment, deployment_count, :success) @@ -36,7 +36,7 @@ def go # it also appears that `can_read_pod_logs?` in ee/app/serializers/clusters/environment_entity.rb # generates 3 additional queries per deployment leeway += deployment_count * 3 - expect { go }.not_to exceed_all_query_limit(control_count + leeway) + expect { go }.not_to exceed_all_query_limit(control).with_threshold(leeway) end end end diff --git a/ee/spec/requests/projects/issue_feature_flags_controller_spec.rb b/ee/spec/requests/projects/issue_feature_flags_controller_spec.rb index 529fd8fe52f4cf0b0f6fa3b37772c7b835d8b544..80d5959fa97ca545bb94b60902c7cf2e49b708e6 100644 --- a/ee/spec/requests/projects/issue_feature_flags_controller_spec.rb +++ b/ee/spec/requests/projects/issue_feature_flags_controller_spec.rb @@ -68,14 +68,14 @@ def get_request(project, issue) feature_flag, _, _ = setup sign_in(developer) - control_count = ActiveRecord::QueryRecorder.new { get_request(project, feature_flag) }.count + control = ActiveRecord::QueryRecorder.new { get_request(project, feature_flag) } issue_b = create(:issue, project: project) issue_c = create(:issue, project: project) create(:feature_flag_issue, feature_flag: feature_flag, issue: issue_b) create(:feature_flag_issue, feature_flag: feature_flag, issue: issue_c) - expect { get_request(project, feature_flag) }.not_to exceed_query_limit(control_count) + expect { get_request(project, feature_flag) }.not_to exceed_query_limit(control) end context 'when feature flag related issues feature is unlicensed' do diff --git a/ee/spec/requests/projects/merge_requests_controller_spec.rb b/ee/spec/requests/projects/merge_requests_controller_spec.rb index d01f898e7c7319f7d1df0f5762150f8205e362a6..a2719b23425645088500a1416da1940bc1d8e87b 100644 --- a/ee/spec/requests/projects/merge_requests_controller_spec.rb +++ b/ee/spec/requests/projects/merge_requests_controller_spec.rb @@ -59,7 +59,7 @@ def get_index create_list(:approval_project_rule, 5, project: project, users: [user, other_user], approvals_required: 2) create_list(:approval_merge_request_rule, 5, merge_request: merge_request, users: [user, other_user], approvals_required: 2) - control_count = ActiveRecord::QueryRecorder.new { get_index }.count + control = ActiveRecord::QueryRecorder.new { get_index } create_list(:approval, 10) create(:approval_project_rule, project: project, users: [user, other_user], approvals_required: 2) @@ -67,7 +67,7 @@ def get_index create(:approval_merge_request_rule, merge_request: mr, users: [user, other_user], approvals_required: 2) end - expect { get_index }.not_to exceed_query_limit(control_count) + expect { get_index }.not_to exceed_query_limit(control) end end diff --git a/ee/spec/requests/search_controller_spec.rb b/ee/spec/requests/search_controller_spec.rb index 627b881eb88005b3e529c0222738cb42b82fef77..8181bd2828b3f07cf60e07bb8b1653bc7ee38fe1 100644 --- a/ee/spec/requests/search_controller_spec.rb +++ b/ee/spec/requests/search_controller_spec.rb @@ -139,7 +139,7 @@ def send_search_request(params) 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 @@ -162,7 +162,7 @@ def send_search_request(params) 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/ee/spec/services/ci/compare_license_scanning_reports_service_spec.rb b/ee/spec/services/ci/compare_license_scanning_reports_service_spec.rb index cf1d7459e2a6f27ea462b40366c84cd92b4404a6..271db52f7db39d7c039450a3d84e1443afe3ab75 100644 --- a/ee/spec/services/ci/compare_license_scanning_reports_service_spec.rb +++ b/ee/spec/services/ci/compare_license_scanning_reports_service_spec.rb @@ -21,15 +21,15 @@ base_pipeline = create(:ee_ci_pipeline, project: project) head_pipeline = create(:ee_ci_pipeline, :with_cyclonedx_report, project: project) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do service.execute(base_pipeline.reload, head_pipeline.reload) - end.count + end new_head_pipeline = create(:ee_ci_pipeline, :with_cyclonedx_report, project: project) expect do service.execute(base_pipeline.reload, new_head_pipeline.reload) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end diff --git a/ee/spec/services/epic_issues/list_service_spec.rb b/ee/spec/services/epic_issues/list_service_spec.rb index 0933829092313f8809710f0e8eac1e9710fa4985..5beb36bc1f8470aeae7e59dbb0d508393209b2cd 100644 --- a/ee/spec/services/epic_issues/list_service_spec.rb +++ b/ee/spec/services/epic_issues/list_service_spec.rb @@ -58,7 +58,7 @@ create(:epic_issue, issue: new_issue1, epic: epic, relative_position: 3) create(:epic_issue, issue: new_issue3, epic: epic, relative_position: 5) - control_count = ActiveRecord::QueryRecorder.new { list_service.execute }.count + control = ActiveRecord::QueryRecorder.new { list_service.execute } new_group2 = create(:group, :private) new_group2.add_developer(user) @@ -67,7 +67,7 @@ create(:epic_issue, issue: new_issue4, epic: epic, relative_position: 6) create(:issue_link, source: create(:issue), target: issue2, link_type: IssueLink::TYPE_BLOCKS) - expect { list_service.execute }.not_to exceed_query_limit(control_count) + expect { list_service.execute }.not_to exceed_query_limit(control) end context 'owner can see all issues and destroy their associations' do diff --git a/ee/spec/services/epics/update_dates_service_spec.rb b/ee/spec/services/epics/update_dates_service_spec.rb index 11df9c0473976755d12292e16524f3b113272cb5..1ac76f32d290e63759d1213c20da4d1b937462f1 100644 --- a/ee/spec/services/epics/update_dates_service_spec.rb +++ b/ee/spec/services/epics/update_dates_service_spec.rb @@ -246,22 +246,22 @@ def setup_control_group ActiveRecord::QueryRecorder.new do described_class.new(epics).execute - end.count + end end it 'does not increase query count when adding epics without milestones' do - control_count = setup_control_group + control = setup_control_group epics << create(:epic) epics << create(:epic) expect do described_class.new(epics).execute - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end it 'does not increase query count when adding epics belongs to same milestones' do - control_count = setup_control_group + control = setup_control_group epics << create(:epic) epics << create(:epic) @@ -271,7 +271,7 @@ def setup_control_group expect do described_class.new(epics).execute - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end end diff --git a/ee/spec/services/issue_feature_flags/list_service_spec.rb b/ee/spec/services/issue_feature_flags/list_service_spec.rb index 682264b2e0213ffcb882ced001392bca9060c3d2..1cce49358bf168933cf8126e9665accca85b0592 100644 --- a/ee/spec/services/issue_feature_flags/list_service_spec.rb +++ b/ee/spec/services/issue_feature_flags/list_service_spec.rb @@ -13,6 +13,7 @@ let(:feature_flag_b) { create(:operations_feature_flag, project: project) } let(:feature_flag_c) { create(:operations_feature_flag, project: project) } let(:feature_flag_d) { create(:operations_feature_flag, project: project) } + let(:feature_flag_e) { create(:operations_feature_flag, project: project) } before do create(:feature_flag_issue, feature_flag: feature_flag_b, issue: issue) @@ -27,9 +28,11 @@ end it 'ensures no N+1 queries are made' do - control_count = ActiveRecord::QueryRecorder.new { described_class.new(issue, user).execute }.count + control = ActiveRecord::QueryRecorder.new { described_class.new(issue, user).execute } - expect { described_class.new(issue, user).execute }.not_to exceed_query_limit(control_count) + create(:feature_flag_issue, feature_flag: feature_flag_e, issue: issue) + + expect { described_class.new(issue, user).execute }.not_to exceed_query_limit(control) end it 'returns related feature flags' do diff --git a/ee/spec/workers/iterations/roll_over_issues_worker_spec.rb b/ee/spec/workers/iterations/roll_over_issues_worker_spec.rb index 0c42493fbad5d4a726ef0c67d5ddc6416ceeadb0..4436ca913124505be76ddbfdd7d16bf794d92985 100644 --- a/ee/spec/workers/iterations/roll_over_issues_worker_spec.rb +++ b/ee/spec/workers/iterations/roll_over_issues_worker_spec.rb @@ -92,7 +92,7 @@ worker.send(:automation_bot) # this will trigger the check and initiate the @automation_bot instance var representative = group1.iterations.closed.first - control_count = ActiveRecord::QueryRecorder.new { worker.perform(representative) }.count + control = ActiveRecord::QueryRecorder.new { worker.perform(representative) } # for each iteration 2 extra queries are needed: # - find the next open iteration @@ -100,7 +100,7 @@ # so we have 2 extra closed iterations compared to control count so we need 4 more queries iteration_ids = [group1.iterations.closed.pluck(:id) + group2.iterations.closed.pluck(:id)].flatten - expect { worker.perform(iteration_ids) }.not_to exceed_query_limit(control_count + 4) + expect { worker.perform(iteration_ids) }.not_to exceed_query_limit(control).with_threshold(4) end context 'with batches' do diff --git a/ee/spec/workers/scan_security_report_secrets_worker_spec.rb b/ee/spec/workers/scan_security_report_secrets_worker_spec.rb index cec423aef982406a2f2a0e6b10ab6e51f45a8b25..2030e148bc350647c21d991659da04d0ebb5d7a9 100644 --- a/ee/spec/workers/scan_security_report_secrets_worker_spec.rb +++ b/ee/spec/workers/scan_security_report_secrets_worker_spec.rb @@ -78,7 +78,7 @@ include_context 'when pipeline has revocable keys' it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new { worker.perform(pipeline.id) } + control = ActiveRecord::QueryRecorder.new { worker.perform(pipeline.id) } 3.times do finding = create(:vulnerabilities_finding, @@ -90,7 +90,7 @@ create(:vulnerabilities_finding_pipeline, finding: finding, pipeline: pipeline) end - expect { worker.perform(pipeline.id) }.not_to exceed_query_limit(control_count) + expect { worker.perform(pipeline.id) }.not_to exceed_query_limit(control) end end diff --git a/spec/controllers/admin/projects_controller_spec.rb b/spec/controllers/admin/projects_controller_spec.rb index d81b067ffb682b8c7040a7f120653cedac52d46d..95986b5c0341eeeb616efe2902b252d78442f5d4 100644 --- a/spec/controllers/admin/projects_controller_spec.rb +++ b/spec/controllers/admin/projects_controller_spec.rb @@ -49,11 +49,11 @@ it 'does not have N+1 queries', :use_clean_rails_memory_store_caching, :request_store do get :index - control_count = ActiveRecord::QueryRecorder.new { get :index }.count + control = ActiveRecord::QueryRecorder.new { get :index } create(:project) - expect { get :index }.not_to exceed_query_limit(control_count) + expect { get :index }.not_to exceed_query_limit(control) end end diff --git a/spec/controllers/admin/runners_controller_spec.rb b/spec/controllers/admin/runners_controller_spec.rb index d88fe41a8699cce95d89c3579efbaff1827d5d13..186e1b13856deb1826f0a6d4a611575223e7fcd6 100644 --- a/spec/controllers/admin/runners_controller_spec.rb +++ b/spec/controllers/admin/runners_controller_spec.rb @@ -89,11 +89,11 @@ it 'avoids N+1 queries', :request_store do get :edit, params: { id: runner.id } - control_count = ActiveRecord::QueryRecorder.new { get :edit, params: { id: runner.id } }.count + control = ActiveRecord::QueryRecorder.new { get :edit, params: { id: runner.id } } # There is one additional query looking up subject.group in ProjectPolicy for the # needs_new_sso_session permission - expect { get :edit, params: { id: runner.id } }.not_to exceed_query_limit(control_count + 1) + expect { get :edit, params: { id: runner.id } }.not_to exceed_query_limit(control).with_threshold(1) expect(response).to have_gitlab_http_status(:ok) end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index f4384dbaa696c0438d81383ae5f40ccf41df560a..d920fbc3c32f92e1fbfd30895c6b9872a5cc792c 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -470,7 +470,7 @@ def index enforce_terms - expect { get :index }.not_to exceed_query_limit(control.count).with_threshold(1) + expect { get :index }.not_to exceed_query_limit(control).with_threshold(1) end context 'when terms are enforced' do diff --git a/spec/controllers/concerns/renders_commits_spec.rb b/spec/controllers/concerns/renders_commits_spec.rb index 45f194b63e7991a8ad0f6894c6e7020dce1d5325..754107efee8a7b5af1dc78c64ac1e6d5862c3d20 100644 --- a/spec/controllers/concerns/renders_commits_spec.rb +++ b/spec/controllers/concerns/renders_commits_spec.rb @@ -46,15 +46,15 @@ def go it 'avoids N + 1', :request_store do stub_const("MergeRequestDiff::COMMITS_SAFE_SIZE", 5) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do go - end.count + end stub_const("MergeRequestDiff::COMMITS_SAFE_SIZE", 15) expect do go - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end @@ -73,7 +73,7 @@ def go expect do subject.prepare_commits_for_rendering(merge_request.commits) merge_request.commits.each(&:latest_pipeline) - end.not_to exceed_all_query_limit(control.count) + end.not_to exceed_all_query_limit(control) end end end diff --git a/spec/controllers/groups/labels_controller_spec.rb b/spec/controllers/groups/labels_controller_spec.rb index 3dcf41941bbe29b1d0df790cf0db378dde5fdfc7..38e39da273368146573a5c4c1b770bdd19a529b6 100644 --- a/spec/controllers/groups/labels_controller_spec.rb +++ b/spec/controllers/groups/labels_controller_spec.rb @@ -62,7 +62,9 @@ create_list(:group_label, 3, group: group) # some n+1 queries still exist - expect { get :index, params: { group_id: group.to_param } }.not_to exceed_all_query_limit(control.count).with_threshold(10) + expect do + get :index, params: { group_id: group.to_param } + end.not_to exceed_all_query_limit(control).with_threshold(10) expect(assigns(:labels).count).to eq(4) end end diff --git a/spec/controllers/groups/releases_controller_spec.rb b/spec/controllers/groups/releases_controller_spec.rb index 4b4333dea0ee0ecf7accd45dea1cdd5abf5ab600..1ca540ebb993b80c844e94c89b02dcaa64fc13a7 100644 --- a/spec/controllers/groups/releases_controller_spec.rb +++ b/spec/controllers/groups/releases_controller_spec.rb @@ -62,12 +62,12 @@ context 'N+1 queries' do it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new { subject }.count + control = ActiveRecord::QueryRecorder.new { subject } create_list(:release, 5, project: project) create_list(:release, 5, project: private_project) - expect { subject }.not_to exceed_query_limit(control_count) + expect { subject }.not_to exceed_query_limit(control) end end end diff --git a/spec/controllers/projects/issues_controller_spec.rb b/spec/controllers/projects/issues_controller_spec.rb index b29a172f5b1e03d265a8f73b46fe3cb59364002c..721125749a5e78e7a4e3741ea84e30e943159c79 100644 --- a/spec/controllers/projects/issues_controller_spec.rb +++ b/spec/controllers/projects/issues_controller_spec.rb @@ -987,11 +987,11 @@ def go(id:) labels = create_list(:label, 10, project: project).map(&:to_reference) issue = create(:issue, project: project, description: 'Test issue') - control_count = ActiveRecord::QueryRecorder.new { issue.update!(description: [issue.description, label].join(' ')) }.count + control = ActiveRecord::QueryRecorder.new { issue.update!(description: [issue.description, label].join(' ')) } # Follow-up to get rid of this `2 * label.count` requirement: https://gitlab.com/gitlab-org/gitlab-foss/issues/52230 expect { issue.update!(description: [issue.description, labels].join(' ')) } - .not_to exceed_query_limit(control_count + 2 * labels.count) + .not_to exceed_query_limit(control).with_threshold(2 * labels.count) end it 'logs the view with Gitlab::Search::RecentIssues' do @@ -1849,15 +1849,17 @@ def get_service_desk(extra_params = {}) RequestStore.clear! - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do get :discussions, params: { namespace_id: project.namespace, project_id: project, id: issue.iid } - end.count + end RequestStore.clear! create_list(:discussion_note_on_issue, 2, :system, noteable: issue, project: issue.project, note: cross_reference) - expect { get :discussions, params: { namespace_id: project.namespace, project_id: project, id: issue.iid } }.not_to exceed_query_limit(control_count) + expect do + get :discussions, params: { namespace_id: project.namespace, project_id: project, id: issue.iid } + end.not_to exceed_query_limit(control) end end diff --git a/spec/controllers/projects/labels_controller_spec.rb b/spec/controllers/projects/labels_controller_spec.rb index db8cac8bb4aafc6772c457d37a6591d3fbe87b37..2333ff0a937f0f86b21776669a465f8e316e5e2c 100644 --- a/spec/controllers/projects/labels_controller_spec.rb +++ b/spec/controllers/projects/labels_controller_spec.rb @@ -108,7 +108,7 @@ # some n+1 queries still exist # calls to get max project authorization access level - expect { list_labels }.not_to exceed_all_query_limit(control.count).with_threshold(25) + expect { list_labels }.not_to exceed_all_query_limit(control).with_threshold(25) expect(assigns(:labels).count).to eq(10) end end diff --git a/spec/controllers/projects/notes_controller_spec.rb b/spec/controllers/projects/notes_controller_spec.rb index 678991b91a573461df3a73ff5a881d060e967c4e..6b440b90f3771d53fa4a1028231346c5af4b36c0 100644 --- a/spec/controllers/projects/notes_controller_spec.rb +++ b/spec/controllers/projects/notes_controller_spec.rb @@ -249,15 +249,15 @@ RequestStore.clear! - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do get :index, params: request_params - end.count + end RequestStore.clear! create_list(:discussion_note_on_issue, 2, :system, noteable: issue, project: issue.project, note: cross_reference) - expect { get :index, params: request_params }.not_to exceed_query_limit(control_count) + expect { get :index, params: request_params }.not_to exceed_query_limit(control) end end end diff --git a/spec/controllers/projects/pipeline_schedules_controller_spec.rb b/spec/controllers/projects/pipeline_schedules_controller_spec.rb index 7cd4f43d4da47c3a8a99b287ed6393a1a85095a9..9fe2e4c23e098e6161ce90bc2caf56cf2bf7384f 100644 --- a/spec/controllers/projects/pipeline_schedules_controller_spec.rb +++ b/spec/controllers/projects/pipeline_schedules_controller_spec.rb @@ -108,11 +108,11 @@ end it 'avoids N + 1 queries', :request_store do - control_count = ActiveRecord::QueryRecorder.new { visit_pipelines_schedules }.count + control = ActiveRecord::QueryRecorder.new { visit_pipelines_schedules } create_list(:ci_pipeline_schedule, 2, project: project) - expect { visit_pipelines_schedules }.not_to exceed_query_limit(control_count) + expect { visit_pipelines_schedules }.not_to exceed_query_limit(control) end context 'when the scope is set to active' do diff --git a/spec/controllers/projects/pipelines_controller_spec.rb b/spec/controllers/projects/pipelines_controller_spec.rb index deaed8e1162413d0568e935ff07d8e9138a73db5..82c1aa3e18c7d39d37a92af3a23c19413264f7e3 100644 --- a/spec/controllers/projects/pipelines_controller_spec.rb +++ b/spec/controllers/projects/pipelines_controller_spec.rb @@ -381,7 +381,7 @@ def create_bridge(stage, stage_idx, name, status) # Set up all required variables get_pipeline_json - control_count = ActiveRecord::QueryRecorder.new { get_pipeline_json }.count + control = ActiveRecord::QueryRecorder.new { get_pipeline_json } first_build = pipeline.builds.first first_build.tag_list << [:hello, :world] @@ -391,9 +391,7 @@ def create_bridge(stage, stage_idx, name, status) second_build.tag_list << [:docker, :ruby] create(:deployment, deployable: second_build) - new_count = ActiveRecord::QueryRecorder.new { get_pipeline_json }.count - - expect(new_count).to be_within(1).of(control_count) + expect { get_pipeline_json }.not_to exceed_query_limit(control).with_threshold(1) end end @@ -1074,7 +1072,7 @@ def post_request clear_controller_memoization - control_count = ActiveRecord::QueryRecorder.new { get_test_report_json }.count + control = ActiveRecord::QueryRecorder.new { get_test_report_json } create(:ci_build, name: 'karma', pipeline: pipeline).tap do |build| create(:ci_job_artifact, :junit, job: build) @@ -1082,7 +1080,7 @@ def post_request clear_controller_memoization - expect { get_test_report_json }.not_to exceed_query_limit(control_count) + expect { get_test_report_json }.not_to exceed_query_limit(control) end end diff --git a/spec/controllers/projects/starrers_controller_spec.rb b/spec/controllers/projects/starrers_controller_spec.rb index 2148f495c31b215ede5c2215f9d6e7dbc39dd352..236bb408d32d404a97291687df6bfa9c93b5609d 100644 --- a/spec/controllers/projects/starrers_controller_spec.rb +++ b/spec/controllers/projects/starrers_controller_spec.rb @@ -40,11 +40,11 @@ def user_ids it 'avoids N+1s loading users', :request_store do get_starrers - control_count = ActiveRecord::QueryRecorder.new { get_starrers }.count + control = ActiveRecord::QueryRecorder.new { get_starrers } create_list(:user, 5).each { |user| user.toggle_star(project) } - expect { get_starrers }.not_to exceed_query_limit(control_count) + expect { get_starrers }.not_to exceed_query_limit(control) end end diff --git a/spec/features/issuables/issuable_list_spec.rb b/spec/features/issuables/issuable_list_spec.rb index 1020ea341cecd29c888ccb4e02783ad81017b598..fee0f8a8f32463a407f36e0172ded0ad39fe2e5e 100644 --- a/spec/features/issuables/issuable_list_spec.rb +++ b/spec/features/issuables/issuable_list_spec.rb @@ -16,11 +16,11 @@ issuable_types.each do |issuable_type| it "avoids N+1 database queries for #{issuable_type.to_s.humanize.pluralize}", quarantine: { issue: 'https://gitlab.com/gitlab-org/gitlab/-/issues/231426' } do - control_count = ActiveRecord::QueryRecorder.new { visit_issuable_list(issuable_type) }.count + control = ActiveRecord::QueryRecorder.new { visit_issuable_list(issuable_type) } create_issuables(issuable_type) - expect { visit_issuable_list(issuable_type) }.not_to exceed_query_limit(control_count) + expect { visit_issuable_list(issuable_type) }.not_to exceed_query_limit(control) end it "counts upvotes, downvotes and notes count for each #{issuable_type.to_s.humanize}" do diff --git a/spec/features/projects/branches_spec.rb b/spec/features/projects/branches_spec.rb index a29d643b15bb6dac5d9872130ff9c8b35325d50d..41b8ad7825c51ba20c40196c099bba10074687c8 100644 --- a/spec/features/projects/branches_spec.rb +++ b/spec/features/projects/branches_spec.rb @@ -171,12 +171,12 @@ new_branches_count = 20 sql_queries_count_threshold = 10 - control_count = ActiveRecord::QueryRecorder.new { visit project_branches_path(project) }.count + control = ActiveRecord::QueryRecorder.new { visit project_branches_path(project) } (1..new_branches_count).each { |number| repository.add_branch(user, "new-branch-#{number}", 'master') } expect { visit project_branches_filtered_path(project, state: 'all') } - .not_to exceed_query_limit(control_count).with_threshold(sql_queries_count_threshold) + .not_to exceed_query_limit(control).with_threshold(sql_queries_count_threshold) end end diff --git a/spec/features/tags/developer_views_tags_spec.rb b/spec/features/tags/developer_views_tags_spec.rb index 154311853f832d3266a87a3a264d0cbfaa2d0e00..bc2d33b3a02d5fef9aa3e49916096f5ee094b68a 100644 --- a/spec/features/tags/developer_views_tags_spec.rb +++ b/spec/features/tags/developer_views_tags_spec.rb @@ -41,11 +41,11 @@ end it 'avoids a N+1 query in branches index' do - control_count = ActiveRecord::QueryRecorder.new { visit project_tags_path(project) }.count + control = ActiveRecord::QueryRecorder.new { visit project_tags_path(project) } %w[one two three four five].each { |tag| repository.add_tag(user, tag, 'master', 'foo') } - expect { visit project_tags_path(project) }.not_to exceed_query_limit(control_count) + expect { visit project_tags_path(project) }.not_to exceed_query_limit(control) end it 'views the tags list page' do diff --git a/spec/finders/deployments_finder_spec.rb b/spec/finders/deployments_finder_spec.rb index 807a7ca8e26a04ed69d501cc38bcdc15b400b43a..f45042d9c360534252968db06fd1a97e59b2ed35 100644 --- a/spec/finders/deployments_finder_spec.rb +++ b/spec/finders/deployments_finder_spec.rb @@ -343,14 +343,14 @@ it 'avoids N+1 queries' do execute_queries = -> { described_class.new({ group: group }).execute.first } - control_count = ActiveRecord::QueryRecorder.new { execute_queries }.count + control = ActiveRecord::QueryRecorder.new { execute_queries } new_project = create(:project, :repository, group: group) new_env = create(:environment, project: new_project, name: "production") create_list(:deployment, 2, status: :success, project: new_project, environment: new_env) group.reload - expect { execute_queries }.not_to exceed_query_limit(control_count) + expect { execute_queries }.not_to exceed_query_limit(control) end end end diff --git a/spec/finders/members_finder_spec.rb b/spec/finders/members_finder_spec.rb index e0fc494d033d9d6a0f0baa79b5de7ff63628f79c..9c8b8658538934e3012cf830c5aa7a1cd754ef20 100644 --- a/spec/finders/members_finder_spec.rb +++ b/spec/finders/members_finder_spec.rb @@ -166,12 +166,12 @@ # warm up # We need this warm up because there is 1 query being fired in one of the policies, - # and policy results are cached. Without a warm up, the control_count will be X queries + # and policy results are cached. Without a warm up, the control.count will be X queries # but the test phase will only fire X-1 queries, due the fact that the # result of the policy is already available in the cache. described_class.new(project, user2).execute.map(&:user) - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do described_class.new(project, user2).execute.map(&:user) end @@ -179,7 +179,7 @@ expect do described_class.new(project, user2).execute.map(&:user) - end.to issue_same_number_of_queries_as(control_count) + end.to issue_same_number_of_queries_as(control) end context 'with :shared_into_ancestors' do diff --git a/spec/finders/releases/group_releases_finder_spec.rb b/spec/finders/releases/group_releases_finder_spec.rb index daefc94828bbcbf319feb63b11f5486fad195e22..3430fe834d1379be03b9a7b7a4aea5dff4ea4787 100644 --- a/spec/finders/releases/group_releases_finder_spec.rb +++ b/spec/finders/releases/group_releases_finder_spec.rb @@ -168,9 +168,9 @@ let(:params) { query_params } it 'subgroups avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do releases - end.count + end subgroups = create_list(:group, 10, parent: group) projects = create_list(:project, 10, namespace: subgroups[0]) @@ -178,7 +178,7 @@ expect do releases - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end end diff --git a/spec/finders/resource_milestone_event_finder_spec.rb b/spec/finders/resource_milestone_event_finder_spec.rb index 27e124afe2e5aeac8a10114f657e2338470e357a..a05059328e3ab0ed7bd0fdd88e0811fac5e8afaa 100644 --- a/spec/finders/resource_milestone_event_finder_spec.rb +++ b/spec/finders/resource_milestone_event_finder_spec.rb @@ -49,8 +49,8 @@ milestone1 = create(:milestone, project: issue_project) milestone2 = create(:milestone, project: issue_project) - control_count = ActiveRecord::QueryRecorder.new { described_class.new(user, issue).execute }.count - expect(control_count).to eq(1) # 1 events query + control = ActiveRecord::QueryRecorder.new { described_class.new(user, issue).execute } + expect(control.count).to eq(1) # 1 events query create_event(milestone1, :add) create_event(milestone1, :remove) @@ -60,7 +60,7 @@ create_event(milestone2, :remove) # 1 milestones + 1 project + 1 user + 4 ability - expect { described_class.new(user, issue).execute }.not_to exceed_query_limit(control_count + 6) + expect { described_class.new(user, issue).execute }.not_to exceed_query_limit(control).with_threshold(6) end end diff --git a/spec/graphql/resolvers/design_management/versions_resolver_spec.rb b/spec/graphql/resolvers/design_management/versions_resolver_spec.rb index eb39e5bafc50f28b11dbc400571b54c49ec9ff2c..744e7f358917191657ec8b624dd741027f692e80 100644 --- a/spec/graphql/resolvers/design_management/versions_resolver_spec.rb +++ b/spec/graphql/resolvers/design_management/versions_resolver_spec.rb @@ -43,15 +43,15 @@ context 'loading associations' do it 'prevents N+1 queries when loading author' do - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do resolve_versions(object).items.map(&:author) - end.count + end create_list(:design_version, 3, issue: issue) expect do resolve_versions(object).items.map(&:author) - end.not_to exceed_all_query_limit(control_count) + end.not_to exceed_all_query_limit(control) end end end diff --git a/spec/graphql/resolvers/group_labels_resolver_spec.rb b/spec/graphql/resolvers/group_labels_resolver_spec.rb index 08e17cedfcc7668c45ce5e1c0f6a9b1b8420cdc1..2e583a1703df5535a6dbe55b5491f5744546a23d 100644 --- a/spec/graphql/resolvers/group_labels_resolver_spec.rb +++ b/spec/graphql/resolvers/group_labels_resolver_spec.rb @@ -78,7 +78,7 @@ Gitlab::SafeRequestStore.ensure_request_store do resolve_labels(group, params).to_a end - end.not_to exceed_query_limit(control.count) + end.not_to exceed_query_limit(control) end end diff --git a/spec/graphql/resolvers/labels_resolver_spec.rb b/spec/graphql/resolvers/labels_resolver_spec.rb index 16cf2e73736747bc87f76b58f5701661483c7b76..fd55c3131b4eb9f1709d32822ae406a619d3e72d 100644 --- a/spec/graphql/resolvers/labels_resolver_spec.rb +++ b/spec/graphql/resolvers/labels_resolver_spec.rb @@ -78,7 +78,7 @@ Gitlab::SafeRequestStore.ensure_request_store do resolve_labels(project, params).to_a end - end.not_to exceed_query_limit(control.count) + end.not_to exceed_query_limit(control) end end diff --git a/spec/graphql/types/current_user_todos_type_spec.rb b/spec/graphql/types/current_user_todos_type_spec.rb index febbe868f3359827477fb370b8356ac3f32f30a7..2b33a705ae211cfb5330b3cbb0acf170e01fdd94 100644 --- a/spec/graphql/types/current_user_todos_type_spec.rb +++ b/spec/graphql/types/current_user_todos_type_spec.rb @@ -159,17 +159,17 @@ # AND ("todos"."state" IN ('done','pending')) # AND "todos"."target_id" = merge_request # AND "todos"."target_type" = 'MergeRequest' ORDER BY "todos"."id" DESC - baseline = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do execute_query(query_type, graphql: base_query) end expect do execute_query(query_type, graphql: query_without_state_arguments) - end.not_to exceed_query_limit(baseline) # at present this is 3 + end.not_to exceed_query_limit(control) # at present this is 3 expect do execute_query(query_type, graphql: with_state_arguments) - end.not_to exceed_query_limit(baseline.count + 1) + end.not_to exceed_query_limit(control).with_threshold(1) end it 'returns correct data' do diff --git a/spec/helpers/groups_helper_spec.rb b/spec/helpers/groups_helper_spec.rb index 8aee337f51c46ec3a5d2ae7d1ed711eec5b616ea..5f192701b3375def4cf360df3f2541a450f10bf4 100644 --- a/spec/helpers/groups_helper_spec.rb +++ b/spec/helpers/groups_helper_spec.rb @@ -114,13 +114,13 @@ end it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do helper.group_title(nested_group) end expect do helper.group_title(very_deep_nested_group) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb index e1c0aafc3c3a0b362aa23f7f0bcfefc169c1be91..bad30b5033d9f392112ccdbbefbdbb7eee125bfc 100644 --- a/spec/helpers/search_helper_spec.rb +++ b/spec/helpers/search_helper_spec.rb @@ -656,12 +656,12 @@ def simple_sanitize(str) @project = create(:project) description = FFaker::Lorem.characters(210) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { search_md_sanitize(description) }.count + control = ActiveRecord::QueryRecorder.new(skip_cached: false) { search_md_sanitize(description) } issues = create_list(:issue, 4, project: @project) description_with_issues = description + ' ' + issues.map { |issue| "##{issue.iid}" }.join(' ') - expect { search_md_sanitize(description_with_issues) }.not_to exceed_all_query_limit(control_count) + expect { search_md_sanitize(description_with_issues) }.not_to exceed_all_query_limit(control) end end diff --git a/spec/lib/atlassian/jira_connect/client_spec.rb b/spec/lib/atlassian/jira_connect/client_spec.rb index a692d76da77f51da686a2c0fe61dd12d05dc8369..e1159b9fab2e1a9871470bd4ef81f5119f6f2771 100644 --- a/spec/lib/atlassian/jira_connect/client_spec.rb +++ b/spec/lib/atlassian/jira_connect/client_spec.rb @@ -433,16 +433,16 @@ def expected_headers(path) end it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do subject.send(:store_dev_info, project: project, merge_requests: merge_requests) - end.count + end merge_requests << create(:merge_request, :unique_branches, source_project: project) expect do subject.send(:store_dev_info, project: project, merge_requests: merge_requests) - end.not_to exceed_query_limit(control_count) + end.not_to exceed_query_limit(control) end end diff --git a/spec/lib/atlassian/jira_connect/serializers/pull_request_entity_spec.rb b/spec/lib/atlassian/jira_connect/serializers/pull_request_entity_spec.rb index 0ed320e863c3bd8a47b547ed247f7d4e1ec90c88..15cb4994d466f8873354fffe56ed93d06151c6e3 100644 --- a/spec/lib/atlassian/jira_connect/serializers/pull_request_entity_spec.rb +++ b/spec/lib/atlassian/jira_connect/serializers/pull_request_entity_spec.rb @@ -24,9 +24,9 @@ subject { described_class.represent(merge_requests, user_notes_count: user_notes_count).as_json } it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do described_class.represent(merge_requests, user_notes_count: user_notes_count) - end.count + end merge_requests << create(:merge_request, :unique_branches) @@ -35,7 +35,7 @@ records: merge_requests, associations: { merge_request_reviewers: :reviewer } ).call - expect { subject }.not_to exceed_query_limit(control_count) + expect { subject }.not_to exceed_query_limit(control) end it 'uses counts from user_notes_count' do diff --git a/spec/lib/backup/repositories_spec.rb b/spec/lib/backup/repositories_spec.rb index 024f6c5db963cade9fe9dfb29c1b44849b092373..679be62393e6c6ece8e2bee3ad07ea25e1be9a86 100644 --- a/spec/lib/backup/repositories_spec.rb +++ b/spec/lib/backup/repositories_spec.rb @@ -68,20 +68,20 @@ end it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new do + control = ActiveRecord::QueryRecorder.new do subject.dump(destination, backup_id) - end.count + end create_list(:project, 2, :repository) create_list(:snippet, 2, :repository) - # Number of expected queries are 2 more than control_count + # Number of expected queries are 2 more than control.count # to account for the queries for project.design_management_repository # for each project. # We are using 2 projects here. expect do subject.dump(destination, backup_id) - end.not_to exceed_query_limit(control_count + 2) + end.not_to exceed_query_limit(control).with_threshold(2) end describe 'storages' do diff --git a/spec/lib/banzai/filter/custom_emoji_filter_spec.rb b/spec/lib/banzai/filter/custom_emoji_filter_spec.rb index 4fc9d9dd4f687033e289df39690cb6b53d0b9d1a..701a45aa54de84c643e35a8657efd51c45644b1a 100644 --- a/spec/lib/banzai/filter/custom_emoji_filter_spec.rb +++ b/spec/lib/banzai/filter/custom_emoji_filter_spec.rb @@ -47,13 +47,13 @@ it 'does not do N+1 query' do create(:custom_emoji, name: 'party-parrot', group: group) - control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + control = ActiveRecord::QueryRecorder.new(skip_cached: false) do filter('<p>:tanuki:</p>') end expect do filter('<p>:tanuki:</p> <p>:party-parrot:</p>') - 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 06bb0edc92c62715a9599c14337f02240b98fb1f..d14f218763f0fadda0c1d7db12ddf14d0f38537a 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 @@ def issuable_link(issuable) # 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 @@ def issuable_link(issuable) # 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 9a2e68aaae0323006db416fef8c4da4a4572bbc0..0bdd64c360db7cc96df51e2377d74e8793bc1e6a 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 @@ 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 @@ # 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 35a3f20f7b7a5d3f27a6d9374b0503b2ff8825a0..730554857df5fa4b842d1ca1b2e71c501a3cfb41 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 @@ 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 @@ # 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 fd03d7c0d271f10858022e2e6161fff4e576079d..678d661910180d6c9d92e3a28fc814cd4c054904 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 @@ * #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 @@ # 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 823f006c98a9d15642f76b854d2f418cb39c2e45..acc59c85cbf852884e3e484ae111b5db3f86b522 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 @@ 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 d16188e99a3253571498a5bc8f9128608f51876a..fd947e3e9cbb1855f76f92abd79dc51ee0368547 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 @@ def helper 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 81b08a4c51663f8cae880de0c0006c7b45857261..bcc256813c9e0ee9d1ce86b55dd34682a576ea52 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 @@ # 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 ccc8478c7d8af3129c232d8169d695ea358a0495..e3036993f7b245d1ef312034f0b2fc9e64d3ef8a 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 @@ 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 c55fff787560326c79f45ffd383226fc891dd75e..12af94507b675e5b65bf911b92e3e0ab9be56a3c 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 @@ def get_reference(project) # 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 04877931610de11e819d7d87bd3d568300f38938..b4d9a08e4c6895a2205484a798f92eaa08ae5606 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 @@ 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 @@ # 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 00eac7262f440d2206ca5c6814843a7fee76a4ea..51c5551dda88963edc0db6b3bbcb5095c17318fa 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 @@ 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 @@ # 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 e59e53891bfcff05dbab8bf2b03205da7fda487f..cf245ccc72a4246575dbc578e5587c018a59773f 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 @@ def item_url(item) 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 5bbd98592e7fe614573c567e70d21cffa094e3b7..fe1a2bd9a2e14f72efb3e84182f1b5117d9b268e 100644 --- a/spec/lib/banzai/issuable_extractor_spec.rb +++ b/spec/lib/banzai/issuable_extractor_spec.rb @@ -45,9 +45,9 @@ def html_to_node(html) 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 8f4148be2dcba6d8ce0d6fd8f48589bda5e37b46..0f3834c2dc84f553d75d25f78098a13749a9016a 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 @@ def visible_references(snippet_visibility, user = nil) # 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 ad7cd2dc7367a2966a7ead4b583ebf20c41bff8b..5fa61b1680dd88509d203bfcb5feab000590e8a9 100644 --- a/spec/lib/gitlab/data_builder/pipeline_spec.rb +++ b/spec/lib/gitlab/data_builder/pipeline_spec.rb @@ -184,14 +184,14 @@ 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 @@ 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 71f9a7d0f0ea9068e275662c2cb261f53dcb7b56..82daaba6448d05a5c592c847dbf5a25dd57a1b0a 100644 --- a/spec/lib/gitlab/git_access_spec.rb +++ b/spec/lib/gitlab/git_access_spec.rb @@ -1062,14 +1062,14 @@ def self.run_permission_checks(permissions_matrix) # 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/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb index e835681a23333b6df5ddc98924d089c4d9c988af..34311a8ae22af0412b27e88f9aa1de8e3346e74c 100644 --- a/spec/mailers/notify_spec.rb +++ b/spec/mailers/notify_spec.rb @@ -2461,23 +2461,27 @@ def invite_to_group(group, inviter:, user: nil) 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/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 5fc5bbd41ffd4a38d914dafa25a992e1c9f21b42..a95f56ea714ff50b0fbb28622ae2d77e957117b6 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -576,9 +576,9 @@ 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 @@ 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/routable_spec.rb b/spec/models/concerns/routable_spec.rb index e71392f7bbcd54468ac31c4360a5378b34dad4a8..c6135e769cc3135e323f38a998fcee0dd120e503 100644 --- a/spec/models/concerns/routable_spec.rb +++ b/spec/models/concerns/routable_spec.rb @@ -89,7 +89,7 @@ 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 @@ 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/namespace_spec.rb b/spec/models/namespace_spec.rb index 5665bce8f0878b790d365ebbbfdaeaa10937cd68..67b8931f0c5220815e524a546176297af622215c 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -897,12 +897,14 @@ 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 5aa3ac3a2ea93c271c20256b830093e5f536b742..597950596422e30363f51a069d55ed019f7116b2 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -448,13 +448,13 @@ def retrieve_participants # 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 85ea784335c906a10241392336ee5fdd7ed08c7a..0453b6267eda3bad50d0acd02d7855b59b2cb5a5 100644 --- a/spec/models/preloaders/commit_status_preloader_spec.rb +++ b/spec/models/preloaders/commit_status_preloader_spec.rb @@ -21,13 +21,13 @@ 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 a3db29e7ec4d406f81e207c14a32c198e44c1b6c..7b8b6376aeaa5afcf4a9b4062c021bad8163c1d0 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2342,11 +2342,11 @@ def has_external_wiki 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 @@ def has_external_wiki 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 @@ def has_external_wiki 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 68fdbeba84d94ad67c62a3fc2868cf6deb1885ad..e231264e4489ff00ec974a84bef676a2511fe0a0 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -4573,13 +4573,13 @@ def login_method(login) 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 941aa3734a3af9962ad7db2563ca846efb91904e..9e1203bc720988af01b15442055c80b2895c9cf9 100644 --- a/spec/requests/api/ci/jobs_spec.rb +++ b/spec/requests/api/ci/jobs_spec.rb @@ -133,12 +133,12 @@ def perform_request 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 @@ def perform_request 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 @@ def perform_request 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 f534b093b7c673d75320950955c449e6ee0927e4..588991096b59fbcbb7ef73fcc7780de66287534b 100644 --- a/spec/requests/api/ci/pipeline_schedules_spec.rb +++ b/spec/requests/api/ci/pipeline_schedules_spec.rb @@ -42,15 +42,15 @@ def create_pipeline_schedules(count) # 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 eef125e1bc333947af63f0104a2e4542be27bc0d..ef169dbe872406bf35c07fbad0d1dc95b7011e31 100644 --- a/spec/requests/api/ci/pipelines_spec.rb +++ b/spec/requests/api/ci/pipelines_spec.rb @@ -471,15 +471,15 @@ 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 @@ 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 187880e16a450941c2d360290948fc0bf1aef452..b4394f47105362be9464eac57963af48e51ac75c 100644 --- a/spec/requests/api/ci/runners_spec.rb +++ b/spec/requests/api/ci/runners_spec.rb @@ -965,7 +965,7 @@ def update_runner(id, user, args) 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 4ec5d195ff8b2ceea9a0a402b8c9bdf46b16a024..67388f657f4785f793af94eaebc9bc87f3711fa9 100644 --- a/spec/requests/api/commits_spec.rb +++ b/spec/requests/api/commits_spec.rb @@ -1842,11 +1842,11 @@ def push_params(branch_name) 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 @@ def perform_request(user) 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 30c345ef4584b3a688e56c1754b374360489aeea..ca19a97ae492d24aa42679f8a4b1e075290f58b3 100644 --- a/spec/requests/api/deploy_keys_spec.rb +++ b/spec/requests/api/deploy_keys_spec.rb @@ -135,11 +135,11 @@ def perform_request 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 5a8e1649e756ebacff0d5e3962c644f616f7812e..f68307df779eeb71b682bdfd90e8a959ff6f0a98 100644 --- a/spec/requests/api/deployments_spec.rb +++ b/spec/requests/api/deployments_spec.rb @@ -143,11 +143,11 @@ def expect_deployments(ordered_deployments) 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 4fb0dfbb0704e69bffa7a148032dc20989b44cf4..2e51319462717e8ef381c67a0e0e737a555f46bc 100644 --- a/spec/requests/api/feature_flags_spec.rb +++ b/spec/requests/api/feature_flags_spec.rb @@ -67,12 +67,12 @@ 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 32048ea1432c4f5096a064fbf898e0492a7efe4e..94678bd18da36e228514827a600b0aca58e1ac7a 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 @@ 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 84b8c2285f0be7ffd18ed64ecbb50574f3b6e62d..dfebcb7c42ce0b423e251d778d700a6f38a6aa10 100644 --- a/spec/requests/api/graphql/projects/projects_spec.rb +++ b/spec/requests/api/graphql/projects/projects_spec.rb @@ -45,14 +45,14 @@ 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 2e6c3dcba61623370bf452606b7854bc8241394d..ccff5bdf9199f72cb438ba3f5b17da26677eba0d 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 @@ 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 fe77b7ae736107a06f30d103433de1d0dbc5c4ac..c6d44b057a73c7cb97b74322c8495de0ce561889 100644 --- a/spec/requests/api/graphql/work_item_spec.rb +++ b/spec/requests/api/graphql/work_item_spec.rb @@ -199,7 +199,7 @@ 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 @@ 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 82a4311f7d0a719c67e1586ea68a1a565d989ff3..7b4075b3aeb57f0bac722bdd97c42e1d4d84c95a 100644 --- a/spec/requests/api/group_milestones_spec.rb +++ b/spec/requests/api/group_milestones_spec.rb @@ -141,11 +141,11 @@ def perform_request 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 d1158cba16e905edb80a24955931b716b784421e..6b949962e5356728b75841295b64243a1ff22945 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -660,24 +660,24 @@ def response_project_ids(json_response, key) 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 @@ def response_project_ids(json_response, key) # 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 @@ def make_upload_request 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 @@ def make_upload_request 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 dc02e830027882d89c11dbafa66eb1649af828b6..60f3c4780eb1ce5fc96efdfb29a59236c424ebad 100644 --- a/spec/requests/api/invitations_spec.rb +++ b/spec/requests/api/invitations_spec.rb @@ -412,7 +412,7 @@ def invite_member_by_email(source, source_type, email, created_by, access_level: 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 @@ def invite_member_by_email(source, source_type, email, created_by, access_level: 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 @@ def invite_member_by_email(source, source_type, email, created_by, access_level: 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 @@ def invite_member_by_email(source, source_type, email, created_by, access_level: 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 @@ def invite_member_by_email(source, source_type, email, created_by, access_level: 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 fcb199a91a486e7acc4d7d2f0153b34616abd830..a4a9eca92b964020bbecedbc8a355c33ea22562e 100644 --- a/spec/requests/api/issue_links_spec.rb +++ b/spec/requests/api/issue_links_spec.rb @@ -40,11 +40,11 @@ def perform_request(user = nil, params = {}) 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 9e54ec08486d1bfd6766e9fccda5c3f119bfa61c..6719297f54fa64fca823dace95c26613944fe5dc 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 @@ 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 @@ 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 @@ 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 @@ 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 6000fa29dc4dca9d7fa9a03721e89f929d5682b2..6ba51080bf00894c576d5d30c991070dd3bde649 100644 --- a/spec/requests/api/merge_requests_spec.rb +++ b/spec/requests/api/merge_requests_spec.rb @@ -193,7 +193,7 @@ 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 @@ 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 49471b98ebaf95baa66609bc9e404a577a222a96..a73f3366dcbe19fbbbdf76c364baa15f1a4c4824 100644 --- a/spec/requests/api/project_import_spec.rb +++ b/spec/requests/api/project_import_spec.rb @@ -62,9 +62,9 @@ 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 b8e029385e3cdef4127faf7ced8c4c38fc945f39..cf6152a9b670e82bb366dbe34fb472812f720fdb 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -1152,7 +1152,7 @@ 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 @@ def failure_message(diff) 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 493dc4e72c6fb376da03e44e3f2cf4e7a73b4aa3..0c811a21fb052c3a953e73c90c25828278cd8d9a 100644 --- a/spec/requests/api/releases_spec.rb +++ b/spec/requests/api/releases_spec.rb @@ -156,9 +156,9 @@ 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 @@ 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 @@ def assert_snowplow_event(action, release_cli, user = maintainer) 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 @@ def assert_snowplow_event(action, release_cli, user = maintainer) 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 86c4e04ef71a4b4daffbb6aca9671e5f399ee4bf..de3460208b75ca0ebb41fd668365e5996fe8e06a 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -265,9 +265,9 @@ 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 @@ 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 @@ def update_password(user, admin, password = User.random_password) 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 @@ def update_password(user, admin, password = User.random_password) 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 @@ def update_password(user, admin, password = User.random_password) 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 54a25333c021ca1f73ae4c4306e1e87e53513aa3..ed24ad6489f4910d8daa28d2112f3d8be1e5a4dd 100644 --- a/spec/requests/groups/milestones_controller_spec.rb +++ b/spec/requests/groups/milestones_controller_spec.rb @@ -18,14 +18,16 @@ 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 @@ def perform_request 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 f54acf118bba8eefd669b5b8ff1b1c464b3feb22..e4818676f81018ec23027732b9538acb1efc2296 100644 --- a/spec/requests/groups/registry/repositories_controller_spec.rb +++ b/spec/requests/groups/registry/repositories_controller_spec.rb @@ -20,13 +20,13 @@ 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 aa3fefdef140f18b15675d6cda27f23cbf5ee75c..8be4fecea044d624afd2bc6c146ba0ab37aacaeb 100644 --- a/spec/requests/projects/pipelines_controller_spec.rb +++ b/spec/requests/projects/pipelines_controller_spec.rb @@ -25,14 +25,14 @@ 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 @@ def get_pipelines_index 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 @@ def get_pipelines_index 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 @@ def get_pipelines_index 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 365b20ad4aa49a0ad5cabbbd67f8254c882120f6..eaf11653256af9ac41f877dee322bb9af5229b03 100644 --- a/spec/requests/search_controller_spec.rb +++ b/spec/requests/search_controller_spec.rb @@ -112,7 +112,7 @@ def send_search_request(params) 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 @@ def send_search_request(params) 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 60f3ee114426db48252302648047f22c3ecf9959..af6a70989c9e2ab3217c5c0091847a328e04fb10 100644 --- a/spec/services/ci/abort_pipelines_service_spec.rb +++ b/spec/services/ci/abort_pipelines_service_spec.rb @@ -70,12 +70,12 @@ def abort_project_pipelines 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 3d0ce456aa53a561468d166d4b8a39824b6f3f8b..a74b820de0969b373e951f0dc4a44a9cc320f866 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 @@ 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 c060c72ffb20e44c78f9a18393a0cb3b4fc65f6f..bdb4ed182dc95c838028f4ee426dc4f583a56d09 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 @@ 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 1646afde21d4ccb4e47a77bf1000090375b95414..1708f475e6b7f5fd28f1232aa24b7b564135d13c 100644 --- a/spec/services/ci/retry_job_service_spec.rb +++ b/spec/services/ci/retry_job_service_spec.rb @@ -403,11 +403,11 @@ def drop_build! 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 4fd4492278df405023243ad5f51e33e880f85159..c5959127f343b0fc6981442d6259d1e496b66c29 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 @@ 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 b5cc8c4dcdcda55506f28f191f761fec29e2f0aa..f9e5e88aff0eae28d0195f703b8ef7e2807a0535 100644 --- a/spec/services/issue_links/list_service_spec.rb +++ b/spec/services/issue_links/list_service_spec.rb @@ -33,7 +33,7 @@ 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 @@ 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 83dfca923fb005ead1c03761d2d280fd557131ac..016174f9888b699ebe2627cfd5cac88efde20f10 100644 --- a/spec/services/issues/export_csv_service_spec.rb +++ b/spec/services/issues/export_csv_service_spec.rb @@ -175,11 +175,11 @@ def csv 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 4781daf7688d4b36c6e5af3fca3936a20a243ed9..6748292d3892f2aa25bfd8c10ee5ab019be067b0 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 @@ def create_closing_mr(attributes = {}) 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 @@ def create_closing_mr(attributes = {}) 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 @@ def create_closing_mr(attributes = {}) 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 @@ def create_closing_mr(attributes = {}) 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 2b398210034b6744768fab1ae848a716f6811636..3a1474e4fefffdd26a5bbc033f69a30fe5fce342 100644 --- a/spec/services/labels/available_labels_service_spec.rb +++ b/spec/services/labels/available_labels_service_spec.rb @@ -42,11 +42,15 @@ 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 de99fb244d3e902fbac4e5e87afb53369f79b5f4..bcde2fd51655e749ea63729dcae3a78c6b080ed2 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 @@ 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 77056cbe541c58a4efa147326944630f61049fd1..a6654989374a7226f353e26a44f0e197595be8be 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 @@ 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 bfd1dcd7d805f80d912a0a04d6b2b39e9c2698f3..b4788428f1436896339320fa75a29d66a38ef846 100644 --- a/spec/services/notification_recipients/build_service_spec.rb +++ b/spec/services/notification_recipients/build_service_spec.rb @@ -21,13 +21,13 @@ 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 @@ def create_user 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 @@ def create_user 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/participants_service_spec.rb b/spec/services/projects/participants_service_spec.rb index 2d048d5258d53b3d4bc511110627a2d158c09a42..167df7996cae45173e70ef6d7c9a71de59033d55 100644 --- a/spec/services/projects/participants_service_spec.rb +++ b/spec/services/projects/participants_service_spec.rb @@ -43,27 +43,27 @@ def run_service 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 1b5300672e3f79ad1ab0300c0601460d2fe378b8..d77a68288a52664b21512cb11c2f7d68f587f492 100644 --- a/spec/services/repositories/changelog_service_spec.rb +++ b/spec/services/repositories/changelog_service_spec.rb @@ -164,7 +164,7 @@ 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 6e246f21e84107824e03fd903393b26ae13f8502..df00859fd52098cb77f3ca9f8d7f441374b7c796 100644 --- a/spec/services/todo_service_spec.rb +++ b/spec/services/todo_service_spec.rb @@ -1276,9 +1276,9 @@ # 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 63ff189ede54a8c65dade3a97d25b2f7fd49b9f2..cccf1a2cfa831652289f7b9bccab12f70f8ce15d 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 @@ 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 @@ 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 a50bd3ee2f1fe6c0925004080c797577bfaa2a53..8236d8920729207c994dddb931ca48162f468b1b 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 @@ 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 @@ .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 eec637cf5b45097647381742b2b56bf9fedba590..d69a4ba99b7af0ae44a2467821503439dfdd3cf4 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 @@ def execute_single 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/shared_examples/controllers/githubish_import_controller_shared_examples.rb b/spec/support/shared_examples/controllers/githubish_import_controller_shared_examples.rb index c921da103479d0deca9dbb01fd9625556e299f30..94208e29d774c32bd1b0a97e345021bb7854760d 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 @@ def assign_session_token(provider) 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 @@ def assign_session_token(provider) 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 446bc4cd92f37f38450b3efc4d7a14d27ed727b8..461dcf2fcb60d85247827918833a0cae3c9a7a6e 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 @@ def create_issuable(issuable_type, project, source_branch:) 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 f49cc97936870c687119562b1a6b4ee5aa89bb29..bf8183525a910f6db681e807b4cea258f8f53144 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 @@ 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/relative_positioning_shared_examples.rb b/spec/support/shared_examples/models/relative_positioning_shared_examples.rb index 2b46c8c8fb9a7c633532d1d3be250fcec54e5fce..692320d45d5e7902b94132c054a94d610ec82f08 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 @@ def as_items(items) 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 b7247f1f2438deafd86d29b744d1ff5cd421506e..2976018b60fe551423bc98d4912e47e98b44c793 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 @@ # 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 @@ # 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 54c6ff799761e35485b9a5239c658ea46dbc1d6a..42fe170d2c40e2c8dbaa4b7cf732ae3047e0c2a0 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 @@ 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 d2b52468c25126383065e13ced295221fca70f62..459c957091c227954c24fce040f4d6208be1334c 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 @@ def execute 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 cb544f42765be2fb72dd3c4efa226101b85e96ee..97dd2aa96d40a692b6a4bbb450c046f327d7d988 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 @@ def check_component_file( 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 b617508bb3a988bb2711e61d7b065a40eff55b04..83bce97cd513f5a3fbfb1592c29f7f0074b6ef0c 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 @@ def perform(project_id, update_sequence_id) 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