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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# frozen_string_literal: true
require 'spec_helper'
describe CacheMarkdownField, :clean_gitlab_redis_cache do
let(:ar_class) do
Class.new(ActiveRecord::Base) do
self.table_name = 'issues'
include CacheMarkdownField
cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
end
end
let(:other_class) do
Class.new do
include CacheMarkdownField
def initialize(args = {})
@title, @description, @cached_markdown_version = args[:title], args[:description], args[:cached_markdown_version]
@title_html, @description_html = args[:title_html], args[:description_html]
@author, @project = args[:author], args[:project]
end
attr_accessor :title, :description, :cached_markdown_version
cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
def id
"test-markdown-cache"
end
end
end
let(:markdown) { '`Foo`' }
let(:html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Foo</code></p>' }
let(:updated_markdown) { '`Bar`' }
let(:updated_html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Bar</code></p>' }
let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }
def thing_subclass(klass, extra_attribute)
Class.new(klass) { attr_accessor(extra_attribute) }
end
shared_examples 'a class with cached markdown fields' do
describe '#cached_html_up_to_date?' do
let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
subject { thing.cached_html_up_to_date?(:title) }
it 'returns false when the version is absent' do
thing.cached_markdown_version = nil
is_expected.to be_falsy
end
it 'returns false when the version is too early' do
thing.cached_markdown_version -= 1
is_expected.to be_falsy
end
it 'returns false when the version is too late' do
thing.cached_markdown_version += 1
is_expected.to be_falsy
end
it 'returns false when the local version was bumped' do
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:local_markdown_version).and_return(2)
thing.cached_markdown_version = cache_version
is_expected.to be_falsy
end
it 'returns true when the local version is default' do
thing.cached_markdown_version = cache_version
is_expected.to be_truthy
end
it 'returns true when the cached version is just right' do
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:local_markdown_version).and_return(2)
thing.cached_markdown_version = cache_version + 2
is_expected.to be_truthy
end
end
describe '#latest_cached_markdown_version' do
let(:thing) { klass.new }
subject { thing.latest_cached_markdown_version }
it 'returns default version' do
thing.cached_markdown_version = nil
is_expected.to eq(cache_version)
end
end
describe '#refresh_markdown_cache' do
let(:thing) { klass.new(description: markdown, description_html: html, cached_markdown_version: cache_version) }
before do
thing.description = updated_markdown
end
it 'fills all html fields' do
thing.refresh_markdown_cache
expect(thing.description_html).to eq(updated_html)
end
it 'does not save the result' do
expect(thing).not_to receive(:save_markdown)
thing.refresh_markdown_cache
end
it 'updates the markdown cache version' do
thing.cached_markdown_version = nil
thing.refresh_markdown_cache
expect(thing.cached_markdown_version).to eq(cache_version)
end
end
describe '#refresh_markdown_cache!' do
let(:thing) { klass.new(description: markdown, description_html: html, cached_markdown_version: cache_version) }
before do
thing.description = updated_markdown
end
it 'fills all html fields' do
thing.refresh_markdown_cache!
expect(thing.description_html).to eq(updated_html)
end
it 'saves the changes' do
expect(thing)
.to receive(:save_markdown)
.with("description_html" => updated_html, "title_html" => "", "cached_markdown_version" => cache_version)
thing.refresh_markdown_cache!
end
end
describe '#banzai_render_context' do
let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
subject(:context) { thing.banzai_render_context(:title) }
it 'sets project to nil if the object lacks a project' do
is_expected.to have_key(:project)
expect(context[:project]).to be_nil
end
it 'excludes author if the object lacks an author' do
is_expected.not_to have_key(:author)
end
it 'raises if the context for an unrecognised field is requested' do
expect { thing.banzai_render_context(:not_found) }.to raise_error(ArgumentError)
end
it 'includes the pipeline' do
title_context = thing.banzai_render_context(:title)
expect(title_context[:pipeline]).to eq(:single_line)
end
it 'returns copies of the context template' do
template = thing.cached_markdown_fields[:description]
copy = thing.banzai_render_context(:description)
expect(copy).not_to be(template)
end
context 'with a project' do
let(:project) { build(:project, group: create(:group)) }
let(:thing) { thing_subclass(klass, :project).new(title: markdown, title_html: html, project: project) }
it 'sets the project in the context' do
is_expected.to have_key(:project)
expect(context[:project]).to eq(project)
end
end
context 'with an author' do
let(:thing) { thing_subclass(klass, :author).new(title: markdown, title_html: html, author: :author_value) }
it 'sets the author in the context' do
is_expected.to have_key(:author)
expect(context[:author]).to eq(:author_value)
end
end
end
end
context 'for Active record classes' do
let(:klass) { ar_class }
it_behaves_like 'a class with cached markdown fields'
end
context 'for other classes' do
let(:klass) { other_class }
it_behaves_like 'a class with cached markdown fields'
end
end
|