2019-09-30 18:06:34 +00:00
|
|
|
# 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 }
|
|
|
|
|
2020-01-14 15:07:55 +00:00
|
|
|
describe '#save!' do
|
|
|
|
context 'when IID is provided' do
|
|
|
|
before do
|
|
|
|
milestone.iid = external_iid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'tracks the value' do
|
|
|
|
expect(milestone).to receive(:track_project_iid!)
|
|
|
|
|
|
|
|
milestone.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when importing' do
|
|
|
|
before do
|
|
|
|
milestone.importing = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not track the value' do
|
|
|
|
expect(milestone).not_to receive(:track_project_iid!)
|
|
|
|
|
|
|
|
milestone.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
describe '#track_project_iid!' do
|
|
|
|
subject { milestone.track_project_iid! }
|
2019-09-30 18:06:34 +00:00
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
it 'tracks the present value' do
|
2019-09-30 18:06:34 +00:00
|
|
|
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
|
|
|
|
|
2019-10-04 12:06:14 +00:00
|
|
|
context 'when value is set by ensure_project_iid!' do
|
2019-10-15 21:06:25 +00:00
|
|
|
it 'does not track the value' do
|
|
|
|
expect(InternalId).not_to receive(:track_greatest)
|
2019-09-30 18:06:34 +00:00
|
|
|
|
2019-10-15 21:06:25 +00:00
|
|
|
milestone.ensure_project_iid!
|
|
|
|
subject
|
2019-09-30 18:06:34 +00:00
|
|
|
end
|
|
|
|
|
2019-10-15 21:06:25 +00:00
|
|
|
it 'tracks the iid for the scope that is actually present' do
|
|
|
|
milestone.iid = external_iid
|
2019-09-30 18:06:34 +00:00
|
|
|
|
2019-10-15 21:06:25 +00:00
|
|
|
expect(InternalId).to receive(:track_greatest).once.with(milestone, scope_attrs, usage, external_iid, anything)
|
|
|
|
expect(InternalId).not_to receive(:generate_next)
|
2019-09-30 18:06:34 +00:00
|
|
|
|
2019-10-15 21:06:25 +00:00
|
|
|
# group scope is not present here, the milestone does not have a group
|
|
|
|
milestone.track_group_iid!
|
|
|
|
subject
|
2019-09-30 18:06:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-04 12:06:14 +00:00
|
|
|
|
|
|
|
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
|
2019-09-30 18:06:34 +00:00
|
|
|
end
|