summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/discussions_diff/highlight_cache_spec.rb
blob: 15ee8c40b55fa8b81bbb572746143f10ccada1b6 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::DiscussionsDiff::HighlightCache, :clean_gitlab_redis_cache do
  def fake_file(offset)
    {
      text: 'foo',
      type: 'new',
      index: 2 + offset,
      old_pos: 10 + offset,
      new_pos: 11 + offset,
      line_code: 'xpto',
      rich_text: '<blips>blops</blips>'
    }
  end

  let(:mapping) do
    {
      3 => [
        fake_file(0),
        fake_file(1)
      ],
      4 => [
        fake_file(2)
      ]
    }
  end

  describe '#write_multiple' do
    it 'sets multiple keys serializing content as JSON' do
      described_class.write_multiple(mapping)

      mapping.each do |key, value|
        full_key = described_class.cache_key_for(key)
        found = Gitlab::Redis::Cache.with { |r| r.get(full_key) }

        expect(found).to eq(value.to_json)
      end
    end
  end

  describe '#read_multiple' do
    it 'reads multiple keys and serializes content into Gitlab::Diff::Line objects' do
      described_class.write_multiple(mapping)

      found = described_class.read_multiple(mapping.keys)

      expect(found.size).to eq(2)
      expect(found.first.size).to eq(2)
      expect(found.first).to all(be_a(Gitlab::Diff::Line))
    end

    it 'returns nil when cached key is not found' do
      described_class.write_multiple(mapping)

      found = described_class.read_multiple([2, 3])

      expect(found.size).to eq(2)

      expect(found.first).to eq(nil)
      expect(found.second.size).to eq(2)
      expect(found.second).to all(be_a(Gitlab::Diff::Line))
    end
  end

  describe '#clear_multiple' do
    it 'removes all named keys' do
      described_class.write_multiple(mapping)

      described_class.clear_multiple(mapping.keys)

      expect(described_class.read_multiple(mapping.keys)).to all(be_nil)
    end

    it 'only removed named keys' do
      to_clear, to_leave = mapping.keys

      described_class.write_multiple(mapping)
      described_class.clear_multiple([to_clear])

      cleared, left = described_class.read_multiple([to_clear, to_leave])

      expect(cleared).to be_nil
      expect(left).to all(be_a(Gitlab::Diff::Line))
    end
  end
end