1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Banzai::Filter::References::ReferenceCache, feature_category: :team_planning do
let_it_be(:project) { create(:project) }
let_it_be(:project2) { create(:project) }
let_it_be(:issue1) { create(:issue, project: project) }
let_it_be(:issue2) { create(:issue, project: project) }
let_it_be(:issue3) { create(:issue, project: project2) }
let_it_be(:doc) { Nokogiri::HTML.fragment("#{issue1.to_reference} #{issue2.to_reference} #{issue3.to_reference(full: true)}") }
let(:filter_class) { Banzai::Filter::References::IssueReferenceFilter }
let(:filter) { filter_class.new(doc, project: project) }
let(:cache) { described_class.new(filter, { project: project }, result) }
let(:result) { {} }
describe '#load_references_per_parent' do
subject { cache.load_references_per_parent(filter.nodes) }
it 'loads references grouped per parent paths' do
expect(doc).to receive(:to_html).and_call_original
subject
expect(cache.references_per_parent).to eq({ project.full_path => [issue1.iid, issue2.iid].to_set,
project2.full_path => [issue3.iid].to_set })
end
context 'when rendered_html is memoized' do
let(:result) { { rendered_html: 'html' } }
it 'reuses memoized rendered HTML when available' do
expect(doc).not_to receive(:to_html)
subject
end
end
context 'when result is not available' do
let(:result) { nil }
it { expect { subject }.not_to raise_error }
end
end
describe '#load_parent_per_reference' do
it 'returns a Hash containing projects grouped per parent paths' do
cache.load_references_per_parent(filter.nodes)
cache.load_parent_per_reference
expect(cache.parent_per_reference).to match({ project.full_path => project, project2.full_path => project2 })
end
end
describe '#load_records_per_parent' do
it 'returns a Hash containing projects grouped per parent paths' do
cache.load_references_per_parent(filter.nodes)
cache.load_parent_per_reference
cache.load_records_per_parent
expect(cache.records_per_parent).to match({ project => { issue1.iid => issue1, issue2.iid => issue2 },
project2 => { issue3.iid => issue3 } })
end
end
describe '#initialize_reference_cache' do
it 'does not have an N+1 query problem with cross projects' do
doc_single = Nokogiri::HTML.fragment("#1")
filter_single = filter_class.new(doc_single, project: project)
cache_single = described_class.new(filter_single, { project: project }, {})
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
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
# 1 for projects belonging to the above routes
# 1 for preloading routes of the projects
# 1 for loading the namespaces associated to the project
# 1 for loading the routes associated with the namespace
# 1x2 for issues
# 1x2 for groups
# 1x2 for work_item_types
# Total = 11
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(control).with_threshold(8)
end
end
describe '#find_for_paths' do
context 'with RequestStore disabled' do
it 'returns a list of Projects for a list of paths' do
expect(cache.find_for_paths([project.full_path]))
.to eq([project])
end
it 'return an empty array for paths that do not exist' do
expect(cache.find_for_paths(['nonexistent/project']))
.to eq([])
end
end
context 'with RequestStore enabled', :request_store do
it 'returns a list of Projects for a list of paths' do
expect(cache.find_for_paths([project.full_path]))
.to eq([project])
end
context 'when no project with that path exists' do
it 'returns no value' do
expect(cache.find_for_paths(['nonexistent/project']))
.to eq([])
end
it 'adds the ref to the project refs cache' do
project_refs_cache = {}
allow(cache).to receive(:refs_cache).and_return(project_refs_cache)
cache.find_for_paths(['nonexistent/project'])
expect(project_refs_cache).to eq({ 'nonexistent/project' => nil })
end
end
end
end
describe '#current_parent_path' do
it 'returns the path of the current parent' do
expect(cache.current_parent_path).to eq project.full_path
end
end
describe '#current_project_namespace_path' do
it 'returns the path of the current project namespace' do
expect(cache.current_project_namespace_path).to eq project.namespace.full_path
end
end
describe '#full_project_path' do
it 'returns current parent path when no ref specified' do
expect(cache.full_project_path('something', nil)).to eq cache.current_parent_path
end
it 'returns combined namespace and project ref' do
expect(cache.full_project_path('something', 'cool')).to eq 'something/cool'
end
it 'returns uses default namespace and project ref when namespace nil' do
expect(cache.full_project_path(nil, 'cool')).to eq "#{project.namespace.full_path}/cool"
end
end
describe '#full_group_path' do
it 'returns current parent path when no group ref specified' do
expect(cache.full_group_path(nil)).to eq cache.current_parent_path
end
it 'returns group ref' do
expect(cache.full_group_path('cool_group')).to eq 'cool_group'
end
end
end
|