diff options
Diffstat (limited to 'spec/lib/banzai/filter/references')
4 files changed, 94 insertions, 0 deletions
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 7c6b0cac24b..cba41166be4 100644 --- a/spec/lib/banzai/filter/references/alert_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/alert_reference_filter_spec.rb @@ -220,4 +220,33 @@ RSpec.describe Banzai::Filter::References::AlertReferenceFilter do expect(reference_filter(act, project: nil, group: group).to_html).to eq exp end end + + context 'checking N+1' do + let(:namespace) { create(:namespace) } + let(:project2) { create(:project, :public, namespace: namespace) } + let(:alert2) { create(:alert_management_alert, project: project2) } + let(:alert_reference) { alert.to_reference } + let(:alert2_reference) { alert2.to_reference(full: true) } + + it 'does not have N+1 per multiple references per project', :use_sql_query_cache do + markdown = "#{alert_reference}" + max_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + reference_filter(markdown) + end.count + + expect(max_count).to eq 1 + + markdown = "#{alert_reference} ^alert#2 ^alert#3 ^alert#4 #{alert2_reference}" + + # Since we're not batching alert queries across projects, + # we have to account for that. + # 1 for both projects, 1 for alerts in each project == 3 + # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359 + max_count += 2 + + expect do + reference_filter(markdown) + end.not_to exceed_all_query_limit(max_count) + 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 bee8e42d12e..6bcea41a603 100644 --- a/spec/lib/banzai/filter/references/commit_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/commit_reference_filter_spec.rb @@ -269,4 +269,34 @@ RSpec.describe Banzai::Filter::References::CommitReferenceFilter do expect(reference_filter(act, context).css('a').first.text).to eql("#{project.full_path}@#{commit.short_id}") end end + + context 'checking N+1' do + let(:namespace2) { create(:namespace) } + let(:namespace3) { create(:namespace) } + let(:project2) { create(:project, :public, :repository, namespace: namespace2) } + let(:project3) { create(:project, :public, :repository, namespace: namespace3) } + let(:commit2) { project2.commit } + let(:commit3) { project3.commit } + let(:commit_reference) { commit.to_reference } + let(:commit2_reference) { commit2.to_reference(full: true) } + let(:commit3_reference) { commit3.to_reference(full: true) } + + it 'does not have N+1 per multiple references per project', :use_sql_query_cache do + markdown = "#{commit_reference}" + max_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + reference_filter(markdown) + end.count + + markdown = "#{commit_reference} 8b95f2f1 8b95f2f2 8b95f2f3 #{commit2_reference} #{commit3_reference}" + + # Commits are not DB entries, they are on the project itself. + # So adding commits from two more projects to the markdown should + # only increase by 1 query + max_count += 1 + + expect do + reference_filter(markdown) + end.not_to exceed_all_query_limit(max_count) + end + end end diff --git a/spec/lib/banzai/filter/references/milestone_reference_filter_spec.rb b/spec/lib/banzai/filter/references/milestone_reference_filter_spec.rb index f8a00716680..cdf6110dd6c 100644 --- a/spec/lib/banzai/filter/references/milestone_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/milestone_reference_filter_spec.rb @@ -92,6 +92,11 @@ RSpec.describe Banzai::Filter::References::MilestoneReferenceFilter do expect(doc.to_html).to match(%r(\(<a.+>#{milestone.reference_link_text}</a>\.\))) end + it 'links with adjacent html tags' do + doc = reference_filter("Milestone <p>#{reference}</p>.") + expect(doc.to_html).to match(%r(<p><a.+>#{milestone.reference_link_text}</a></p>)) + end + it 'ignores invalid milestone names' do exp = act = "Milestone #{Milestone.reference_prefix}#{milestone.name.reverse}" 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 63a5a9184c1..d88e262883f 100644 --- a/spec/lib/banzai/filter/references/project_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/project_reference_filter_spec.rb @@ -97,4 +97,34 @@ RSpec.describe Banzai::Filter::References::ProjectReferenceFilter do expect(filter.send(:projects)).to eq([project.full_path]) end end + + context 'checking N+1' do + let_it_be(:normal_project) { create(:project, :public) } + let_it_be(:group) { create(:group) } + let_it_be(:group_project) { create(:project, group: group) } + let_it_be(:nested_group) { create(:group, :nested) } + let_it_be(:nested_project) { create(:project, group: nested_group) } + let_it_be(:normal_project_reference) { get_reference(normal_project) } + let_it_be(:group_project_reference) { get_reference(group_project) } + let_it_be(:nested_project_reference) { get_reference(nested_project) } + + it 'does not have N+1 per multiple project references', :use_sql_query_cache do + markdown = "#{normal_project_reference}" + + # warm up first + reference_filter(markdown) + + max_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + reference_filter(markdown) + end.count + + expect(max_count).to eq 1 + + 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 + end end |