summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/markdown_cache/redis/store_spec.rb
blob: 95c68e7d491524b1e45929967fdc80a366283c42 (plain)
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
# frozen_string_literal: true
require 'spec_helper'

describe Gitlab::MarkdownCache::Redis::Store, :clean_gitlab_redis_cache do
  let(:storable_class) do
    Class.new do
      cattr_reader :cached_markdown_fields do
        Gitlab::MarkdownCache::FieldData.new.tap do |field_data|
          field_data[:field_1] = {}
          field_data[:field_2] = {}
        end
      end

      attr_accessor :field_1, :field_2, :field_1_html, :field_2_html, :cached_markdown_version

      def cache_key
        "cache-key"
      end
    end
  end
  let(:storable) { storable_class.new }
  let(:cache_key) { "markdown_cache:#{storable.cache_key}" }

  subject(:store) { described_class.new(storable) }

  def read_values
    Gitlab::Redis::Cache.with do |r|
      r.mapped_hmget(cache_key,
                     :field_1_html, :field_2_html, :cached_markdown_version)
    end
  end

  def store_values(values)
    Gitlab::Redis::Cache.with do |r|
      r.mapped_hmset(cache_key,
                     values)
    end
  end

  describe '#save' do
    it 'stores updates to html fields and version' do
      values_to_store = { field_1_html: "hello", field_2_html: "world", cached_markdown_version: 1 }

      store.save(values_to_store)

      expect(read_values)
        .to eq({ field_1_html: "hello", field_2_html: "world", cached_markdown_version: "1" })
    end
  end

  describe '#read' do
    it 'reads the html fields and version from redis if they were stored' do
      stored_values = { field_1_html: "hello", field_2_html: "world", cached_markdown_version: 1 }

      store_values(stored_values)

      expect(store.read.symbolize_keys).to eq(stored_values)
    end

    it 'is mared loaded after reading' do
      expect(store).not_to be_loaded

      store.read

      expect(store).to be_loaded
    end
  end
end