2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 14:09:01 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe InternalId do
|
2018-03-06 14:09:01 -05:00
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:usage) { :issues }
|
2018-03-12 10:38:56 -04:00
|
|
|
let(:issue) { build(:issue, project: project) }
|
2020-11-10 19:08:58 -05:00
|
|
|
let(:id_subject) { issue }
|
2018-03-12 11:51:38 -04:00
|
|
|
let(:scope) { { project: project } }
|
2020-11-10 19:08:58 -05:00
|
|
|
let(:init) { ->(issue, scope) { issue&.project&.issues&.size || Issue.where(**scope).count } }
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2018-12-04 05:24:21 -05:00
|
|
|
it_behaves_like 'having unique enum values'
|
|
|
|
|
2018-03-06 14:09:01 -05:00
|
|
|
context 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:usage) }
|
|
|
|
end
|
|
|
|
|
2019-01-02 10:50:37 -05: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
|
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
describe '.generate_next' do
|
|
|
|
subject { described_class.generate_next(id_subject, scope, usage, init) }
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2021-09-09 23:10:59 -04: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)
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'stores record attributes' do
|
|
|
|
subject
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
described_class.first.tap do |record|
|
|
|
|
expect(record.project).to eq(project)
|
|
|
|
expect(record.usage).to eq(usage.to_s)
|
2021-07-19 23:08:21 -04:00
|
|
|
end
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'with existing issues' do
|
|
|
|
before do
|
|
|
|
create_list(:issue, 2, project: project)
|
|
|
|
described_class.delete_all
|
2021-07-19 23:08:21 -04:00
|
|
|
end
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'calculates last_value values automatically' do
|
|
|
|
expect(subject).to eq(project.issues.size + 1)
|
2021-07-19 23:08:21 -04:00
|
|
|
end
|
2021-03-26 14:09:16 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'generates a strictly monotone, gapless sequence' do
|
|
|
|
seq = Array.new(10).map do
|
|
|
|
described_class.generate_next(issue, scope, usage, init)
|
2019-03-22 11:51:15 -04:00
|
|
|
end
|
2021-09-09 23:10:59 -04:00
|
|
|
normalized = seq.map { |i| i - seq.min }
|
2019-03-22 11:51:15 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
expect(normalized).to eq((0..seq.size - 1).to_a)
|
|
|
|
end
|
2019-03-22 11:51:15 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'there are no instances to pass in' do
|
|
|
|
let(:id_subject) { Issue }
|
2019-03-22 11:51:15 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'accepts classes instead' do
|
|
|
|
expect(subject).to eq(1)
|
2019-03-22 11:51:15 -04:00
|
|
|
end
|
2021-09-09 23:10:59 -04:00
|
|
|
end
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'when executed outside of transaction' do
|
|
|
|
it 'increments counter with in_transaction: "false"' do
|
2021-12-20 07:10:29 -05:00
|
|
|
allow(ApplicationRecord.connection).to receive(:transaction_open?) { false }
|
2021-05-11 14:10:36 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
expect(InternalId.internal_id_transactions_total).to receive(:increment)
|
|
|
|
.with(operation: :generate, usage: 'issues', in_transaction: 'false').and_call_original
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
subject
|
2021-03-26 14:09:16 -04:00
|
|
|
end
|
2021-09-09 23:10:59 -04:00
|
|
|
end
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'when executed within transaction' do
|
|
|
|
it 'increments counter with in_transaction: "true"' do
|
|
|
|
expect(InternalId.internal_id_transactions_total).to receive(:increment)
|
|
|
|
.with(operation: :generate, usage: 'issues', in_transaction: 'true').and_call_original
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
InternalId.transaction { subject }
|
2021-03-26 14:09:16 -04:00
|
|
|
end
|
|
|
|
end
|
2021-09-09 23:10:59 -04:00
|
|
|
end
|
2019-03-22 11:51:15 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
describe '.reset' do
|
|
|
|
subject { described_class.reset(issue, scope, usage, value) }
|
2019-12-18 19:08:01 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'in the absence of a record' do
|
|
|
|
let(:value) { 2 }
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'does not revert back the value' do
|
|
|
|
expect { subject }.not_to change { described_class.count }
|
|
|
|
expect(subject).to be_falsey
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
2021-09-09 23:10:59 -04:00
|
|
|
end
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'when valid iid is used to reset' do
|
|
|
|
let!(:value) { generate_next }
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
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)
|
2021-07-19 23:08:21 -04:00
|
|
|
end
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'and iid is not a latest one' do
|
|
|
|
it 'does not rewind' do
|
|
|
|
generate_next
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
expect(subject).to be_falsey
|
|
|
|
expect(generate_next).to be > value
|
2021-07-19 23:08:21 -04:00
|
|
|
end
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
2020-11-10 19:08:58 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
def generate_next
|
|
|
|
described_class.generate_next(issue, scope, usage, init)
|
2020-11-10 19:08:58 -05:00
|
|
|
end
|
2021-09-09 23:10:59 -04:00
|
|
|
end
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'when executed outside of transaction' do
|
|
|
|
let(:value) { 2 }
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'increments counter with in_transaction: "false"' do
|
2021-12-20 07:10:29 -05:00
|
|
|
allow(ApplicationRecord.connection).to receive(:transaction_open?) { false }
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
expect(InternalId.internal_id_transactions_total).to receive(:increment)
|
|
|
|
.with(operation: :reset, usage: 'issues', in_transaction: 'false').and_call_original
|
2021-03-26 14:09:16 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
subject
|
2021-03-26 14:09:16 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'when executed within transaction' do
|
|
|
|
let(:value) { 2 }
|
2021-07-19 23:08:21 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'increments counter with in_transaction: "true"' do
|
|
|
|
expect(InternalId.internal_id_transactions_total).to receive(:increment)
|
|
|
|
.with(operation: :reset, usage: 'issues', in_transaction: 'true').and_call_original
|
2021-07-19 23:08:21 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
InternalId.transaction { subject }
|
|
|
|
end
|
2021-08-23 05:10:23 -04:00
|
|
|
end
|
2021-07-19 23:08:21 -04:00
|
|
|
end
|
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
describe '.track_greatest' do
|
|
|
|
let(:value) { 9001 }
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
subject { described_class.track_greatest(id_subject, scope, usage, value, init) }
|
2018-03-16 08:34:08 -04:00
|
|
|
|
2021-09-09 23:10:59 -04: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
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'stores record attributes' do
|
2018-03-06 14:09:01 -05:00
|
|
|
subject
|
2018-03-16 08:34:08 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
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
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'with existing issues' do
|
|
|
|
before do
|
|
|
|
create(:issue, project: project)
|
|
|
|
described_class.delete_all
|
|
|
|
end
|
2018-03-06 14:09:01 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'still returns the last value to that of the given value' do
|
|
|
|
expect(subject).to eq(value)
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|
|
|
|
end
|
2019-12-18 19:08:01 -05:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
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)
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
expect(subject).to eq 10_001
|
|
|
|
end
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'there are no instances to pass in' do
|
|
|
|
let(:id_subject) { Issue }
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
it 'accepts classes instead' do
|
|
|
|
expect(subject).to eq(value)
|
|
|
|
end
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
context 'when executed outside of transaction' do
|
|
|
|
it 'increments counter with in_transaction: "false"' do
|
2021-12-20 07:10:29 -05:00
|
|
|
allow(ApplicationRecord.connection).to receive(:transaction_open?) { false }
|
2021-09-09 23:10:59 -04:00
|
|
|
|
|
|
|
expect(InternalId.internal_id_transactions_total).to receive(:increment)
|
|
|
|
.with(operation: :track_greatest, usage: 'issues', in_transaction: 'false').and_call_original
|
2018-08-01 05:03:14 -04:00
|
|
|
|
|
|
|
subject
|
2021-09-09 23:10:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when executed within transaction' do
|
|
|
|
it 'increments counter with in_transaction: "true"' do
|
|
|
|
expect(InternalId.internal_id_transactions_total).to receive(:increment)
|
|
|
|
.with(operation: :track_greatest, usage: 'issues', in_transaction: 'true').and_call_original
|
2018-08-01 05:03:14 -04:00
|
|
|
|
2021-09-09 23:10:59 -04:00
|
|
|
InternalId.transaction { subject }
|
2018-08-01 05:03:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-06 14:09:01 -05:00
|
|
|
end
|