gitlab-org--gitlab-foss/spec/lib/banzai/renderer_spec.rb
Patrick Bajao 2eecfd8f9d Use Redis for CacheMarkDownField on non AR models
This allows using `CacheMarkdownField` for models that are not backed
by ActiveRecord.

When the including class inherits `ActiveRecord::Base` we include
`Gitlab::MarkdownCache::ActiveRecord::Extension`. This will cause the
markdown fields to be rendered and the generated HTML stored in a
`<field>_html` attribute on the record. We also store the version
used for generating the markdown.

All other classes that include this model will include the
`Gitlab::MarkdownCache::Redis::Extension`. This add the `<field>_html`
attributes to that model and will generate the html in them. The
generated HTML will be cached in redis under the key
`markdown_cache:<class>:<id>`. The class this included in must
therefore respond to `id`.
2019-06-05 13:19:59 +08:00

66 lines
1.8 KiB
Ruby

require 'spec_helper'
describe Banzai::Renderer do
def fake_object(fresh:)
object = double('object')
allow(object).to receive(:respond_to?).with(:cached_markdown_fields).and_return(true)
allow(object).to receive(:cached_html_up_to_date?).with(:field).and_return(fresh)
allow(object).to receive(:cached_html_for).with(:field).and_return('field_html')
object
end
def fake_cacheless_object
object = double('cacheless object')
allow(object).to receive(:respond_to?).with(:cached_markdown_fields).and_return(false)
object
end
describe '#render_field' do
let(:renderer) { described_class }
context 'without cache' do
let(:commit) { fake_cacheless_object }
it 'returns cacheless render field' do
expect(renderer).to receive(:cacheless_render_field).with(commit, :field, {})
renderer.render_field(commit, :field)
end
end
context 'with cache' do
subject { renderer.render_field(object, :field) }
context 'with a stale cache' do
let(:object) { fake_object(fresh: false) }
it 'caches and returns the result' do
expect(object).to receive(:refresh_markdown_cache!)
is_expected.to eq('field_html')
end
it "skips database caching on a GitLab read-only instance" do
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
expect(object).to receive(:refresh_markdown_cache!)
is_expected.to eq('field_html')
end
end
context 'with an up-to-date cache' do
let(:object) { fake_object(fresh: true) }
it 'uses the cache' do
expect(object).to receive(:refresh_markdown_cache!).never
is_expected.to eq('field_html')
end
end
end
end
end