diff options
Diffstat (limited to 'spec/lib/banzai')
7 files changed, 170 insertions, 27 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 diff --git a/spec/lib/banzai/filter/table_of_contents_tag_filter_spec.rb b/spec/lib/banzai/filter/table_of_contents_tag_filter_spec.rb index 56f36af5066..082e5c92e53 100644 --- a/spec/lib/banzai/filter/table_of_contents_tag_filter_spec.rb +++ b/spec/lib/banzai/filter/table_of_contents_tag_filter_spec.rb @@ -6,18 +6,42 @@ RSpec.describe Banzai::Filter::TableOfContentsTagFilter do include FilterSpecHelper context 'table of contents' do - let(:html) { '<p>[[<em>TOC</em>]]</p>' } + shared_examples 'table of contents tag' do + it 'replaces toc tag with ToC result' do + doc = filter(html, {}, { toc: "FOO" }) - it 'replaces [[<em>TOC</em>]] with ToC result' do - doc = filter(html, {}, { toc: "FOO" }) + expect(doc.to_html).to eq("FOO") + end - expect(doc.to_html).to eq("FOO") + it 'handles an empty ToC result' do + doc = filter(html) + + expect(doc.to_html).to eq '' + end + end + + context '[[_TOC_]] as tag' do + it_behaves_like 'table of contents tag' do + let(:html) { '<p>[[<em>TOC</em>]]</p>' } + end end - it 'handles an empty ToC result' do - doc = filter(html) + context '[[_toc_]] as tag' do + it_behaves_like 'table of contents tag' do + let(:html) { '<p>[[<em>toc</em>]]</p>' } + end + end + + context '[TOC] as tag' do + it_behaves_like 'table of contents tag' do + let(:html) { '<p>[TOC]</p>' } + end + end - expect(doc.to_html).to eq '' + context '[toc] as tag' do + it_behaves_like 'table of contents tag' do + let(:html) { '<p>[toc]</p>' } + end end end end diff --git a/spec/lib/banzai/pipeline/full_pipeline_spec.rb b/spec/lib/banzai/pipeline/full_pipeline_spec.rb index 989e06a992d..72661003361 100644 --- a/spec/lib/banzai/pipeline/full_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/full_pipeline_spec.rb @@ -102,33 +102,45 @@ RSpec.describe Banzai::Pipeline::FullPipeline do describe 'table of contents' do let(:project) { create(:project, :public) } - let(:markdown) do - <<-MARKDOWN.strip_heredoc - [[_TOC_]] + + shared_examples 'table of contents tag' do |tag, tag_html| + let(:markdown) do + <<-MARKDOWN.strip_heredoc + #{tag} # Header - MARKDOWN - end + MARKDOWN + end - let(:invalid_markdown) do - <<-MARKDOWN.strip_heredoc - test [[_TOC_]] + let(:invalid_markdown) do + <<-MARKDOWN.strip_heredoc + test #{tag} # Header - MARKDOWN - end + MARKDOWN + end - it 'inserts a table of contents' do - output = described_class.to_html(markdown, project: project) + it 'inserts a table of contents' do + output = described_class.to_html(markdown, project: project) - expect(output).to include("<ul class=\"section-nav\">") - expect(output).to include("<li><a href=\"#header\">Header</a></li>") + expect(output).to include("<ul class=\"section-nav\">") + expect(output).to include("<li><a href=\"#header\">Header</a></li>") + end + + it 'does not insert a table of contents' do + output = described_class.to_html(invalid_markdown, project: project) + + expect(output).to include("test #{tag_html}") + end end - it 'does not insert a table of contents' do - output = described_class.to_html(invalid_markdown, project: project) + context 'with [[_TOC_]] as tag' do + it_behaves_like 'table of contents tag', '[[_TOC_]]', '[[<em>TOC</em>]]' + end - expect(output).to include("test [[<em>TOC</em>]]") + context 'with [toc] as tag' do + it_behaves_like 'table of contents tag', '[toc]', '[toc]' + it_behaves_like 'table of contents tag', '[TOC]', '[TOC]' end end diff --git a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb index 007d310247b..59f5e4a6900 100644 --- a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb @@ -27,7 +27,7 @@ RSpec.describe Banzai::Pipeline::WikiPipeline do end end - it 'is case-sensitive' do + it 'is not case-sensitive' do markdown = <<-MD.strip_heredoc [[_toc_]] @@ -36,9 +36,22 @@ RSpec.describe Banzai::Pipeline::WikiPipeline do Foo MD - output = described_class.to_html(markdown, project: project, wiki: wiki) + result = described_class.call(markdown, project: project, wiki: wiki) + + expect(result[:output].to_html).to include(result[:toc]) + end + + it 'works with alternative [toc] tag' do + markdown = <<-MD.strip_heredoc + [toc] - expect(output).to include('[[<em>toc</em>]]') + # Header 1 + + Foo + MD + + result = described_class.call(markdown, project: project, wiki: wiki) + expect(result[:output].to_html).to include(result[:toc]) end it 'handles an empty pipeline result' do |