2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe CopyNamingAlgorithm do
|
|
|
|
describe '.create_copy_name' do
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:base_name_short) { 'b' }
|
2018-03-25 17:30:42 +00:00
|
|
|
let(:base_name_short_copy) { 'b_copy-00' }
|
|
|
|
let(:base_name_short_copy_1) { 'b_copy-01' }
|
|
|
|
let(:base_name_short_copy_2) { 'b_copy-02' }
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:base_name_long) { 'ten digits' }
|
|
|
|
let(:base_name_long_copy) { 'te_copy-00' }
|
|
|
|
let(:base_name_long_copy_1) { 'te_copy-01' }
|
|
|
|
let(:base_name_long_copy_2) { 'te_copy-02' }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'create copy when none exist already' do
|
|
|
|
names = [base_name_short]
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_short)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_short_copy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create copy when one exists already' do
|
|
|
|
names = [base_name_short, base_name_short_copy]
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_short)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_short_copy_1
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_short_copy)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_short_copy_1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create copy when two exist already' do
|
|
|
|
names = [base_name_short, base_name_short_copy, base_name_short_copy_1]
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_short)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_short_copy_2
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_short_copy)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_short_copy_2
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_short_copy_1)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_short_copy_2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create copy when none exists - longer' do
|
|
|
|
names = [base_name_long]
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_long)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_long_copy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create copy when one exists - longer' do
|
|
|
|
names = [base_name_long, base_name_long_copy]
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_long)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_long_copy_1
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_long_copy)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_long_copy_1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'create copy when two exists - longer' do
|
|
|
|
names = [base_name_long, base_name_long_copy, base_name_long_copy_1]
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_long)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_long_copy_2
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_long_copy)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_long_copy_2
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result = TestCopyNamingAlgorithm.new(name_entities: names).create_copy_name(base_name_long_copy_1)
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(result).to eq base_name_long_copy_2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises ArgumentError on length limit problem' do
|
|
|
|
names = ['c']
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { TestCopyNamingAlgorithm.new(name_entities: names, max_length: 4).create_copy_name(names[0]) }.to(raise_error do |e|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(e).to be_a ArgumentError
|
|
|
|
expect(e.message).to start_with("It's not possible to generate a name using name_to_copy:")
|
2019-07-30 21:29:24 +00:00
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises ArgumentError on copy limit problem' do
|
|
|
|
names = ['c', 'c_copy-0']
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { TestCopyNamingAlgorithm.new(name_entities: names, max_copies: 1).create_copy_name(names[0]) }.to(raise_error do |e|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(e).to be_a ArgumentError
|
|
|
|
expect(e.message).to start_with("It's not possible to generate a UNIQUE name using name_to_copy:")
|
2019-07-30 21:29:24 +00:00
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.generate_copy_number' do
|
|
|
|
it 'adds one digit for copy number if under 10' do
|
2019-07-30 21:29:24 +00:00
|
|
|
algo = TestCopyNamingAlgorithm.new(max_copies: 9)
|
|
|
|
(0..9).each do |i|
|
|
|
|
expect(algo.generate_copy_number(i)).to eq i.to_s
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
it 'adds 2 digits for copy number if under 100' do
|
2019-07-30 21:29:24 +00:00
|
|
|
algo = TestCopyNamingAlgorithm.new(max_copies: 99)
|
|
|
|
(0..99).each do |i|
|
|
|
|
if i < 10
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(algo.generate_copy_number(i)).to eq "0#{i}"
|
|
|
|
else
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(algo.generate_copy_number(i)).to eq i.to_s
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds 3 digits for copy number if under 1000' do
|
2019-07-30 21:29:24 +00:00
|
|
|
algo = TestCopyNamingAlgorithm.new(max_copies: 999)
|
|
|
|
(0..999).each do |i|
|
|
|
|
if i < 10
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(algo.generate_copy_number(i)).to eq "00#{i}"
|
2019-07-30 21:29:24 +00:00
|
|
|
elsif i >= 10 && i < 100
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(algo.generate_copy_number(i)).to eq "0#{i}"
|
|
|
|
else
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(algo.generate_copy_number(i)).to eq i.to_s
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestCopyNamingAlgorithm < CopyNamingAlgorithm
|
|
|
|
attr_accessor :name_entities, :max_copies, :max_length
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def initialize(name_entities: [], max_length: 10, max_copies: 20)
|
2018-03-25 17:30:42 +00:00
|
|
|
@name_entities = name_entities
|
|
|
|
@max_copies = max_copies
|
|
|
|
@max_length = max_length
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_addition
|
|
|
|
'_copy'
|
|
|
|
end
|
|
|
|
|
|
|
|
def separator_before_copy_number
|
|
|
|
'-'
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
attr_reader :max_length
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def get_already_used_name_entities(_base_name)
|
2018-03-25 17:30:42 +00:00
|
|
|
@name_entities
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
attr_reader :max_copies
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|