summaryrefslogtreecommitdiff
path: root/spec/models/concerns/atomic_internal_id_spec.rb
blob: 80f296d8825ef7dde8e8972c3e50180f4d39af2d (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
# frozen_string_literal: true

require 'spec_helper'

describe AtomicInternalId do
  let(:milestone) { build(:milestone) }
  let(:iid) { double('iid', to_i: 42) }
  let(:external_iid) { 100 }
  let(:scope_attrs) { { project: milestone.project } }
  let(:usage) { :milestones }

  describe '#track_project_iid!' do
    subject { milestone.track_project_iid! }

    it 'tracks the present value' do
      milestone.iid = external_iid

      expect(InternalId).to receive(:track_greatest).once.with(milestone, scope_attrs, usage, external_iid, anything)
      expect(InternalId).not_to receive(:generate_next)

      subject
    end

    context 'when value is set by ensure_project_iid!' do
      context 'with iid_always_track false' do
        before do
          stub_feature_flags(iid_always_track: false)
        end

        it 'does not track the value' do
          expect(InternalId).not_to receive(:track_greatest)

          milestone.ensure_project_iid!
          subject
        end

        it 'tracks the iid for the scope that is actually present' do
          milestone.iid = external_iid

          expect(InternalId).to receive(:track_greatest).once.with(milestone, scope_attrs, usage, external_iid, anything)
          expect(InternalId).not_to receive(:generate_next)

          # group scope is not present here, the milestone does not have a group
          milestone.track_group_iid!
          subject
        end
      end

      context 'with iid_always_track enabled' do
        before do
          stub_feature_flags(iid_always_track: true)
        end

        it 'does not track the value' do
          expect(InternalId).to receive(:track_greatest)

          milestone.ensure_project_iid!
          subject
        end
      end
    end
  end

  describe '#ensure_project_iid!' do
    subject { milestone.ensure_project_iid! }

    it 'generates a new value if non is present' do
      expect(InternalId).to receive(:generate_next).with(milestone, scope_attrs, usage, anything).and_return(iid)

      expect { subject }.to change { milestone.iid }.from(nil).to(iid.to_i)
    end

    it 'generates a new value if first set with iid= but later set to nil' do
      expect(InternalId).to receive(:generate_next).with(milestone, scope_attrs, usage, anything).and_return(iid)

      milestone.iid = external_iid
      milestone.iid = nil

      expect { subject }.to change { milestone.iid }.from(nil).to(iid.to_i)
    end
  end
end