2019-09-30 14:06:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe AtomicInternalId do
|
2019-09-30 14:06:34 -04:00
|
|
|
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 10:07:55 -05: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 08:06:14 -04:00
|
|
|
describe '#track_project_iid!' do
|
|
|
|
subject { milestone.track_project_iid! }
|
2019-09-30 14:06:34 -04:00
|
|
|
|
2019-10-04 08:06:14 -04:00
|
|
|
it 'tracks the present value' do
|
2019-09-30 14:06:34 -04: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 08:06:14 -04:00
|
|
|
context 'when value is set by ensure_project_iid!' do
|
2019-10-15 17:06:25 -04:00
|
|
|
it 'does not track the value' do
|
|
|
|
expect(InternalId).not_to receive(:track_greatest)
|
2019-09-30 14:06:34 -04:00
|
|
|
|
2019-10-15 17:06:25 -04:00
|
|
|
milestone.ensure_project_iid!
|
|
|
|
subject
|
2019-09-30 14:06:34 -04:00
|
|
|
end
|
|
|
|
|
2019-10-15 17:06:25 -04:00
|
|
|
it 'tracks the iid for the scope that is actually present' do
|
|
|
|
milestone.iid = external_iid
|
2019-09-30 14:06:34 -04:00
|
|
|
|
2019-10-15 17:06:25 -04: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 14:06:34 -04:00
|
|
|
|
2019-10-15 17:06:25 -04:00
|
|
|
# group scope is not present here, the milestone does not have a group
|
|
|
|
milestone.track_group_iid!
|
|
|
|
subject
|
2019-09-30 14:06:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-04 08:06:14 -04: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
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2021-01-29 10:09:40 -05:00
|
|
|
describe '#clear_scope_iid!' do
|
|
|
|
context 'when no ensure_if condition is given' do
|
|
|
|
it 'clears automatically set IIDs' do
|
|
|
|
expect(milestone).to receive(:clear_project_iid!).and_call_original
|
|
|
|
|
|
|
|
expect_iid_to_be_set_and_rollback(milestone)
|
|
|
|
|
|
|
|
expect(milestone.iid).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not clear manually set IIDS' do
|
|
|
|
milestone.iid = external_iid
|
|
|
|
|
|
|
|
expect(milestone).to receive(:clear_project_iid!).and_call_original
|
|
|
|
|
|
|
|
expect_iid_to_be_set_and_rollback(milestone)
|
|
|
|
|
|
|
|
expect(milestone.iid).to eq(external_iid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an ensure_if condition is given' do
|
|
|
|
let(:test_class) do
|
|
|
|
Class.new(ApplicationRecord) do
|
|
|
|
include AtomicInternalId
|
|
|
|
include Importable
|
|
|
|
|
|
|
|
self.table_name = :milestones
|
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
|
|
|
|
has_internal_id :iid, scope: :project, track_if: -> { !importing }, ensure_if: -> { !importing }
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
'TestClass'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:instance) { test_class.new(milestone.attributes) }
|
|
|
|
|
|
|
|
context 'when the ensure_if condition evaluates to true' do
|
|
|
|
it 'clears automatically set IIDs' do
|
|
|
|
expect(instance).to receive(:clear_project_iid!).and_call_original
|
|
|
|
|
|
|
|
expect_iid_to_be_set_and_rollback(instance)
|
|
|
|
|
|
|
|
expect(instance.iid).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not clear manually set IIDs' do
|
|
|
|
instance.iid = external_iid
|
|
|
|
|
|
|
|
expect(instance).to receive(:clear_project_iid!).and_call_original
|
|
|
|
|
|
|
|
expect_iid_to_be_set_and_rollback(instance)
|
|
|
|
|
|
|
|
expect(instance.iid).to eq(external_iid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the ensure_if condition evaluates to false' do
|
|
|
|
before do
|
|
|
|
instance.importing = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not clear IIDs' do
|
|
|
|
instance.iid = external_iid
|
|
|
|
|
|
|
|
expect(instance).not_to receive(:clear_project_iid!)
|
|
|
|
|
|
|
|
expect_iid_to_be_set_and_rollback(instance)
|
|
|
|
|
|
|
|
expect(instance.iid).to eq(external_iid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_iid_to_be_set_and_rollback(instance)
|
|
|
|
ActiveRecord::Base.transaction(requires_new: true) do
|
|
|
|
instance.save!
|
|
|
|
|
|
|
|
expect(instance.iid).not_to be_nil
|
|
|
|
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#validate_scope_iid_exists!' do
|
|
|
|
let(:test_class) do
|
|
|
|
Class.new(ApplicationRecord) do
|
|
|
|
include AtomicInternalId
|
|
|
|
include Importable
|
|
|
|
|
|
|
|
self.table_name = :milestones
|
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
'TestClass'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:instance) { test_class.new(milestone.attributes) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
test_class.has_internal_id :iid, scope: :project, presence: presence, ensure_if: -> { !importing }
|
|
|
|
|
|
|
|
instance.importing = true
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the presence flag is set' do
|
|
|
|
let(:presence) { true }
|
|
|
|
|
|
|
|
it 'raises an error for blank iids on create' do
|
|
|
|
expect do
|
|
|
|
instance.save!
|
|
|
|
end.to raise_error(described_class::MissingValueError, 'iid was unexpectedly blank!')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error for blank iids on update' do
|
|
|
|
instance.iid = 100
|
|
|
|
instance.save!
|
|
|
|
|
|
|
|
instance.iid = nil
|
|
|
|
|
|
|
|
expect do
|
|
|
|
instance.save!
|
|
|
|
end.to raise_error(described_class::MissingValueError, 'iid was unexpectedly blank!')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the presence flag is not set' do
|
|
|
|
let(:presence) { false }
|
|
|
|
|
|
|
|
it 'does not raise an error for blank iids on create' do
|
|
|
|
expect { instance.save! }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an error for blank iids on update' do
|
|
|
|
instance.iid = 100
|
|
|
|
instance.save!
|
|
|
|
|
|
|
|
instance.iid = nil
|
|
|
|
|
|
|
|
expect { instance.save! }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-10 19:08:58 -05:00
|
|
|
describe '.with_project_iid_supply' do
|
|
|
|
let(:iid) { 100 }
|
|
|
|
|
|
|
|
it 'wraps generate and track_greatest in a concurrency-safe lock' do
|
|
|
|
expect_next_instance_of(InternalId::InternalIdGenerator) do |g|
|
|
|
|
expect(g).to receive(:with_lock).and_call_original
|
|
|
|
expect(g.record).to receive(:last_value).and_return(iid)
|
|
|
|
expect(g).to receive(:track_greatest).with(iid + 4)
|
|
|
|
end
|
|
|
|
|
|
|
|
::Milestone.with_project_iid_supply(milestone.project) do |supply|
|
|
|
|
4.times { supply.next_value }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-30 14:06:34 -04:00
|
|
|
end
|