2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 19:09:01 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe InternalId do
|
2018-03-06 19:09:01 +00:00
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:usage) { :issues }
|
2018-03-12 14:38:56 +00:00
|
|
|
let(:issue) { build(:issue, project: project) }
|
2020-11-11 00:08:58 +00:00
|
|
|
let(:id_subject) { issue }
|
2018-03-12 15:51:38 +00:00
|
|
|
let(:scope) { { project: project } }
|
2020-11-11 00:08:58 +00:00
|
|
|
let(:init) { ->(issue, scope) { issue&.project&.issues&.size || Issue.where(**scope).count } }
|
2018-03-06 19:09:01 +00:00
|
|
|
|
2018-12-04 10:24:21 +00:00
|
|
|
it_behaves_like 'having unique enum values'
|
|
|
|
|
2018-03-06 19:09:01 +00:00
|
|
|
context 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:usage) }
|
|
|
|
end
|
|
|
|
|
2019-01-02 15:50:37 +00:00
|
|
|
describe '.flush_records!' do
|
|
|
|
subject { described_class.flush_records!(project: project) }
|
|
|
|
|
|
|
|
let(:another_project) { create(:project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create_list(:issue, 2, project: project)
|
|
|
|
create_list(:issue, 2, project: another_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes all records for the given project' do
|
|
|
|
expect { subject }.to change { described_class.where(project: project).count }.from(1).to(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retains records for other projects' do
|
|
|
|
expect { subject }.not_to change { described_class.where(project: another_project).count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow an empty filter' do
|
|
|
|
expect { described_class.flush_records!({}) }.to raise_error(/filter cannot be empty/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-06 19:09:01 +00:00
|
|
|
describe '.generate_next' do
|
2020-11-11 00:08:58 +00:00
|
|
|
subject { described_class.generate_next(id_subject, scope, usage, init) }
|
2018-03-06 19:09:01 +00:00
|
|
|
|
2018-03-13 15:53:55 +00:00
|
|
|
context 'in the absence of a record' do
|
2018-03-06 19:09:01 +00:00
|
|
|
it 'creates a record if not yet present' do
|
|
|
|
expect { subject }.to change { described_class.count }.from(0).to(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores record attributes' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
described_class.first.tap do |record|
|
|
|
|
expect(record.project).to eq(project)
|
2018-03-12 14:38:56 +00:00
|
|
|
expect(record.usage).to eq(usage.to_s)
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with existing issues' do
|
|
|
|
before do
|
2018-08-14 11:18:25 +00:00
|
|
|
create_list(:issue, 2, project: project)
|
2018-03-12 15:51:38 +00:00
|
|
|
described_class.delete_all
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calculates last_value values automatically' do
|
|
|
|
expect(subject).to eq(project.issues.size + 1)
|
|
|
|
end
|
|
|
|
end
|
2018-03-18 16:13:09 +00:00
|
|
|
|
|
|
|
context 'with concurrent inserts on table' do
|
|
|
|
it 'looks up the record if it was created concurrently' do
|
|
|
|
args = { **scope, usage: described_class.usages[usage.to_s] }
|
|
|
|
record = double
|
|
|
|
expect(described_class).to receive(:find_by).with(args).and_return(nil) # first call, record not present
|
|
|
|
expect(described_class).to receive(:find_by).with(args).and_return(record) # second call, record was created by another process
|
|
|
|
expect(described_class).to receive(:create!).and_raise(ActiveRecord::RecordNotUnique, 'record not unique')
|
|
|
|
expect(record).to receive(:increment_and_save!)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates a strictly monotone, gapless sequence' do
|
2018-08-14 11:18:25 +00:00
|
|
|
seq = Array.new(10).map do
|
2018-03-12 14:38:56 +00:00
|
|
|
described_class.generate_next(issue, scope, usage, init)
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|
|
|
|
normalized = seq.map { |i| i - seq.min }
|
2018-03-16 12:34:08 +00:00
|
|
|
|
2018-03-06 19:09:01 +00:00
|
|
|
expect(normalized).to eq((0..seq.size - 1).to_a)
|
|
|
|
end
|
2020-11-11 00:08:58 +00:00
|
|
|
|
|
|
|
context 'there are no instances to pass in' do
|
|
|
|
let(:id_subject) { Issue }
|
|
|
|
|
|
|
|
it 'accepts classes instead' do
|
|
|
|
expect(subject).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|
|
|
|
|
2019-03-22 15:51:15 +00:00
|
|
|
describe '.reset' do
|
|
|
|
subject { described_class.reset(issue, scope, usage, value) }
|
|
|
|
|
|
|
|
context 'in the absence of a record' do
|
|
|
|
let(:value) { 2 }
|
|
|
|
|
|
|
|
it 'does not revert back the value' do
|
|
|
|
expect { subject }.not_to change { described_class.count }
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when valid iid is used to reset' do
|
|
|
|
let!(:value) { generate_next }
|
|
|
|
|
|
|
|
context 'and iid is a latest one' do
|
|
|
|
it 'does rewind and next generated value is the same' do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
expect(generate_next).to eq(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and iid is not a latest one' do
|
|
|
|
it 'does not rewind' do
|
|
|
|
generate_next
|
|
|
|
|
|
|
|
expect(subject).to be_falsey
|
|
|
|
expect(generate_next).to be > value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_next
|
|
|
|
described_class.generate_next(issue, scope, usage, init)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-01 09:03:14 +00:00
|
|
|
describe '.track_greatest' do
|
|
|
|
let(:value) { 9001 }
|
2019-12-19 00:08:01 +00:00
|
|
|
|
2020-11-11 00:08:58 +00:00
|
|
|
subject { described_class.track_greatest(id_subject, scope, usage, value, init) }
|
2018-08-01 09:03:14 +00:00
|
|
|
|
|
|
|
context 'in the absence of a record' do
|
|
|
|
it 'creates a record if not yet present' do
|
|
|
|
expect { subject }.to change { described_class.count }.from(0).to(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores record attributes' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
described_class.first.tap do |record|
|
|
|
|
expect(record.project).to eq(project)
|
|
|
|
expect(record.usage).to eq(usage.to_s)
|
|
|
|
expect(record.last_value).to eq(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with existing issues' do
|
|
|
|
before do
|
|
|
|
create(:issue, project: project)
|
|
|
|
described_class.delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'still returns the last value to that of the given value' do
|
|
|
|
expect(subject).to eq(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is less than the current last_value' do
|
|
|
|
it 'returns the current last_value' do
|
|
|
|
described_class.create!(**scope, usage: usage, last_value: 10_001)
|
|
|
|
|
|
|
|
expect(subject).to eq 10_001
|
|
|
|
end
|
|
|
|
end
|
2020-11-11 00:08:58 +00:00
|
|
|
|
|
|
|
context 'there are no instances to pass in' do
|
|
|
|
let(:id_subject) { Issue }
|
|
|
|
|
|
|
|
it 'accepts classes instead' do
|
|
|
|
expect(subject).to eq(value)
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 09:03:14 +00:00
|
|
|
end
|
|
|
|
|
2018-03-06 19:09:01 +00:00
|
|
|
describe '#increment_and_save!' do
|
|
|
|
let(:id) { create(:internal_id) }
|
2019-12-19 00:08:01 +00:00
|
|
|
|
2018-05-28 10:44:07 +00:00
|
|
|
subject { id.increment_and_save! }
|
2018-03-06 19:09:01 +00:00
|
|
|
|
|
|
|
it 'returns incremented iid' do
|
|
|
|
value = id.last_value
|
2018-03-16 12:34:08 +00:00
|
|
|
|
2018-03-06 19:09:01 +00:00
|
|
|
expect(subject).to eq(value + 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the record' do
|
|
|
|
subject
|
2018-03-16 12:34:08 +00:00
|
|
|
|
2018-03-06 19:09:01 +00:00
|
|
|
expect(id.changed?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with last_value=nil' do
|
|
|
|
let(:id) { build(:internal_id, last_value: nil) }
|
|
|
|
|
|
|
|
it 'returns 1' do
|
|
|
|
expect(subject).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 09:03:14 +00:00
|
|
|
|
|
|
|
describe '#track_greatest_and_save!' do
|
|
|
|
let(:id) { create(:internal_id) }
|
|
|
|
let(:new_last_value) { 9001 }
|
2019-12-19 00:08:01 +00:00
|
|
|
|
2018-08-01 09:03:14 +00:00
|
|
|
subject { id.track_greatest_and_save!(new_last_value) }
|
|
|
|
|
|
|
|
it 'returns new last value' do
|
|
|
|
expect(subject).to eq new_last_value
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves the record' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(id.changed?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when new last value is lower than the max' do
|
|
|
|
it 'does not update the last value' do
|
|
|
|
id.update!(last_value: 10_001)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(id.reload.last_value).to eq 10_001
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 19:09:01 +00:00
|
|
|
end
|