2018-03-13 18:43:02 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-06-01 06:37:36 +00:00
|
|
|
shared_examples_for 'AtomicInternalId' do |validate_presence: true|
|
2018-03-13 18:43:02 +00:00
|
|
|
describe '.has_internal_id' do
|
|
|
|
describe 'Module inclusion' do
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(AtomicInternalId) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Validation' do
|
|
|
|
before do
|
2018-06-01 06:37:36 +00:00
|
|
|
allow_any_instance_of(described_class).to receive(:"ensure_#{scope}_#{internal_id_attribute}!")
|
2018-03-13 18:43:02 +00:00
|
|
|
|
2018-05-10 06:56:47 +00:00
|
|
|
instance.valid?
|
2018-06-01 06:37:36 +00:00
|
|
|
end
|
2018-05-08 07:01:18 +00:00
|
|
|
|
2018-06-01 06:46:15 +00:00
|
|
|
context 'when presence validation is required' do
|
2018-06-01 06:37:36 +00:00
|
|
|
before do
|
|
|
|
skip unless validate_presence
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates presence' do
|
2018-05-14 08:41:56 +00:00
|
|
|
expect(instance.errors[internal_id_attribute]).to include("can't be blank")
|
2018-05-11 07:49:18 +00:00
|
|
|
end
|
2018-05-10 06:56:47 +00:00
|
|
|
end
|
2018-06-01 06:37:36 +00:00
|
|
|
|
2018-06-01 06:46:15 +00:00
|
|
|
context 'when presence validation is not required' do
|
2018-06-01 06:37:36 +00:00
|
|
|
before do
|
|
|
|
skip if validate_presence
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not validate presence' do
|
|
|
|
expect(instance.errors[internal_id_attribute]).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2018-03-13 18:43:02 +00:00
|
|
|
end
|
|
|
|
|
2018-04-20 14:00:15 +00:00
|
|
|
describe 'Creating an instance' do
|
|
|
|
subject { instance.save! }
|
|
|
|
|
|
|
|
it 'saves a new instance properly' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-13 18:43:02 +00:00
|
|
|
describe 'internal id generation' do
|
|
|
|
subject { instance.save! }
|
|
|
|
|
|
|
|
it 'calls InternalId.generate_next and sets internal id attribute' do
|
|
|
|
iid = rand(1..1000)
|
2018-03-16 12:34:08 +00:00
|
|
|
|
2018-03-13 18:43:02 +00:00
|
|
|
expect(InternalId).to receive(:generate_next).with(instance, scope_attrs, usage, any_args).and_return(iid)
|
|
|
|
subject
|
2019-03-22 15:51:15 +00:00
|
|
|
expect(read_internal_id).to eq(iid)
|
2018-03-13 18:43:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not overwrite an existing internal id' do
|
2019-03-22 15:51:15 +00:00
|
|
|
write_internal_id(4711)
|
2018-03-16 12:34:08 +00:00
|
|
|
|
2019-03-22 15:51:15 +00:00
|
|
|
expect { subject }.not_to change { read_internal_id }
|
2018-03-13 18:43:02 +00:00
|
|
|
end
|
2018-08-01 09:03:14 +00:00
|
|
|
|
|
|
|
context 'when the instance has an internal ID set' do
|
|
|
|
let(:internal_id) { 9001 }
|
|
|
|
|
|
|
|
it 'calls InternalId.update_last_value and sets the `last_value` to that of the instance' do
|
2019-03-22 15:51:15 +00:00
|
|
|
write_internal_id(internal_id)
|
2018-08-01 09:03:14 +00:00
|
|
|
|
|
|
|
expect(InternalId)
|
|
|
|
.to receive(:track_greatest)
|
|
|
|
.with(instance, scope_attrs, usage, internal_id, any_args)
|
|
|
|
.and_return(internal_id)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2018-03-13 18:43:02 +00:00
|
|
|
end
|
2019-03-22 15:51:15 +00:00
|
|
|
|
|
|
|
describe "#reset_scope_internal_id_attribute" do
|
|
|
|
it 'rewinds the allocated IID' do
|
|
|
|
expect { ensure_scope_attribute! }.not_to raise_error
|
|
|
|
expect(read_internal_id).not_to be_nil
|
|
|
|
|
|
|
|
expect(reset_scope_attribute).to be_nil
|
|
|
|
expect(read_internal_id).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allocates the same IID' do
|
|
|
|
internal_id = ensure_scope_attribute!
|
|
|
|
reset_scope_attribute
|
|
|
|
expect(read_internal_id).to be_nil
|
|
|
|
|
|
|
|
expect(ensure_scope_attribute!).to eq(internal_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_scope_attribute!
|
|
|
|
instance.public_send(:"ensure_#{scope}_#{internal_id_attribute}!")
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_scope_attribute
|
|
|
|
instance.public_send(:"reset_#{scope}_#{internal_id_attribute}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_internal_id
|
|
|
|
instance.public_send(internal_id_attribute)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_internal_id(value)
|
|
|
|
instance.public_send(:"#{internal_id_attribute}=", value)
|
|
|
|
end
|
2018-03-13 18:43:02 +00:00
|
|
|
end
|
|
|
|
end
|